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.MailAddress; 26 import org.apache.mailet.MailetException; 27 28 import javax.mail.MessagingException ; 29 import javax.mail.internet.ParseException ; 30 31 import java.sql.Connection ; 32 import java.sql.DatabaseMetaData ; 33 import java.sql.PreparedStatement ; 34 import java.sql.ResultSet ; 35 import java.sql.SQLException ; 36 import java.util.Collection ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 40 90 public class JDBCVirtualUserTable extends AbstractVirtualUserTable 91 { 92 protected DataSourceComponent datasource; 93 94 97 protected String query = null; 98 99 102 private final JDBCUtil theJDBCUtil = new JDBCUtil() { 103 protected void delegatedLog(String logString) { 104 log("JDBCVirtualUserTable: " + logString); 105 } 106 }; 107 108 111 public void init() throws MessagingException { 112 if (getInitParameter("table") == null) { 113 throw new MailetException("Table location not specified for JDBCVirtualUserTable"); 114 } 115 116 String tableURL = getInitParameter("table"); 117 118 String datasourceName = tableURL.substring(5); 119 int pos = datasourceName.indexOf("/"); 120 String tableName = datasourceName.substring(pos + 1); 121 datasourceName = datasourceName.substring(0, pos); 122 Connection conn = null; 123 124 try { 125 ComponentManager componentManager = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); 126 DataSourceSelector datasources = (DataSourceSelector)componentManager.lookup(DataSourceSelector.ROLE); 128 datasource = (DataSourceComponent)datasources.select(datasourceName); 130 131 conn = datasource.getConnection(); 132 133 DatabaseMetaData dbMetaData = conn.getMetaData(); 135 if (!(theJDBCUtil.tableExists(dbMetaData, tableName))) { 138 StringBuffer exceptionBuffer = 139 new StringBuffer (128) 140 .append("Could not find table '") 141 .append(tableName) 142 .append("' in datasource '") 143 .append(datasourceName) 144 .append("'"); 145 throw new MailetException(exceptionBuffer.toString()); 146 } 147 148 query = getInitParameter("sqlquery"); 150 if (query == null) { 151 query = "select VirtualUserTable.target_address from VirtualUserTable, VirtualUserTable as VUTDomains where (VirtualUserTable.user like ? or VirtualUserTable.user like '\\%') and (VirtualUserTable.domain like ? or (VirtualUserTable.domain like '\\%' and VUTDomains.domain like ?)) order by concat(VirtualUserTable.user,'@',VirtualUserTable.domain) desc limit 1"; 152 } 153 } catch (MailetException me) { 154 throw me; 155 } catch (Exception e) { 156 throw new MessagingException ("Error initializing JDBCVirtualUserTable", e); 157 } finally { 158 theJDBCUtil.closeJDBCConnection(conn); 159 } 160 } 161 162 168 protected void mapRecipients(Map recipientsMap) throws MessagingException { 169 Connection conn = null; 170 PreparedStatement mappingStmt = null; 171 172 Collection recipients = recipientsMap.keySet(); 173 174 try { 175 conn = datasource.getConnection(); 176 mappingStmt = conn.prepareStatement(query); 177 178 for (Iterator i = recipients.iterator(); i.hasNext(); ) { 179 ResultSet mappingRS = null; 180 try { 181 MailAddress source = (MailAddress)i.next(); 182 mappingStmt.setString(1, source.getUser()); 183 mappingStmt.setString(2, source.getHost()); 184 mappingStmt.setString(3, source.getHost()); 185 mappingRS = mappingStmt.executeQuery(); 186 if (mappingRS.next()) { 187 String targetString = mappingRS.getString(1); 188 recipientsMap.put(source, targetString); 189 } 190 } finally { 191 theJDBCUtil.closeJDBCResultSet(mappingRS); 192 } 193 } 194 } catch (SQLException sqle) { 195 throw new MessagingException ("Error accessing database", sqle); 196 } finally { 197 theJDBCUtil.closeJDBCStatement(mappingStmt); 198 theJDBCUtil.closeJDBCConnection(conn); 199 } 200 } 201 202 public String getMailetInfo() { 203 return "JDBC Virtual User Table mailet"; 204 } 205 } 206 | Popular Tags |