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/08/19 14:35:55 UTC
svn commit: r805775 - in
/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport:
AbstractLoader.java JamesMailetLoader.java JamesMatcherLoader.java
Author: rdonkin
Date: Wed Aug 19 12:35:54 2009
New Revision: 805775
URL: http://svn.apache.org/viewvc?rev=805775&view=rev
Log:
Tidied up code, generified and abstracted shared code into superclass
Modified:
james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/AbstractLoader.java
james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/JamesMailetLoader.java
james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/JamesMatcherLoader.java
Modified: james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/AbstractLoader.java
URL: http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/AbstractLoader.java?rev=805775&r1=805774&r2=805775&view=diff
==============================================================================
--- james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/AbstractLoader.java (original)
+++ james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/AbstractLoader.java Wed Aug 19 12:35:54 2009
@@ -39,13 +39,13 @@
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
import org.apache.mailet.MailetContext;
+import org.apache.mailet.MailetException;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
- *
- * $Id$
+ * Common services for loaders.
*/
public abstract class AbstractLoader extends AbstractLogEnabled implements Serviceable, Configurable, Initializable {
@@ -55,7 +55,7 @@
/**
* The list of packages that may contain Mailets or matchers
*/
- protected Vector packages;
+ protected Vector<String> packages;
/**
* System service manager
@@ -75,10 +75,16 @@
public void setMailetContext(MailetContext mailetContext) {
this.mailetContext = mailetContext;
}
+
+
+ protected Object load(String className) throws InstantiationException,
+ IllegalAccessException, ClassNotFoundException {
+ return Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
+ }
protected void getPackages(Configuration conf, String packageType)
throws ConfigurationException {
- packages = new Vector();
+ packages = new Vector<String>();
packages.addElement("");
final Configuration[] pkgConfs = conf.getChildren(packageType);
for (int i = 0; i < pkgConfs.length; i++) {
@@ -116,9 +122,50 @@
* @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
*/
public abstract void configure(Configuration arg0) throws ConfigurationException;
+
+ /**
+ * Gets a human readable description of the loader.
+ * Used for messages.
+ * @return not null
+ */
+ protected abstract String getDisplayName();
+
+ /**
+ * Constructs an appropriate exception with an appropriate message
+ * @param name not null
+ * @return not null
+ */
+ protected ClassNotFoundException classNotFound(String name) throws ClassNotFoundException {
+ final StringBuilder builder =
+ new StringBuilder(128)
+ .append("Requested ")
+ .append(getDisplayName())
+ .append(" not found: ")
+ .append(name)
+ .append(". Package searched: ");
+ for (final String packageName:packages) {
+ builder.append(packageName);
+ builder.append(" ");
+ }
+ return new ClassNotFoundException(builder.toString());
+ }
+
+ /**
+ * 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(getDisplayName())
+ .append(" (").append(name).append(")");
+ final MailetException mailetException = new MailetException(builder.toString(), e);
+ return mailetException;
+ }
/**
- * Wrapper fot a MailetContext that simply override the used logger.
+ * Wrapper for a MailetContext that simply override the used logger.
*/
protected final static class MailetContextWrapper implements MailetContext {
@@ -163,6 +210,7 @@
/**
* @see org.apache.mailet.MailetContext#getAttributeNames()
*/
+ @SuppressWarnings("unchecked")
public Iterator getAttributeNames() {
return mailetContext.getAttributeNames();
}
@@ -170,6 +218,7 @@
/**
* @see org.apache.mailet.MailetContext#getMailServers(java.lang.String)
*/
+ @SuppressWarnings("unchecked")
public Collection getMailServers(String host) {
return mailetContext.getMailServers(host);
}
@@ -198,6 +247,7 @@
/**
* @see org.apache.mailet.MailetContext#getSMTPHostAddresses(java.lang.String)
*/
+ @SuppressWarnings("unchecked")
public Iterator getSMTPHostAddresses(String domainName) {
return mailetContext.getSMTPHostAddresses(domainName);
}
@@ -226,6 +276,7 @@
/**
* @see org.apache.mailet.MailetContext#isLocalUser(java.lang.String)
*/
+ @SuppressWarnings("deprecation")
public boolean isLocalUser(String userAccount) {
return mailetContext.isLocalUser(userAccount);
}
@@ -261,6 +312,7 @@
/**
* @see org.apache.mailet.MailetContext#sendMail(org.apache.mailet.MailAddress, java.util.Collection, javax.mail.internet.MimeMessage)
*/
+ @SuppressWarnings("unchecked")
public void sendMail(MailAddress sender, Collection recipients, MimeMessage msg) throws MessagingException {
mailetContext.sendMail(sender, recipients, msg);
}
@@ -268,6 +320,7 @@
/**
* @see org.apache.mailet.MailetContext#sendMail(org.apache.mailet.MailAddress, java.util.Collection, javax.mail.internet.MimeMessage, java.lang.String)
*/
+ @SuppressWarnings("unchecked")
public void sendMail(MailAddress sender, Collection recipients, MimeMessage msg, String state) throws MessagingException {
mailetContext.sendMail(sender, recipients, msg, state);
}
@@ -289,6 +342,7 @@
/**
* @see org.apache.mailet.MailetContext#storeMail(MailAddress, MailAddress, MimeMessage)
*/
+ @SuppressWarnings("deprecation")
public void storeMail(MailAddress sender, MailAddress recipient, MimeMessage msg) throws MessagingException {
mailetContext.storeMail(sender, recipient, msg);
}
Modified: james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/JamesMailetLoader.java
URL: http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/JamesMailetLoader.java?rev=805775&r1=805774&r2=805775&view=diff
==============================================================================
--- james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/JamesMailetLoader.java (original)
+++ james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/JamesMailetLoader.java Wed Aug 19 12:35:54 2009
@@ -29,13 +29,13 @@
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.james.api.kernel.ServiceLocator;
import org.apache.mailet.Mailet;
-import org.apache.mailet.MailetException;
/**
* Loads Mailets for use inside James.
*
*/
public class JamesMailetLoader extends AbstractLoader implements MailetLoader {
+ private static final String DISPLAY_NAME = "mailet";
private ServiceLocator serviceLocator;
/**
@@ -65,14 +65,13 @@
/**
* @see org.apache.james.transport.MailetLoader#getMailet(java.lang.String, org.apache.avalon.framework.configuration.Configuration)
*/
- public Mailet getMailet(String mailetName, Configuration configuration)
- throws MessagingException {
+ public Mailet getMailet(final String mailetName, final Configuration configuration) throws MessagingException {
try {
- for (int i = 0; i < packages.size(); i++) {
- String className = (String) packages.elementAt(i) + mailetName;
+ for (final String packageName:packages) {
+ final String className = packageName + mailetName;
try {
- Mailet mailet = (Mailet) Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
- MailetConfigImpl configImpl = new MailetConfigImpl();
+ final Mailet mailet = (Mailet) load(className);
+ final MailetConfigImpl configImpl = new MailetConfigImpl();
configImpl.setMailetName(mailetName);
configImpl.setConfiguration(configuration);
configImpl.setMailetContext(new MailetContextWrapper(mailetContext, getLogger().getChildLogger(mailetName)));
@@ -85,20 +84,11 @@
//do this so we loop through all the packages
}
}
- StringBuilder exceptionBuffer =
- new StringBuilder(128)
- .append("Requested mailet not found: ")
- .append(mailetName)
- .append(". looked in ")
- .append(packages.toString());
- throw new ClassNotFoundException(exceptionBuffer.toString());
+ throw classNotFound(mailetName);
} catch (MessagingException me) {
throw me;
} catch (Exception e) {
- StringBuilder exceptionBuffer =
- new StringBuilder(128).append("Could not load mailet (").append(mailetName).append(
- ")");
- throw new MailetException(exceptionBuffer.toString(), e);
+ throw loadFailed(mailetName, e);
}
}
@@ -125,4 +115,12 @@
}
}
}
+
+ /**
+ * @see AbstractLoader#getDisplayName()
+ */
+ @Override
+ protected String getDisplayName() {
+ return DISPLAY_NAME;
+ }
}
Modified: james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/JamesMatcherLoader.java
URL: http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/JamesMatcherLoader.java?rev=805775&r1=805774&r2=805775&view=diff
==============================================================================
--- james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/JamesMatcherLoader.java (original)
+++ james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/JamesMatcherLoader.java Wed Aug 19 12:35:54 2009
@@ -24,13 +24,14 @@
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.mailet.MailetException;
import org.apache.mailet.Matcher;
/**
* Loads Matchers for use inside James.
*
*/
public class JamesMatcherLoader extends AbstractLoader implements MatcherLoader {
+ private static final String DISPLAY_NAME = "matcher";
+
/**
* @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
*/
@@ -38,9 +39,9 @@
getPackages(conf,MATCHER_PACKAGE);
}
- /* (non-Javadoc)
- * @see org.apache.james.transport.MatcherLoader#getMatcher(java.lang.String)
- */
+ /**
+ * @see org.apache.james.transport.MatcherLoader#getMatcher(java.lang.String)
+ */
public Matcher getMatcher(String matchName) throws MessagingException {
try {
String condition = (String) null;
@@ -49,11 +50,11 @@
condition = matchName.substring(i + 1);
matchName = matchName.substring(0, i);
}
- for (i = 0; i < packages.size(); i++) {
- String className = (String) packages.elementAt(i) + matchName;
+ for (final String packageName: packages) {
+ final String className = packageName + matchName;
try {
- Matcher matcher = (Matcher) Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
- MatcherConfigImpl configImpl = new MatcherConfigImpl();
+ final Matcher matcher = (Matcher) load(className);
+ final MatcherConfigImpl configImpl = new MatcherConfigImpl();
configImpl.setMatcherName(matchName);
configImpl.setCondition(condition);
configImpl.setMailetContext(new MailetContextWrapper(mailetContext, getLogger().getChildLogger(matchName)));
@@ -63,20 +64,19 @@
//do this so we loop through all the packages
}
}
- StringBuilder exceptionBuffer =
- new StringBuilder(128)
- .append("Requested matcher not found: ")
- .append(matchName)
- .append(". looked in ")
- .append(packages.toString());
- throw new ClassNotFoundException(exceptionBuffer.toString());
+ throw classNotFound(matchName);
} catch (MessagingException me) {
throw me;
} catch (Exception e) {
- StringBuilder exceptionBuffer =
- new StringBuilder(128).append("Could not load matcher (").append(matchName).append(
- ")");
- throw new MailetException(exceptionBuffer.toString(), e);
+ throw loadFailed(matchName, e);
}
}
+
+ /**
+ * @see AbstractLoader#getDisplayName()
+ */
+ @Override
+ protected String getDisplayName() {
+ return DISPLAY_NAME;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org