⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcalias.java

📁 java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识
💻 JAVA
字号:
/*********************************************************************** * Copyright (c) 2000-2004 The Apache Software Foundation.             * * All rights reserved.                                                * * ------------------------------------------------------------------- * * Licensed under the Apache License, Version 2.0 (the "License"); you * * may not use this file except in compliance with the License. You    * * may obtain a copy of the License at:                                * *                                                                     * *     http://www.apache.org/licenses/LICENSE-2.0                      * *                                                                     * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS,   * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     * * implied.  See the License for the specific language governing       * * permissions and limitations under the License.                      * ***********************************************************************/package org.apache.james.transport.mailets;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.james.util.JDBCUtil;import org.apache.mailet.GenericMailet;import org.apache.mailet.Mail;import org.apache.mailet.MailAddress;import org.apache.mailet.MailetException;import javax.mail.MessagingException;import javax.mail.internet.ParseException;import java.sql.*;import java.util.Collection;import java.util.Iterator;import java.util.Locale;import java.util.Vector;/** * Rewrites recipient addresses based on a database table.  The connection * is configured by passing the URL to a conn definition.  You need to set * the table name to check (or view) along with the source and target columns * to use.  For example, * <mailet match="All" class="JDBCAlias"> *   <mappings>db://maildb/Aliases</mappings> *   <source_column>source_email_address</source_column> *   <target_column>target_email_address</target_column> * </mailet> * */public class JDBCAlias extends GenericMailet {    protected DataSourceComponent datasource;    protected String query = null;    // The JDBCUtil helper class    private final JDBCUtil theJDBCUtil =            new JDBCUtil() {                protected void delegatedLog(String logString) {                    log("JDBCAlias: " + logString);                }            };    /**     * Initialize the mailet     */    public void init() throws MessagingException {        String mappingsURL = getInitParameter("mappings");        String datasourceName = mappingsURL.substring(5);        int pos = datasourceName.indexOf("/");        String tableName = datasourceName.substring(pos + 1);        datasourceName = datasourceName.substring(0, pos);        Connection conn = null;        if (getInitParameter("source_column") == null) {            throw new MailetException("source_column not specified for JDBCAlias");        }        if (getInitParameter("target_column") == null) {            throw new MailetException("target_column not specified for JDBCAlias");        }        try {            ComponentManager componentManager = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);            // 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)))  {                StringBuffer exceptionBuffer =                    new StringBuffer(128)                            .append("Could not find table '")                            .append(tableName)                            .append("' in datasource '")                            .append(datasourceName)                            .append("'");                throw new MailetException(exceptionBuffer.toString());            }            //Build the query            StringBuffer queryBuffer =                new StringBuffer(128)                        .append("SELECT ")                        .append(getInitParameter("target_column"))                        .append(" FROM ")                        .append(tableName)                        .append(" WHERE ")                        .append(getInitParameter("source_column"))                        .append(" = ?");            query = queryBuffer.toString();        } catch (MailetException me) {            throw me;        } catch (Exception e) {            throw new MessagingException("Error initializing JDBCAlias", e);        } finally {            theJDBCUtil.closeJDBCConnection(conn);        }    }    public void service(Mail mail) throws MessagingException {        //Then loop through each address in the recipient list and try to map it according to the alias table        Connection conn = null;        PreparedStatement mappingStmt = null;        ResultSet mappingRS = null;        Collection recipients = mail.getRecipients();        Collection recipientsToRemove = new Vector();        Collection recipientsToAdd = new Vector();        try {            conn = datasource.getConnection();            mappingStmt = conn.prepareStatement(query);            for (Iterator i = recipients.iterator(); i.hasNext(); ) {                try {                    MailAddress source = (MailAddress)i.next();                    mappingStmt.setString(1, source.toString());                    mappingRS = mappingStmt.executeQuery();                    if (!mappingRS.next()) {                        //This address was not found                        continue;                    }                    try {                        String targetString = mappingRS.getString(1);                        MailAddress target = new MailAddress(targetString);                        //Mark this source address as an address to remove from the recipient list                        recipientsToRemove.add(source);                        recipientsToAdd.add(target);                    } catch (ParseException pe) {                        //Don't alias this address... there's an invalid address mapping here                        StringBuffer exceptionBuffer =                            new StringBuffer(128)                                    .append("There is an invalid alias from ")                                    .append(source)                                    .append(" to ")                                    .append(mappingRS.getString(1));                        log(exceptionBuffer.toString());                        continue;                    }                } finally {                    ResultSet localRS = mappingRS;                    // Clear reference to result set                    mappingRS = null;                    theJDBCUtil.closeJDBCResultSet(localRS);                }            }        } catch (SQLException sqle) {            throw new MessagingException("Error accessing database", sqle);        } finally {            theJDBCUtil.closeJDBCStatement(mappingStmt);            theJDBCUtil.closeJDBCConnection(conn);        }        recipients.removeAll(recipientsToRemove);        recipients.addAll(recipientsToAdd);    }    /**     * Return a string describing this mailet.     *     * @return a string describing this mailet     */    public String getMailetInfo() {        return "JDBC aliasing mailet";    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -