View Javadoc

1   /*
2    * $Id: BaseApplicationInterface.java,v 1.8 2004/09/22 10:52:50 pah Exp $
3    *
4    * Created on 26 November 2003 by Paul Harrison
5    * Copyright 2003 AstroGrid. All rights reserved.
6    *
7    * This software is published under the terms of the AstroGrid
8    * Software License version 1.2, a copy of which has been included
9    * with this distribution in the LICENSE.txt file.
10   */
11  
12  package org.astrogrid.applications.description.base;
13  
14  import java.util.HashMap;
15  import java.util.Map;
16  
17  import org.astrogrid.applications.description.ApplicationDescription;
18  import org.astrogrid.applications.description.ApplicationInterface;
19  import org.astrogrid.applications.description.Cardinality;
20  import org.astrogrid.applications.description.ParameterDescription;
21  import org.astrogrid.applications.description.ParameterDirection;
22  import org.astrogrid.applications.description.exception.ParameterDescriptionNotFoundException;
23  import org.astrogrid.applications.description.exception.ParameterNotInInterfaceException;
24  /***
25   * Basic implementation of {@link org.astrogrid.applications.description.ApplicationInterface}
26    <p />
27    Implements all the methods of <tt>ApplicationInterface</tt>, plus methods to add parameter to the interface description.
28    
29   * @author Paul Harison (pah@jb.man.ac.uk)
30   * @author Noel Winstanley
31   * @version $Name:  $
32   * @since iteration4
33   * @TODO implement the stores of parameter names as Lists to retain parameter order for the user interface.
34   */
35  public class BaseApplicationInterface implements ApplicationInterface {
36     static private org.apache.commons.logging.Log logger =
37        org.apache.commons.logging.LogFactory.getLog(BaseApplicationInterface.class);
38     /***
39      *  Construct a new BaseApplicationInterface
40      * @param name the name of the interface
41      * @param description the applicationDescription this interface belongs to
42      */
43      public BaseApplicationInterface(String name, ApplicationDescription description) {
44          this.name = name;
45          this.applicationDescription = description;
46      } 
47         
48        
49     public String getName() {
50        return name;
51     }
52  
53     private final ApplicationDescription applicationDescription;
54  
55     protected final String name;
56  
57     //FIXME - need to implement Lists for parameters to preserve order for the user interface
58     private final  Map inputs = new HashMap();
59     private final Map outputs = new HashMap();
60  
61  
62     /***
63      * @return
64      */
65     public ApplicationDescription getApplicationDescription() {
66        return applicationDescription;
67     }
68  
69  /*** add input parameter to the inteface.
70   * 
71   * @param parameterName name of the parameter
72   * @throws ParameterDescriptionNotFoundException if parameter name is not already defined in the applicationDescription this
73   * interface is owned by.
74   */
75     public void addInputParameter(String parameterName)
76        throws ParameterDescriptionNotFoundException {
77         this.addInputParameter(parameterName,Cardinality.MANDATORY);
78     }
79     
80     public void addInputParameter(String parameterName,Cardinality card)
81     throws ParameterDescriptionNotFoundException {
82         applicationDescription.getParameterDescription(parameterName);// will throw if parameter not known.
83        inputs.put(parameterName,card);
84  
85     }
86   
87     public void addInputParameter(String parameterName,int minoccurs, int maxoccurs)
88     throws ParameterDescriptionNotFoundException {
89         applicationDescription.getParameterDescription(parameterName);// will throw if parameter not known.
90        inputs.put(parameterName, new Cardinality(minoccurs,maxoccurs));
91  
92     }
93     public void addOutputParameter(String parameterName)
94        throws ParameterDescriptionNotFoundException {
95         this.addOutputParameter(parameterName,Cardinality.MANDATORY);
96     }
97     public void addOutputParameter(String parameterName,Cardinality card)
98     throws ParameterDescriptionNotFoundException {   
99        applicationDescription.getParameterDescription(parameterName);
100       outputs.put(parameterName,card);
101 
102    }
103    
104    public void addOutputParameter(String parameterName,int minoccurs, int maxoccurs)
105    throws ParameterDescriptionNotFoundException {   
106       applicationDescription.getParameterDescription(parameterName);
107       outputs.put(parameterName, new Cardinality(minoccurs,maxoccurs));
108 
109    }
110    
111    
112    
113    public String[] getArrayofInputs() {
114       return (String[])inputs.keySet().toArray(new String[0]);
115    }
116    public String[] getArrayofOutputs() {
117       return (String[])outputs.keySet().toArray(new String[0]);
118    }
119 
120    public ParameterDescription getInputParameter(String parameterName)
121       throws ParameterNotInInterfaceException {
122       ParameterDescription ad = null;
123       if (inputs.containsKey(parameterName)) {
124          try {
125             ad = applicationDescription.getParameterDescription(parameterName);
126          }
127          catch (ParameterDescriptionNotFoundException e) {
128             logger.error(
129                "this should not happen - the checks on the original storage of the parameters should prevent it - internal program error",
130                e);
131          }
132       }
133       else {
134          throw new ParameterNotInInterfaceException("unknown parameter="+parameterName);
135       }
136       return ad;
137    }
138    public ParameterDirection getParameterDirection(String parameterName)
139    {
140       ParameterDirection retval = ParameterDirection.NOTFOUND;
141       if(inputs.containsKey(parameterName))
142       {
143          retval = ParameterDirection.INPUT;
144       }
145       if (outputs.containsKey(parameterName)) {
146          retval = ParameterDirection.OUTPUT;
147       }
148       return retval;
149    }
150    public ParameterDescription getOutputParameter(String parameterName)
151        throws ParameterNotInInterfaceException {
152        ParameterDescription ad = null;
153        if (outputs.containsKey(parameterName)) {
154           try {
155              ad = applicationDescription.getParameterDescription(parameterName);
156           }
157           catch (ParameterDescriptionNotFoundException e) {
158              logger.error(
159                 "this should not happen - the checks on the original storage of the parameters should prevent it - internal program error",
160                 e);
161           }
162        }
163        else {
164           throw new ParameterNotInInterfaceException("unknown parameter="+parameterName);
165        }
166        return ad;
167     }
168     
169 
170 
171 
172     /***
173      * @see org.astrogrid.applications.description.ApplicationInterface#getParameterCardinality(java.lang.String)
174      */
175     public Cardinality getParameterCardinality(String name) throws ParameterNotInInterfaceException {
176         if (outputs.containsKey(name)) {
177             return (Cardinality)outputs.get(name);
178         } else if (inputs.containsKey(name)) {
179             return (Cardinality)inputs.get(name);
180         } else {
181             throw new ParameterNotInInterfaceException(name);
182         }
183         
184     }
185 
186     public String toString() {
187         StringBuffer buffer = new StringBuffer();
188         buffer.append("[ApplicationInterface:");
189         buffer.append(" name: ");
190         buffer.append(name);
191         buffer.append("\n\t applicationDescription: ");
192         buffer.append(applicationDescription.getName());
193 
194         buffer.append("\n\t inputs: ");
195         buffer.append(inputs);
196         buffer.append("\n\t outputs: ");
197         buffer.append(outputs);
198         buffer.append("\n]");
199         return buffer.toString();
200     }
201 }