KickJava   Java API By Example, From Geeks To Geeks.

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


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.BusinessKey;
25 import org.apache.juddi.datatype.KeyedReference;
26 import org.apache.juddi.datatype.RegistryObject;
27 import org.apache.juddi.datatype.request.FindQualifiers;
28 import org.apache.juddi.datatype.request.FindRelatedBusinesses;
29 import org.apache.juddi.datatype.response.RelatedBusinessInfos;
30 import org.apache.juddi.datatype.response.RelatedBusinessesList;
31 import org.apache.juddi.error.RegistryException;
32 import org.apache.juddi.registry.RegistryEngine;
33 import org.apache.juddi.util.Config;
34
35 /**
36  * @author Steve Viens (sviens@apache.org)
37  */

38 public class FindRelatedBusinessesFunction extends AbstractFunction
39 {
40   // private reference to jUDDI Logger
41
private static Log log = LogFactory.getLog(FindRelatedBusinessesFunction.class);
42
43   /**
44    *
45    */

46   public FindRelatedBusinessesFunction(RegistryEngine registry)
47   {
48     super(registry);
49   }
50
51   /**
52    *
53    */

54   public RegistryObject execute(RegistryObject regObject)
55     throws RegistryException
56   {
57     FindRelatedBusinesses request = (FindRelatedBusinesses)regObject;
58     String JavaDoc generic = request.getGeneric();
59     String JavaDoc businessKey = request.getBusinessKey();
60     KeyedReference keyedRef = request.getKeyedReference();
61     FindQualifiers qualifiers = request.getFindQualifiers();
62     int maxRows = request.getMaxRows();
63
64     // aquire a jUDDI datastore instance
65
DataStore dataStore = DataStoreFactory.getDataStore();
66
67     try
68     {
69       dataStore.beginTrans();
70
71       Vector JavaDoc infoVector = null;
72       boolean truncatedResults = false;
73
74       // validate request parameters & execute
75
// nothing that requires validation has been identified
76

77       // perform the search for matching business entities (return only keys in requested order)
78
infoVector = dataStore.findRelatedBusinesses(businessKey,keyedRef,qualifiers);
79       if ((infoVector != null) && (infoVector.size() > 0))
80       {
81         // if the number of keys returned is greater than maxRows then truncate the results.
82
int rowCount = infoVector.size();
83         if ((maxRows > 0) && (maxRows < rowCount))
84         {
85           rowCount = maxRows;
86           truncatedResults = true;
87         }
88       }
89
90       // create a new BusinessInfos instance and stuff
91
// the new Vector of BusinessInfos into it.
92
RelatedBusinessInfos infos = new RelatedBusinessInfos();
93       infos.setRelatedBusinessInfoVector(infoVector);
94
95       dataStore.commit();
96
97       // create a new RelatedBusinessesList instance and
98
// stuff the new relatedBusinessInfoVector into it.
99
RelatedBusinessesList list = new RelatedBusinessesList();
100       list.setGeneric(generic);
101       list.setOperator(Config.getOperator());
102       list.setTruncated(truncatedResults);
103       list.setBusinessKey(new BusinessKey(businessKey));
104       list.setRelatedBusinessInfos(infos);
105       return list;
106     }
107     catch(RegistryException regex)
108     {
109       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
110       log.error(regex);
111       throw (RegistryException)regex;
112     }
113     catch(Exception JavaDoc ex)
114     {
115       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
116       log.error(ex);
117       throw new RegistryException(ex);
118     }
119     finally
120     {
121       if (dataStore != null)
122         dataStore.release();
123     }
124   }
125
126
127   /***************************************************************************/
128   /***************************** TEST DRIVER *********************************/
129   /***************************************************************************/
130
131
132   public static void main(String JavaDoc[] args)
133   {
134     // initialize the registry
135
RegistryEngine reg = new RegistryEngine();
136     reg.init();
137
138     try
139     {
140     }
141     catch (Exception JavaDoc ex)
142     {
143       // write execption to the console
144
ex.printStackTrace();
145     }
146     finally
147     {
148       // destroy the registry
149
reg.dispose();
150     }
151   }
152 }
Popular Tags