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.security.SecurityAdaptor;
5   import fr.in2p3.jsaga.engine.schema.config.Context;
6   import org.ogf.saga.error.NoSuccessException;
7   
8   import java.util.*;
9   
10  /* ***************************************************
11  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
12  * ***             http://cc.in2p3.fr/             ***
13  * ***************************************************
14  * File:   SecurityAdaptorDescriptor
15  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
16  * Date:   21 juin 2007
17  * ***************************************************
18  * Description:                                      */
19  /**
20   *
21   */
22  public class SecurityAdaptorDescriptor {
23      private Map m_adaptorClasses;
24      private Map m_credentialClasses;
25      private Map m_usages;
26      protected Context[] m_xml;
27  
28      public SecurityAdaptorDescriptor(Class[] adaptorClasses) throws IllegalAccessException, InstantiationException {
29          m_adaptorClasses = new HashMap();
30          m_credentialClasses = new HashMap();
31          m_usages = new HashMap();
32          m_xml = new Context[adaptorClasses.length];
33          for (int i=0; i<adaptorClasses.length; i++) {
34              SecurityAdaptor adaptor = (SecurityAdaptor) adaptorClasses[i].newInstance();
35  
36              // type
37              m_adaptorClasses.put(adaptor.getType(), adaptorClasses[i]);
38              m_credentialClasses.put(adaptor.getType(), adaptor.getSecurityCredentialClass());
39              Usage usage = adaptor.getUsage();
40              if (usage != null) {
41                  m_usages.put(adaptor.getType(), usage);
42              }
43              m_xml[i] = toXML(adaptor);
44          }
45      }
46  
47      public Class getAdaptorClass(String type) throws NoSuccessException {
48          Class clazz = (Class) m_adaptorClasses.get(type);
49          if (clazz != null) {
50              return clazz;
51          } else {
52              throw new NoSuccessException("Found no security adaptor supporting type: "+type);
53          }
54      }
55  
56      public Usage getUsage(String type) {
57          return (Usage) m_usages.get(type);
58      }
59  
60      public String[] getSupportedContextTypes(Class[] supportedSecurityAdaptorClasses) {
61          Set contextTypeSet = new HashSet();
62          for (Iterator it= m_credentialClasses.entrySet().iterator(); it.hasNext(); ) {
63              Map.Entry entry = (Map.Entry) it.next();
64              String contextType = (String) entry.getKey();
65              Class securityAdaptorClazz = (Class) entry.getValue();
66              if (isSupported(securityAdaptorClazz, supportedSecurityAdaptorClasses)) {
67                  contextTypeSet.add(contextType);
68              }
69          }
70          if (isSupportedNoContext(supportedSecurityAdaptorClasses)) {
71              contextTypeSet.add("None");
72          }
73          return (String[]) contextTypeSet.toArray(new String[contextTypeSet.size()]);
74      }
75  
76      public static boolean isSupportedNoContext(Class[] supportedClazzArray) {
77          if (supportedClazzArray!=null && supportedClazzArray.length>0) {
78              for (int i=0; supportedClazzArray!=null && i<supportedClazzArray.length; i++) {
79                  if (supportedClazzArray[i] == null) {
80                      return true;
81                  }
82              }
83              return false;
84          } else {
85              return true;
86          }
87      }
88  
89      public static boolean isSupported(Class securityAdaptorClazz, Class[] supportedClazzArray) {
90          for (int i=0; supportedClazzArray!=null && i<supportedClazzArray.length; i++) {
91              if (superClassesContain(securityAdaptorClazz, supportedClazzArray[i])) {
92                  return true;
93              }
94          }
95          return false;
96      }
97      private static boolean superClassesContain(Class securityAdaptorClazz, Class supportedClazz) {
98          if (securityAdaptorClazz.equals(supportedClazz)) {
99              // stop condition
100             return true;
101         } else {
102             // recurse
103             Class superClazz = securityAdaptorClazz.getSuperclass();
104             if (superClazz != null) {
105                 return superClassesContain(superClazz, supportedClazz);
106             } else {
107                 return false;
108             }
109         }
110     }
111 
112     private static Context toXML(SecurityAdaptor adaptor) {
113         Context ctx = new Context();
114         ctx.setName(adaptor.getType()); // default identifier
115         ctx.setType(adaptor.getType());
116         ctx.setImpl(adaptor.getClass().getName());
117         if (adaptor.getUsage() != null) {
118             ctx.setUsage(adaptor.getUsage().toString());
119         }
120         AdaptorDescriptors.setDefaults(ctx, adaptor);
121         return ctx;
122     }
123 }