KickJava   Java API By Example, From Geeks To Geeks.

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


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.RegistryObject;
25 import org.apache.juddi.datatype.publisher.Publisher;
26 import org.apache.juddi.datatype.request.AuthInfo;
27 import org.apache.juddi.datatype.request.DeletePublisher;
28 import org.apache.juddi.datatype.response.DispositionReport;
29 import org.apache.juddi.datatype.response.Result;
30 import org.apache.juddi.error.RegistryException;
31 import org.apache.juddi.registry.RegistryEngine;
32 import org.apache.juddi.util.Config;
33
34 /**
35  * @author Steve Viens (sviens@apache.org)
36  */

37 public class DeletePublisherFunction extends AbstractFunction
38 {
39   // private reference to jUDDI Logger
40
private static Log log = LogFactory.getLog(DeletePublisherFunction.class);
41
42   /**
43    *
44    */

45   public DeletePublisherFunction(RegistryEngine registry)
46   {
47     super(registry);
48   }
49
50   /**
51    * Here are the rules:
52    *
53    * 1. A publisher must have administrative privs to
54    * delete anything.
55    * 2. A publisher cannot delete itself.
56    */

57   public RegistryObject execute(RegistryObject regObject)
58     throws RegistryException
59   {
60     // extract individual parameters
61
DeletePublisher request = (DeletePublisher)regObject;
62     String JavaDoc generic = request.getGeneric();
63     AuthInfo authInfo = request.getAuthInfo();
64     Vector JavaDoc publisherIDVector = request.getPublisherIDVector();
65
66     // aquire a jUDDI datastore instance
67
DataStore dataStore = DataStoreFactory.getDataStore();
68
69     try
70     {
71       dataStore.beginTrans();
72
73       // validate authentication parameters
74
Publisher publisher = getPublisher(authInfo,dataStore);
75       String JavaDoc publisherID = publisher.getPublisherID();
76
77       // Only a user with administrative access can delete publishers
78
if (!dataStore.isAdministrator(publisherID))
79         throw new RegistryException("Invalid Operation, You must have " +
80           "administrative priveledges to delete a publisher account.");
81
82       // validate request parameters
83
for (int i=0; i<publisherIDVector.size(); i++)
84       {
85         // grab the next key from the vector
86
String JavaDoc pubID = (String JavaDoc)publisherIDVector.elementAt(i);
87
88         if (pubID.equalsIgnoreCase(publisherID))
89           throw new RegistryException("Invalid Operation, A publisher " +
90             "cannot delete it's own publisher account.");
91
92         // TModel exists and we control it so let's delete it.
93
dataStore.deletePublisher(pubID);
94       }
95
96       // delete the TModels
97
for (int i=0; i<publisherIDVector.size(); i++)
98       {
99         String JavaDoc tModelKey = (String JavaDoc)publisherIDVector.elementAt(i);
100         dataStore.deleteTModel(tModelKey);
101
102         log.info("Publisher '"+publisherID+"' deleted TModel with key: "+tModelKey);
103       }
104
105       dataStore.commit();
106     }
107     catch(RegistryException regex)
108     {
109       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
110       log.error(regex);
111       throw (RegistryException)regex;
112     }
113     catch(Exception JavaDoc ex)
114     {
115       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
116       log.error(ex);
117       throw new RegistryException(ex);
118     }
119     finally
120     {
121       if (dataStore != null)
122         dataStore.release();
123     }
124
125     // We didn't encounter any problems so let's create an
126
// E_SUCCESS Result, embed it in a DispositionReport
127
// and return it.
128
Result result = new Result(Result.E_SUCCESS);
129     result.setErrCode(Result.lookupErrCode(Result.E_SUCCESS));
130     DispositionReport dispRpt = new DispositionReport();
131     dispRpt.setGeneric(generic);
132     dispRpt.setOperator(Config.getOperator());
133     dispRpt.addResult(result);
134     
135     return dispRpt;
136   }
137
138
139   /***************************************************************************/
140   /***************************** TEST DRIVER *********************************/
141   /***************************************************************************/
142
143
144   public static void main(String JavaDoc[] args)
145   {
146     // initialize the registry
147
RegistryEngine reg = new RegistryEngine();
148     reg.init();
149
150     try
151     {
152     }
153     catch (Exception JavaDoc ex)
154     {
155       // write execption to the console
156
ex.printStackTrace();
157     }
158     finally
159     {
160       // destroy the registry
161
reg.dispose();
162     }
163   }
164 }
Popular Tags