View Javadoc

1   package fr.in2p3.jsaga.engine.descriptors;
2   
3   import fr.in2p3.jsaga.adaptor.base.usage.Usage;
4   import fr.in2p3.jsaga.adaptor.resource.ResourceAdaptor;
5   import fr.in2p3.jsaga.engine.schema.config.Resource;
6   import org.ogf.saga.error.IncorrectURLException;
7   import org.ogf.saga.error.NoSuccessException;
8   
9   import java.util.HashMap;
10  import java.util.Map;
11  
12  /* ***************************************************
13  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
14  * ***             http://cc.in2p3.fr/             ***
15  * ***************************************************
16  * File:   DataAdaptorDescriptor
17  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
18  * Date:   21 juin 2007
19  * ***************************************************
20  * Description:                                      */
21  /**
22   *
23   */
24  public class ResourceAdaptorDescriptor {
25      private Map m_classes;
26      private Map m_usages;
27      private Map<String,Map> m_defaults;
28      protected Resource[] m_xml;
29  
30      public ResourceAdaptorDescriptor(Class[] adaptorClasses, SecurityAdaptorDescriptor securityDesc) throws IllegalAccessException, InstantiationException, IncorrectURLException {
31          m_classes = new HashMap();
32          m_usages = new HashMap();
33          m_defaults = new HashMap<String,Map>();
34          m_xml = new Resource[adaptorClasses.length];
35          for (int i=0; i<adaptorClasses.length; i++) {
36              ResourceAdaptor adaptor = (ResourceAdaptor) adaptorClasses[i].newInstance();
37              if (adaptor.getType() == null) {
38                  throw new InstantiationException("Bad adaptor: no type defined");
39              }
40  
41              // set class
42              m_classes.put(adaptor.getType(), adaptorClasses[i]);
43  
44              // set usage
45              Usage usage = adaptor.getUsage();
46              if (usage != null) {
47                  m_usages.put(adaptor.getType(), usage);
48              }
49  
50              // set defaults
51              m_defaults.put(adaptor.getType(), AdaptorDescriptors.getDefaultsMap(adaptor));
52  
53              // set XML
54              m_xml[i] = toXML(adaptor, securityDesc);
55          }
56      }
57  
58      public Class getClass(String scheme) throws NoSuccessException {
59          Class clazz = (Class) m_classes.get(scheme);
60          if (clazz != null) {
61              return clazz;
62          } else {
63              throw new NoSuccessException("Found no data adaptor supporting scheme: "+ scheme);
64          }
65      }
66  
67      public Usage getUsage(String scheme) {
68          return (Usage) m_usages.get(scheme);
69      }
70  
71      public Map getDefaultsMap(String scheme) {
72          return m_defaults.get(scheme);
73      }
74  
75      public Resource[] getXML() {
76          return m_xml;
77      }
78  
79      private static Resource toXML(ResourceAdaptor adaptor, SecurityAdaptorDescriptor securityDesc) throws IncorrectURLException {
80          Resource resc = new Resource();
81  
82          // add default data service
83          resc.setType(adaptor.getType());
84          resc.setImpl(adaptor.getClass().getName());
85          if (adaptor.getSupportedSecurityCredentialClasses() != null) {
86              String[] supportedContextTypes = securityDesc.getSupportedContextTypes(adaptor.getSupportedSecurityCredentialClasses());
87              resc.setSupportedContextType(supportedContextTypes);
88          }
89          if (adaptor.getUsage() != null) {
90              resc.setUsage(adaptor.getUsage().toString());
91          }
92          AdaptorDescriptors.setDefaults(resc, adaptor);
93          return resc;
94      }
95  }