001    package org.shiftone.jrat.core;
002    
003    
004    import org.shiftone.jrat.core.config.CacheMethodHandlerFactory;
005    import org.shiftone.jrat.core.config.ConfigMethodHandlerFactory;
006    import org.shiftone.jrat.core.spi.MethodHandler;
007    import org.shiftone.jrat.core.spi.MethodHandlerFactory;
008    import org.shiftone.jrat.provider.silent.SilentMethodHandler;
009    import org.shiftone.jrat.util.log.Logger;
010    
011    
012    public class HandlerFactory {
013    
014        private static final Logger LOG = Logger.getLogger(HandlerFactory.class);
015    
016        /**
017         * Force initialization.  This should really only be called from test cases to
018         * initialize a particular configuration.
019         */
020        public static void initialize() {
021            Singleton.initialize();
022        }
023    
024    
025        private static class Singleton {
026    
027            private static MethodHandlerFactory rootHandlerFactory;
028    
029            static {
030    
031               // Mode.set(Mode.RUNTIME);
032    
033                try {
034    
035                    rootHandlerFactory = new CacheMethodHandlerFactory(
036                            new ConfigMethodHandlerFactory(
037                                    Environment.getConfiguration()
038                            )
039                    );
040    
041                    rootHandlerFactory.startup(new RuntimeContextImpl());
042    
043                } catch (Exception e) {
044    
045                    rootHandlerFactory = SilentMethodHandler.HANDLER_FACTORY;
046    
047                    LOG.error("There was an error starting up a handler factory", e);
048                    LOG.info("JRat will ignore all configuration and use the slient handler");
049                }
050            }
051    
052            public static void initialize() {
053            }
054    
055            public static synchronized MethodHandler getMethodHandler(MethodKey methodKey) {
056                try {
057                    return rootHandlerFactory.createMethodHandler(methodKey);
058                } catch (Exception e) {
059                    LOG.error("failed to column MethodHandler for : " + methodKey, e);
060                    return SilentMethodHandler.METHOD_HANDLER;
061                }
062            }
063        }
064    
065        public static synchronized MethodHandler getMethodHandler(MethodKey methodKey)  {
066            return Singleton.getMethodHandler(methodKey);
067        }
068    
069        public static synchronized MethodHandler getMethodHandler(String className, String methodName, String signature) {
070            return Singleton.getMethodHandler(MethodKey.getInstance(className, methodName, signature));
071        }
072    
073        public static synchronized MethodHandler getMethodHandler(Class klass, String methodName, String signature) {
074            return Singleton.getMethodHandler(MethodKey.getInstance(klass.getName(), methodName, signature));
075        }
076    }