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.DiscoveryURL;
26  import org.apache.juddi.datatype.DiscoveryURLs;
27  import org.apache.juddi.datatype.IdentifierBag;
28  import org.apache.juddi.datatype.KeyedReference;
29  import org.apache.juddi.datatype.Name;
30  import org.apache.juddi.datatype.RegistryObject;
31  import org.apache.juddi.datatype.TModelBag;
32  import org.apache.juddi.datatype.request.FindBusiness;
33  import org.apache.juddi.datatype.request.FindQualifier;
34  import org.apache.juddi.datatype.request.FindQualifiers;
35  import org.apache.juddi.datatype.response.BusinessInfos;
36  import org.apache.juddi.datatype.response.BusinessList;
37  import org.apache.juddi.datatype.tmodel.TModel;
38  import org.apache.juddi.error.NameTooLongException;
39  import org.apache.juddi.error.RegistryException;
40  import org.apache.juddi.error.TooManyOptionsException;
41  import org.apache.juddi.error.UnsupportedException;
42  import org.apache.juddi.registry.RegistryEngine;
43  import org.apache.juddi.util.Config;
44  
45  /***
46   * "This [FindBusiness] API call returns a businessList on success. This
47   * structure contains information about each matching business, and
48   * summaries of the businessServices exposed by the individual businesses.
49   * If a tModelBag was used in the search, the resulting serviceInfos
50   * structure will only reflect data for the businessServices that
51   * actually contained a matching bindingTemplate. In the event that
52   * no matches were located for the specified criteria, a businessList
53   * structure with zero businessInfo structures is returned. If no
54   * arguments are passed, a zero-match result set will be returned."
55   *
56   * "In the event of a large number of matches, (as determined by each
57   * Operator Site), or if the number of matches exceeds the value of the
58   * 'maxRows' attribute, the Operator Site will truncate the result set.
59   * If this occurs, the businessList will contain the 'truncated' attribute
60   * with the value 'true'".
61   *
62   * From UDDI Version 2 Programmers API Specification (Pg. 18)
63   *
64   * @author Steve Viens (sviens@apache.org)
65   */
66  public class FindBusinessFunction extends AbstractFunction
67  {
68    // private reference to jUDDI Logger
69    private static Log log = LogFactory.getLog(FindBusinessFunction.class);
70  
71    /***
72     *
73     */
74    public FindBusinessFunction(RegistryEngine registry)
75    {
76      super(registry);
77    }
78  
79    /***
80     *
81     */
82    public RegistryObject execute(RegistryObject regObject)
83      throws RegistryException
84    {
85      FindBusiness request = (FindBusiness) regObject;
86      String generic = request.getGeneric();
87      Vector nameVector = request.getNameVector();
88      DiscoveryURLs discoveryURLs = request.getDiscoveryURLs();
89      IdentifierBag identifierBag = request.getIdentifierBag();
90      CategoryBag categoryBag = request.getCategoryBag();
91      TModelBag tModelBag = request.getTModelBag();
92      FindQualifiers qualifiers = request.getFindQualifiers();
93      int maxRows = request.getMaxRows();
94  
95      // make sure we need to continue with this request. If
96      // no arguments were passed in then we'll simply return
97      // an empty BusinessList (aka "a zero match result set").
98      if (((nameVector == null) || (nameVector.size() == 0))
99        && ((discoveryURLs == null) || (discoveryURLs.size() == 0))
100       && ((identifierBag == null) || (identifierBag.size() == 0))
101       && ((categoryBag == null) || (categoryBag.size() == 0))
102       && ((tModelBag == null) || (tModelBag.size() == 0)))
103     {
104       BusinessList list = new BusinessList();
105       list.setGeneric(generic);
106       list.setBusinessInfos(new BusinessInfos());
107       list.setOperator(Config.getOperator());
108       list.setTruncated(false);
109       return list;
110     }
111 
112     // Validate CategoryBag and (if neccessary) add TModelKey for: uddiorg:general_keywords
113     if (categoryBag != null)
114     {
115       Vector keyedRefVector = categoryBag.getKeyedReferenceVector();
116       if (keyedRefVector != null)
117       {
118         int vectorSize = keyedRefVector.size();
119         if (vectorSize > 0)
120         {
121           for (int i=0; i<vectorSize; i++)
122           {
123             KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i);
124             String key = keyedRef.getTModelKey();
125             
126             // A null or zero-length tModelKey is treated as 
127             // though the tModelKey for uddiorg:general_keywords 
128             // had been specified.
129             //
130             if ((key == null) || (key.trim().length() == 0))
131               keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY);
132           }
133         }
134       }
135     }            
136     
137     // aquire a jUDDI datastore instance
138     DataStore dataStore = DataStoreFactory.getDataStore();
139 
140     try
141     {
142       dataStore.beginTrans();
143 
144       // validate the 'name' parameters as much as possible up-front before
145       // calling into the data layer for relational validation.
146       if (nameVector != null)
147       {
148         // only allowed to specify a maximum of 5 names (implementation
149         // dependent).  This value is configurable in jUDDI.
150         int maxNames = Config.getMaxNameElementsAllowed();
151         if ((nameVector != null) && (nameVector.size() > maxNames))
152           throw new TooManyOptionsException("find_business: "+
153               "names="+nameVector.size()+", "+
154               "maxNames=" + maxNames);
155 
156         // names can not exceed the maximum character length specified by the
157         // UDDI specification (v2.0 specifies a max character length of 255). This
158         // value is configurable in jUDDI.
159         int maxNameLength = Config.getMaxNameLengthAllowed();
160         for (int i = 0; i < nameVector.size(); i++)
161         {
162           String name = ((Name) nameVector.elementAt(i)).getValue();
163           if (name.length() > maxNameLength)
164             throw new NameTooLongException("find_business: "+
165                 "name="+name+", "+
166                 "length="+name.length()+", "+
167                 "maxNameLength="+maxNameLength);
168         }
169       }
170 
171       // validate the 'qualifiers' parameter as much as possible up-front before
172       // calling into the data layer for relational validation.
173       if (qualifiers != null)
174       {
175         Vector qVector = qualifiers.getFindQualifierVector();
176         if ((qVector!=null) && (qVector.size() > 0))
177         {
178           for (int i = 0; i < qVector.size(); i++)
179           {
180             FindQualifier qualifier = (FindQualifier) qVector.elementAt(i);
181             String qValue = qualifier.getValue();
182 
183             if ((!qValue.equals(FindQualifier.EXACT_NAME_MATCH))
184               && (!qValue.equals(FindQualifier.CASE_SENSITIVE_MATCH))
185               && (!qValue.equals(FindQualifier.OR_ALL_KEYS))
186               && (!qValue.equals(FindQualifier.OR_LIKE_KEYS))
187               && (!qValue.equals(FindQualifier.AND_ALL_KEYS))
188               && (!qValue.equals(FindQualifier.SORT_BY_NAME_ASC))
189               && (!qValue.equals(FindQualifier.SORT_BY_NAME_DESC))
190               && (!qValue.equals(FindQualifier.SORT_BY_DATE_ASC))
191               && (!qValue.equals(FindQualifier.SORT_BY_DATE_DESC))
192               && (!qValue.equals(FindQualifier.SERVICE_SUBSET))
193               && (!qValue
194                 .equals(FindQualifier.COMBINE_CATEGORY_BAGS)))
195               throw new UnsupportedException("find_business: "+
196                 "findQualifier="+qValue);
197           }
198         }
199       }
200 
201       Vector infoVector = null;
202       boolean truncatedResults = false;
203 
204       // perform the search for matching business entities (returns only business keys in the order requested)
205       Vector keyVector =
206         dataStore.findBusiness(
207           nameVector,
208           discoveryURLs,
209           identifierBag,
210           categoryBag,
211           tModelBag,
212           qualifiers);
213       if ((keyVector != null) && (keyVector.size() > 0))
214       {
215         // if a maxRows value has been specified and it's less than
216         // the number of rows we are about to return then only return
217         // maxRows specified.
218         int rowCount = keyVector.size();
219         if ((maxRows > 0) && (maxRows < rowCount))
220         {
221           rowCount = maxRows;
222           truncatedResults = true;
223         }
224 
225         // iterate through the business entity keys fetching
226         // each associated BusinessInfo in sequence.
227         infoVector = new Vector(rowCount);
228         for (int i = 0; i < rowCount; i++)
229           infoVector.addElement(
230             dataStore.fetchBusinessInfo(
231               (String) keyVector.elementAt(i)));
232       }
233 
234       dataStore.commit();
235 
236       // create a new BusinessInfos instance and stuff
237       // the new Vector of BusinessInfos into it.
238       BusinessInfos infos = new BusinessInfos();
239       infos.setBusinessInfoVector(infoVector);
240 
241       // create a new BusinessList instance and stuff
242       // the new businessInfos instance into it.
243       BusinessList list = new BusinessList();
244       list.setBusinessInfos(infos);
245       list.setGeneric(generic);
246       list.setOperator(Config.getOperator());
247       list.setTruncated(truncatedResults);
248       return list;
249     }
250     catch(TooManyOptionsException tmoex)
251     {
252       try { dataStore.rollback(); } catch(Exception e) { }
253       log.info(tmoex.getMessage());
254       throw (RegistryException)tmoex;
255     }
256     catch(NameTooLongException ntlex)
257     {
258       try { dataStore.rollback(); } catch(Exception e) { }
259       log.info(ntlex.getMessage());
260       throw (RegistryException)ntlex;
261     }
262     catch(UnsupportedException suppex)
263     {
264       try { dataStore.rollback(); } catch(Exception e) { }
265       log.info(suppex.getMessage());
266       throw (RegistryException)suppex;
267     }
268     catch(RegistryException regex)
269     {
270       try { dataStore.rollback(); } catch(Exception e) { }
271       log.error(regex);
272       throw (RegistryException)regex;
273     }
274     catch(Exception ex)
275     {
276       try { dataStore.rollback(); } catch(Exception e) { }
277       log.error(ex);
278       throw new RegistryException(ex);
279     }
280     finally
281     {
282       if (dataStore != null)
283         dataStore.release();
284     }
285   }
286 
287   /****************************************************************************/
288   /****************************** TEST DRIVER *********************************/
289   /****************************************************************************/
290 
291   public static void main(String[] args)
292   {
293     // initialize the registry
294     RegistryEngine reg = new RegistryEngine();
295     reg.init();
296 
297     try
298     {
299       // create a request
300       Vector nameVector = new Vector(5);
301       nameVector.addElement(new Name("InflexionPoint"));
302       nameVector.addElement(new Name("SteveViens.com"));
303       nameVector.addElement(new Name("Liberty Mutual"));
304       nameVector.addElement(new Name("Bowstreet"));
305       nameVector.addElement(new Name("CMGi"));
306       //nameVector.addElement(new Name("BusinessName #6 (1 over the maximum)"));
307 
308       Vector qualifierVector = new Vector(1);
309       qualifierVector.add(
310         new FindQualifier(FindQualifier.EXACT_NAME_MATCH));
311       //qualifierVector.add(new FindQualifier("anInvalidFindQualifier"));
312 
313       FindQualifiers qualifiers = new FindQualifiers();
314       qualifiers.setFindQualifierVector(qualifierVector);
315 
316       Vector categoryVector = new Vector();
317       categoryVector.addElement(new KeyedReference("name1", "value1"));
318       categoryVector.addElement(new KeyedReference("name2", "value2"));
319       categoryVector.addElement(new KeyedReference("name3", "value3"));
320 
321       CategoryBag categoryBag = new CategoryBag();
322       categoryBag.setKeyedReferenceVector(categoryVector);
323 
324       Vector identifierVector = new Vector();
325       identifierVector.addElement(new KeyedReference("name1", "value1"));
326       identifierVector.addElement(new KeyedReference("name1", "value1"));
327       identifierVector.addElement(new KeyedReference("name1", "value1"));
328 
329       IdentifierBag identifierBag = new IdentifierBag();
330       identifierBag.setKeyedReferenceVector(identifierVector);
331 
332       Vector tModelKeyVector = new Vector();
333       tModelKeyVector.addElement(
334         new String("6240b6f0-d4dd-4091-851b-d59fedbd0491"));
335       tModelKeyVector.addElement(
336         new String("ee0a154b-43ed-47be-b24f-878ab2956a31"));
337 
338       TModelBag tModelBag = new TModelBag();
339       tModelBag.setTModelKeyVector(tModelKeyVector);
340 
341       Vector discoveryURLVector = new Vector();
342       discoveryURLVector.addElement(new DiscoveryURL());
343       discoveryURLVector.addElement(new DiscoveryURL());
344       discoveryURLVector.addElement(new DiscoveryURL());
345 
346       DiscoveryURLs discoveryURLs = new DiscoveryURLs();
347       discoveryURLs.setDiscoveryURLVector(discoveryURLVector);
348 
349       FindBusiness request = new FindBusiness();
350       request.setNameVector(nameVector);
351       request.setMaxRows(10);
352       request.setCategoryBag(categoryBag);
353       request.setIdentifierBag(identifierBag);
354       request.setTModelBag(tModelBag);
355       request.setDiscoveryURLs(discoveryURLs);
356       request.setFindQualifiers(qualifiers);
357 
358       // invoke the server
359       BusinessList response =
360         (BusinessList) (new FindBusinessFunction(reg).execute(request));
361       System.out.println(response);
362     }
363     catch (Exception ex)
364     {
365       // write execption to the console
366       ex.printStackTrace();
367     }
368     finally
369     {
370       // destroy the registry
371       reg.dispose();
372     }
373   }
374 }