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.RegistryObject;
25  import org.apache.juddi.datatype.request.FindPublisher;
26  import org.apache.juddi.datatype.request.FindQualifier;
27  import org.apache.juddi.datatype.request.FindQualifiers;
28  import org.apache.juddi.datatype.response.PublisherInfos;
29  import org.apache.juddi.datatype.response.PublisherList;
30  import org.apache.juddi.error.NameTooLongException;
31  import org.apache.juddi.error.RegistryException;
32  import org.apache.juddi.error.UnsupportedException;
33  import org.apache.juddi.registry.RegistryEngine;
34  import org.apache.juddi.util.Config;
35  
36  /***
37   * "This [FindPublisher] API call returns a publisherList on success. This
38   * structure contains information about each matching publisher. In the
39   * event that no matches were located for the specified criteria, a
40   * publisherList structure with zero publisher structures is returned. If
41   * no arguments are passed, a zero-match result set will be returned."
42   *
43   * In the event of a large number of matches, (as determined by each
44   * Operator Site), or if the number of matches exceeds the value of the
45   * 'maxRows' attribute, the Operator Site will truncate the result set.
46   * If this occurs, the publisherList will contain the 'truncated' attribute
47   * with the value 'true'.
48   *
49   * @author Steve Viens (sviens@apache.org)
50   */
51  public class FindPublisherFunction extends AbstractFunction
52  {
53    // private reference to jUDDI Logger
54    private static Log log = LogFactory.getLog(FindPublisherFunction.class);
55  
56    /***
57     *
58     */
59    public FindPublisherFunction(RegistryEngine registry)
60    {
61      super(registry);
62    }
63  
64    /***
65     *
66     */
67    public RegistryObject execute(RegistryObject regObject)
68      throws RegistryException
69    {
70      FindPublisher request = (FindPublisher)regObject;
71      String generic = request.getGeneric();
72      String name = request.getNameString();
73      FindQualifiers qualifiers = request.getFindQualifiers();
74      int maxRows = request.getMaxRows();
75  
76      // make sure we need to continue with this request. If
77      // no arguments were passed in then we'll simply return
78      // an empty PublisherList (aka "a zero match result set").
79      if ((name == null) || (name.length() == 0))
80      {
81        PublisherList list = new PublisherList();
82        list.setGeneric(generic);
83        list.setPublisherInfos(new PublisherInfos());
84        list.setOperator(Config.getOperator());
85        list.setTruncated(false);
86        return list;
87      }
88  
89      // aquire a jUDDI datastore instance
90      DataStore dataStore = DataStoreFactory.getDataStore();
91  
92      try
93      {
94        dataStore.beginTrans();
95  
96        // validate the 'name' parameters as much as possible up-front before
97        // calling into the data layer for relational validation.
98        if (name != null)
99        {
100         // names can not exceed the maximum character length specified by the
101         // UDDI specification (v2.0 specifies a max character length of 255). This
102         // value is configurable in jUDDI.
103         int maxNameLength = Config.getMaxNameLengthAllowed();
104         if (name.length() > maxNameLength)
105           throw new NameTooLongException("find_publisher: "+
106               "name="+name+", "+
107               "length="+name.length()+", "+
108               "maxNameLength="+maxNameLength);
109       }
110 
111       // validate the 'qualifiers' parameter as much as possible up-front before
112       // calling into the data layer for relational validation.
113       if (qualifiers != null)
114       {
115         Vector qVector = qualifiers.getFindQualifierVector();
116         if ((qVector!=null) && (qVector.size() > 0))
117         {
118           for (int i=0; i<qVector.size(); i++)
119           {
120             FindQualifier qualifier = (FindQualifier)qVector.elementAt(i);
121             String qValue = qualifier.getValue();
122 
123             if ((!qValue.equals(FindQualifier.EXACT_NAME_MATCH)) &&
124                 (!qValue.equals(FindQualifier.CASE_SENSITIVE_MATCH)) &&
125                 (!qValue.equals(FindQualifier.OR_ALL_KEYS)) &&
126                 (!qValue.equals(FindQualifier.OR_LIKE_KEYS)) &&
127                 (!qValue.equals(FindQualifier.AND_ALL_KEYS)) &&
128                 (!qValue.equals(FindQualifier.SORT_BY_NAME_ASC)) &&
129                 (!qValue.equals(FindQualifier.SORT_BY_NAME_DESC)) &&
130                 (!qValue.equals(FindQualifier.SORT_BY_DATE_ASC)) &&
131                 (!qValue.equals(FindQualifier.SORT_BY_DATE_DESC)) &&
132                 (!qValue.equals(FindQualifier.SERVICE_SUBSET)) &&
133                 (!qValue.equals(FindQualifier.COMBINE_CATEGORY_BAGS)))
134               throw new UnsupportedException("find_publisher: "+
135                   "findQualifier="+qValue);
136           }
137         }
138       }
139 
140       Vector infoVector = null;
141       boolean truncatedResults = false;
142 
143       // perform the search for matching technical models (return only keys in requested order)
144       Vector idVector = dataStore.findPublisher(name,qualifiers);
145       if ((idVector != null) && (idVector.size() > 0))
146       {
147         // if a maxRows value has been specified and it's less than
148         // the number of rows we are about to return then only return
149         // maxRows specified.
150         int rowCount = idVector.size();
151         if ((maxRows > 0) && (maxRows < rowCount))
152         {
153           rowCount = maxRows;
154           truncatedResults = true;
155         }
156 
157         // iterate through the publisher IDs fetching
158         // each associated PublisherInfo in sequence.
159         infoVector = new Vector(rowCount);
160         for (int i=0; i<rowCount; i++)
161           infoVector.addElement(dataStore.fetchPublisherInfo((String)idVector.elementAt(i)));
162       }
163 
164       dataStore.commit();
165 
166       // create a new PublisherInfos instance and stuff
167       // the new Vector of PublisherInfos into it.
168       PublisherInfos infos = new PublisherInfos();
169       infos.setPublisherInfoVector(infoVector);
170 
171       // create a new PublisherList instance and
172       // stuff the new PublisherInfoVector into it.
173       PublisherList list = new PublisherList();
174       list.setGeneric(generic);
175       list.setPublisherInfos(infos);
176       list.setOperator(Config.getOperator());
177       list.setTruncated(truncatedResults);
178       return list;
179     }
180     catch(NameTooLongException ntlex)
181     {
182       try { dataStore.rollback(); } catch(Exception e) { }
183       log.info(ntlex.getMessage());
184       throw (RegistryException)ntlex;
185     }
186     catch(UnsupportedException suppex)
187     {
188       try { dataStore.rollback(); } catch(Exception e) { }
189       log.info(suppex.getMessage());
190       throw (RegistryException)suppex;
191     }
192     catch(RegistryException regex)
193     {
194       try { dataStore.rollback(); } catch(Exception e) { }
195       log.error(regex);
196       throw (RegistryException)regex;
197     }
198     catch(Exception ex)
199     {
200       try { dataStore.rollback(); } catch(Exception e) { }
201       log.error(ex);
202       throw new RegistryException(ex);
203     }
204     finally
205     {
206       if (dataStore != null)
207         dataStore.release();
208     }
209   }
210 
211   /****************************************************************************/
212   /****************************** TEST DRIVER *********************************/
213   /****************************************************************************/
214 
215   public static void main(String[] args)
216   {
217     // initialize the registry
218     RegistryEngine reg = new RegistryEngine();
219     reg.init();
220 
221     try
222     {
223     }
224     catch (Exception ex)
225     {
226       // write execption to the console
227       ex.printStackTrace();
228     }
229     finally
230     {
231       // destroy the registry
232       reg.dispose();
233     }
234   }
235 }