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