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 no...@apache.org on 2009/11/21 21:20:45 UTC

svn commit: r882985 - in /james/server/trunk/spoolmanager-function/src: main/java/org/apache/james/transport/ test/java/org/apache/james/transport/

Author: norman
Date: Sat Nov 21 20:20:43 2009
New Revision: 882985

URL: http://svn.apache.org/viewvc?rev=882985&view=rev
Log:
More decoupling of Avalon to move to Guice (JAMES-893)

Modified:
    james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/AvalonJamesSpoolManager.java
    james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/LinearProcessor.java
    james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/StateAwareProcessorList.java
    james/server/trunk/spoolmanager-function/src/test/java/org/apache/james/transport/LinearProcessorTest.java

Modified: james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/AvalonJamesSpoolManager.java
URL: http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/AvalonJamesSpoolManager.java?rev=882985&r1=882984&r2=882985&view=diff
==============================================================================
--- james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/AvalonJamesSpoolManager.java (original)
+++ james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/AvalonJamesSpoolManager.java Sat Nov 21 20:20:43 2009
@@ -33,6 +33,8 @@
     private ConfigurationAdapter config;
     private AvalonLogger logger;
     private SpoolRepository repos;
+    private MailetLoader mailetLoader;
+    private MatcherLoader matcherLoader;
 
     /**
      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
@@ -58,6 +60,8 @@
      */
     public void service(ServiceManager comp) throws ServiceException {
         repos = (SpoolRepository) comp.lookup(SpoolRepository.ROLE);
+        mailetLoader = (MailetLoader) comp.lookup(MailetLoader.ROLE);
+        matcherLoader = (MatcherLoader) comp.lookup(MatcherLoader.ROLE);
     }
 
 
@@ -83,6 +87,8 @@
             bind(org.apache.commons.configuration.HierarchicalConfiguration.class).annotatedWith(Names.named("org.apache.commons.configuration.Configuration")).toInstance(config);
             bind(Log.class).annotatedWith(Names.named("org.apache.commons.logging.Log")).toInstance(logger);
             bind(SpoolRepository.class).annotatedWith(Names.named("org.apache.james.services.SpoolRepository")).toInstance(repos);
+            bind(MailetLoader.class).annotatedWith(Names.named("org.apache.james.transport.MailetLoader")).toInstance(mailetLoader);
+            bind(MatcherLoader.class).annotatedWith(Names.named("org.apache.james.transport.MatcherLoader")).toInstance(matcherLoader);
             bind(LoaderService.class).annotatedWith(Names.named("org.apache.james.LoaderService")).toProvider(new Provider<LoaderService>() {
 
                 public LoaderService get() {

Modified: james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/LinearProcessor.java
URL: http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/LinearProcessor.java?rev=882985&r1=882984&r2=882985&view=diff
==============================================================================
--- james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/LinearProcessor.java (original)
+++ james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/LinearProcessor.java Sat Nov 21 20:20:43 2009
@@ -21,21 +21,26 @@
 
 package org.apache.james.transport;
 
-import org.apache.avalon.framework.activity.Disposable;
-import org.apache.avalon.framework.configuration.Configurable;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Locale;
+
+import javax.annotation.PreDestroy;
+import javax.annotation.Resource;
+import javax.mail.MessagingException;
+
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.container.ContainerUtil;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
-import org.apache.avalon.framework.service.ServiceException;
-import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.Serviceable;
+import org.apache.commons.logging.Log;
 import org.apache.james.core.MailImpl;
 import org.apache.james.services.SpoolRepository;
 import org.apache.james.util.ConfigurationAdapter;
-import org.apache.mailet.base.MatcherInverter;
-import org.apache.mailet.base.GenericMailet;
-import org.apache.mailet.base.GenericMatcher;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.Mailet;
@@ -43,17 +48,9 @@
 import org.apache.mailet.MailetException;
 import org.apache.mailet.Matcher;
 import org.apache.mailet.MatcherConfig;
-
-import javax.mail.MessagingException;
-
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Locale;
+import org.apache.mailet.base.GenericMailet;
+import org.apache.mailet.base.GenericMatcher;
+import org.apache.mailet.base.MatcherInverter;
 
 /**
  * Implements a processor for mails, directing the mail down
@@ -88,9 +85,7 @@
  * <P>CVS $Id$</P>
  * @version 2.2.0
  */
-public class LinearProcessor 
-    extends AbstractLogEnabled
-    implements Disposable, Configurable, Serviceable, MailProcessor, MailetContainer {
+public class LinearProcessor implements  MailProcessor, MailetContainer {
 
     /**
      *  The name of the matcher used to terminate the matcher chain.  The
@@ -119,6 +114,8 @@
 
     private MatcherLoader matchLoader;
 
+    private Log logger;
+
     /**
      * Set the spool to be used by this LinearProcessor.
      *
@@ -126,7 +123,8 @@
      *
      * @throws IllegalArgumentException when the spool passed in is null
      */
-    public void setSpool(SpoolRepository spool) {
+    @Resource(name="org.apache.james.services.SpoolRepository")
+    public void setSpoolRepository(SpoolRepository spool) {
         if (spool == null) {
             throw new IllegalArgumentException("The spool cannot be null");
         }
@@ -138,6 +136,7 @@
      * 
      * @param mailetLoader the MailetLoader
      */
+    @Resource(name="org.apache.james.transport.MailetLoader")
     public void setMailetLoader(MailetLoader mailetLoader) {
         this.mailetLoader = mailetLoader;
     }
@@ -147,9 +146,17 @@
      * 
      * @param matchLoader the MatcherLoader
      */
+    @Resource(name="org.apache.james.transport.MatcherLoader")
     public void setMatchLoader(MatcherLoader matchLoader) {
         this.matchLoader = matchLoader;
     }
+    
+    
+    @Resource(name="org.apache.commons.logging.Log")
+    public final void setLogger(Log logger) {
+        this.logger = logger;
+    }
+    
 
     /**
      * <p>The dispose operation is called at the end of a components lifecycle.
@@ -159,15 +166,15 @@
      * <p>This implementation disposes of all the mailet instances added to the
      * processor</p>
      *
-     * @see org.apache.avalon.framework.activity.Disposable#dispose()
      */
+    @PreDestroy
     public void dispose() {
         Iterator<Mailet> it = mailets.iterator();
-        boolean debugEnabled = getLogger().isDebugEnabled();
+        boolean debugEnabled = logger.isDebugEnabled();
         while (it.hasNext()) {
             Mailet mailet = it.next();
             if (debugEnabled) {
-                getLogger().debug("Shutdown mailet " + mailet.getMailetInfo());
+                logger.debug("Shutdown mailet " + mailet.getMailetInfo());
             }
             mailet.destroy();
         }
@@ -244,7 +251,7 @@
                                               .append("Message ")
                                               .append(mail.getName())
                                               .append(" reached the end of this processor, and is automatically deleted.  This may indicate a configuration error.");
-                        LinearProcessor.this.getLogger().warn(warnBuffer.toString());
+                        LinearProcessor.this.logger.warn(warnBuffer.toString());
                     }
                     mail.setState(Mail.GHOST);
                 }
@@ -290,8 +297,8 @@
             throw new IllegalStateException("Attempt to service mail before matcher/mailet lists have been closed");
         }
 
-        if (getLogger().isDebugEnabled()) {
-            getLogger().debug("Servicing mail: " + mail.getName());
+        if (logger.isDebugEnabled()) {
+            logger.debug("Servicing mail: " + mail.getName());
         }
         //  unprocessed is an array of Lists of Mail objects
         //  the array indicates which matcher/mailet (stage in the linear
@@ -367,14 +374,14 @@
             Collection<MailAddress> recipients = null;
             Matcher matcher = (Matcher) matchers.get(i);
             StringBuffer logMessageBuffer = null;
-            if (getLogger().isDebugEnabled()) {
+            if (logger.isDebugEnabled()) {
                 logMessageBuffer =
                     new StringBuffer(128)
                             .append("Checking ")
                             .append(mail.getName())
                             .append(" with ")
                             .append(matcher);
-                getLogger().debug(logMessageBuffer.toString());
+                logger.debug(logMessageBuffer.toString());
             }
             try {
                 recipients = matcher.match(mail);
@@ -434,14 +441,14 @@
             }
             // We have messages that need to process... time to run the mailet.
             Mailet mailet = (Mailet) mailets.get(i);
-            if (getLogger().isDebugEnabled()) {
+            if (logger.isDebugEnabled()) {
                 logMessageBuffer =
                     new StringBuffer(128)
                             .append("Servicing ")
                             .append(mail.getName())
                             .append(" by ")
                             .append(mailet.getMailetInfo());
-                getLogger().debug(logMessageBuffer.toString());
+                logger.debug(logMessageBuffer.toString());
             }
             try {
                 mailet.service(mail);
@@ -548,7 +555,7 @@
         }
         String errorString = sout.toString();
         mail.setErrorMessage(errorString);
-        getLogger().error(errorString);
+        logger.error(errorString);
         throw me;
     }
     
@@ -598,26 +605,26 @@
                 }
                 
                 //The matcher itself should log that it's been inited.
-                if (getLogger().isInfoEnabled()) {
+                if (logger.isInfoEnabled()) {
                     StringBuffer infoBuffer =
                         new StringBuffer(64)
                                 .append("Matcher ")
                                 .append(matcherName)
                                 .append(" instantiated.");
-                    getLogger().info(infoBuffer.toString());
+                    logger.info(infoBuffer.toString());
                 }
             } catch (MessagingException ex) {
                 // **** Do better job printing out exception
-                if (getLogger().isErrorEnabled()) {
+                if (logger.isErrorEnabled()) {
                     StringBuffer errorBuffer =
                         new StringBuffer(256)
                                 .append("Unable to init matcher ")
                                 .append(matcherName)
                                 .append(": ")
                                 .append(ex.toString());
-                    getLogger().error( errorBuffer.toString(), ex );
+                    logger.error( errorBuffer.toString(), ex );
                     if (ex.getNextException() != null) {
-                        getLogger().error( "Caused by nested exception: ", ex.getNextException());
+                        logger.error( "Caused by nested exception: ", ex.getNextException());
                     }
                 }
                 System.err.println("Unable to init matcher " + matcherName);
@@ -627,26 +634,26 @@
             }
             try {
                 mailet = mailetLoader.getMailet(mailetClassName, new ConfigurationAdapter(c));
-                if (getLogger().isInfoEnabled()) {
+                if (logger.isInfoEnabled()) {
                     StringBuffer infoBuffer =
                         new StringBuffer(64)
                                 .append("Mailet ")
                                 .append(mailetClassName)
                                 .append(" instantiated.");
-                    getLogger().info(infoBuffer.toString());
+                    logger.info(infoBuffer.toString());
                 }
             } catch (MessagingException ex) {
                 // **** Do better job printing out exception
-                if (getLogger().isErrorEnabled()) {
+                if (logger.isErrorEnabled()) {
                     StringBuffer errorBuffer =
                         new StringBuffer(256)
                                 .append("Unable to init mailet ")
                                 .append(mailetClassName)
                                 .append(": ")
                                 .append(ex.toString());
-                    getLogger().error( errorBuffer.toString(), ex );
+                    logger.error( errorBuffer.toString(), ex );
                     if (ex.getNextException() != null) {
-                        getLogger().error( "Caused by nested exception: ", ex.getNextException());
+                        logger.error( "Caused by nested exception: ", ex.getNextException());
                     }
                 }
                 System.err.println("Unable to init mailet " + mailetClassName);
@@ -669,15 +676,6 @@
     }
 
     /**
-     * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
-     */
-    public void service(ServiceManager comp) throws ServiceException {
-        setMailetLoader((MailetLoader) comp.lookup(MailetLoader.ROLE));
-        setMatchLoader((MatcherLoader) comp.lookup(MatcherLoader.ROLE));
-        setSpool( (SpoolRepository) comp.lookup(SpoolRepository.ROLE));
-    }
-
-    /**
      * @see org.apache.james.transport.MailetContainer#getMailetConfigs()
      */
     public List<MailetConfig> getMailetConfigs() {

Modified: james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/StateAwareProcessorList.java
URL: http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/StateAwareProcessorList.java?rev=882985&r1=882984&r2=882985&view=diff
==============================================================================
--- james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/StateAwareProcessorList.java (original)
+++ james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/StateAwareProcessorList.java Sat Nov 21 20:20:43 2009
@@ -58,6 +58,10 @@
     private Log logger;
 
     private HierarchicalConfiguration config;
+
+    private MailetLoader mailetLoader;
+
+    private MatcherLoader matcherLoader;
     
     public StateAwareProcessorList() {
         super();
@@ -76,6 +80,15 @@
         this.config = config;
     }
     
+    @Resource(name="org.apache.james.transport.MailetLoader")
+    public final void setMailetLoader(MailetLoader mailetLoader) {
+        this.mailetLoader = mailetLoader;
+    }
+    
+    @Resource(name="org.apache.james.transport.MatcherLoader")
+    public final void setMatcherLoader(MatcherLoader matcherLoader) {
+        this.matcherLoader = matcherLoader;
+    }
 
     /**
      * @see org.apache.avalon.framework.activity.Initializable#initialize()
@@ -98,6 +111,9 @@
                     protected void configure() {
                         bind(org.apache.commons.configuration.HierarchicalConfiguration.class).annotatedWith(Names.named("org.apache.commons.configuration.Configuration")).toInstance(processorConf);
                         bind(Log.class).annotatedWith(Names.named("org.apache.commons.logging.Log")).toInstance(logger);
+                        bind(MailetLoader.class).annotatedWith(Names.named("org.apache.james.transport.MailetLoader")).toInstance(mailetLoader);
+                        bind(MatcherLoader.class).annotatedWith(Names.named("org.apache.james.transport.MatcherLoader")).toInstance(matcherLoader);
+
                     }
                     
                 }).getInstance(cObj);

Modified: james/server/trunk/spoolmanager-function/src/test/java/org/apache/james/transport/LinearProcessorTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/test/java/org/apache/james/transport/LinearProcessorTest.java?rev=882985&r1=882984&r2=882985&view=diff
==============================================================================
--- james/server/trunk/spoolmanager-function/src/test/java/org/apache/james/transport/LinearProcessorTest.java (original)
+++ james/server/trunk/spoolmanager-function/src/test/java/org/apache/james/transport/LinearProcessorTest.java Sat Nov 21 20:20:43 2009
@@ -22,6 +22,7 @@
 import org.apache.avalon.framework.container.ContainerUtil;
 import org.apache.avalon.framework.logger.ConsoleLogger;
 import org.apache.avalon.framework.logger.Logger;
+import org.apache.commons.logging.impl.SimpleLog;
 import org.apache.james.core.MailImpl;
 import org.apache.james.core.MimeMessageCopyOnWriteProxy;
 import org.apache.james.core.MimeMessageInputStreamSource;
@@ -124,7 +125,8 @@
     }
 
     public void testCopyOnWrite() throws IOException, MessagingException {
-        linearProcessor.setSpool(new InMemorySpoolRepository());
+        linearProcessor.setSpoolRepository(new InMemorySpoolRepository());
+        linearProcessor.setLogger(new SimpleLog("Test"));
         Matcher recipientIs = new RecipientIs();
         recipientIs.init(new DummyMatcherConfig("rec1@domain.com"));
 
@@ -174,12 +176,14 @@
     }
 
     public void testStateChange() throws IOException, MessagingException {
-        linearProcessor.setSpool(new InMemorySpoolRepository() {
+        linearProcessor.setSpoolRepository(new InMemorySpoolRepository() {
             public void store(Mail mc) throws MessagingException {
                 assertEquals("MYSTATE", mc.getState());
                 super.store(mc);
             }
         });
+        linearProcessor.setLogger(new SimpleLog("Test"));
+
 
         Matcher recipientIs = new RecipientIs();
         recipientIs.init(new DummyMatcherConfig("rec1@domain.com"));



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