KickJava   Java API By Example, From Geeks To Geeks.

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


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.DeleteBusiness;
28 import org.apache.juddi.datatype.response.DispositionReport;
29 import org.apache.juddi.datatype.response.Result;
30 import org.apache.juddi.error.InvalidKeyPassedException;
31 import org.apache.juddi.error.RegistryException;
32 import org.apache.juddi.error.UserMismatchException;
33 import org.apache.juddi.registry.RegistryEngine;
34 import org.apache.juddi.util.Config;
35
36 /**
37  * @author Steve Viens (sviens@apache.org)
38  */

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

47   public DeleteBusinessFunction(RegistryEngine registry)
48   {
49     super(registry);
50   }
51
52   /**
53    *
54    */

55   public RegistryObject execute(RegistryObject regObject)
56     throws RegistryException
57   {
58     // extract individual parameters
59
DeleteBusiness request = (DeleteBusiness)regObject;
60     String JavaDoc generic = request.getGeneric();
61     AuthInfo authInfo = request.getAuthInfo();
62     Vector JavaDoc businessKeyVector = request.getBusinessKeyVector();
63
64     // aquire a jUDDI datastore instance
65
DataStore dataStore = DataStoreFactory.getDataStore();
66
67     try
68     {
69       dataStore.beginTrans();
70
71       // validate authentication parameters
72
Publisher publisher = getPublisher(authInfo,dataStore);
73       String JavaDoc publisherID = publisher.getPublisherID();
74
75       // validate request parameters
76
for (int i=0; i<businessKeyVector.size(); i++)
77       {
78         // grab the next key from the vector
79
String JavaDoc businessKey = (String JavaDoc)businessKeyVector.elementAt(i);
80
81         // check that this business entity really exists.
82
// If not then throw an InvalidKeyPassedException.
83
if ((!dataStore.isValidBusinessKey(businessKey)))
84           throw new InvalidKeyPassedException("delete_business: "+
85               "businessKey="+businessKey);
86
87         // check to make sure that 'authorizedName' controls this
88
// business entity. If not then throw a UserMismatchException.
89
if (!dataStore.isBusinessPublisher(businessKey,publisherID))
90           throw new UserMismatchException("delete_business: "+
91               "userID="+publisherID+", "+
92               "businessKey="+businessKey);
93       }
94
95       // delete the BusinessEntities
96
for (int i=0; i<businessKeyVector.size(); i++)
97       {
98         String JavaDoc businessKey = (String JavaDoc)businessKeyVector.elementAt(i);
99         dataStore.deleteBusiness(businessKey);
100
101         log.info("Publisher '"+publisherID+"' deleted BusinessEntity with key: "+businessKey);
102       }
103
104       dataStore.commit();
105     }
106     catch(InvalidKeyPassedException keyex)
107     {
108       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
109       log.info(keyex.getMessage());
110       throw (RegistryException)keyex;
111     }
112     catch(UserMismatchException umex)
113     {
114       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
115       log.info(umex.getMessage());
116       throw (RegistryException)umex;
117     }
118     catch(RegistryException regex)
119     {
120       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
121       log.error(regex);
122       throw (RegistryException)regex;
123     }
124     catch(Exception JavaDoc ex)
125     {
126       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
127       log.error(ex);
128       throw new RegistryException(ex);
129     }
130     finally
131     {
132       if (dataStore != null)
133         dataStore.release();
134     }
135
136     // We didn't encounter any problems so let's create an
137
// E_SUCCESS Result, embed it in a DispositionReport
138
// and return it.
139
Result result = new Result(Result.E_SUCCESS);
140     result.setErrCode(Result.lookupErrCode(Result.E_SUCCESS));
141     DispositionReport dispRpt = new DispositionReport();
142     dispRpt.setGeneric(generic);
143     dispRpt.setOperator(Config.getOperator());
144     dispRpt.addResult(result);
145     
146     return dispRpt;
147   }
148
149
150   /***************************************************************************/
151   /***************************** TEST DRIVER *********************************/
152   /***************************************************************************/
153
154
155   public static void main(String JavaDoc[] args)
156   {
157     // initialize the registry
158
RegistryEngine reg = new RegistryEngine();
159     reg.init();
160
161     try
162     {
163       // create a request
164
DeleteBusiness request = new DeleteBusiness();
165
166       // invoke the server
167
DispositionReport response = (DispositionReport)reg.execute(request);
168       System.out.println("errno: "+response.toString());
169     }
170     catch (Exception JavaDoc ex)
171     {
172       // write execption to the console
173
ex.printStackTrace();
174     }
175     finally
176     {
177       // destroy the registry
178
reg.dispose();
179     }
180   }
181 }
Popular Tags