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.KeyedReference;
26  import org.apache.juddi.datatype.Name;
27  import org.apache.juddi.datatype.RegistryObject;
28  import org.apache.juddi.datatype.TModelBag;
29  import org.apache.juddi.datatype.request.FindQualifier;
30  import org.apache.juddi.datatype.request.FindQualifiers;
31  import org.apache.juddi.datatype.request.FindService;
32  import org.apache.juddi.datatype.response.ServiceInfos;
33  import org.apache.juddi.datatype.response.ServiceList;
34  import org.apache.juddi.datatype.tmodel.TModel;
35  import org.apache.juddi.error.NameTooLongException;
36  import org.apache.juddi.error.RegistryException;
37  import org.apache.juddi.error.TooManyOptionsException;
38  import org.apache.juddi.error.UnsupportedException;
39  import org.apache.juddi.registry.RegistryEngine;
40  import org.apache.juddi.util.Config;
41  
42  /***
43   * @author Steve Viens (sviens@apache.org)
44   */
45  public class FindServiceFunction extends AbstractFunction
46  {
47    // private reference to jUDDI Logger
48    private static Log log = LogFactory.getLog(FindServiceFunction.class);
49  
50    /***
51     *
52     */
53    public FindServiceFunction(RegistryEngine registry)
54    {
55      super(registry);
56    }
57  
58    /***
59     *
60     */
61    public RegistryObject execute(RegistryObject regObject)
62      throws RegistryException
63    {
64      FindService request = (FindService)regObject;
65      String generic = request.getGeneric();
66      String businessKey = request.getBusinessKey();
67      Vector nameVector = request.getNameVector();
68      CategoryBag categoryBag = request.getCategoryBag();
69      TModelBag tModelBag = request.getTModelBag();
70      FindQualifiers qualifiers = request.getFindQualifiers();
71      int maxRows = request.getMaxRows();
72  
73      // first make sure we need to continue with this request. If
74      // no arguments were passed in then we'll simply return
75      // an empty ServiceList (aka "a zero match result set").
76      if(((businessKey == null) || (businessKey.length() == 0)) &&
77         ((nameVector == null) || (nameVector.size() == 0))  &&
78         ((categoryBag == null) || (categoryBag.size() == 0)) &&
79         ((tModelBag == null)   || (tModelBag.size() == 0)))
80      {
81        ServiceList list = new ServiceList();
82        list.setServiceInfos(new ServiceInfos());
83        list.setGeneric(generic);
84        list.setOperator(Config.getOperator());
85        list.setTruncated(false);
86        return list;
87      }
88  
89      // Validate CategoryBag and (if neccessary) add TModelKey for: uddiorg:general_keywords
90      if (categoryBag != null)
91      {
92        Vector keyedRefVector = categoryBag.getKeyedReferenceVector();
93        if (keyedRefVector != null)
94        {
95          int vectorSize = keyedRefVector.size();
96          if (vectorSize > 0)
97          {
98            for (int i=0; i<vectorSize; i++)
99            {
100             KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i);
101             String key = keyedRef.getTModelKey();
102             
103             // A null or zero-length tModelKey is treated as 
104             // though the tModelKey for uddiorg:general_keywords 
105             // had been specified.
106             //
107             if ((key == null) || (key.trim().length() == 0))
108               keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY);
109           }
110         }
111       }
112     }            
113     
114     // aquire a jUDDI datastore instance
115     DataStore dataStore = DataStoreFactory.getDataStore();
116 
117     try
118     {
119       dataStore.beginTrans();
120 
121       // validate the 'name' parameters as much as possible up-front before
122       // calling into the data layer for relational validation.
123       if (nameVector != null)
124       {
125         // only allowed to specify a maximum of 5 names (implementation
126         // dependent).  This value is configurable in jUDDI.
127         int maxNames = Config.getMaxNameElementsAllowed();
128         if ((nameVector != null) && (nameVector.size() > maxNames))
129           throw new TooManyOptionsException("find_service: "+
130               "names="+nameVector.size()+", "+
131               "maxNames=" + maxNames);
132 
133         // names can not exceed the maximum character length specified by the
134         // UDDI specification (v2.0 specifies a max character length of 255). This
135         // value is configurable in jUDDI.
136         int maxNameLength = Config.getMaxNameLengthAllowed();
137         for (int i=0; i<nameVector.size(); i++)
138         {
139           String name = ((Name)nameVector.elementAt(i)).getValue();
140            if (name.length() > maxNameLength)
141             throw new NameTooLongException("find_service: "+
142                 "name="+name+", "+
143                 "length="+name.length()+", "+
144                 "maxNameLength="+maxNameLength);
145         }
146       }
147 
148       // validate the 'qualifiers' parameter as much as possible up-front before
149       // calling into the data layer for relational validation.
150       if (qualifiers != null)
151       {
152         Vector qVector = qualifiers.getFindQualifierVector();
153         if ((qVector!=null) && (qVector.size() > 0))
154         {
155           for (int i=0; i<qVector.size(); i++)
156           {
157             FindQualifier qualifier = (FindQualifier)qVector.elementAt(i);
158             String qValue = qualifier.getValue();
159 
160             if ((!qValue.equals(FindQualifier.EXACT_NAME_MATCH)) &&
161                 (!qValue.equals(FindQualifier.CASE_SENSITIVE_MATCH)) &&
162                 (!qValue.equals(FindQualifier.OR_ALL_KEYS)) &&
163                 (!qValue.equals(FindQualifier.OR_LIKE_KEYS)) &&
164                 (!qValue.equals(FindQualifier.AND_ALL_KEYS)) &&
165                 (!qValue.equals(FindQualifier.SORT_BY_NAME_ASC)) &&
166                 (!qValue.equals(FindQualifier.SORT_BY_NAME_DESC)) &&
167                 (!qValue.equals(FindQualifier.SORT_BY_DATE_ASC)) &&
168                 (!qValue.equals(FindQualifier.SORT_BY_DATE_DESC)) &&
169                 (!qValue.equals(FindQualifier.SERVICE_SUBSET)) &&
170                 (!qValue.equals(FindQualifier.COMBINE_CATEGORY_BAGS)))
171               throw new UnsupportedException("find_service: "+
172                   "findQualifier="+qValue);
173           }
174         }
175       }
176 
177       Vector infoVector = null;
178       boolean truncatedResults = false;
179 
180       // perform the search for matching business services (return only keys in requested order)
181       Vector keyVector = dataStore.findService(businessKey,nameVector,categoryBag,tModelBag,qualifiers);
182       if ((keyVector != null) && (keyVector.size() > 0))
183       {
184         // if a maxRows value has been specified and it's less than
185         // the number of rows we are about to return then only return
186         // maxRows specified.
187         int rowCount = keyVector.size();
188         if ((maxRows > 0) && (maxRows < rowCount))
189         {
190           rowCount = maxRows;
191           truncatedResults = true;
192         }
193 
194         // iterate through the business server keys fetching
195         // each associated ServiceInfo in sequence.
196         infoVector = new Vector(rowCount);
197         for (int i=0; i<rowCount; i++)
198           infoVector.addElement(dataStore.fetchServiceInfo((String)keyVector.elementAt(i)));
199       }
200 
201       dataStore.commit();
202 
203       // create a new ServiceInfos instance and stuff
204       // the new Vector of ServiceInfos into it.
205       ServiceInfos infos = new ServiceInfos();
206       infos.setServiceInfoVector(infoVector);
207 
208       // create a new ServiceList instance and stuff
209       // the new serviceInfos instance into it.
210       ServiceList list = new ServiceList();
211       list.setGeneric(generic);
212       list.setServiceInfos(infos);
213       list.setOperator(Config.getOperator());
214       list.setTruncated(truncatedResults);
215       return list;
216     }
217     catch(TooManyOptionsException tmoex)
218     {
219       try { dataStore.rollback(); } catch(Exception e) { }
220       log.info(tmoex.getMessage());
221       throw (RegistryException)tmoex;
222     }
223     catch(NameTooLongException ntlex)
224     {
225       try { dataStore.rollback(); } catch(Exception e) { }
226       log.info(ntlex.getMessage());
227       throw (RegistryException)ntlex;
228     }
229     catch(UnsupportedException suppex)
230     {
231       try { dataStore.rollback(); } catch(Exception e) { }
232       log.info(suppex.getMessage());
233       throw (RegistryException)suppex;
234     }
235     catch(RegistryException regex)
236     {
237       try { dataStore.rollback(); } catch(Exception e) { }
238       log.error(regex);
239       throw (RegistryException)regex;
240     }
241     catch(Exception ex)
242     {
243       try { dataStore.rollback(); } catch(Exception e) { }
244       log.error(ex);
245       throw new RegistryException(ex);
246     }
247     finally
248     {
249       if (dataStore != null)
250         dataStore.release();
251     }
252   }
253 
254 
255   /****************************************************************************/
256   /****************************** TEST DRIVER *********************************/
257   /****************************************************************************/
258 
259 
260   public static void main(String[] args)
261   {
262     // initialize the registry
263     RegistryEngine reg = new RegistryEngine();
264     reg.init();
265 
266     try
267     {
268       TModelBag bag = new TModelBag();
269       bag.addTModelKey("uuid:4C2A8920-BE0C-11D7-8920-8DA7324351E5");
270 
271       FindService request = new FindService();
272       request.setGeneric("2.0");
273       request.setTModelBag(bag);
274 
275       // invoke the server
276       ServiceList response = (ServiceList) (new FindServiceFunction(reg).execute(request));
277 
278       System.out.println(response);
279     }
280     catch (Exception ex)
281     {
282       // write execption to the console
283       ex.printStackTrace();
284     }
285     finally
286     {
287       // destroy the registry
288       reg.dispose();
289     }
290   }
291 }