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.RegistryObject;
25  import org.apache.juddi.datatype.publisher.Publisher;
26  import org.apache.juddi.datatype.request.AuthInfo;
27  import org.apache.juddi.datatype.request.SavePublisher;
28  import org.apache.juddi.datatype.response.PublisherDetail;
29  import org.apache.juddi.error.RegistryException;
30  import org.apache.juddi.registry.RegistryEngine;
31  import org.apache.juddi.util.Config;
32  
33  /***
34   * @author Steve Viens (sviens@apache.org)
35   */
36  public class SavePublisherFunction extends AbstractFunction
37  {
38    // private reference to jUDDI Logger
39    private static Log log = LogFactory.getLog(SavePublisherFunction.class);
40  
41    /***
42     *
43     */
44    public SavePublisherFunction(RegistryEngine registry)
45    {
46      super(registry);
47    }
48  
49    /***
50     *
51     */
52    public RegistryObject execute(RegistryObject regObject)
53      throws RegistryException
54    {
55      SavePublisher request = (SavePublisher)regObject;
56      String generic = request.getGeneric();
57      AuthInfo authInfo = request.getAuthInfo();
58      Vector publisherVector = request.getPublisherVector();
59  
60      // aquire a jUDDI datastore instance
61      DataStore dataStore = DataStoreFactory.getDataStore();
62  
63      try
64      {
65        dataStore.beginTrans();
66  
67        // validate authentication parameters
68        Publisher publisher = getPublisher(authInfo,dataStore);
69        String publisherID = publisher.getPublisherID();
70  
71        // Only a user with administrative access can create new publishers
72        if (!dataStore.isAdministrator(publisherID))
73          throw new RegistryException("Invalid Operation, You must have " +
74            "administrative priveledges to create a new publisher account.");
75        
76        // validate request parameters
77        for (int i=0; i<publisherVector.size(); i++)
78        {
79          // move the Publisher into a form we can work with easily
80          Publisher pub = (Publisher)publisherVector.elementAt(i);
81          String pubID = pub.getPublisherID();
82  
83          // Make sure a PublisherID was specified.
84          if ((pubID == null) || (pubID.length() == 0))
85            throw new RegistryException("A valid Publisher ID was " +
86              "not specified: "+pubID);
87        }
88  
89        for (int i=0; i<publisherVector.size(); i++)
90        {
91          // move the Publisher into a form we can work with easily
92          Publisher pub = (Publisher)publisherVector.elementAt(i);
93          String pubID = pub.getPublisherID();
94  
95          // if the publisher account arleady exists then delete it.
96          dataStore.deletePublisher(pubID);
97  
98          // Everything checks out so let's save it.
99          dataStore.savePublisher(pub);
100       }
101 
102       dataStore.commit();
103 
104       PublisherDetail detail = new PublisherDetail();
105       detail.setGeneric(generic);
106       detail.setOperator(Config.getOperator());
107       detail.setTruncated(false);
108       detail.setPublisherVector(publisherVector);
109       return detail;
110     }
111     catch(RegistryException regex)
112     {
113       try { dataStore.rollback(); } catch(Exception e) { }
114       log.error(regex);
115       throw (RegistryException)regex;
116     }
117     catch(Exception ex)
118     {
119       try { dataStore.rollback(); } catch(Exception e) { }
120       log.error(ex);
121       throw new RegistryException(ex);
122     }
123     finally
124     {
125       if (dataStore != null)
126         dataStore.release();
127     }
128   }
129 
130   /****************************************************************************/
131   /****************************** TEST DRIVER *********************************/
132   /****************************************************************************/
133 
134 
135   public static void main(String[] args)
136   {
137     // initialize the registry
138     RegistryEngine reg = new RegistryEngine();
139     reg.init();
140 
141     try
142     {
143     }
144     catch (Exception ex)
145     {
146       // write execption to the console
147       ex.printStackTrace();
148     }
149     finally
150     {
151       // destroy the registry
152       reg.dispose();
153     }
154   }
155 }