001    package com.khubla.pragmatach.framework.scanner;
002    
003    import java.lang.annotation.Annotation;
004    import java.lang.reflect.Method;
005    import java.net.URL;
006    import java.util.HashSet;
007    import java.util.Set;
008    
009    import javax.servlet.ServletContext;
010    
011    import org.scannotation.AnnotationDB;
012    import org.scannotation.ClasspathUrlFinder;
013    import org.scannotation.WarUrlFinder;
014    import org.slf4j.Logger;
015    import org.slf4j.LoggerFactory;
016    
017    import com.khubla.pragmatach.framework.api.PragmatachException;
018    
019    /**
020     * @author tome
021     */
022    public class AnnotationScanner {
023       /**
024        * logger
025        */
026       private static final Logger logger = LoggerFactory.getLogger(AnnotationScanner.class);
027       /**
028        * the DB
029        */
030       private static AnnotationDB annotationDB = new AnnotationDB();
031    
032       /**
033        * get classes with annotation
034        */
035       public static Set<Class<?>> getAllClasses(Class<?> annotationClass) throws PragmatachException {
036          try {
037             final Set<Class<?>> ret = new HashSet<Class<?>>();
038             final Set<String> classNames = annotationDB.getAnnotationIndex().get(annotationClass.getCanonicalName());
039             if (null != classNames) {
040                for (final String name : classNames) {
041                   final Class<?> clazz = Class.forName(name);
042                   ret.add(clazz);
043                }
044             }
045             return ret;
046          } catch (final Exception e) {
047             throw new PragmatachException("Exception in getAllClasses", e);
048          }
049       }
050    
051       /**
052        * get classes with annotation
053        */
054       public static Set<Method> getAllMethods(Class<?> annotationClass) throws PragmatachException {
055          try {
056             final Set<Method> ret = new HashSet<Method>();
057             final Set<String> classNames = annotationDB.getAnnotationIndex().get(annotationClass.getCanonicalName());
058             if (null != classNames) {
059                for (final String name : classNames) {
060                   final Class<?> clazz = Class.forName(name);
061                   for (final Method method : clazz.getMethods()) {
062                      for (final Annotation annotation : method.getDeclaredAnnotations()) {
063                         if (0 == annotation.getClass().getCanonicalName().compareTo(annotationClass.getCanonicalName())) {
064                            ret.add(method);
065                         }
066                      }
067                   }
068                }
069             }
070             return ret;
071          } catch (final Exception e) {
072             throw new PragmatachException("Exception in getAllMethods", e);
073          }
074       }
075    
076       public static AnnotationDB getAnnotationDB() {
077          return annotationDB;
078       }
079    
080       /**
081        * do the scan
082        */
083       public static void scan(ServletContext servletContext) throws PragmatachException {
084          scanClassPath();
085          scanWar(servletContext);
086       }
087    
088       private static void scanClassPath() throws PragmatachException {
089          try {
090             final URL[] urls = ClasspathUrlFinder.findClassPaths();
091             annotationDB.scanArchives(urls);
092          } catch (final Exception e) {
093             throw new PragmatachException("Exception in scanClassPath", e);
094          }
095       }
096    
097       private static void scanWar(ServletContext servletContext) throws PragmatachException {
098          try {
099             if (null != servletContext) {
100                final URL[] libURLs = WarUrlFinder.findWebInfLibClasspaths(servletContext);
101                final URL classesURL = WarUrlFinder.findWebInfClassesPath(servletContext);
102                if ((null != libURLs) && (libURLs.length > 0)) {
103                   annotationDB.scanArchives(libURLs);
104                } else {
105                   logger.error("Unable to get WebInfLibClasspaths");
106                }
107                if (null != classesURL) {
108                   annotationDB.scanArchives(classesURL);
109                } else {
110                   logger.error("Unable to get WebInfClassesPath");
111                }
112             }
113          } catch (final Exception e) {
114             throw new PragmatachException("Exception in scanWar", e);
115          }
116       }
117    }