You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rd...@apache.org on 2009/09/05 11:36:34 UTC

svn commit: r811610 - in /james/server/trunk/smtpserver-function/src: main/java/org/apache/james/smtpserver/SMTPHandlerChain.java test/java/org/apache/james/smtpserver/FakeLoader.java

Author: rdonkin
Date: Sat Sep  5 09:36:34 2009
New Revision: 811610

URL: http://svn.apache.org/viewvc?rev=811610&view=rev
Log:
Inject loader service and use to create handler instances

Modified:
    james/server/trunk/smtpserver-function/src/main/java/org/apache/james/smtpserver/SMTPHandlerChain.java
    james/server/trunk/smtpserver-function/src/test/java/org/apache/james/smtpserver/FakeLoader.java

Modified: james/server/trunk/smtpserver-function/src/main/java/org/apache/james/smtpserver/SMTPHandlerChain.java
URL: http://svn.apache.org/viewvc/james/server/trunk/smtpserver-function/src/main/java/org/apache/james/smtpserver/SMTPHandlerChain.java?rev=811610&r1=811609&r2=811610&view=diff
==============================================================================
--- james/server/trunk/smtpserver-function/src/main/java/org/apache/james/smtpserver/SMTPHandlerChain.java (original)
+++ james/server/trunk/smtpserver-function/src/main/java/org/apache/james/smtpserver/SMTPHandlerChain.java Sat Sep  5 09:36:34 2009
@@ -27,6 +27,8 @@
 import java.util.List;
 import java.util.Properties;
 
+import javax.annotation.Resource;
+
 import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
@@ -36,6 +38,7 @@
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
+import org.apache.james.api.kernel.LoaderService;
 import org.apache.james.smtpserver.core.CoreCmdHandlerLoader;
 import org.apache.james.smtpserver.core.CoreMessageHookLoader;
 
@@ -52,6 +55,26 @@
 
     private ServiceManager serviceManager;
     
+    /** Loads instances */
+    private LoaderService loader;
+    
+    /**
+     * Gets the current instance loader.
+     * @return the loader
+     */
+    public final LoaderService getLoader() {
+        return loader;
+    }
+
+    /**
+     * Sets the loader to be used for instances.
+     * @param loader the loader to set, not null
+     */
+    @Resource(name="org.apache.james.LoaderService")
+    public final void setLoader(LoaderService loader) {
+        this.loader = loader;
+    }
+    
     /**
      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
      */
@@ -169,7 +192,8 @@
     private void loadClass(ClassLoader classLoader, String className,
             Configuration config) throws ConfigurationException {
         try {
-            Object handler = classLoader.loadClass(className).newInstance();
+            final Class<?> handlerClass = classLoader.loadClass(className);
+            Object handler = loader.load(handlerClass);
 
             // enable logging
             ContainerUtil.enableLogging(handler, getLogger());
@@ -211,20 +235,6 @@
             }
             throw new ConfigurationException("Failed to add Commandhandler: "
                     + className, ex);
-        } catch (IllegalAccessException ex) {
-            if (getLogger().isErrorEnabled()) {
-                getLogger().error("Failed to add Commandhandler: " + className,
-                        ex);
-            }
-            throw new ConfigurationException("Failed to add Commandhandler: "
-                    + className, ex);
-        } catch (InstantiationException ex) {
-            if (getLogger().isErrorEnabled()) {
-                getLogger().error("Failed to add Commandhandler: " + className,
-                        ex);
-            }
-            throw new ConfigurationException("Failed to add Commandhandler: "
-                    + className, ex);
         } catch (ServiceException e) {
             if (getLogger().isErrorEnabled()) {
                 getLogger().error(

Modified: james/server/trunk/smtpserver-function/src/test/java/org/apache/james/smtpserver/FakeLoader.java
URL: http://svn.apache.org/viewvc/james/server/trunk/smtpserver-function/src/test/java/org/apache/james/smtpserver/FakeLoader.java?rev=811610&r1=811609&r2=811610&view=diff
==============================================================================
--- james/server/trunk/smtpserver-function/src/test/java/org/apache/james/smtpserver/FakeLoader.java (original)
+++ james/server/trunk/smtpserver-function/src/test/java/org/apache/james/smtpserver/FakeLoader.java Sat Sep  5 09:36:34 2009
@@ -19,13 +19,64 @@
 
 package org.apache.james.smtpserver;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.Resource;
+
 import org.apache.james.api.kernel.LoaderService;
 
 public class FakeLoader implements LoaderService {
 
+    private final Map<String, Object> servicesByName;
+    
+    public FakeLoader() {
+        servicesByName = new HashMap<String, Object>();
+        servicesByName.put("org.apache.james.LoaderService", this);
+    }
+    
+    
+    public Object get(String name) {
+        Object service = servicesByName.get(name);
+        return service;
+    }
+    
+    private void injectResources(Object resource) {
+        final Method[] methods = resource.getClass().getMethods();
+        for (Method method : methods) {
+            final Resource resourceAnnotation = method.getAnnotation(Resource.class);
+            if (resourceAnnotation != null) {
+                final String name = resourceAnnotation.name();
+                if (name == null) {
+                    // Unsupported
+                } else {
+                    // Name indicates a service
+                    final Object service = get(name);
+                    if (service == null) {
+                   } else {
+                        try {
+                            Object[] args = {service};
+                            method.invoke(resource, args);
+                        } catch (IllegalAccessException e) {
+                            throw new RuntimeException("Injection failed", e);
+                        } catch (IllegalArgumentException e) {
+                            throw new RuntimeException("Injection failed", e);
+                        } catch (InvocationTargetException e) {
+                            throw new RuntimeException("Injection failed", e);
+                        }
+                    }
+                }
+            }
+        }
+    }
+    
     public <T> T load(Class<T> type) {
         try {
-            return type.newInstance();
+            final T newInstance = type.newInstance();
+            injectResources(newInstance);
+            return newInstance;
         } catch (Exception e) {
             throw new RuntimeException(e);
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org