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.AgreementFactoryContext;
44 import org.ogf.graap.wsag.api.AgreementOffer;
45 import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
46 import org.ogf.graap.wsag.api.exceptions.NegotiationException;
47 import org.ogf.graap.wsag.api.logging.LogMessage;
48 import org.ogf.graap.wsag.server.actions.ActionInitializationException;
49 import org.ogf.graap.wsag.server.actions.IAction;
50 import org.ogf.graap.wsag.server.actions.ICreateAgreementAction;
51 import org.ogf.graap.wsag.server.actions.IGetTemplateAction;
52 import org.ogf.graap.wsag.server.actions.INegotiationAction;
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 @Override
181 public void initialize() throws ActionInitializationException
182 {
183 if ( ( getTemplateAction == null ) || ( createAgreementAction == null ) )
184 {
185 throw new ActionInitializationException(
186 "getTemplateAction and createAgreementAction must not be null." );
187 }
188
189 try
190 {
191 getTemplateAction.initialize();
192 createAgreementAction.initialize();
193 negotiationAction.initialize();
194 }
195 catch ( Exception e )
196 {
197
198
199
200 String detailsText =
201 "Configured actions: GetTemplateAction [{0}], NegotiationAction: {1}), CreateAgreementAction: {2})";
202
203 String details =
204 LogMessage.format( detailsText, getTemplateAction.getClass().getName(),
205 negotiationAction.getClass().getName(), createAgreementAction.getClass().getName() );
206
207 String msgText = "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
277
278 public Agreement createAgreement( AgreementOffer offer, Map<String, Object> context )
279 throws AgreementFactoryException
280 {
281 XmlObject[] sessionDocuments =
282 offer.getContext().selectChildren( WSAG4JSessionDocument.type.getDocumentElementName() );
283 if ( sessionDocuments.length > 0 )
284 {
285 if ( sessionDocuments.length > 1 )
286 {
287 String message =
288 "Found multiple wsag4j session documents in agreement context. Using the first, ignoring the rest.";
289 LOG.warn( message );
290 }
291
292 WSAG4JSessionType sessionToken = (WSAG4JSessionType) sessionDocuments[0];
293
294 WsagSession session = WsagSessionManager.getSession( sessionToken.getSessionID() );
295 if ( session != null )
296 {
297 createAgreementAction.getHandlerContext().setSession( session );
298 }
299 else
300 {
301 Object[] filler = new Object[] { sessionToken.getSessionID() };
302 String message = MessageFormat.format( "WSAG4J session with id [{0}] not found.", filler );
303 LOG.error( message );
304 }
305 }
306
307 return createAgreementAction.createAgreement( offer, context );
308 }
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328 public NegotiationOfferType[] negotiate( NegotiationOfferType quote, Map<String, Object> context )
329 throws NegotiationException
330 {
331
332
333
334
335
336
337 if ( negotiationAction == null )
338 {
339 String message = "Negotiation is not supported by this agreement factory";
340 throw new UnsupportedOperationException( message );
341 }
342
343 return negotiationAction.negotiate( quote, context );
344 }
345
346
347
348
349 public boolean isUsingSession()
350 {
351 return useSession;
352 }
353
354
355
356
357
358 public void setUseSession( boolean useSession )
359 {
360 this.useSession = useSession;
361 }
362
363
364
365
366 public String getName()
367 {
368 if ( name == null )
369 {
370 return getClass().getName();
371 }
372
373 return name;
374 }
375
376
377
378
379
380 public void setName( String name )
381 {
382 this.name = name;
383 }
384
385
386
387
388
389
390 private ImplementationConfigurationType actionConfiguration;
391
392
393
394
395
396
397
398
399
400 @Override
401 public ImplementationConfigurationType getActionConfiguration()
402 {
403
404 return actionConfiguration;
405
406 }
407
408
409
410
411
412
413
414
415
416 @Override
417 public void setActionConfiguration( ImplementationConfigurationType configuration )
418 {
419
420 this.actionConfiguration = configuration;
421
422 }
423
424
425
426
427
428
429
430 private AgreementFactoryContext factoryContext;
431
432 private WsagEngine engine;
433
434
435
436
437
438
439
440
441
442 @Override
443 public AgreementFactoryContext getFactoryContext()
444 {
445
446 return factoryContext;
447
448 }
449
450
451
452
453
454
455
456
457 @Override
458 public void setFactoryContext( AgreementFactoryContext factoryContext )
459 {
460
461 this.factoryContext = factoryContext;
462
463 }
464
465
466
467
468
469 public INegotiationAction getNegotiationAction()
470 {
471 return negotiationAction;
472 }
473
474
475
476
477
478
479 @Override
480 public WsagEngine getEngine()
481 {
482 return engine;
483 }
484
485
486
487
488
489
490 @Override
491 public void setEngine( WsagEngine engine )
492 {
493 this.engine = engine;
494
495 }
496 }