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.KeyedReference;
26  import org.apache.juddi.datatype.RegistryObject;
27  import org.apache.juddi.datatype.binding.BindingTemplate;
28  import org.apache.juddi.datatype.publisher.Publisher;
29  import org.apache.juddi.datatype.request.AuthInfo;
30  import org.apache.juddi.datatype.request.SaveBinding;
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.UserMismatchException;
36  import org.apache.juddi.registry.RegistryEngine;
37  import org.apache.juddi.util.Config;
38  import org.apache.juddi.uuidgen.UUIDGen;
39  import org.apache.juddi.uuidgen.UUIDGenFactory;
40  
41  /***
42   * @author Steve Viens (sviens@apache.org)
43   */
44  public class SaveBindingFunction extends AbstractFunction
45  {
46    // private reference to jUDDI Logger
47    private static Log log = LogFactory.getLog(SaveBindingFunction.class);
48  
49    /***
50     *
51     */
52    public SaveBindingFunction(RegistryEngine registry)
53    {
54      super(registry);
55    }
56  
57    /***
58     *
59     */
60    public RegistryObject execute(RegistryObject regObject)
61      throws RegistryException
62    {
63      SaveBinding request = (SaveBinding)regObject;
64      String generic = request.getGeneric();
65      AuthInfo authInfo = request.getAuthInfo();
66      Vector bindingVector = request.getBindingTemplateVector();
67      UUIDGen uuidgen = UUIDGenFactory.getUUIDGen();
68  
69      // Aquire a jUDDI datastore instance
70      DataStore dataStore = DataStoreFactory.getDataStore();
71  
72      try
73      {
74        dataStore.beginTrans();
75  
76        // Validate authentication parameters
77        Publisher publisher = getPublisher(authInfo,dataStore);
78        String publisherID = publisher.getPublisherID();
79  
80        // Validate request parameters
81        for (int i=0; i<bindingVector.size(); i++)
82        {
83          // Move the BindingTemplate into a form we can work with easily
84          BindingTemplate binding = (BindingTemplate)bindingVector.elementAt(i);
85          String serviceKey = binding.getServiceKey();
86          String bindingKey = binding.getBindingKey();
87  
88          // Confirm that the 'BusinessService' that this binding belongs to
89          // really exists. If not then throw an InvalidKeyPassedException.
90         if ((serviceKey == null) || (serviceKey.length() == 0) || (!dataStore.isValidServiceKey(serviceKey)))
91            throw new InvalidKeyPassedException("save_binding: "+
92                "serviceKey="+serviceKey);
93  
94          // Confirm that 'publisherID' controls the BusinessService that this
95          // binding template belongs to.  If not then throw a UserMismatchException.
96          if (!dataStore.isServicePublisher(serviceKey,publisherID))
97            throw new UserMismatchException("save_binding: "+
98                "publisherID="+publisherID+", "+
99                "serviceKey="+serviceKey);
100 
101         // If a BindingKey was specified then make sure it's a valid one.
102         if ((bindingKey != null) && (bindingKey.length() > 0) && (!dataStore.isValidBindingKey(bindingKey)))
103           throw new InvalidKeyPassedException("save_binding: "+
104               "bindingKey="+bindingKey);
105 
106         // Normally, a valid tModelKey MUST be specified for the keyedReference 
107         // to be valid. However, in the case of a keyedReference that is used in 
108         // a categoryBag, the tModelKey may be omitted or specified as a 
109         // zero-length string to indicate that the taxonomy being used is
110         // uddi-org:general_keywords. When it is omitted in this manner, the UDDI 
111         // registry will insert the proper key during the save_xx operation.
112         // - UDDI Programmers API v2.04 Section 4.3.5.1 Specifying keyedReferences
113         //
114         CategoryBag categoryBag = binding.getCategoryBag();
115         if (categoryBag != null)
116         {
117           Vector keyedRefVector = categoryBag.getKeyedReferenceVector();
118           if (keyedRefVector != null)
119           {
120             int vectorSize = keyedRefVector.size();
121             if (vectorSize > 0)
122             {
123               for (int j=0; j<vectorSize; j++)
124               {
125                 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(j);
126                 String key = keyedRef.getTModelKey();
127                 
128                 // A null or zero-length tModelKey is treated as 
129                 // though the tModelKey for uddiorg:general_keywords 
130                 // had been specified.
131                 //
132                 if ((key == null) || (key.trim().length() == 0))
133                   keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY);
134               }
135             }
136           }
137         }
138       }
139 
140       for (int i=0; i<bindingVector.size(); i++)
141       {
142         // move the BindingTemplate data into a form we can work with easily
143         BindingTemplate binding = (BindingTemplate)bindingVector.elementAt(i);
144         String bindingKey = binding.getBindingKey();
145 
146         // If the new BindingTemplate has a BindingKey then it must already
147         // exists so delete the old one. It a BindingKey isn't specified then
148         // this is a new BindingTemplate so create a new BindingKey for it.
149         if ((bindingKey != null) && (bindingKey.length() > 0))
150           dataStore.deleteBinding(bindingKey);
151         else
152           binding.setBindingKey(uuidgen.uuidgen());
153 
154         // everything checks out so let's save it.
155         dataStore.saveBinding(binding);
156       }
157 
158       dataStore.commit();
159 
160       BindingDetail detail = new BindingDetail();
161       detail.setGeneric(generic);
162       detail.setOperator(Config.getOperator());
163       detail.setTruncated(false);
164       detail.setBindingTemplateVector(bindingVector);
165       return detail;
166     }
167     catch(InvalidKeyPassedException ikpex)
168     {
169       try { dataStore.rollback(); } catch(Exception e) { }
170       log.info(ikpex);
171       throw (RegistryException)ikpex;
172     }
173     catch(UserMismatchException umex)
174     {
175       try { dataStore.rollback(); } catch(Exception e) { }
176       log.info(umex);
177       throw (RegistryException)umex;
178     }
179     catch(RegistryException regex)
180     {
181       try { dataStore.rollback(); } catch(Exception e) { }
182       log.error(regex);
183       throw (RegistryException)regex;
184     }
185     catch(Exception ex)
186     {
187       try { dataStore.rollback(); } catch(Exception e) { }
188       log.error(ex);
189       throw new RegistryException(ex);
190     }
191     finally
192     {
193       if (dataStore != null)
194         dataStore.release();
195     }
196   }
197 
198 
199   /****************************************************************************/
200   /****************************** TEST DRIVER *********************************/
201   /****************************************************************************/
202 
203 
204   public static void main(String[] args)
205   {
206     // initialize the registry
207     RegistryEngine reg = new RegistryEngine();
208     reg.init();
209 
210     try
211     {
212     }
213     catch (Exception ex)
214     {
215       // write execption to the console
216       ex.printStackTrace();
217     }
218     finally
219     {
220       // destroy the registry
221       reg.dispose();
222     }
223   }
224 }
225