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.engine;
36
37 import java.io.InputStream;
38 import java.text.MessageFormat;
39 import java.util.Properties;
40 import java.util.Vector;
41
42 import javax.security.auth.login.LoginContext;
43
44 import org.apache.log4j.Logger;
45 import org.apache.xml.resolver.tools.CatalogResolver;
46 import org.apache.xmlbeans.XmlObject;
47 import org.ogf.graap.wsag.api.WsagConstants;
48 import org.ogf.graap.wsag.api.configuration.WSAG4JConfiguration;
49 import org.ogf.graap.wsag.api.logging.LogMessage;
50 import org.ogf.graap.wsag.server.api.WsagMessageContext;
51 import org.ogf.graap.wsag.server.persistence.EmfRegistry;
52 import org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome;
53 import org.ogf.graap.wsag.server.persistence.IAgreementHome;
54 import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
55 import org.ogf.graap.wsag.server.persistence.impl.DatabaseAgreementHome;
56 import org.ogf.graap.wsag.server.persistence.impl.WSAG4JPersistenceFacade;
57 import org.ogf.graap.wsag4j.types.configuration.ConfigurationType;
58 import org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineConfigurationDocument;
59 import org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineConfigurationType;
60 import org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineInstanceType;
61 import org.ogf.graap.wsag4j.types.configuration.WSRFEngineConfigurationType;
62 import org.quartz.Scheduler;
63 import org.quartz.SchedulerException;
64 import org.quartz.SchedulerFactory;
65 import org.quartz.impl.StdSchedulerFactory;
66
67
68
69
70
71
72
73
74
75
76 public class WsagEngine
77 {
78
79 private static final Logger LOG = Logger.getLogger( WsagEngine.class );
80
81 private static ThreadLocal<WsagMessageContext> messageContext = null;
82
83 private static WsagEngine engine = null;
84
85 private ConfigurationType wsag4jConfiguration = null;
86
87 private WSRFEngineConfigurationType wsrfConfiguration = null;
88
89 private WSAG4JEngineConfigurationType[] engineConfigurations = null;
90
91 private String deploymentURI = null;
92
93 private LoginContext serverLoginContext;
94
95 private IAgreementFactoryHome agreementFactoryHome;
96
97 private IAgreementHome agreementHome;
98
99 private boolean allowAnonymousAccess = false;
100
101
102
103
104
105
106 public static final String WSAG4J_WSRF_ENGINE_FILE_NAME = "wsag4j.wsrf-engine.config.filename";
107
108
109
110
111
112
113 public static WsagMessageContext getWsagMessageContext()
114 {
115
116 synchronized ( messageContext )
117 {
118 WsagMessageContext wsagcontext = messageContext.get();
119
120 if ( wsagcontext == null )
121 {
122 wsagcontext = new WsagMessageContext();
123 messageContext.set( wsagcontext );
124 }
125
126 return wsagcontext;
127 }
128 }
129
130
131
132
133
134
135
136 public static void setWsagMessageContext( WsagMessageContext context )
137 {
138 WsagEngine.messageContext.set( context );
139 }
140
141 private static synchronized WsagEngine getInstance()
142 {
143 if ( engine == null )
144 {
145 engine = new WsagEngine();
146 }
147
148 return engine;
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public static void initializeEngine( String defaultGatewayURL ) throws Exception
166 {
167 getInstance().initialize( defaultGatewayURL );
168 }
169
170 private void initialize( String defaultGatewayURL ) throws Exception
171 {
172
173 if ( LOG.isTraceEnabled() )
174 {
175 LOG.trace( "WsagEngine -> initializeServer" );
176 }
177
178
179
180
181 System.setProperty( "xmlbean.entityResolver", CatalogResolver.class.getName() );
182
183 messageContext = new ThreadLocal<WsagMessageContext>();
184
185 loadWSAG4JConfiguration();
186
187 if ( LOG.isTraceEnabled() )
188 {
189 LOG.trace( "AFTER: WsagEngine -> loadWSAG4JConfiguration" );
190 }
191
192 initializeGatewayURI( defaultGatewayURL );
193
194 if ( LOG.isTraceEnabled() )
195 {
196 LOG.trace( "AFTER: WsagEngine -> initializeGatewayURI" );
197 }
198
199 loadEngineConfigurations();
200
201 initializePersistenceLayer();
202
203 if ( LOG.isTraceEnabled() )
204 {
205 LOG.trace( "AFTER: WsagEngine -> initializePersistenceLayer" );
206 }
207 }
208
209
210
211
212
213
214
215 public static void shutdownEngine() throws Exception
216 {
217
218 try
219 {
220 SchedulerFactory factory = new StdSchedulerFactory();
221 Scheduler scheduler = factory.getScheduler();
222
223 if ( scheduler.isStarted() )
224 {
225 scheduler.shutdown();
226 }
227 }
228 catch ( SchedulerException e )
229 {
230 LOG.error( "Failed to shutdown quartz scheduler.", e );
231 }
232
233 getInstance().shutdownPersistenceLayer();
234
235
236
237
238 if ( messageContext != null )
239 {
240 messageContext.set( null );
241 messageContext = null;
242 }
243 engine = null;
244 }
245
246
247
248
249
250
251
252 private void loadWSAG4JConfiguration() throws Exception
253 {
254
255 if ( LOG.isTraceEnabled() )
256 {
257 LOG.trace( "WsagEngine -> loadWSAG4JConfiguration" );
258 }
259
260
261
262
263 String filename =
264 System.getProperty( WSAG4J_WSRF_ENGINE_FILE_NAME, WsagConstants.WSAG4J_WSRF_ENGINE_CONFIG_FILE );
265
266
267
268
269 wsag4jConfiguration = WSAG4JConfiguration.findWSAG4JConfiguration( filename );
270 if ( wsag4jConfiguration != null )
271 {
272
273 wsrfConfiguration = wsag4jConfiguration.getWSRFEngineConfiguration();
274
275
276
277
278 if ( WsagEngine.getWSRFConfiguration() == null )
279 {
280 String message = "WSAG4J WSRF configuration was not found.";
281 throw new Exception( message );
282 }
283
284 }
285 else
286 {
287
288
289
290
291 String message = "WSAG4J configuration was not found.";
292 throw new Exception( message );
293
294 }
295 }
296
297
298
299
300
301 private void loadEngineConfigurations()
302 {
303 Vector<WSAG4JEngineConfigurationType> engineConfig = new Vector<WSAG4JEngineConfigurationType>();
304
305 WSAG4JEngineInstanceType[] instances = new WSAG4JEngineInstanceType[0];
306 if ( ( wsrfConfiguration != null ) && wsrfConfiguration.isSetWSAG4JEngineInstances() )
307 {
308 instances = wsrfConfiguration.getWSAG4JEngineInstances().getWSAG4JEngineArray();
309 }
310
311 if ( instances == null )
312 {
313 String message =
314 "Missing section WSAG4JEngineInstances in wsag4j engine configuration. "
315 + "No Agreement Factories were instantiated.";
316 LOG.warn( message );
317 }
318 else
319 {
320 for ( int i = 0; i < instances.length; i++ )
321 {
322 String configFile = instances[i].getEngineConfigurationFile();
323
324 try
325 {
326 InputStream in = getClass().getResourceAsStream( configFile );
327
328 if ( in == null )
329 {
330 LOG.warn( LogMessage.getMessage(
331 "The wsag4j engine configuration file [{0}] was not found. Skipping this entry.",
332 configFile ) );
333
334 continue;
335 }
336
337 WSAG4JEngineConfigurationDocument engineConfiguration =
338 (WSAG4JEngineConfigurationDocument) XmlObject.Factory.parse( in );
339 engineConfig.add( engineConfiguration.getWSAG4JEngineConfiguration() );
340 }
341 catch ( Exception e )
342 {
343 LOG.error( LogMessage.getMessage(
344 "Could not load WSAG4J engine configuration {0}. Ignoring this instance. Error: {1}",
345 configFile, e.getMessage() ) );
346 }
347 }
348 }
349
350 this.engineConfigurations =
351 engineConfig.toArray( new WSAG4JEngineConfigurationType[engineConfig.size()] );
352 }
353
354
355
356
357
358
359
360 private void initializeGatewayURI( String defaultDeploymentURI ) throws Exception
361 {
362
363 LOG.info( "WsagEngine -> initializeGatewayURI" );
364
365
366 String spConfiguredURI = loadViaSystemProperties();
367 if ( spConfiguredURI != null )
368 {
369
370 LOG.info( "Gateway address for this service is configured via System Properties." );
371 LOG.info( LogMessage.getMessage( "WS-Resources will be deployed at URI: {0}", spConfiguredURI ) );
372
373 deploymentURI = spConfiguredURI;
374 return;
375 }
376
377
378
379 String gatewayAddress = wsrfConfiguration.getGatewayAddress();
380 if ( ( gatewayAddress != null ) && ( !gatewayAddress.equals( "" ) ) )
381 {
382 Object[] filler = new Object[] { gatewayAddress };
383 String message =
384 MessageFormat.format(
385 "Loaded gateway url from configuration file. Resources will be deployed at [{0}]",
386 filler );
387 LOG.info( message );
388
389 deploymentURI = gatewayAddress;
390 return;
391 }
392
393
394
395
396
397
398
399
400 String noDeploymentURI = "No gateway address is configured for this service";
401 LOG.warn( noDeploymentURI );
402
403 String msgGeneratedWarning = "Try to generate gateway address for service";
404 LOG.warn( msgGeneratedWarning );
405
406 deploymentURI = defaultDeploymentURI;
407 }
408
409 private String loadViaSystemProperties()
410 {
411 try
412 {
413 InputStream in = WSAG4JConfiguration.findResource( WsagConstants.WSAG4J_CONFIG_FILE );
414 Properties properties = new Properties();
415 properties.load( in );
416
417 String key =
418 properties.getProperty( "org.ogf.graap.wsag.gateway.key",
419 WsagConstants.WSAG4J_GATEWAY_PROPERTY );
420
421 return System.getProperty( key );
422 }
423 catch ( Exception ex )
424 {
425 return System.getProperty( WsagConstants.WSAG4J_GATEWAY_PROPERTY );
426 }
427 }
428
429
430
431
432 public static ConfigurationType getWSAG4JConfiguration()
433 {
434 return getInstance().wsag4jConfiguration;
435 }
436
437
438
439
440 public static WSRFEngineConfigurationType getWSRFConfiguration()
441 {
442 return getInstance().wsrfConfiguration;
443 }
444
445
446
447
448 public static String getGatewayURL()
449 {
450 return getInstance().deploymentURI;
451 }
452
453
454
455
456
457
458
459
460 public static void setLoginContext( LoginContext context )
461 {
462 getInstance().serverLoginContext = context;
463 }
464
465
466
467
468
469
470
471 public static LoginContext getLoginContext()
472 {
473 return getInstance().serverLoginContext;
474 }
475
476
477
478
479
480
481 public static IAgreementFactoryHome getAgreementFactoryHome()
482 {
483 return getInstance().agreementFactoryHome;
484 }
485
486
487
488
489
490
491
492
493 public static void setAgreementFactoryHome( IAgreementFactoryHome agreementFactoryHome )
494 {
495 getInstance().agreementFactoryHome = agreementFactoryHome;
496 }
497
498
499
500
501
502
503 @Deprecated
504 public static IAgreementHome getAgreementHome()
505 {
506 return getInstance().agreementHome;
507 }
508
509
510
511
512
513
514
515
516 public static void setAgreementHome( IAgreementHome agreementHome )
517 {
518 getInstance().agreementHome = agreementHome;
519 }
520
521
522
523
524 private void initializePersistenceLayer() throws Exception
525 {
526
527 try
528 {
529 LOG.info( "WsagEngine -> initialize PersistenceLayer" );
530
531
532 agreementFactoryHome = new WSAG4JPersistenceFacade( engineConfigurations );
533 agreementFactoryHome.initialize();
534
535
536 agreementHome = new DatabaseAgreementHome( agreementFactoryHome );
537
538 LOG.info( "WsagEngine -> Persistence Layer initialized" );
539 }
540 catch ( Exception e )
541 {
542 LOG.error( "WsagEngine -> failed to initialize Persistence Layer", e );
543 throw new Exception( "Failed to initialize persistence layer.", e );
544 }
545 }
546
547
548
549
550
551
552
553 private void shutdownPersistenceLayer() throws Exception
554 {
555
556 PersistentAgreementFactory[] factories = getAgreementFactoryHome().list();
557 for ( int i = 0; i < factories.length; i++ )
558 {
559 try
560 {
561 final String msgDoSave = "Save agreement factory ''{0}''.";
562 LOG.debug( LogMessage.getMessage( msgDoSave, factories[i].getResourceId() ) );
563
564 factories[i].save();
565
566 final String msgSaved = "Agreement factory ''{0}'' saved.";
567 LOG.debug( LogMessage.getMessage( msgSaved, factories[i].getResourceId() ) );
568 }
569 catch ( Exception e )
570 {
571 String message = "Failed to save agreement factory ''{0}''.";
572 LOG.error( MessageFormat.format( message, new Object[] { factories[i].getResourceId() } ), e );
573 }
574 }
575
576
577 agreementFactoryHome = null;
578 agreementHome = null;
579
580
581 EmfRegistry.finalizeEmfRegistry();
582 }
583
584
585
586
587
588
589
590 public static void setAllowAnonymousAccess( boolean allowAnonymousAccess )
591 {
592 getInstance().allowAnonymousAccess = allowAnonymousAccess;
593 }
594
595
596
597
598
599
600 public static boolean isAllowAnonymousAccess()
601 {
602 return getInstance().allowAnonymousAccess;
603 }
604 }