--- C:\temp\james servernames\James-trunk.java Mon Oct 17 09:34:46 2005 +++ C:\temp\james servernames\James-patched.java Mon Oct 17 09:41:27 2005 @@ -67,10 +67,20 @@ import java.util.Iterator; import java.util.Locale; import java.util.Map; import java.util.Vector; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import org.apache.avalon.cornerstone.services.datasource.DataSourceSelector; +import org.apache.avalon.excalibur.datasource.DataSourceComponent; +import org.apache.james.util.JDBCUtil; + + + /** * Core class for JAMES. Provides three primary services: *
1) Instantiates resources, such as user repository, and protocol * handlers *
2) Handles interactions between components @@ -180,10 +190,16 @@ * TODO We should remove this and its initialization when we remove storeMail method. */ protected Mailet localDeliveryMailet; /** + * The database and table to use for checking virtual domains. + */ + private String domainTable = null; + + + /** * @see org.apache.avalon.framework.context.Contextualizable#contextualize(Context) */ public void contextualize(final Context context) { this.myContext = context; } @@ -253,10 +269,17 @@ context = new DefaultContext(); context.put("HostName", hostName); getLogger().info("Local host is: " + hostName); // Get the domains and hosts served by this instance + + try { + domainTable = serverConf.getAttribute("domainTable"); + } catch (Exception e) { + domainTable = ""; + } + serverNames = new HashSet(); Configuration serverConf = conf.getChild("servernames"); if (serverConf.getAttributeAsBoolean("autodetect") && (!hostName.equals("localhost"))) { serverNames.add(hostName.toLowerCase(Locale.US)); } @@ -722,11 +745,71 @@ * * @param serverName the name of the server to check * @return whether the server is local */ public boolean isLocalServer( final String serverName ) { - return serverNames.contains(serverName.toLowerCase(Locale.US)); + if (serverNames.contains(serverName.toLowerCase(Locale.US))) { + return true; + } + + if (domainTable!=null && !domainTable.equals("")){ + + + + String datasourceName = domainTable.substring(5); + int pos = datasourceName.indexOf("/"); + String tableName = datasourceName.substring(pos + 1); + datasourceName = datasourceName.substring(0, pos); + Connection conn = null; + DataSourceComponent datasource; + JDBCUtil theJDBCUtil = new JDBCUtil() { + protected void delegatedLog(String logString) { + log("JDBCVirtualDomains: " + logString); + } + }; + String query = null; + + try { + ComponentManager componentManager = compMgr; + // Get the DataSourceSelector service + DataSourceSelector datasources = (DataSourceSelector)componentManager.lookup(DataSourceSelector.ROLE); + // Get the data-source required. + datasource = (DataSourceComponent)datasources.select(datasourceName); + + conn = datasource.getConnection(); + + + // Check if the required table exists. If not, complain. + DatabaseMetaData dbMetaData = conn.getMetaData(); + // Need to ask in the case that identifiers are stored, ask the DatabaseMetaInfo. + // Try UPPER, lower, and MixedCase, to see if the table is there. + if (!(theJDBCUtil.tableExists(dbMetaData, tableName))) { + return false; + } + + //Build the query + + query = "select distinct domain from " + tableName + " where domain=?"; + PreparedStatement domainStatement = conn.prepareStatement(query); + domainStatement.setString(1,serverName); + + ResultSet domainRS = domainStatement.executeQuery(); + + if (domainRS.next()) { + return true; + } + + + } catch (Exception e) { + return false; + } finally { + theJDBCUtil.closeJDBCConnection(conn); + } + + } + + return false; } /** * Return the type of the server *