📄 jdbcmailrepository.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.mailrepository;import org.apache.avalon.cornerstone.services.datasource.DataSourceSelector;import org.apache.avalon.cornerstone.services.store.Store;import org.apache.avalon.cornerstone.services.store.StreamRepository;import org.apache.avalon.excalibur.datasource.DataSourceComponent;import org.apache.avalon.framework.activity.Initializable;import org.apache.avalon.framework.component.Component;import org.apache.avalon.framework.component.ComponentException;import org.apache.avalon.framework.component.ComponentManager;import org.apache.avalon.framework.component.Composable;import org.apache.avalon.framework.configuration.Configurable;import org.apache.avalon.framework.configuration.Configuration;import org.apache.avalon.framework.configuration.ConfigurationException;import org.apache.avalon.framework.configuration.DefaultConfiguration;import org.apache.avalon.framework.context.Context;import org.apache.avalon.framework.context.ContextException;import org.apache.avalon.framework.context.Contextualizable;import org.apache.avalon.framework.logger.AbstractLogEnabled;import org.apache.james.context.AvalonContextUtilities;import org.apache.james.core.MailImpl;import org.apache.james.core.MimeMessageWrapper;import org.apache.james.services.MailRepository;import org.apache.james.util.JDBCUtil;import org.apache.james.util.Lock;import org.apache.james.util.SqlResources;import org.apache.mailet.MailAddress;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.ObjectInputStream;import java.io.OutputStream;import java.sql.*;import java.util.*;/** * Implementation of a MailRepository on a database. * * <p>Requires a configuration element in the .conf.xml file of the form: * <br><repository destinationURL="db://<datasource>/<table_name>/<repository_name>" * <br> type="MAIL" * <br> model="SYNCHRONOUS"/> * <br></repository> * <p>destinationURL specifies..(Serge??) * <br>Type can be SPOOL or MAIL * <br>Model is currently not used and may be dropped * * <p>Requires a logger called MailRepository. * * @version CVS $Revision: 1.30.4.16 $ $Date: 2004/05/19 10:40:03 $ */public class JDBCMailRepository extends AbstractLogEnabled implements MailRepository, Component, Contextualizable, Composable, Configurable, Initializable { /** * Whether 'deep debugging' is turned on. */ private static final boolean DEEP_DEBUG = false; /** * The Avalon componentManager used by the instance */ private ComponentManager componentManager; /** * The Avalon context used by the instance */ protected Context context; /** * A lock used to control access to repository elements, locking access * based on the key */ private Lock lock; // Configuration elements /** * Destination URL for the repository. See class description for more info */// protected String destination; /** * The table name parsed from the destination URL */ protected String tableName; /** * The repository name parsed from the destination URL */ protected String repositoryName; /** * The name of the filestore to be used to store mail when configured to use dbfile mode. */// protected String filestore; /** * The name of the SQL configuration file to be used to configure this repository. */ private String sqlFileName; /** * The stream repository used in dbfile mode */ private StreamRepository sr = null; //The data-source for this repository /** * The selector used to obtain the JDBC datasource */ protected DataSourceSelector datasources; /** * The JDBC datasource that provides the JDBC connection */ protected DataSourceComponent datasource; /** * The name of the datasource used by this repository */ protected String datasourceName; /** * Contains all of the sql strings for this component. */ protected SqlResources sqlQueries; /** * The JDBCUtil helper class */ protected JDBCUtil theJDBCUtil; /** * "Support for Mail Attributes under JDBC repositories is ready" indicator. */ protected boolean jdbcMailAttributesReady = false; /** * @see org.apache.avalon.framework.context.Contextualizable#contextualize(Context) */ public void contextualize(final Context context) throws ContextException { this.context = context; } /** * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager) */ public void compose( final ComponentManager componentManager ) throws ComponentException { StringBuffer logBuffer = null; if (getLogger().isDebugEnabled()) { logBuffer = new StringBuffer(64) .append(this.getClass().getName()) .append(".compose()"); getLogger().debug(logBuffer.toString()); } // Get the DataSourceSelector service datasources = (DataSourceSelector)componentManager.lookup( DataSourceSelector.ROLE ); this.componentManager = componentManager; } /** * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) */ public void configure(Configuration conf) throws ConfigurationException { if (getLogger().isDebugEnabled()) { getLogger().debug(this.getClass().getName() + ".configure()"); } String destination = conf.getAttribute("destinationURL"); // normalize the destination, to simplify processing. if ( ! destination.endsWith("/") ) { destination += "/"; } // Parse the DestinationURL for the name of the datasource, // the table to use, and the (optional) repository Key. // Split on "/", starting after "db://" List urlParams = new ArrayList(); int start = 5; if (destination.startsWith("dbfile")) { //this is dbfile:// instead of db:// start += 4; } int end = destination.indexOf('/', start); while ( end > -1 ) { urlParams.add(destination.substring(start, end)); start = end + 1; end = destination.indexOf('/', start); } // Build SqlParameters and get datasource name from URL parameters if (urlParams.size() == 0) { StringBuffer exceptionBuffer = new StringBuffer(256) .append("Malformed destinationURL - Must be of the format '") .append("db://<data-source>[/<table>[/<repositoryName>]]'. Was passed ") .append(conf.getAttribute("destinationURL")); throw new ConfigurationException(exceptionBuffer.toString()); } if (urlParams.size() >= 1) { datasourceName = (String)urlParams.get(0); } if (urlParams.size() >= 2) { tableName = (String)urlParams.get(1); } if (urlParams.size() >= 3) { repositoryName = ""; for (int i = 2; i < urlParams.size(); i++) { if (i >= 3) { repositoryName += '/'; } repositoryName += (String)urlParams.get(i); } } if (getLogger().isDebugEnabled()) { StringBuffer logBuffer = new StringBuffer(128) .append("Parsed URL: table = '") .append(tableName) .append("', repositoryName = '") .append(repositoryName) .append("'"); getLogger().debug(logBuffer.toString()); } String filestore = conf.getChild("filestore").getValue(null); sqlFileName = conf.getChild("sqlFile").getValue(); if (!sqlFileName.startsWith("file://")) { throw new ConfigurationException ("Malformed sqlFile - Must be of the format 'file://<filename>'."); } try { if (filestore != null) { Store store = (Store)componentManager. lookup("org.apache.avalon.cornerstone.services.store.Store"); //prepare Configurations for stream repositories DefaultConfiguration streamConfiguration = new DefaultConfiguration( "repository", "generated:JDBCMailRepository.configure()" ); streamConfiguration.setAttribute( "destinationURL", filestore ); streamConfiguration.setAttribute( "type", "STREAM" ); streamConfiguration.setAttribute( "model", "SYNCHRONOUS" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -