KickJava   Java API By Example, From Geeks To Geeks.

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


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

47   public DeleteTModelFunction(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
DeleteTModel request = (DeleteTModel)regObject;
60     String JavaDoc generic = request.getGeneric();
61     AuthInfo authInfo = request.getAuthInfo();
62     Vector JavaDoc tModelKeyVector = request.getTModelKeyVector();
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<tModelKeyVector.size(); i++)
77       {
78         // grab the next key from the vector
79
String JavaDoc tModelKey = (String JavaDoc)tModelKeyVector.elementAt(i);
80
81         // check that this TModel really exists. If not
82
// then throw an InvalidKeyPassedException.
83
if ((tModelKey == null) || (tModelKey.length() == 0) ||
84             (!dataStore.isValidTModelKey(tModelKey)))
85           throw new InvalidKeyPassedException("delete_tModel: "+
86               "tModelKey="+tModelKey);
87
88         // check to make sure that 'authorizedName' controls this
89
// TModel. If not then throw a UserMismatchException.
90
if (!dataStore.isTModelPublisher(tModelKey,publisherID))
91           throw new UserMismatchException("delete_tModel: "+
92               "userID="+publisherID+", "+
93               "tModelKey="+tModelKey);
94
95         // TModel exists and we control it so let's marke it as deleted.
96
dataStore.markTModelAsDeleted(tModelKey);
97       }
98
99       // delete the TModels
100
for (int i=0; i<tModelKeyVector.size(); i++)
101       {
102         String JavaDoc tModelKey = (String JavaDoc)tModelKeyVector.elementAt(i);
103         dataStore.deleteTModel(tModelKey);
104
105         log.info("Publisher '"+publisherID+"' deleted TModel with key: "+tModelKey);
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 a request
168
DeleteTModel request = new DeleteTModel();
169
170       // invoke the server
171
DispositionReport response = (DispositionReport)(new DeleteTModelFunction(reg).execute(request));
172       System.out.println("errno: "+response.toString());
173     }
174     catch (Exception JavaDoc ex)
175     {
176       // write execption to the console
177
ex.printStackTrace();
178     }
179     finally
180     {
181       // destroy the registry
182
reg.dispose();
183     }
184   }
185 }
Popular Tags