1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.juddi.function;
17
18 import java.util.Vector;
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
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
68 AddPublisherAssertions request = (AddPublisherAssertions)regObject;
69 AuthInfo authInfo = request.getAuthInfo();
70 Vector assertionVector = request.getPublisherAssertionVector();
71 String generic = request.getGeneric();
72
73
74 DataStore dataStore = DataStoreFactory.getDataStore();
75
76 try
77 {
78 dataStore.beginTrans();
79
80
81 Publisher publisher = getPublisher(authInfo,dataStore);
82 String publisherID = publisher.getPublisherID();
83
84
85 for (int i=0; i<assertionVector.size(); i++)
86 {
87
88 PublisherAssertion assertion = (PublisherAssertion)assertionVector.elementAt(i);
89
90
91 String fromKey = assertion.getFromKey();
92 if ((fromKey == null) || (fromKey.length() == 0))
93 throw new InvalidKeyPassedException("add_publisherAssertion: "+
94 "fromKey="+fromKey);
95
96
97 String toKey = assertion.getToKey();
98 if ((toKey == null) || (toKey.length() == 0))
99 throw new InvalidKeyPassedException("add_publisherAssertion: "+
100 "toKey="+toKey);
101
102
103 KeyedReference keyedRef = assertion.getKeyedReference();
104 if (keyedRef == null)
105 throw new InvalidKeyPassedException("add_publisherAssertion: "+
106 "keyedRef="+keyedRef);
107
108
109 String tModelKey = keyedRef.getTModelKey();
110 if ((tModelKey == null) || (tModelKey.length() == 0))
111 throw new InvalidKeyPassedException("add_publisherAssertion: "+
112 "tModelKey="+keyedRef);
113
114
115
116 if ((!dataStore.isValidBusinessKey(fromKey)) && (!dataStore.isValidTModelKey(fromKey)))
117 throw new InvalidKeyPassedException("add_publisherAssertion: "+
118 "fromKey="+fromKey);
119
120
121
122 if ((!dataStore.isValidBusinessKey(toKey)) && (!dataStore.isValidTModelKey(toKey)))
123 throw new InvalidKeyPassedException("add_publisherAssertion: "+
124 "toKey="+toKey);
125
126
127
128
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 e) { }
145 log.info(keyex.getMessage());
146 throw (RegistryException)keyex;
147 }
148 catch(UserMismatchException keyex)
149 {
150 try { dataStore.rollback(); } catch(Exception e) { }
151 log.info(keyex.getMessage());
152 throw (RegistryException)keyex;
153 }
154 catch(RegistryException regex)
155 {
156 try { dataStore.rollback(); } catch(Exception e) { }
157 log.error(regex);
158 throw (RegistryException)regex;
159 }
160 catch(Exception ex)
161 {
162 try { dataStore.rollback(); } catch(Exception e) { }
163 log.error(ex);
164 throw new RegistryException(ex);
165 }
166 finally
167 {
168 if (dataStore != null)
169 dataStore.release();
170 }
171
172
173
174
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[] args)
192 {
193
194 RegistryEngine reg = new RegistryEngine();
195 reg.init();
196
197 try
198 {
199
200 GetAuthToken authTokenRequest1 = new GetAuthToken("sviens","password");
201 AuthToken authToken1 = (AuthToken)reg.execute(authTokenRequest1);
202 AuthInfo authInfo1 = authToken1.getAuthInfo();
203
204
205 BusinessEntity business1 = new BusinessEntity();
206 business1.addName(new Name("Blockbuster","en"));
207
208
209 Vector businessVector1 = new Vector(1);
210 businessVector1.addElement(business1);
211
212
213 SaveBusiness sbReq1 = new SaveBusiness();
214 sbReq1.setAuthInfo(authInfo1);
215 sbReq1.setBusinessEntityVector(businessVector1);
216 BusinessDetail detail1 = (BusinessDetail)reg.execute(sbReq1);
217 Vector detailVector1 = detail1.getBusinessEntityVector();
218 BusinessEntity b1 = (BusinessEntity)detailVector1.elementAt(0);
219
220
221
222 GetAuthToken authTokenRequest2 = new GetAuthToken("steveviens","password");
223 AuthToken authToken2 = (AuthToken)reg.execute(authTokenRequest2);
224 AuthInfo authInfo2 = authToken2.getAuthInfo();
225
226
227 BusinessEntity business2 = new BusinessEntity();
228 business2.addName(new Name("PopSecret","en"));
229
230
231 Vector businessVector2 = new Vector(1);
232 businessVector2.addElement(business2);
233
234
235 SaveBusiness sbReq2 = new SaveBusiness();
236 sbReq2.setAuthInfo(authInfo2);
237 sbReq2.setBusinessEntityVector(businessVector2);
238 BusinessDetail detail2 = (BusinessDetail)reg.execute(sbReq2);
239 Vector detailVector2 = detail2.getBusinessEntityVector();
240 BusinessEntity b2 = (BusinessEntity)detailVector2.elementAt(0);
241
242
243
244 String fromKey = b1.getBusinessKey();
245 String 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
251 Vector assertionVector = new Vector();
252 assertionVector.addElement(assertion);
253
254
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 ex)
265 {
266
267 ex.printStackTrace();
268 }
269 finally
270 {
271
272 reg.dispose();
273 }
274 }
275 }