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