KickJava   Java API By Example, From Geeks To Geeks.

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


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.DeleteService;
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 DeleteServiceFunction extends AbstractFunction
40 {
41   // private reference to jUDDI Logger
42
private static Log log = LogFactory.getLog(DeleteServiceFunction.class);
43
44   /**
45    *
46    */

47   public DeleteServiceFunction(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
DeleteService request = (DeleteService)regObject;
60     String JavaDoc generic = request.getGeneric();
61     AuthInfo authInfo = request.getAuthInfo();
62     Vector JavaDoc serviceKeyVector = request.getServiceKeyVector();
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<serviceKeyVector.size(); i++)
77       {
78         // grab the next key from the vector
79
String JavaDoc serviceKey = (String JavaDoc)serviceKeyVector.elementAt(i);
80
81         // check that this business server really exists.
82
// If not then throw an InvalidKeyPassedException.
83
if ((serviceKey == null) || (serviceKey.length() == 0) ||
84             (!dataStore.isValidServiceKey(serviceKey)))
85           throw new InvalidKeyPassedException("delete_service: "+
86               "serviceKey="+serviceKey);
87
88         // check to make sure that 'authorizedName' controls the
89
// business entity that this server belongs to. If not
90
// then throw a UserMismatchException.
91
if (!dataStore.isServicePublisher(serviceKey,publisherID))
92           throw new UserMismatchException("delete_service: "+
93               "userID="+publisherID+", "+
94               "serviceKey="+serviceKey);
95       }
96
97       // delete the BusinessServices
98
for (int i=0; i<serviceKeyVector.size(); i++)
99       {
100         String JavaDoc serviceKey = (String JavaDoc)serviceKeyVector.elementAt(i);
101         dataStore.deleteService(serviceKey);
102
103         log.info("Publisher '"+publisherID+"' deleted BusinessService with key: "+serviceKey);
104       }
105
106       dataStore.commit();
107     }
108     catch(InvalidKeyPassedException keyex)
109     {
110       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
111       log.info(keyex.getMessage());
112       throw (RegistryException)keyex;
113     }
114     catch(UserMismatchException umex)
115     {
116       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
117       log.info(umex.getMessage());
118       throw (RegistryException)umex;
119     }
120     catch(RegistryException regex)
121     {
122       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
123       log.error(regex);
124       throw (RegistryException)regex;
125     }
126     catch(Exception JavaDoc ex)
127     {
128       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
129       log.error(ex);
130       throw new RegistryException(ex);
131     }
132     finally
133     {
134       if (dataStore != null)
135         dataStore.release();
136     }
137
138     // We didn't encounter any problems so let's create an
139
// E_SUCCESS Result, embed it in a DispositionReport
140
// and return it.
141
Result result = new Result(Result.E_SUCCESS);
142     result.setErrCode(Result.lookupErrCode(Result.E_SUCCESS));
143     DispositionReport dispRpt = new DispositionReport();
144     dispRpt.setGeneric(generic);
145     dispRpt.setOperator(Config.getOperator());
146     dispRpt.addResult(result);
147     
148     return dispRpt;
149   }
150
151
152   /***************************************************************************/
153   /***************************** TEST DRIVER *********************************/
154   /***************************************************************************/
155
156
157   public static void main(String JavaDoc[] args)
158   {
159     // initialize the registry
160
RegistryEngine reg = new RegistryEngine();
161     reg.init();
162
163     try
164     {
165       // create a request
166
DeleteService request = new DeleteService();
167
168       // invoke the server
169
DispositionReport response = (DispositionReport)(new DeleteServiceFunction(reg).execute(request));
170       System.out.println("errno: "+response.toString());
171     }
172     catch (Exception JavaDoc ex)
173     {
174       // write execption to the console
175
ex.printStackTrace();
176     }
177     finally
178     {
179       // destroy the registry
180
reg.dispose();
181     }
182   }
183 }
Popular Tags