View Javadoc

1   package fr.in2p3.jsaga.impl.context;
2   
3   import fr.in2p3.jsaga.Base;
4   import fr.in2p3.jsaga.EngineProperties;
5   import fr.in2p3.jsaga.engine.config.ConfigurationException;
6   import fr.in2p3.jsaga.engine.session.SessionConfiguration;
7   import fr.in2p3.jsaga.generated.session.Attribute;
8   import org.ogf.saga.context.Context;
9   import org.ogf.saga.context.ContextFactory;
10  import org.ogf.saga.error.*;
11  
12  /* ***************************************************
13  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
14  * ***             http://cc.in2p3.fr/             ***
15  * ***************************************************
16  * File:   ConfigurableContextFactory
17  * Author: Lionel Schwarz (lionel.schwarz@in2p3.fr)
18  * Date:   31/03/2011
19  * ***************************************************
20  * Description:                                      */
21  
22  public class ConfigurableContextFactory {
23      private static final String JSAGA_FACTORY = Base.getSagaFactory();
24  
25      public static Context[] getContextsOfDefaultSession() throws IncorrectStateException, TimeoutException, NoSuccessException {
26          SessionConfiguration cfg = new SessionConfiguration(EngineProperties.getURL(EngineProperties.JSAGA_DEFAULT_CONTEXTS));
27          fr.in2p3.jsaga.generated.session.Context[] sessionContextsCfg = cfg.getSessionContextsCfg();
28          Context[] sessionContexts = new Context[sessionContextsCfg.length];
29          for (int i=0; i<sessionContextsCfg.length; i++) {
30              // create SAGA context
31              sessionContexts[i] = ContextFactory.createContext(JSAGA_FACTORY, sessionContextsCfg[i].getType());
32              // set attributes
33              SessionConfiguration.setDefaultContext(sessionContexts[i], sessionContextsCfg[i]);
34          }
35          return sessionContexts;
36      }
37  	
38      public static ConfiguredContext[] listConfiguredContext() throws ConfigurationException {
39      	SessionConfiguration cfg = new SessionConfiguration(EngineProperties.getURL(EngineProperties.JSAGA_DEFAULT_CONTEXTS));
40      	fr.in2p3.jsaga.generated.session.Context[] sessionContextsCfg = cfg.getSessionContextsCfg();
41      	ConfiguredContext[] cfgContexts = new ConfiguredContext[sessionContextsCfg.length];
42      	for (int i=0; i<sessionContextsCfg.length; i++) {
43      		for (Attribute attr : sessionContextsCfg[i].getAttribute()) {
44      			if (ContextImpl.URL_PREFIX.equals(attr.getName())) {
45      	    		cfgContexts[i] = new ConfiguredContext(attr.getValue(), sessionContextsCfg[i].getType());
46      	    		break;
47      			}
48      		}
49      	}
50      	return cfgContexts;
51      }
52      
53      public static Context createContext(ConfiguredContext ctx) throws IncorrectStateException, TimeoutException, NoSuccessException {
54      	SessionConfiguration cfg = new SessionConfiguration(EngineProperties.getURL(EngineProperties.JSAGA_DEFAULT_CONTEXTS));
55      	fr.in2p3.jsaga.generated.session.Context[] sessionContextsCfg = cfg.getSessionContextsCfg();
56      	for (int i=0; i<sessionContextsCfg.length; i++) {
57      		for (Attribute attr : sessionContextsCfg[i].getAttribute()) {
58      			if (ContextImpl.URL_PREFIX.equals(attr.getName())) {
59  		          	if (ctx.getUrlPrefix().equals(attr.getValue())) {
60  		        		// create SAGA context
61  		        		Context context = ContextFactory.createContext(JSAGA_FACTORY, sessionContextsCfg[i].getType());
62  		        		// set attributes
63  		        		SessionConfiguration.setDefaultContext(context, sessionContextsCfg[i]);
64  		        		return context;
65  		        	}
66      			}
67      		}
68          }
69      	throw new NoSuccessException("No context with id: " + ctx.getUrlPrefix());
70      }
71  
72  }