001    package com.khubla.pragmatach.framework.controller;
002    
003    import java.lang.reflect.Method;
004    import java.util.HashSet;
005    import java.util.Set;
006    
007    import org.slf4j.Logger;
008    import org.slf4j.LoggerFactory;
009    
010    import com.khubla.pragmatach.framework.annotation.Controller;
011    import com.khubla.pragmatach.framework.annotation.Route;
012    import com.khubla.pragmatach.framework.api.PragmatachException;
013    import com.khubla.pragmatach.framework.scanner.AnnotationScanner;
014    
015    /**
016     * @author tome
017     */
018    public class ControllerClasses {
019       /**
020        * logger
021        */
022       private static Logger logger = LoggerFactory.getLogger(ControllerClasses.class);
023       /**
024        * routes
025        */
026       private static Set<Method> routerMethods = new HashSet<Method>();
027       /**
028        * controllers
029        */
030       private static Set<Class<?>> controllers = new HashSet<Class<?>>();
031    
032       /**
033        * do the annotations scan
034        */
035       public static void buildDB() throws PragmatachException {
036          try {
037             /*
038              * controllers
039              */
040             final Set<Class<?>> annotatedControllers = AnnotationScanner.getAllClasses(Controller.class);
041             for (final Class<?> clazz : annotatedControllers) {
042                controllers.add(clazz);
043                logger.info("Found controller '" + clazz.getName() + "'");
044             }
045             /*
046              * routes
047              */
048             final Set<Class<?>> controllersWithRoutes = AnnotationScanner.getAllClasses(Route.class);
049             for (final Class<?> clazz : controllersWithRoutes) {
050                final Method[] methods = clazz.getMethods();
051                if (null != methods) {
052                   for (final Method method : methods) {
053                      if (method.isAnnotationPresent(Route.class)) {
054                         if (method.isAnnotationPresent(Route.class)) {
055                            routerMethods.add(method);
056                            logger.info("Found router method '" + method.getDeclaringClass().getName() + ":" + method.getName() + "' with route specification '" + method.getAnnotation(Route.class).uri()
057                                  + "'");
058                         }
059                      }
060                   }
061                }
062             }
063          } catch (final Exception e) {
064             throw new PragmatachException("Exception in processAnnotations", e);
065          }
066       }
067    
068       public static Set<Class<?>> getControllers() {
069          return controllers;
070       }
071    
072       public static Set<Method> getRouterMethods() {
073          return routerMethods;
074       }
075    
076       public static void setControllers(Set<Class<?>> controllers) {
077          ControllerClasses.controllers = controllers;
078       }
079    
080       public static void setRouterMethods(Set<Method> routerMethods) {
081          ControllerClasses.routerMethods = routerMethods;
082       }
083    }