package org.apache.james.transport.matchers;
/*
* 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.
*/
import org.apache.avalon.cornerstone.services.datasource.DataSourceSelector;
import org.apache.avalon.excalibur.datasource.DataSourceComponent;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.james.Constants;
import org.apache.mailet.GenericRecipientMatcher;
import org.apache.mailet.MailAddress;
import org.apache.mailet.MailetException;
import org.apache.mailet.GenericMatcher;
import org.apache.mailet.Mail;
import javax.mail.MessagingException;
import javax.mail.internet.ParseException;
import java.sql.*;
import java.util.Collection;
import java.util.Vector;
/**
*
* Special Matcher for org.apache.james.transport.mailets.JDBCListsListserv
*
* @author Danny Angus <danny@apache.org>
* This is $Revision: 1.22 $
* Committed on $Date: 2002/03/11 21:46:32 $ by: $Author: serge $
* @see org.apache.james.transport.mailets.JDBCListsListserv
*
*/
public class InJDBCListsListserv extends GenericRecipientMatcher {
private String ListTable="";
private DataSourceComponent datasource;
public void init() throws MessagingException {
try{
ListTable = getCondition();
ComponentManager componentManager = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
// Get the DataSourceSelector block
DataSourceSelector datasources = (DataSourceSelector)componentManager.lookup(DataSourceSelector.ROLE);
// Get the data-source required.
int stindex = ListTable.indexOf("://")+3;
int endindex = ListTable.indexOf("/",stindex);
String datasourceName = ListTable.substring(stindex,endindex);
datasource = (DataSourceComponent)datasources.select(datasourceName);
}catch(Exception e){
log("can't get datasource ");
}
}
public boolean matchRecipient(MailAddress recipient) {
try{
Connection conn = datasource.getConnection();
int stindex = ListTable.lastIndexOf("/")+1;
int endindex = ListTable.length();
String Table = ListTable.substring(stindex,endindex);
String query="Select count(*) from "+Table+" where list_address = '"+recipient.toString()+"'";
System.out.println(query);
PreparedStatement q = conn.prepareStatement(query);
ResultSet rs = q.executeQuery();
rs.first();
int count = rs.getInt(1);
if(count >0){
return true;
}
}catch(Exception e){
log("Cant connect to lists table for matching");
}
return false;
}
}