View Javadoc

1   package fr.in2p3.jsaga.engine.descriptors;
2   
3   import fr.in2p3.jsaga.engine.config.ConfigurationException;
4   
5   import java.net.URL;
6   import java.util.*;
7   
8   /* ***************************************************
9   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
10  * ***             http://cc.in2p3.fr/             ***
11  * ***************************************************
12  * File:   AdaptorLoader
13  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
14  * Date:   20 juin 2007
15  * ***************************************************
16  * Description:                                      */
17  /**
18   *
19   */
20  public class AdaptorLoader {
21      private static final String ADAPTOR_PROPERTIES = "META-INF/adaptor.properties";
22      private Class[] m_adaptorClasses;
23  
24      public AdaptorLoader() throws ConfigurationException {
25      	String clazzName=null;
26          try {
27              List list = new ArrayList();
28              Enumeration e = AdaptorLoader.class.getClassLoader().getResources(ADAPTOR_PROPERTIES);
29              while (e.hasMoreElements()) {
30                  URL url = (URL) e.nextElement();
31                  Properties prop = new Properties();
32                  prop.load(url.openStream());
33                  for (Enumeration e2=prop.propertyNames(); e2.hasMoreElements(); ) {
34                      clazzName = (String) e2.nextElement();
35                      Class clazz = Class.forName(clazzName);
36                      list.add(clazz);
37                  }
38              }
39              m_adaptorClasses = (Class[]) list.toArray(new Class[list.size()]);
40          } catch(Exception e) {
41              throw new ConfigurationException("Failed to load class " + clazzName, e);
42          }
43      }
44  
45      public Class[] getClasses(Class itf) {
46          List list = new ArrayList();
47          for (int i=0; i<m_adaptorClasses.length; i++) {
48              Class current = m_adaptorClasses[i];
49              if (hasInterface(current, itf)) {
50                  list.add(current);
51              }
52          }
53          return (Class[]) list.toArray(new Class[list.size()]);
54      }
55  
56      private static boolean hasInterface(Class clazz, Class itf) {
57          Set itfSet = new HashSet();
58          listInterfaces(clazz, itfSet);
59          return itfSet.contains(itf);
60      }
61      
62      private static void listInterfaces(Class clazz, Set set) {
63          // list interfaces of implemented interfaces
64          Class[] interfaces = clazz.getInterfaces();
65          for (int i=0; i<interfaces.length; i++) {
66              set.add(interfaces[i]);
67              listInterfaces(interfaces[i], set);
68          }
69          // list interfaces of extended class
70          Class extended = clazz.getSuperclass();
71          if (extended != null) {
72              listInterfaces(extended, set);
73          }
74      }
75  }