Hi,
I think it would be nice if James mailets can be changed into Avalon
components. By doing so, you could get access to the might be useful
components from Excalibur or Cornerstone from your mailets. I don't think
there would be many changes in James, all you need is to have
GenericMailet to extend AbstractLoggable and implement Component, and also
some lines in the JamesSpoolManager like in the following: (the "if
(mailet instanceof Component)" might not be needed).
try {
mailet = mailetLoader.getMailet(mailetClassName, mailetcontext,c);
if (mailet instanceof Component) {
if (mailet instanceof AbstractLoggable) {
((GenericMailet) mailet).setLogger(getLogger());
}
if (mailet instanceof Composable) {
((Composable) mailet).compose(compMgr);
}
if (mailet instanceof Initializable) {
((Initializable) mailet).initialize();
}
}
getLogger().info("Mailet " + mailetClassName + " instantiated");
} catch (MessagingException ex) {
// ...
}
The init() method in the mailets can be replaced by initialize(), but the
init() signature should be left there (it is invoked by
mailetLoader.getMailet()). But since the code changes in
JamesSpoolRepository tests whether the mailet is a Component or not, the
current mailets (which are not Component) would work as they should be.
The following is a mailet which stores some information of a message into
a database table; could be useful if you want to have some statistics of
your email users.
As I understand it, the latest James already uses JdbcDataSource, so the
componentManager.lookup() below can be changed to use the interface (role)
of the block where the latest James gets the JdbcDataSource from. And
also, it would be nicer if the SQL statement used is retrieved from the
SQL repository.
public class StoreSenderAndRecipients extends GenericMailet
implements Composable, Initializable {
private String sender;
private String recipients;
private String remoteAddr;
private int size;
private JdbcDataSource dataSource;
private DataSourceSelector dataSourceSelector;
private String storeSQL;
private JdbcConnection conn;
private PreparedStatement ps;
public StoreSenderAndRecipients() {}
public void compose(ComponentManager componentManager)
throws ComponentException {
dataSourceSelector = (DataSourceSelector)
componentManager.lookup("com.pindad.james.services.DataSourceSelector");
getLogger().debug(getClass().getName() +
".compose(ComponentManager): " +
"Composed");
}
public void initialize() throws Exception {
dataSource = (JdbcDataSource)
dataSourceSelector.select("mailetsource");
storeSQL =
"insert into SenderRecipients (sender, recipients,
remote_addr, size) " +
"values (?, ?, ?, ?)";
conn = (JdbcConnection) dataSource.getConnection();
ps = conn.prepareStatement(storeSQL);
getLogger().debug(getClass().getName() + ".initialize():
Initialized");
}
public void init() {
}
public void service(Mail mail) throws MessagingException {
MimeMessage message = mail.getMessage();
size = message.getSize();
MailImpl mc = (MailImpl) mail;
sender = mc.getSender().toString();
remoteAddr = mc.getRemoteAddr();
StringBuffer buff = new StringBuffer();
Collection rcpts = mc.getRecipients();
for (Iterator i = rcpts.iterator(); i.hasNext();) {
String address = ((MailAddress) i.next()).toString();
buff.append(address + " ");
}
recipients = buff.toString();
// System.out.println("Sender: " + sender);
// System.out.println("Recipients: " + recipients);
// System.out.println("Size: " + size);
try {
ps.setString(1, sender);
ps.setString(2, recipients);
ps.setString(3, remoteAddr);
ps.setInt(4, size);
ps.execute();
} catch(SQLException sqe) {
getLogger().error(getClass().getName() + ".service(Mail): " +
sqe.getMessage());
}
}
public void finalize() {
try {
conn.close();
} catch(Exception e) {
getLogger().error(getClass().getName() + ".finalize(): " +
e.getMessage());
}
}
public String getMailetInfo() {
return getClass().getName();
}
}
Just some ideas of improvement (I think),
Oki
ps: Could be nicer if matchers are Components too; say, you'd like to have
your own spam blackhole matcher based on a database table; which could be
neat, because you don't have to restart James to have it in effect.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]