1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.server.actions.impl;
36
37 import java.text.MessageFormat;
38 import java.util.Map;
39
40 import org.apache.log4j.Logger;
41 import org.apache.xmlbeans.XmlObject;
42 import org.ogf.graap.wsag.api.Agreement;
43 import org.ogf.graap.wsag.api.AgreementOffer;
44 import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
45 import org.ogf.graap.wsag.api.exceptions.NegotiationException;
46 import org.ogf.graap.wsag.api.logging.LogMessage;
47 import org.ogf.graap.wsag.server.actions.ActionInitializationException;
48 import org.ogf.graap.wsag.server.actions.IAction;
49 import org.ogf.graap.wsag.server.actions.ICreateAgreementAction;
50 import org.ogf.graap.wsag.server.actions.IGetTemplateAction;
51 import org.ogf.graap.wsag.server.actions.INegotiationAction;
52 import org.ogf.graap.wsag.server.api.IAgreementFactoryContext;
53 import org.ogf.graap.wsag.server.api.WsagSession;
54 import org.ogf.graap.wsag.server.api.WsagSessionManager;
55 import org.ogf.graap.wsag.server.engine.WsagEngine;
56 import org.ogf.graap.wsag4j.types.configuration.ImplementationConfigurationType;
57 import org.ogf.graap.wsag4j.types.engine.WSAG4JSessionDocument;
58 import org.ogf.graap.wsag4j.types.engine.WSAG4JSessionType;
59 import org.ogf.schemas.graap.wsAgreement.AgreementContextType;
60 import org.ogf.schemas.graap.wsAgreement.AgreementRoleType;
61 import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
62 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferType;
63 import org.w3c.dom.Node;
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public class AgreementFactoryAction
78 implements IAction
79 {
80
81 private static final Logger LOG = Logger.getLogger( AgreementFactoryAction.class );
82
83 private ICreateAgreementAction createAgreementAction;
84
85 private IGetTemplateAction getTemplateAction;
86
87 private INegotiationAction negotiationAction;
88
89 private boolean useSession = false;
90
91 private boolean supportsNegotiation = false;
92
93 private String name;
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public AgreementFactoryAction( IGetTemplateAction templateAction,
111 ICreateAgreementAction createAgreementAction,
112 INegotiationAction negotiationAction )
113 {
114 super();
115
116 setGetTemplateAction( templateAction );
117 setCreateAgreementAction( createAgreementAction );
118 setNegotiationAction( negotiationAction );
119 }
120
121
122
123
124
125
126
127
128
129 public boolean isNegotiationSupported()
130 {
131 return supportsNegotiation;
132 }
133
134
135
136
137
138
139
140 public void setGetTemplateAction( IGetTemplateAction templateAction )
141 {
142 getTemplateAction = templateAction;
143 }
144
145
146
147
148
149
150
151 public void setCreateAgreementAction( ICreateAgreementAction createAgreement )
152 {
153 createAgreementAction = createAgreement;
154 }
155
156
157
158
159
160
161
162 public void setNegotiationAction( INegotiationAction negotiationAction )
163 {
164 this.negotiationAction = negotiationAction;
165 if ( ( negotiationAction == null ) || ( negotiationAction instanceof NegotiationUnsupportedAction ) )
166 {
167 supportsNegotiation = false;
168 }
169 else
170 {
171 supportsNegotiation = true;
172 }
173 }
174
175
176
177
178
179
180 public void initialize() throws ActionInitializationException
181 {
182 if ( ( getTemplateAction == null ) || ( createAgreementAction == null ) )
183 {
184 throw new ActionInitializationException(
185 "getTemplateAction and createAgreementAction must not be null." );
186 }
187
188 try
189 {
190 getTemplateAction.initialize();
191 createAgreementAction.initialize();
192 negotiationAction.initialize();
193 }
194 catch ( Exception e )
195 {
196
197
198
199 String detailsText =
200 "Configured actions: GetTemplateAction [{0}], NegotiationAction: {1}), CreateAgreementAction: {2})";
201
202 String details =
203 LogMessage.format( detailsText, getTemplateAction.getClass().getName(),
204 negotiationAction.getClass().getName(), createAgreementAction.getClass().getName() );
205
206 String msgText =
207 "Error while initializing AgreementFactoryAction. \nReason: {0} \nDetails: {1}";
208 String message = LogMessage.format( msgText, e.getMessage(), details );
209
210 throw new ActionInitializationException( message, e );
211 }
212 }
213
214
215
216
217
218
219
220
221 public AgreementTemplateType getTemplate()
222 {
223
224 AgreementTemplateType template = getTemplateAction.getTemplate();
225 template = (AgreementTemplateType) template.copy();
226
227 if ( useSession )
228 {
229
230 AgreementContextType context = template.getContext();
231
232
233
234
235 WsagSession session = WsagSessionManager.createSession();
236
237
238
239
240 if ( context == null )
241 {
242 context = template.addNewContext();
243 context.setTemplateId( template.getTemplateId() );
244 context.setTemplateName( template.getName() );
245 context.setServiceProvider( AgreementRoleType.AGREEMENT_RESPONDER );
246 }
247
248
249
250
251 WSAG4JSessionDocument sessionDoc = WSAG4JSessionDocument.Factory.newInstance();
252 sessionDoc.addNewWSAG4JSession().setSessionID( session.getSessionId() );
253 Node imported =
254 context.getDomNode().getOwnerDocument()
255 .importNode( sessionDoc.getWSAG4JSession().getDomNode(), true );
256 context.getDomNode().appendChild( imported );
257 }
258
259 return template;
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 public Agreement createAgreement( AgreementOffer offer ) throws AgreementFactoryException
277 {
278 XmlObject[] sessionDocuments =
279 offer.getContext().selectChildren( WSAG4JSessionDocument.type.getDocumentElementName() );
280 if ( sessionDocuments.length > 0 )
281 {
282 if ( sessionDocuments.length > 1 )
283 {
284 String message =
285 "Found multiple wsag4j session documents in agreement context. Using the first, ignoring the rest.";
286 LOG.warn( message );
287 }
288
289 WSAG4JSessionType sessionToken = (WSAG4JSessionType) sessionDocuments[0];
290
291 WsagSession session = WsagSessionManager.getSession( sessionToken.getSessionID() );
292 if ( session != null )
293 {
294 WsagEngine.getWsagMessageContext().setSession( session );
295 }
296 else
297 {
298 Object[] filler = new Object[] { sessionToken.getSessionID() };
299 String message = MessageFormat.format( "WSAG4J session with id [{0}] not found.", filler );
300 LOG.error( message );
301 }
302 }
303
304 return createAgreementAction.createAgreement( offer );
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325 public NegotiationOfferType[] negotiate( NegotiationOfferType quote, Map<String, Object> context )
326 throws NegotiationException
327 {
328
329
330
331
332
333
334 if ( negotiationAction == null )
335 {
336 String message = "Negotiation is not supported by this agreement factory";
337 throw new UnsupportedOperationException( message );
338 }
339
340 return negotiationAction.negotiate( quote, context );
341 }
342
343
344
345
346 public boolean isUsingSession()
347 {
348 return useSession;
349 }
350
351
352
353
354
355 public void setUseSession( boolean useSession )
356 {
357 this.useSession = useSession;
358 }
359
360
361
362
363 public String getName()
364 {
365 if ( name == null )
366 {
367 return getClass().getName();
368 }
369
370 return name;
371 }
372
373
374
375
376
377 public void setName( String name )
378 {
379 this.name = name;
380 }
381
382
383
384
385
386
387 private ImplementationConfigurationType actionConfiguration;
388
389
390
391
392
393
394
395
396
397 public ImplementationConfigurationType getActionConfiguration()
398 {
399
400 return actionConfiguration;
401
402 }
403
404
405
406
407
408
409
410
411
412 public void setActionConfiguration( ImplementationConfigurationType configuration )
413 {
414
415 this.actionConfiguration = configuration;
416
417 }
418
419
420
421
422
423
424
425 private IAgreementFactoryContext factoryContext;
426
427
428
429
430
431
432
433
434
435 public IAgreementFactoryContext getFactoryContext()
436 {
437
438 return factoryContext;
439
440 }
441
442
443
444
445
446
447
448
449 public void setFactoryContext( IAgreementFactoryContext factoryContext )
450 {
451
452 this.factoryContext = factoryContext;
453
454 }
455
456
457
458
459
460 public INegotiationAction getNegotiationAction()
461 {
462 return negotiationAction;
463 }
464 }