View Javadoc

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