I got it to work using some of the James provided source code in the
org.apache.james.transport.mailets package. I utilized the
JDBCVirtualUserTable.java which gave me a real good feel of what I needed to
do:
package com.company.my.mailets;
import java.sql.*;
import javax.mail.MessagingException;
import org.apache.mailet.*;
import org.apache.james.util.JDBCUtil;
import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector
;
import org.apache.avalon.excalibur.datasource.DataSourceComponent;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.james.Constants;
public class myMailet extends GenericMailet {
public static final String DATASOURCE = "ds";
private DataSourceComponent datasource;
private final JDBCUtil theJDBCUtil = new JDBCUtil() {
protected void delegatedLog(String logString) {
log("JDBCVirtualUserTable: " + logString);
}
};
public myMailet () {
}
public void init() throws MessagingException {
try {
if (this.getInitParameter(DATASOURCE) == null)
throw new MailetException ("datasource not specified");
String ds = this.getInitParameter(DATASOURCE);
ServiceManager componentManager =
(ServiceManager) getMailetContext()
.getAttribute(Constants.AVALON_COMPONENT_MANAGER);
DataSourceSelector datasources =
(DataSourceSelector)componentManager
.lookup(DataSourceSelector.ROLE);
// Get the data-source required.
datasource = (DataSourceComponent)datasources.select(ds);
}
catch (MailetException me) {
throw me;
}
catch (Exception e) {
throw new MessagingException(
"Error initializing CompliantStorage", e);
}
}
public void service(Mail mail) throws MessagingException {
Connection conn = null;
try {
conn = datasource.getConnection();
...
}
catch (Exception e) {
throw new MessagingException
("problem CompliantStorage.service", e);
}
finally {
theJDBCUtil.closeJDBCConnection(conn);
}
}
Cheers,
-Rogier
On Nov 6, 2007 2:41 PM, Rogier Doekes <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Maybe a simple question for everyone involved, but I cannot seem to get
> this to work..
>
> I'm trying to write a mailet which does some processing on each message
> which comes into our James server. I would like to use the JDBC Connection
> Pool I defined in the config.xml, which is working fine as the mail flows
> in and through James and the database without problem.
> I tried to open a connection using the following snippet:
>
> package com.company.my.mailets;
>
> import java.sql.*;
> import javax.mail.MessagingException;
> import org.apache.mailet.*;
> import org.apache.james.util.mordred.JdbcDataSource;
>
> class processing extends GenericMailet
> {
>
> public void service(Mail mail) throws MessagingException
> {
> Connection conn = new JdbcDataSource().getConnection();
> .....
> if (conn != null) conn.close();
> }
> }
>
> This does not seem to be the right way to get a connection. My
> Spoolmanager logs the getConnection() as a java.lang.NullPointerException,
> so I am probably not setting up the connection correctly.
> How do I set up the connection properly?
>
> Thanks a lot,
>
> -Rogier
>
--
Rogier Doekes
[EMAIL PROTECTED]