View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.function;
17  
18  import java.util.Vector;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.juddi.datastore.DataStore;
23  import org.apache.juddi.datastore.DataStoreFactory;
24  import org.apache.juddi.datatype.CategoryBag;
25  import org.apache.juddi.datatype.IdentifierBag;
26  import org.apache.juddi.datatype.KeyedReference;
27  import org.apache.juddi.datatype.RegistryObject;
28  import org.apache.juddi.datatype.request.FindQualifier;
29  import org.apache.juddi.datatype.request.FindQualifiers;
30  import org.apache.juddi.datatype.request.FindTModel;
31  import org.apache.juddi.datatype.response.TModelInfos;
32  import org.apache.juddi.datatype.response.TModelList;
33  import org.apache.juddi.datatype.tmodel.TModel;
34  import org.apache.juddi.error.NameTooLongException;
35  import org.apache.juddi.error.RegistryException;
36  import org.apache.juddi.error.UnsupportedException;
37  import org.apache.juddi.registry.RegistryEngine;
38  import org.apache.juddi.util.Config;
39  
40  /***
41   * @author Steve Viens (sviens@apache.org)
42   */
43  public class FindTModelFunction extends AbstractFunction
44  {
45    // private reference to jUDDI Logger
46    private static Log log = LogFactory.getLog(FindTModelFunction.class);
47  
48    /***
49     *
50     */
51    public FindTModelFunction(RegistryEngine registry)
52    {
53      super(registry);
54    }
55  
56    /***
57     *
58     */
59    public RegistryObject execute(RegistryObject regObject)
60      throws RegistryException
61    {
62      FindTModel request = (FindTModel)regObject;
63      String generic = request.getGeneric();
64      String tModelName = request.getNameString();
65      CategoryBag categoryBag = request.getCategoryBag();
66      IdentifierBag identifierBag = request.getIdentifierBag();
67      FindQualifiers qualifiers = request.getFindQualifiers();
68      int maxRows = request.getMaxRows();
69  
70      // make sure we need to continue with this request. If
71      // no arguments were passed in then we'll simply return
72      // an empty TModelList (aka "a zero match result set").
73      if (((tModelName == null)          || (tModelName.length() == 0))    &&
74          ((identifierBag == null) || (identifierBag.size() == 0)) &&
75          ((categoryBag == null)   || (categoryBag.size() == 0)))
76      {
77        TModelList list = new TModelList();
78        list.setGeneric(generic);
79        list.setTModelInfos(new TModelInfos());
80        list.setOperator(Config.getOperator());
81        list.setTruncated(false);
82        return list;
83      }
84  
85      // Validate CategoryBag and (if neccessary) add TModelKey for: uddiorg:general_keywords
86      if (categoryBag != null)
87      {
88        Vector keyedRefVector = categoryBag.getKeyedReferenceVector();
89        if (keyedRefVector != null)
90        {
91          int vectorSize = keyedRefVector.size();
92          if (vectorSize > 0)
93          {
94            for (int i=0; i<vectorSize; i++)
95            {
96              KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i);
97              String key = keyedRef.getTModelKey();
98              
99              // A null or zero-length tModelKey is treated as 
100             // though the tModelKey for uddiorg:general_keywords 
101             // had been specified.
102             //
103             if ((key == null) || (key.trim().length() == 0))
104               keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY);
105           }
106         }
107       }
108     }            
109     
110     // aquire a jUDDI datastore instance
111     DataStore dataStore = DataStoreFactory.getDataStore();
112 
113     try
114     {
115       dataStore.beginTrans();
116 
117       // validate the 'name' parameters as much as possible up-front before
118       // calling into the data layer for relational validation.
119       if (tModelName != null)
120       {
121         // names can not exceed the maximum character length specified by the
122         // UDDI specification (v2.0 specifies a max character length of 255). This
123         // value is configurable in jUDDI.
124         int maxNameLength = Config.getMaxNameLengthAllowed();
125         if (tModelName.length() > maxNameLength)
126           throw new NameTooLongException("find_tModel: "+
127               "name="+tModelName+", "+
128               "length="+tModelName.length()+", "+
129               "maxNameLength="+maxNameLength);
130       }
131 
132       // validate the 'qualifiers' parameter as much as possible up-front before
133       // calling into the data layer for relational validation.
134       if (qualifiers != null)
135       {
136         Vector qVector = qualifiers.getFindQualifierVector();
137         if ((qVector!=null) && (qVector.size() > 0))
138         {
139           for (int i=0; i<qVector.size(); i++)
140           {
141             FindQualifier qualifier = (FindQualifier)qVector.elementAt(i);
142             String qValue = qualifier.getValue();
143 
144             if ((!qValue.equals(FindQualifier.EXACT_NAME_MATCH)) &&
145                 (!qValue.equals(FindQualifier.CASE_SENSITIVE_MATCH)) &&
146                 (!qValue.equals(FindQualifier.OR_ALL_KEYS)) &&
147                 (!qValue.equals(FindQualifier.OR_LIKE_KEYS)) &&
148                 (!qValue.equals(FindQualifier.AND_ALL_KEYS)) &&
149                 (!qValue.equals(FindQualifier.SORT_BY_NAME_ASC)) &&
150                 (!qValue.equals(FindQualifier.SORT_BY_NAME_DESC)) &&
151                 (!qValue.equals(FindQualifier.SORT_BY_DATE_ASC)) &&
152                 (!qValue.equals(FindQualifier.SORT_BY_DATE_DESC)) &&
153                 (!qValue.equals(FindQualifier.SERVICE_SUBSET)) &&
154                 (!qValue.equals(FindQualifier.COMBINE_CATEGORY_BAGS)))
155               throw new UnsupportedException("find_tModel: "+
156                   "findQualifier="+qValue);
157           }
158         }
159       }
160 
161       Vector infoVector = null;
162       boolean truncatedResults = false;
163 
164       // perform the search for matching technical models (return only keys in requested order)
165       Vector keyVector = dataStore.findTModel(tModelName,categoryBag,identifierBag,qualifiers);
166       if ((keyVector != null) && (keyVector.size() > 0))
167       {
168         // if a maxRows value has been specified and it's less than
169         // the number of rows we are about to return then only return
170         // maxRows specified.
171         int rowCount = keyVector.size();
172         if ((maxRows > 0) && (maxRows < rowCount))
173         {
174           rowCount = maxRows;
175           truncatedResults = true;
176         }
177 
178         // iterate through the technical model keys fetching
179         // each associated TModelInfo in sequence.
180         infoVector = new Vector(rowCount);
181         for (int i=0; i<rowCount; i++)
182           infoVector.addElement(dataStore.fetchTModelInfo((String)keyVector.elementAt(i)));
183       }
184 
185       dataStore.commit();
186 
187       // create a new TModelInfos instance and stuff
188       // the new Vector of TModelInfos into it.
189       TModelInfos infos = new TModelInfos();
190       infos.setTModelInfoVector(infoVector);
191 
192       // create a new TModelList instance and
193       // stuff the new tModelInfoVector into it.
194       TModelList list = new TModelList();
195       list.setGeneric(generic);
196       list.setTModelInfos(infos);
197       list.setOperator(Config.getOperator());
198       list.setTruncated(truncatedResults);
199       return list;
200     }
201     catch(NameTooLongException ntlex)
202     {
203       try { dataStore.rollback(); } catch(Exception e) { }
204       log.info(ntlex.getMessage());
205       throw (RegistryException)ntlex;
206     }
207     catch(UnsupportedException suppex)
208     {
209       try { dataStore.rollback(); } catch(Exception e) { }
210       log.info(suppex.getMessage());
211       throw (RegistryException)suppex;
212     }
213     catch(RegistryException regex)
214     {
215       try { dataStore.rollback(); } catch(Exception e) { }
216       log.error(regex);
217       throw (RegistryException)regex;
218     }
219     catch(Exception ex)
220     {
221       try { dataStore.rollback(); } catch(Exception e) { }
222       log.error(ex);
223       throw new RegistryException(ex);
224     }
225     finally
226     {
227       if (dataStore != null)
228         dataStore.release();
229     }
230   }
231 
232 
233   /****************************************************************************/
234   /****************************** TEST DRIVER *********************************/
235   /****************************************************************************/
236 
237 
238   public static void main(String[] args)
239   {
240     // initialize the registry
241     RegistryEngine reg = new RegistryEngine();
242     reg.init();
243 
244     try
245     {
246     }
247     catch (Exception ex)
248     {
249       // write execption to the console
250       ex.printStackTrace();
251     }
252     finally
253     {
254       // destroy the registry
255       reg.dispose();
256     }
257   }
258 }