KickJava   Java API By Example, From Geeks To Geeks.

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


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.Description;
26 import org.apache.juddi.datatype.KeyedReference;
27 import org.apache.juddi.datatype.Name;
28 import org.apache.juddi.datatype.RegistryObject;
29 import org.apache.juddi.datatype.business.BusinessEntity;
30 import org.apache.juddi.datatype.publisher.Publisher;
31 import org.apache.juddi.datatype.request.AuthInfo;
32 import org.apache.juddi.datatype.request.GetAuthToken;
33 import org.apache.juddi.datatype.request.SaveBusiness;
34 import org.apache.juddi.datatype.request.SaveService;
35 import org.apache.juddi.datatype.response.AuthToken;
36 import org.apache.juddi.datatype.response.BusinessDetail;
37 import org.apache.juddi.datatype.response.ServiceDetail;
38 import org.apache.juddi.datatype.service.BusinessService;
39 import org.apache.juddi.datatype.service.BusinessServices;
40 import org.apache.juddi.datatype.tmodel.TModel;
41 import org.apache.juddi.error.InvalidKeyPassedException;
42 import org.apache.juddi.error.RegistryException;
43 import org.apache.juddi.error.UserMismatchException;
44 import org.apache.juddi.registry.RegistryEngine;
45 import org.apache.juddi.util.Config;
46 import org.apache.juddi.uuidgen.UUIDGen;
47 import org.apache.juddi.uuidgen.UUIDGenFactory;
48
49 /**
50  * @author Steve Viens (sviens@apache.org)
51  */

52 public class SaveServiceFunction extends AbstractFunction
53 {
54   // private reference to jUDDI Logger
55
private static Log log = LogFactory.getLog(SaveServiceFunction.class);
56
57   /**
58    *
59    */

60   public SaveServiceFunction(RegistryEngine registry)
61   {
62     super(registry);
63   }
64
65   /**
66    *
67    */

68   public RegistryObject execute(RegistryObject regObject)
69     throws RegistryException
70   {
71     SaveService request = (SaveService)regObject;
72     String JavaDoc generic = request.getGeneric();
73     AuthInfo authInfo = request.getAuthInfo();
74     Vector JavaDoc serviceVector = request.getBusinessServiceVector();
75     UUIDGen uuidgen = UUIDGenFactory.getUUIDGen();
76
77     // aquire a jUDDI datastore instance
78
DataStore dataStore = DataStoreFactory.getDataStore();
79
80     try
81     {
82       dataStore.beginTrans();
83
84       // validate authentication parameters
85
Publisher publisher = getPublisher(authInfo,dataStore);
86       String JavaDoc publisherID = publisher.getPublisherID();
87
88       // Validate request parameters & execute
89
for (int i=0; i<serviceVector.size(); i++)
90       {
91         // Move the BusinessService data into a form we can work with easily
92
BusinessService service = (BusinessService)serviceVector.elementAt(i);
93         String JavaDoc businessKey = service.getBusinessKey();
94         String JavaDoc serviceKey = service.getServiceKey();
95
96         // If a BusinessKey wasn't included or it is an invalid BusinessKey then
97
// throw an InvalidKeyPassedException
98
if ((businessKey == null) || (businessKey.length() == 0) || (!dataStore.isValidBusinessKey(businessKey)))
99           throw new InvalidKeyPassedException("save_service: "+
100               "businessKey="+businessKey);
101
102         // Confirm that 'publisherID' controls the BusinessEntity that this
103
// BusinessService belongs to. If not then throw a UserMismatchException.
104
if (!dataStore.isBusinessPublisher(businessKey,publisherID))
105           throw new UserMismatchException("save_service: "+
106               "userID="+publisherID+", "+
107               "businessKey="+serviceKey);
108
109         // If a ServiceKey was specified then make sure it's a valid one.
110
if (((serviceKey != null) && (serviceKey.length() > 0)) && (!dataStore.isValidServiceKey(serviceKey)))
111           throw new InvalidKeyPassedException("save_service: "+
112               "serviceKey="+serviceKey);
113
114         // Normally, a valid tModelKey MUST be specified for the keyedReference
115
// to be valid. However, in the case of a keyedReference that is used in
116
// a categoryBag, the tModelKey may be omitted or specified as a
117
// zero-length string to indicate that the taxonomy being used is
118
// uddi-org:general_keywords. When it is omitted in this manner, the UDDI
119
// registry will insert the proper key during the save_xx operation.
120
// - UDDI Programmers API v2.04 Section 4.3.5.1 Specifying keyedReferences
121
//
122
CategoryBag categoryBag = service.getCategoryBag();
123         if (categoryBag != null)
124         {
125           Vector JavaDoc keyedRefVector = categoryBag.getKeyedReferenceVector();
126           if (keyedRefVector != null)
127           {
128             int vectorSize = keyedRefVector.size();
129             if (vectorSize > 0)
130             {
131               for (int j=0; j<vectorSize; j++)
132               {
133                 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(j);
134                 String JavaDoc key = keyedRef.getTModelKey();
135                 
136                 // A null or zero-length tModelKey is treated as
137
// though the tModelKey for uddiorg:general_keywords
138
// had been specified.
139
//
140
if ((key == null) || (key.trim().length() == 0))
141                   keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY);
142               }
143             }
144           }
145         }
146       }
147
148       for (int i=0; i<serviceVector.size(); i++)
149       {
150         // move the BusinessService data into a form we can work with easily
151
BusinessService service = (BusinessService)serviceVector.elementAt(i);
152         String JavaDoc serviceKey = service.getServiceKey();
153
154         // If the new BusinessService has a ServiceKey then it must already
155
// exists so delete the old one. It a ServiceKey isn't specified then
156
// this is a new BusinessService so create a new ServiceKey for it.
157
if ((serviceKey != null) && (serviceKey.length() > 0))
158           dataStore.deleteService(serviceKey);
159         else
160           service.setServiceKey(uuidgen.uuidgen());
161
162         // everything checks out so let's save it.
163
dataStore.saveService(service);
164       }
165
166       dataStore.commit();
167
168       // create a new ServiceDetail and stuff the
169
// original but 'updated' serviceVector in.
170
ServiceDetail detail = new ServiceDetail();
171       detail.setGeneric(generic);
172       detail.setOperator(Config.getOperator());
173       detail.setTruncated(false);
174       detail.setBusinessServiceVector(serviceVector);
175       return detail;
176     }
177     catch(InvalidKeyPassedException ikpex)
178     {
179       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
180       log.info(ikpex);
181       throw (RegistryException)ikpex;
182     }
183     catch(UserMismatchException umex)
184     {
185       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
186       log.info(umex);
187       throw (RegistryException)umex;
188     }
189     catch(RegistryException regex)
190     {
191       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
192       log.error(regex);
193       throw (RegistryException)regex;
194     }
195     catch(Exception JavaDoc ex)
196     {
197       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
198       log.error(ex);
199       throw new RegistryException(ex);
200     }
201     finally
202     {
203       if (dataStore != null)
204         dataStore.release();
205     }
206   }
207
208
209   /***************************************************************************/
210   /***************************** TEST DRIVER *********************************/
211   /***************************************************************************/
212
213
214   public static void main(String JavaDoc[] args)
215   {
216     // initialize the registry
217
RegistryEngine reg = new RegistryEngine();
218     reg.init();
219
220     try
221     {
222       // create & execute the GetAuthToken request
223
GetAuthToken authTokenRequest = new GetAuthToken("sviens","password");
224       AuthToken authToken = (AuthToken)reg.execute(authTokenRequest);
225       AuthInfo authInfo = authToken.getAuthInfo();
226
227       // generate a Name Vector
228
Vector JavaDoc nameVector = new Vector JavaDoc();
229       nameVector.add(new Name("Dow Chemical"));
230
231       // generate a BusinessService
232
BusinessService service = new BusinessService();
233       service.addName(new Name("Reaction Finder"));
234       service.addDescription(new Description("Finds side effects when combining chemicals"));
235
236       // generate a BusinessServices
237
BusinessServices services = new BusinessServices();
238       services.addBusinessService(service);
239
240       // generate a BusinessEntity
241
BusinessEntity businessEntity = new BusinessEntity();
242       businessEntity.setBusinessKey(null);
243       businessEntity.setNameVector(nameVector);
244       businessEntity.setBusinessServices(services);
245
246       // generate a BusinessEntity Vector
247
Vector JavaDoc businessEntityVector = new Vector JavaDoc();
248       businessEntityVector.add(businessEntity);
249
250       // create & execute the SaveBusiness request
251
SaveBusiness request = new SaveBusiness();
252       request.setAuthInfo(authInfo);
253       request.setBusinessEntityVector(businessEntityVector);
254       BusinessDetail detail = (BusinessDetail)reg.execute(request);
255     }
256     catch (Exception JavaDoc ex)
257     {
258       // write execption to the console
259
ex.printStackTrace();
260     }
261     finally
262     {
263       // destroy the registry
264
reg.dispose();
265     }
266   }
267 }
Popular Tags