KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

49   public DeleteBindingFunction(RegistryEngine registry)
50   {
51     super(registry);
52   }
53
54   /**
55    *
56    */

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