KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > function > FindBindingFunction


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 JavaDoc;
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.RegistryObject;
27 import org.apache.juddi.datatype.TModelBag;
28 import org.apache.juddi.datatype.request.FindBinding;
29 import org.apache.juddi.datatype.request.FindQualifier;
30 import org.apache.juddi.datatype.request.FindQualifiers;
31 import org.apache.juddi.datatype.response.BindingDetail;
32 import org.apache.juddi.datatype.tmodel.TModel;
33 import org.apache.juddi.error.InvalidKeyPassedException;
34 import org.apache.juddi.error.RegistryException;
35 import org.apache.juddi.error.UnsupportedException;
36 import org.apache.juddi.registry.RegistryEngine;
37 import org.apache.juddi.util.Config;
38
39 /**
40  * @author Steve Viens (sviens@apache.org)
41  * @author Anil Saldhana (anil@apache.org)
42  */

43 public class FindBindingFunction extends AbstractFunction
44 {
45   // private reference to jUDDI Logger
46
private static Log log = LogFactory.getLog(FindBindingFunction.class);
47
48   /**
49    *
50    */

51   public FindBindingFunction(RegistryEngine registry)
52   {
53     super(registry);
54   }
55
56   /**
57    *
58    */

59   public RegistryObject execute(RegistryObject regObject)
60     throws RegistryException
61   {
62     // extract individual parameters
63
FindBinding request = (FindBinding)regObject;
64     String JavaDoc generic = request.getGeneric();
65     String JavaDoc serviceKey = request.getServiceKey();
66     CategoryBag categoryBag = request.getCategoryBag();
67     TModelBag tModelBag = request.getTModelBag();
68     FindQualifiers qualifiers = request.getFindQualifiers();
69     int maxRows = request.getMaxRows();
70
71     // first make sure we need to continue with this request. If
72
// no arguments were passed in then we'll simply return
73
// an empty ServiceList (aka "a zero match result set").
74
if(((serviceKey == null) || (serviceKey.length() == 0)) &&
75        ((categoryBag == null) || (categoryBag.size() == 0)) &&
76        ((tModelBag == null) || (tModelBag.size() == 0)))
77     {
78       BindingDetail detail = new BindingDetail();
79       detail.setGeneric(generic);
80       detail.setBindingTemplateVector(null);
81       detail.setOperator(Config.getOperator());
82       detail.setTruncated(false);
83       return detail;
84     }
85
86     // Validate CategoryBag and (if neccessary) add TModelKey for: uddiorg:general_keywords
87
if (categoryBag != null)
88     {
89       Vector JavaDoc keyedRefVector = categoryBag.getKeyedReferenceVector();
90       if (keyedRefVector != null)
91       {
92         int vectorSize = keyedRefVector.size();
93         if (vectorSize > 0)
94         {
95           for (int i=0; i<vectorSize; i++)
96           {
97             KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i);
98             String JavaDoc key = keyedRef.getTModelKey();
99             
100             // A null or zero-length tModelKey is treated as
101
// though the tModelKey for uddiorg:general_keywords
102
// had been specified.
103
//
104
if ((key == null) || (key.trim().length() == 0))
105               keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY);
106           }
107         }
108       }
109     }
110     
111     // aquire a jUDDI datastore instance
112
DataStore dataStore = DataStoreFactory.getDataStore();
113
114     try
115     {
116       dataStore.beginTrans();
117
118       // a find_binding request MUST include a service_key attribute
119
if ((serviceKey == null) || (serviceKey.length() == 0))
120         throw new InvalidKeyPassedException("find_binding: "+
121             "serviceKey="+serviceKey);
122
123       // validate the 'qualifiers' parameter as much as possible up-front before
124
// calling into the data layer for relational validation.
125
if (qualifiers != null)
126       {
127         Vector JavaDoc qVector = qualifiers.getFindQualifierVector();
128         if ((qVector!=null) && (qVector.size() > 0))
129         {
130           for (int i=0; i<qVector.size(); i++)
131           {
132             FindQualifier qualifier = (FindQualifier)qVector.elementAt(i);
133             String JavaDoc qValue = qualifier.getValue();
134
135             if ((!qValue.equals(FindQualifier.EXACT_NAME_MATCH)) &&
136                 (!qValue.equals(FindQualifier.CASE_SENSITIVE_MATCH)) &&
137                 (!qValue.equals(FindQualifier.OR_ALL_KEYS)) &&
138                 (!qValue.equals(FindQualifier.OR_LIKE_KEYS)) &&
139                 (!qValue.equals(FindQualifier.AND_ALL_KEYS)) &&
140                 (!qValue.equals(FindQualifier.SORT_BY_NAME_ASC)) &&
141                 (!qValue.equals(FindQualifier.SORT_BY_NAME_DESC)) &&
142                 (!qValue.equals(FindQualifier.SORT_BY_DATE_ASC)) &&
143                 (!qValue.equals(FindQualifier.SORT_BY_DATE_DESC)) &&
144                 (!qValue.equals(FindQualifier.SERVICE_SUBSET)) &&
145                 (!qValue.equals(FindQualifier.COMBINE_CATEGORY_BAGS)))
146               throw new UnsupportedException("find_binding: "+
147                   "findQualifier="+qValue);
148           }
149         }
150       }
151
152       Vector JavaDoc bindingVector = null;
153       boolean truncatedResults = false;
154
155       // perform the search for matching binding templates (return only keys in requested order)
156
Vector JavaDoc keyVector = dataStore.findBinding(serviceKey,categoryBag,tModelBag,qualifiers);
157       if ((keyVector != null) && (keyVector.size() > 0))
158       {
159         // if a maxRows value has been specified and it's less than
160
// the number of rows we are about to return then only return
161
// maxRows specified.
162
int rowCount = keyVector.size();
163         if ((maxRows > 0) && (maxRows < rowCount))
164         {
165           rowCount = maxRows;
166           truncatedResults = true;
167         }
168
169         // iterate through the binding templates keys fetching
170
// each associated BindingTemplate in sequence.
171
bindingVector = new Vector JavaDoc(rowCount);
172         for (int i=0; i<rowCount; i++)
173           bindingVector.addElement(dataStore.fetchBinding((String JavaDoc)keyVector.elementAt(i)));
174       }
175
176       dataStore.commit();
177
178       // create a new BindingDetail instance and stuff
179
// the new bindingTemplateVector into it.
180
BindingDetail detail = new BindingDetail();
181       detail.setBindingTemplateVector(bindingVector);
182       detail.setGeneric(generic);
183       detail.setOperator(Config.getOperator());
184       detail.setTruncated(truncatedResults);
185       return detail;
186     }
187     catch(InvalidKeyPassedException keyex)
188     {
189       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
190       log.info(keyex.getMessage());
191       throw (RegistryException)keyex;
192     }
193     catch(UnsupportedException suppex)
194     {
195       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
196       log.info(suppex.getMessage());
197       throw (RegistryException)suppex;
198     }
199     catch(RegistryException regex)
200     {
201       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
202       log.error(regex);
203       throw (RegistryException)regex;
204     }
205     catch(Exception JavaDoc ex)
206     {
207       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
208       log.error(ex);
209       throw new RegistryException(ex);
210     }
211     finally
212     {
213       if (dataStore != null)
214         dataStore.release();
215     }
216   }
217
218
219   /***************************************************************************/
220   /***************************** TEST DRIVER *********************************/
221   /***************************************************************************/
222
223
224   public static void main(String JavaDoc[] args)
225   {
226     // initialize the registry
227
RegistryEngine reg = new RegistryEngine();
228     reg.init();
229
230     try
231     {
232     }
233     catch (Exception JavaDoc ex)
234     {
235       // write execption to the console
236
ex.printStackTrace();
237     }
238     finally
239     {
240       // destroy the registry
241
reg.dispose();
242     }
243   }
244 }
Popular Tags