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 2010/12/27 19:12:55 UTC

svn commit: r1053120 - in /james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer: AbstractLoaderBeanFactory.java MailetLoaderBeanFactory.java MatcherLoaderBeanFactory.java

Author: norman
Date: Mon Dec 27 18:12:55 2010
New Revision: 1053120

URL: http://svn.apache.org/viewvc?rev=1053120&view=rev
Log:
Share some code...

Added:
    james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/AbstractLoaderBeanFactory.java
Modified:
    james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/MailetLoaderBeanFactory.java
    james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/MatcherLoaderBeanFactory.java

Added: james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/AbstractLoaderBeanFactory.java
URL: http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/AbstractLoaderBeanFactory.java?rev=1053120&view=auto
==============================================================================
--- james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/AbstractLoaderBeanFactory.java (added)
+++ james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/AbstractLoaderBeanFactory.java Mon Dec 27 18:12:55 2010
@@ -0,0 +1,72 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.container.spring.bean.factory.mailetcontainer;
+
+import org.apache.james.container.spring.bean.AbstractBeanFactory;
+import org.apache.mailet.MailetException;
+
+
+public abstract class AbstractLoaderBeanFactory<T> extends AbstractBeanFactory{
+
+    /**
+     * Load the class for the given name. If the name is not a full classname (including package) it will
+     * get suffixed with {@link #getStandardPackage()}
+     * 
+     * @param name
+     * @return instance
+     * @throws ClassNotFoundException
+     */
+    @SuppressWarnings("unchecked")
+    protected T load(String name) throws ClassNotFoundException {
+        String fullName;
+        if (name.indexOf(".") < 1) {
+            fullName = getStandardPackage() + "." + name;
+        } else {
+            fullName = name;
+        }
+        // Use the classloader which is used for bean instance stuff
+        Class<T> c = (Class<T>) getBeanFactory().getBeanClassLoader().loadClass(fullName);
+        return (T) getBeanFactory().createBean(c);
+
+    }
+    
+    /**
+     * Constructs an appropriate exception with an appropriate message.
+     * @param name not null
+     * @param e not null
+     * @return not null
+     */
+    protected MailetException loadFailed(String name, String type, Exception e) {
+        final StringBuilder builder =
+            new StringBuilder(128).append("Could not load ").append(type)
+                .append(" (").append(name).append(")");
+        final MailetException mailetException = new MailetException(builder.toString(), e);
+        return mailetException;
+    }
+
+    
+    /**
+     * Return the package name which will be used as suffix if the name provided for {@link #load(String)} does
+     * not contain a package name
+     * 
+     * @return stdPackage
+     */
+    protected abstract String getStandardPackage();
+    
+}

Modified: james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/MailetLoaderBeanFactory.java
URL: http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/MailetLoaderBeanFactory.java?rev=1053120&r1=1053119&r2=1053120&view=diff
==============================================================================
--- james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/MailetLoaderBeanFactory.java (original)
+++ james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/MailetLoaderBeanFactory.java Mon Dec 27 18:12:55 2010
@@ -20,11 +20,9 @@ package org.apache.james.container.sprin
 
 import javax.mail.MessagingException;
 
-import org.apache.james.container.spring.bean.AbstractBeanFactory;
 import org.apache.james.mailetcontainer.api.MailetLoader;
 import org.apache.mailet.Mailet;
 import org.apache.mailet.MailetConfig;
-import org.apache.mailet.MailetException;
 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 
 /**
@@ -33,27 +31,18 @@ import org.springframework.beans.factory
  * The Mailets are not registered in the factory after loading them!
  *
  */
-public class MailetLoaderBeanFactory extends AbstractBeanFactory implements MailetLoader {
+public class MailetLoaderBeanFactory extends AbstractLoaderBeanFactory<Mailet> implements MailetLoader {
     
     /*
      * (non-Javadoc)
      * @see org.apache.james.mailetcontainer.api.MailetLoader#getMailet(org.apache.mailet.MailetConfig)
      */
-    @SuppressWarnings("unchecked")
     public Mailet getMailet(final MailetConfig config) throws MessagingException {
         String mailetName = config.getMailetName();
 
         try {
-            String fullName;
-            if (mailetName.indexOf(".") < 1) {
-                fullName = "org.apache.james.transport.mailets." + mailetName;
-            } else {
-                fullName = mailetName;
-            }
-            
-            // Use the classloader which is used for bean instance stuff
-            Class clazz = getBeanFactory().getBeanClassLoader().loadClass(fullName);
-            final Mailet mailet = (Mailet) getBeanFactory().createBean(clazz);
+
+            final Mailet mailet = load(mailetName);
 
             // init the mailet
             mailet.init(config);
@@ -63,22 +52,15 @@ public class MailetLoaderBeanFactory ext
         } catch (MessagingException me) {
             throw me;
         } catch (Exception e) {
-            throw loadFailed(mailetName, e);
+            throw loadFailed(mailetName, "mailet", e);
         }
     }
 
-    /**
-     * Constructs an appropriate exception with an appropriate message.
-     * @param name not null
-     * @param e not null
-     * @return not null
-     */
-    protected MailetException loadFailed(String name, Exception e) {
-        final StringBuilder builder =
-            new StringBuilder(128).append("Could not load ").append("mailet")
-                .append(" (").append(name).append(")");
-        final MailetException mailetException = new MailetException(builder.toString(), e);
-        return mailetException;
+
+    @Override
+    protected String getStandardPackage() {
+        return "org.apache.james.transport.mailets";
     }
+
     
 }

Modified: james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/MatcherLoaderBeanFactory.java
URL: http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/MatcherLoaderBeanFactory.java?rev=1053120&r1=1053119&r2=1053120&view=diff
==============================================================================
--- james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/MatcherLoaderBeanFactory.java (original)
+++ james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/bean/factory/mailetcontainer/MatcherLoaderBeanFactory.java Mon Dec 27 18:12:55 2010
@@ -20,9 +20,7 @@ package org.apache.james.container.sprin
 
 import javax.mail.MessagingException;
 
-import org.apache.james.container.spring.bean.AbstractBeanFactory;
 import org.apache.james.mailetcontainer.api.MatcherLoader;
-import org.apache.mailet.MailetException;
 import org.apache.mailet.Matcher;
 import org.apache.mailet.MatcherConfig;
 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -33,54 +31,37 @@ import org.springframework.beans.factory
  * The Matchers are not registered in the factory after loading them!
  *
  */
-public class MatcherLoaderBeanFactory extends AbstractBeanFactory implements MatcherLoader {
+public class MatcherLoaderBeanFactory extends AbstractLoaderBeanFactory<Matcher> implements MatcherLoader {
     
     /*
      * (non-Javadoc)
      * @see org.apache.james.mailetcontainer.api.MatcherLoader#getMatcher(org.apache.mailet.MatcherConfig)
      */
-    @SuppressWarnings("unchecked")
     public Matcher getMatcher(MatcherConfig config) throws MessagingException {
         
         String matchName = config.getMatcherName();
         
         try {
-            
-            String fullName;
-            if (matchName.indexOf(".") < 1) {
-                fullName = "org.apache.james.transport.matchers." + matchName;
-            } else {
-                fullName = matchName;
-            }
-            // Use the classloader which is used for bean instance stuff
-            Class clazz = getBeanFactory().getBeanClassLoader().loadClass(fullName);
-            final Matcher matcher = (Matcher) getBeanFactory().createBean(clazz);
+
+            final Matcher matcher = load(matchName);
 
             // init the matcher
             matcher.init(config);
-            
             return matcher;
 
         } catch (MessagingException me) {
             throw me;
         } catch (Exception e) {
-            throw loadFailed(matchName, e);
+            throw loadFailed(matchName, "matcher", e);
         }
     }
     
 
-    /**
-     * Constructs an appropriate exception with an appropriate message.
-     * @param name not null
-     * @param e not null
-     * @return not null
-     */
-    protected MailetException loadFailed(String name, Exception e) {
-        final StringBuilder builder =
-            new StringBuilder(128).append("Could not load ").append("mailet")
-                .append(" (").append(name).append(")");
-        final MailetException mailetException = new MailetException(builder.toString(), e);
-        return mailetException;
+
+
+    @Override
+    protected String getStandardPackage() {
+        return "org.apache.james.transport.matchers";
     }
     
 }



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