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