KickJava   Java API By Example, From Geeks To Geeks.

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


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.KeyedReference;
25 import org.apache.juddi.datatype.Name;
26 import org.apache.juddi.datatype.RegistryObject;
27 import org.apache.juddi.datatype.assertion.PublisherAssertion;
28 import org.apache.juddi.datatype.business.BusinessEntity;
29 import org.apache.juddi.datatype.publisher.Publisher;
30 import org.apache.juddi.datatype.request.AddPublisherAssertions;
31 import org.apache.juddi.datatype.request.AuthInfo;
32 import org.apache.juddi.datatype.request.GetAuthToken;
33 import org.apache.juddi.datatype.request.SaveBusiness;
34 import org.apache.juddi.datatype.response.AuthToken;
35 import org.apache.juddi.datatype.response.BusinessDetail;
36 import org.apache.juddi.datatype.response.DispositionReport;
37 import org.apache.juddi.datatype.response.Result;
38 import org.apache.juddi.datatype.tmodel.TModel;
39 import org.apache.juddi.error.InvalidKeyPassedException;
40 import org.apache.juddi.error.RegistryException;
41 import org.apache.juddi.error.UserMismatchException;
42 import org.apache.juddi.registry.RegistryEngine;
43 import org.apache.juddi.util.Config;
44
45 /**
46  * @author Steve Viens (sviens@apache.org)
47  */

48 public class AddPublisherAssertionsFunction extends AbstractFunction
49 {
50   // private reference to jUDDI Logger
51
private static Log log = LogFactory.getLog(AddPublisherAssertionsFunction.class);
52   
53   /**
54    *
55    */

56   public AddPublisherAssertionsFunction(RegistryEngine registry)
57   {
58     super(registry);
59   }
60
61   /**
62    *
63    */

64   public RegistryObject execute(RegistryObject regObject)
65     throws RegistryException
66   {
67     // extract individual parameters
68
AddPublisherAssertions request = (AddPublisherAssertions)regObject;
69     AuthInfo authInfo = request.getAuthInfo();
70     Vector JavaDoc assertionVector = request.getPublisherAssertionVector();
71     String JavaDoc generic = request.getGeneric();
72
73     // aquire a jUDDI datastore instance
74
DataStore dataStore = DataStoreFactory.getDataStore();
75
76     try
77     {
78       dataStore.beginTrans();
79
80       // validate authentication parameters
81
Publisher publisher = getPublisher(authInfo,dataStore);
82       String JavaDoc publisherID = publisher.getPublisherID();
83
84       // validate request parameters & execute
85
for (int i=0; i<assertionVector.size(); i++)
86       {
87         // transform each PublisherAssertion data into a form we can work with easily
88
PublisherAssertion assertion = (PublisherAssertion)assertionVector.elementAt(i);
89
90         // make sure we've got a 'fromKey'
91
String JavaDoc fromKey = assertion.getFromKey();
92         if ((fromKey == null) || (fromKey.length() == 0))
93           throw new InvalidKeyPassedException("add_publisherAssertion: "+
94               "fromKey="+fromKey);
95
96         // make sure we've got a 'toKey'
97
String JavaDoc toKey = assertion.getToKey();
98         if ((toKey == null) || (toKey.length() == 0))
99           throw new InvalidKeyPassedException("add_publisherAssertion: "+
100               "toKey="+toKey);
101
102         // make sure we've got a 'KeyedRefernce'
103
KeyedReference keyedRef = assertion.getKeyedReference();
104         if (keyedRef == null)
105           throw new InvalidKeyPassedException("add_publisherAssertion: "+
106               "keyedRef="+keyedRef);
107
108         // make sure the 'KeyedRefernce' contains a 'TModelKey'
109
String JavaDoc tModelKey = keyedRef.getTModelKey();
110         if ((tModelKey == null) || (tModelKey.length() == 0))
111           throw new InvalidKeyPassedException("add_publisherAssertion: "+
112               "tModelKey="+keyedRef);
113
114         // verify that the BusinessEntities or tModel identified by the 'fromKey'
115
// really exists. If not then throw an InvalidKeyPassedException.
116
if ((!dataStore.isValidBusinessKey(fromKey)) && (!dataStore.isValidTModelKey(fromKey)))
117           throw new InvalidKeyPassedException("add_publisherAssertion: "+
118               "fromKey="+fromKey);
119
120         // verify that the BusinessEntitys or tModel identified by the 'fromKey'
121
// really exists. If not then throw an InvalidKeyPassedException.
122
if ((!dataStore.isValidBusinessKey(toKey)) && (!dataStore.isValidTModelKey(toKey)))
123           throw new InvalidKeyPassedException("add_publisherAssertion: "+
124               "toKey="+toKey);
125
126         // verify that the 'publisherID' controls at least one of the
127
// BusinessEntities or TModels that are identified in this
128
// assertion. If not then throw a UserMismatchException.
129
if ((!dataStore.isBusinessPublisher(fromKey,publisherID)) &&
130             (!dataStore.isBusinessPublisher(toKey,publisherID)) &&
131             (!dataStore.isTModelPublisher(fromKey,publisherID)) &&
132             (!dataStore.isTModelPublisher(toKey,publisherID)))
133           throw new UserMismatchException("add_publisherAssertion: "+
134               "userID="+publisherID+", "+
135               "fromKey="+fromKey+", "+
136               "toKey="+toKey);
137       }
138
139       dataStore.saveAssertions(publisherID,assertionVector);
140       dataStore.commit();
141     }
142     catch(InvalidKeyPassedException keyex)
143     {
144       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
145       log.info(keyex.getMessage());
146       throw (RegistryException)keyex;
147     }
148     catch(UserMismatchException keyex)
149     {
150       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
151       log.info(keyex.getMessage());
152       throw (RegistryException)keyex;
153     }
154     catch(RegistryException regex)
155     {
156       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
157       log.error(regex);
158       throw (RegistryException)regex;
159     }
160     catch(Exception JavaDoc ex)
161     {
162       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
163       log.error(ex);
164       throw new RegistryException(ex);
165     }
166     finally
167     {
168       if (dataStore != null)
169         dataStore.release();
170     }
171
172     // We didn't encounter any problems so let's create an
173
// E_SUCCESS Result, embed it in a DispositionReport
174
// and return it.
175
Result result = new Result(Result.E_SUCCESS);
176     result.setErrCode(Result.lookupErrCode(Result.E_SUCCESS));
177     DispositionReport dispRpt = new DispositionReport();
178     dispRpt.setGeneric(generic);
179     dispRpt.setOperator(Config.getOperator());
180     dispRpt.addResult(result);
181     
182     return dispRpt;
183   }
184
185
186   /***************************************************************************/
187   /***************************** TEST DRIVER *********************************/
188   /***************************************************************************/
189
190
191   public static void main(String JavaDoc[] args)
192   {
193     // initialize the registry
194
RegistryEngine reg = new RegistryEngine();
195     reg.init();
196
197     try
198     {
199       // create & execute the GetAuthToken1 request
200
GetAuthToken authTokenRequest1 = new GetAuthToken("sviens","password");
201       AuthToken authToken1 = (AuthToken)reg.execute(authTokenRequest1);
202       AuthInfo authInfo1 = authToken1.getAuthInfo();
203
204       // create a couple of business entities
205
BusinessEntity business1 = new BusinessEntity();
206       business1.addName(new Name("Blockbuster","en"));
207
208       // BusinessEntity Vector
209
Vector JavaDoc businessVector1 = new Vector JavaDoc(1);
210       businessVector1.addElement(business1);
211
212       // create & execute the SaveBusiness request
213
SaveBusiness sbReq1 = new SaveBusiness();
214       sbReq1.setAuthInfo(authInfo1);
215       sbReq1.setBusinessEntityVector(businessVector1);
216       BusinessDetail detail1 = (BusinessDetail)reg.execute(sbReq1);
217       Vector JavaDoc detailVector1 = detail1.getBusinessEntityVector();
218       BusinessEntity b1 = (BusinessEntity)detailVector1.elementAt(0);
219
220
221       // create & execute the GetAuthToken2 request
222
GetAuthToken authTokenRequest2 = new GetAuthToken("steveviens","password");
223       AuthToken authToken2 = (AuthToken)reg.execute(authTokenRequest2);
224       AuthInfo authInfo2 = authToken2.getAuthInfo();
225
226       // create a BusinessEntity
227
BusinessEntity business2 = new BusinessEntity();
228       business2.addName(new Name("PopSecret","en"));
229
230       // create a BusinessEntity Vector
231
Vector JavaDoc businessVector2 = new Vector JavaDoc(1);
232       businessVector2.addElement(business2);
233
234       // create & execute the SaveBusiness request
235
SaveBusiness sbReq2 = new SaveBusiness();
236       sbReq2.setAuthInfo(authInfo2);
237       sbReq2.setBusinessEntityVector(businessVector2);
238       BusinessDetail detail2 = (BusinessDetail)reg.execute(sbReq2);
239       Vector JavaDoc detailVector2 = detail2.getBusinessEntityVector();
240       BusinessEntity b2 = (BusinessEntity)detailVector2.elementAt(0);
241
242
243       // create a new PublisherAssertion
244
String JavaDoc fromKey = b1.getBusinessKey();
245       String JavaDoc toKey = b2.getBusinessKey();
246       KeyedReference keyedReference = new KeyedReference ("Partner Company","peer-peer");
247       keyedReference.setTModelKey(TModel.RELATIONSHIPS_TMODEL_KEY);
248       PublisherAssertion assertion = new PublisherAssertion(fromKey,toKey,keyedReference);
249
250       // create a PublisherAssertion Vector
251
Vector JavaDoc assertionVector = new Vector JavaDoc();
252       assertionVector.addElement(assertion);
253
254       // create an AddPublisherAssertions request & invoke the server
255
AddPublisherAssertions apaReq = new AddPublisherAssertions();
256       apaReq.setAuthInfo(authInfo1);
257       apaReq.setPublisherAssertionVector(assertionVector);
258       DispositionReport dspRpt1 = (DispositionReport)reg.execute(apaReq);
259       System.out.println("errno: "+dspRpt1.toString());
260       System.out.println();
261       DispositionReport dspRpt2 = (DispositionReport)reg.execute(apaReq);
262       System.out.println("errno: "+dspRpt2.toString());
263     }
264     catch (Exception JavaDoc ex)
265     {
266       // write execption to the console
267
ex.printStackTrace();
268     }
269     finally
270     {
271       // destroy the registry
272
reg.dispose();
273     }
274   }
275 }
Popular Tags