danny       2003/01/08 08:53:58

  Modified:    src/java/org/apache/james/transport MailetLoader.java
                        MatchLoader.java
  Added:       src/java/org/apache/james/transport Loader.java
  Log:
  refactored common code into common parent
  
  Revision  Changes    Path
  1.9       +9 -79     
jakarta-james/src/java/org/apache/james/transport/MailetLoader.java
  
  Index: MailetLoader.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-james/src/java/org/apache/james/transport/MailetLoader.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- MailetLoader.java 8 Jan 2003 12:31:05 -0000       1.8
  +++ MailetLoader.java 8 Jan 2003 16:53:58 -0000       1.9
  @@ -6,22 +6,12 @@
    * the LICENSE file.
    */
   package org.apache.james.transport;
  -import java.io.File;
  -import java.io.IOException;
  -import java.net.MalformedURLException;
  -import java.net.URL;
  -import java.net.URLClassLoader;
  -import java.util.Vector;
   import javax.mail.MessagingException;
  +
   import org.apache.avalon.framework.component.Component;
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  -import org.apache.avalon.framework.context.Context;
  -import org.apache.avalon.framework.context.ContextException;
  -import org.apache.avalon.framework.context.Contextualizable;
  -import org.apache.avalon.framework.logger.Logger;
  -import org.apache.avalon.phoenix.BlockContext;
   import org.apache.james.core.MailetConfigImpl;
   import org.apache.mailet.Mailet;
   import org.apache.mailet.MailetContext;
  @@ -32,56 +22,13 @@
    * @author Serge Knystautas <[EMAIL PROTECTED]>
    * @author Federico Barbieri <[EMAIL PROTECTED]>
    */
  -public class MailetLoader implements Component, Configurable, Contextualizable {
  -    private ClassLoader theClassLoader = null;
  -    private String baseDirectory = null;
  -    private Logger logger;
  -    /**
  -     * The list of packages that may contain Mailets
  -     */
  -    private Vector mailetPackages;
  -    /**
  +public class MailetLoader extends Loader implements Component, Configurable {
  +           /**
        * @see 
org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
        */
       public void configure(Configuration conf) throws ConfigurationException {
  -        mailetPackages = new Vector();
  -        mailetPackages.addElement("");
  -        final Configuration[] pkgConfs = conf.getChildren("mailetpackage");
  -        for (int i = 0; i < pkgConfs.length; i++) {
  -            Configuration c = pkgConfs[i];
  -            String packageName = c.getValue();
  -            if (!packageName.endsWith(".")) {
  -                packageName += ".";
  -            }
  -            mailetPackages.addElement(packageName);
  -        }
  -        File base = new File(baseDirectory + "/SAR-INF/lib");
  -        String[] flist = base.list();
  -        Vector jarlist = new Vector();
  -        URL[] classPath = null;
  -        try {
  -            jarlist.add(new URL("file://" + baseDirectory + "/SAR-INF/classes/"));
  -        } catch (MalformedURLException e) {
  -            logger.error(
  -                "cant add "
  -                    + "file://"
  -                    + baseDirectory
  -                    + "/SAR-INF/classes/ to mailet classloader");
  -        }
  -        if (flist != null) {
  -            for (int i = 0; i < flist.length; i++) {
  -                try {
  -                    if (flist[i].indexOf("jar") == flist[i].length() - 3) {
  -                        jarlist.add(new URL("file://" + flist[i]));
  -                        logger.debug("added " + flist[i] + " to Mailet 
Classloader");
  -                    }
  -                } catch (MalformedURLException e) {
  -                    logger.error("cant add " + "file://" + flist[i] + " to mailet 
classloader");
  -                }
  -            }
  -        }
  -        classPath = (URL[]) jarlist.toArray(new URL[jarlist.size()]);
  -        theClassLoader = new URLClassLoader(classPath, 
this.getClass().getClassLoader());
  +           getPackages(conf,MAILET_PACKAGE);
  +           configureMailetClassLoader();
       }
       /**
        * Get a new Mailet with the specified name acting
  @@ -95,17 +42,14 @@
       public Mailet getMailet(String mailetName, MailetContext context, Configuration 
configuration)
           throws MessagingException {
           try {
  -            for (int i = 0; i < mailetPackages.size(); i++) {
  -                String className = (String) mailetPackages.elementAt(i) + 
mailetName;
  +            for (int i = 0; i < packages.size(); i++) {
  +                String className = (String) packages.elementAt(i) + mailetName;
                   try {
                       MailetConfigImpl configImpl = new MailetConfigImpl();
                       configImpl.setMailetName(mailetName);
                       configImpl.setConfiguration(configuration);
                       configImpl.setMailetContext(context);
  -                    if (theClassLoader == null) {
  -                        theClassLoader = this.getClass().getClassLoader();
  -                    }
  -                    Mailet mailet = (Mailet) 
theClassLoader.loadClass(className).newInstance();
  +                    Mailet mailet = (Mailet) 
mailetClassLoader.loadClass(className).newInstance();
                       mailet.init(configImpl);
                       return mailet;
                   } catch (ClassNotFoundException cnfe) {
  @@ -117,7 +61,7 @@
                       .append("Requested mailet not found: ")
                       .append(mailetName)
                       .append(".  looked in ")
  -                    .append(mailetPackages.toString());
  +                    .append(packages.toString());
               throw new ClassNotFoundException(exceptionBuffer.toString());
           } catch (MessagingException me) {
               throw me;
  @@ -127,19 +71,5 @@
                       ")");
               throw new MailetException(exceptionBuffer.toString(), e);
           }
  -    }
  -    /**
  -         * @see 
org.apache.avalon.framework.context.Contextualizable#contextualize(Context)
  -         */
  -    public void contextualize(final Context context) throws ContextException {
  -        try {
  -            baseDirectory = ((BlockContext) 
context).getBaseDirectory().getCanonicalPath();
  -        } catch (IOException e) {
  -            logger.error("cant get base directory for mailet loader");
  -            throw new ContextException("cant contextualise loader " + 
e.getMessage(), e);
  -        }
  -    }
  -    public void setLogger(Logger logger) {
  -        this.logger = logger;
       }
   }
  
  
  
  1.10      +13 -76    
jakarta-james/src/java/org/apache/james/transport/MatchLoader.java
  
  Index: MatchLoader.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-james/src/java/org/apache/james/transport/MatchLoader.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- MatchLoader.java  8 Jan 2003 12:31:05 -0000       1.9
  +++ MatchLoader.java  8 Jan 2003 16:53:58 -0000       1.10
  @@ -6,23 +6,14 @@
    * the LICENSE file.
    */
   package org.apache.james.transport;
  -import java.io.File;
  -import java.io.IOException;
  -import java.net.MalformedURLException;
  -import java.net.URL;
  -import java.net.URLClassLoader;
   import java.util.Vector;
  +
   import javax.mail.MessagingException;
  +
   import org.apache.avalon.framework.component.Component;
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  -import org.apache.avalon.framework.context.Context;
  -import org.apache.avalon.framework.context.ContextException;
  -import org.apache.avalon.framework.context.Contextualizable;
  -import org.apache.avalon.framework.logger.AbstractLogEnabled;
  -import org.apache.avalon.framework.logger.Logger;
  -import org.apache.avalon.phoenix.BlockContext;
   import org.apache.james.core.MatcherConfigImpl;
   import org.apache.mailet.MailetContext;
   import org.apache.mailet.MailetException;
  @@ -33,57 +24,16 @@
    * @author Serge Knystautas <[EMAIL PROTECTED]>
    * @author Federico Barbieri <[EMAIL PROTECTED]>
    */
  -public class MatchLoader implements Component, Configurable, Contextualizable {
  -    /**
  -     * The list of packages that may contain Mailets
  -     */
  -    private Vector matcherPackages;
  -    private ClassLoader theClassLoader = null;
  -    private String baseDirectory = null;
  -    private Logger logger;
  -    /**
  +public class MatchLoader extends Loader implements Component, Configurable {
  +        /**
        * @see 
org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
        */
       public void configure(Configuration conf) throws ConfigurationException {
  -        matcherPackages = new Vector();
  -        matcherPackages.addElement("");
  -        final Configuration[] pkgConfs = conf.getChildren("matcherpackage");
  -        for (int i = 0; i < pkgConfs.length; i++) {
  -            Configuration c = pkgConfs[i];
  -            String packageName = c.getValue();
  -            if (!packageName.endsWith(".")) {
  -                packageName += ".";
  -            }
  -            matcherPackages.addElement(packageName);
  -        }
  -        File base = new File(baseDirectory + "/SAR-INF/lib");
  -        String[] flist = base.list();
  -        Vector jarlist = new Vector();
  -        URL[] classPath = null;
  -        try {
  -            jarlist.add(new URL("file://" + baseDirectory + 
"/SAR-INF/lib/classes/"));
  -        } catch (MalformedURLException e) {
  -            logger.error(
  -                "cant add "
  -                    + "file://"
  -                    + baseDirectory
  -                    + "/SAR-INF/classes/ to matcher classloader");
  -        }
  -        if (flist != null) {
  -            for (int i = 0; i < flist.length; i++) {
  -                try {
  -                    if (flist[i].indexOf("jar") == flist[i].length() - 3) {
  -                        jarlist.add(new URL("file://" + flist[i]));
  -                        logger.debug("added " + flist[i] + " to Matcher 
Classloader");
  -                    }
  -                } catch (MalformedURLException e) {
  -                    logger.error("cant add " + "file://" + flist[i] + " to matcher 
classloader");
  -                }
  -            }
  -        }
  -        classPath = (URL[]) jarlist.toArray(new URL[jarlist.size()]);
  -        theClassLoader = new URLClassLoader(classPath, 
this.getClass().getClassLoader());
  +           getPackages(conf,MATCHER_PACKAGE);
  +           configureMailetClassLoader();
       }
  +    
  +    
       /**
        * Get a new Matcher with the specified name acting
        * in the specified context.
  @@ -101,14 +51,14 @@
                   condition = matchName.substring(i + 1);
                   matchName = matchName.substring(0, i);
               }
  -            for (i = 0; i < matcherPackages.size(); i++) {
  -                String className = (String) matcherPackages.elementAt(i) + 
matchName;
  +            for (i = 0; i < packages.size(); i++) {
  +                String className = (String) packages.elementAt(i) + matchName;
                   try {
                       MatcherConfigImpl configImpl = new MatcherConfigImpl();
                       configImpl.setMatcherName(matchName);
                       configImpl.setCondition(condition);
                       configImpl.setMailetContext(context);
  -                    Matcher matcher = (Matcher) 
theClassLoader.loadClass(className).newInstance();
  +                    Matcher matcher = (Matcher) 
mailetClassLoader.loadClass(className).newInstance();
                       matcher.init(configImpl);
                       return matcher;
                   } catch (ClassNotFoundException cnfe) {
  @@ -120,7 +70,7 @@
                       .append("Requested matcher not found: ")
                       .append(matchName)
                       .append(".  looked in ")
  -                    .append(matcherPackages.toString());
  +                    .append(packages.toString());
               throw new ClassNotFoundException(exceptionBuffer.toString());
           } catch (MessagingException me) {
               throw me;
  @@ -131,18 +81,5 @@
               throw new MailetException(exceptionBuffer.toString(), e);
           }
       }
  -    /**
  -     * @see 
org.apache.avalon.framework.context.Contextualizable#contextualize(Context)
  -     */
  -    public void contextualize(final Context context) throws ContextException {
  -        try {
  -            baseDirectory = ((BlockContext) 
context).getBaseDirectory().getCanonicalPath();
  -        } catch (IOException e) {
  -            logger.error("cant get base directory for matcher loader");
  -            throw new ContextException("cant contextualise loader " + 
e.getMessage(), e);
  -        }
  -    }
  -    public void setLogger(Logger logger) {
  -        this.logger = logger;
  -    }
  +    
   }
  
  
  
  1.1                  jakarta-james/src/java/org/apache/james/transport/Loader.java
  
  Index: Loader.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.james.transport;
  import java.io.File;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.util.Vector;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.avalon.phoenix.BlockContext;
  /**
   * @author <A href="mailto:[EMAIL PROTECTED]";>Danny Angus</a>
   * 
   * $Id: Loader.java,v 1.1 2003/01/08 16:53:58 danny Exp $
   */
  public class Loader implements Contextualizable {
      protected ClassLoader mailetClassLoader = null;
      protected String baseDirectory = null;
      protected Logger logger;
      protected final String MAILET_PACKAGE = "mailetpackage";
      protected final String MATCHER_PACKAGE = "matcherpackage";
        /**
       * The list of packages that may contain Mailets or matchers
       */
      protected Vector packages;
      /**
           * @see 
org.apache.avalon.framework.context.Contextualizable#contextualize(Context)
           */
      public void contextualize(final Context context) throws ContextException {
          try {
              baseDirectory = ((BlockContext) 
context).getBaseDirectory().getCanonicalPath();
          } catch (IOException e) {
              logger.error("cant get base directory for mailet loader");
              throw new ContextException("cant contextualise mailet loader " + 
e.getMessage(), e);
          }
      }
      /**
       * Method setLogger.
       * @param logger
       */
      public void setLogger(Logger logger) {
          this.logger = logger;
      }
    
      protected void getPackages(Configuration conf, String packageType)
          throws ConfigurationException {
          packages = new Vector();
          packages.addElement("");
          final Configuration[] pkgConfs = conf.getChildren(packageType);
          for (int i = 0; i < pkgConfs.length; i++) {
              Configuration c = pkgConfs[i];
              String packageName = c.getValue();
              if (!packageName.endsWith(".")) {
                  packageName += ".";
              }
              packages.addElement(packageName);
          }
      }
      /**
       * Method getMailetClassLoader.
       */
      protected void configureMailetClassLoader() {
          File base = new File(baseDirectory + "/SAR-INF/lib");
          String[] flist = base.list();
          Vector jarlist = new Vector();
          URL[] classPath = null;
          try {
              jarlist.add(new URL("file://" + baseDirectory + 
"/SAR-INF/lib/classes/"));
          } catch (MalformedURLException e) {
              logger.error(
                  "cant add "
                      + "file://"
                      + baseDirectory
                      + "/SAR-INF/classes/ to mailet classloader");
          }
          if (flist != null) {
              for (int i = 0; i < flist.length; i++) {
                  try {
                      if (flist[i].indexOf("jar") == flist[i].length() - 3) {
                          jarlist.add(new URL("file://" + flist[i]));
                          logger.debug("added " + flist[i] + " to mailet Classloader");
                      }
                  } catch (MalformedURLException e) {
                      logger.error("cant add " + "file://" + flist[i] + " to mailet 
classloader");
                  }
              }
          }
          classPath = (URL[]) jarlist.toArray(new URL[jarlist.size()]);
          mailetClassLoader = new URLClassLoader(classPath, 
this.getClass().getClassLoader());
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to