1 17 18 package org.apache.james.transport.mailets; 19 20 import org.apache.avalon.cornerstone.services.datasource.DataSourceSelector; 21 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 22 import org.apache.avalon.framework.component.ComponentManager; 23 import org.apache.james.Constants; 24 import org.apache.james.util.JDBCUtil; 25 import org.apache.mailet.GenericMailet; 26 import org.apache.mailet.Mail; 27 import org.apache.mailet.MailAddress; 28 import org.apache.mailet.MailetException; 29 30 import javax.mail.MessagingException ; 31 import javax.mail.internet.ParseException ; 32 import java.sql.*; 33 import java.util.Collection ; 34 import java.util.Iterator ; 35 import java.util.Locale ; 36 import java.util.Vector ; 37 38 50 public class JDBCAlias extends GenericMailet { 51 52 protected DataSourceComponent datasource; 53 protected String query = null; 54 55 private final JDBCUtil theJDBCUtil = 57 new JDBCUtil() { 58 protected void delegatedLog(String logString) { 59 log("JDBCAlias: " + logString); 60 } 61 }; 62 63 66 public void init() throws MessagingException { 67 String mappingsURL = getInitParameter("mappings"); 68 69 String datasourceName = mappingsURL.substring(5); 70 int pos = datasourceName.indexOf("/"); 71 String tableName = datasourceName.substring(pos + 1); 72 datasourceName = datasourceName.substring(0, pos); 73 74 Connection conn = null; 75 if (getInitParameter("source_column") == null) { 76 throw new MailetException("source_column not specified for JDBCAlias"); 77 } 78 if (getInitParameter("target_column") == null) { 79 throw new MailetException("target_column not specified for JDBCAlias"); 80 } 81 try { 82 ComponentManager componentManager = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); 83 DataSourceSelector datasources = (DataSourceSelector)componentManager.lookup(DataSourceSelector.ROLE); 85 datasource = (DataSourceComponent)datasources.select(datasourceName); 87 88 conn = datasource.getConnection(); 89 90 DatabaseMetaData dbMetaData = conn.getMetaData(); 92 if (!(theJDBCUtil.tableExists(dbMetaData, tableName))) { 95 StringBuffer exceptionBuffer = 96 new StringBuffer (128) 97 .append("Could not find table '") 98 .append(tableName) 99 .append("' in datasource '") 100 .append(datasourceName) 101 .append("'"); 102 throw new MailetException(exceptionBuffer.toString()); 103 } 104 105 StringBuffer queryBuffer = 107 new StringBuffer (128) 108 .append("SELECT ") 109 .append(getInitParameter("target_column")) 110 .append(" FROM ") 111 .append(tableName) 112 .append(" WHERE ") 113 .append(getInitParameter("source_column")) 114 .append(" = ?"); 115 query = queryBuffer.toString(); 116 } catch (MailetException me) { 117 throw me; 118 } catch (Exception e) { 119 throw new MessagingException ("Error initializing JDBCAlias", e); 120 } finally { 121 theJDBCUtil.closeJDBCConnection(conn); 122 } 123 } 124 125 public void service(Mail mail) throws MessagingException { 126 128 Connection conn = null; 129 PreparedStatement mappingStmt = null; 130 ResultSet mappingRS = null; 131 132 Collection recipients = mail.getRecipients(); 133 Collection recipientsToRemove = new Vector (); 134 Collection recipientsToAdd = new Vector (); 135 try { 136 conn = datasource.getConnection(); 137 mappingStmt = conn.prepareStatement(query); 138 139 140 for (Iterator i = recipients.iterator(); i.hasNext(); ) { 141 try { 142 MailAddress source = (MailAddress)i.next(); 143 mappingStmt.setString(1, source.toString()); 144 mappingRS = mappingStmt.executeQuery(); 145 if (!mappingRS.next()) { 146 continue; 148 } 149 try { 150 String targetString = mappingRS.getString(1); 151 MailAddress target = new MailAddress(targetString); 152 153 recipientsToRemove.add(source); 155 recipientsToAdd.add(target); 156 } catch (ParseException pe) { 157 StringBuffer exceptionBuffer = 159 new StringBuffer (128) 160 .append("There is an invalid alias from ") 161 .append(source) 162 .append(" to ") 163 .append(mappingRS.getString(1)); 164 log(exceptionBuffer.toString()); 165 continue; 166 } 167 } finally { 168 ResultSet localRS = mappingRS; 169 mappingRS = null; 171 theJDBCUtil.closeJDBCResultSet(localRS); 172 } 173 } 174 } catch (SQLException sqle) { 175 throw new MessagingException ("Error accessing database", sqle); 176 } finally { 177 theJDBCUtil.closeJDBCStatement(mappingStmt); 178 theJDBCUtil.closeJDBCConnection(conn); 179 } 180 181 recipients.removeAll(recipientsToRemove); 182 recipients.addAll(recipientsToAdd); 183 } 184 185 190 public String getMailetInfo() { 191 return "JDBC aliasing mailet"; 192 } 193 194 } 195 | Popular Tags |