You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ba...@apache.org on 2006/07/27 01:06:49 UTC
svn commit: r425899 - in
/james/server/trunk/src/java/org/apache/james/mailrepository:
AbstractMailRepository.java AvalonMailRepository.java JDBCMailRepository.java
Author: bago
Date: Wed Jul 26 16:06:49 2006
New Revision: 425899
URL: http://svn.apache.org/viewvc?rev=425899&view=rev
Log:
Moved common code for MailRepositories to AbstractMailRepository.
Hint came from http://james.apache.org/server/cpd.html
Added:
james/server/trunk/src/java/org/apache/james/mailrepository/AbstractMailRepository.java (with props)
Modified:
james/server/trunk/src/java/org/apache/james/mailrepository/AvalonMailRepository.java
james/server/trunk/src/java/org/apache/james/mailrepository/JDBCMailRepository.java
Added: james/server/trunk/src/java/org/apache/james/mailrepository/AbstractMailRepository.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/mailrepository/AbstractMailRepository.java?rev=425899&view=auto
==============================================================================
--- james/server/trunk/src/java/org/apache/james/mailrepository/AbstractMailRepository.java (added)
+++ james/server/trunk/src/java/org/apache/james/mailrepository/AbstractMailRepository.java Wed Jul 26 16:06:49 2006
@@ -0,0 +1,220 @@
+/***********************************************************************
+ * Copyright (c) 1999-2006 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.store.Store;
+import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.james.services.MailRepository;
+import org.apache.james.util.Lock;
+import org.apache.mailet.Mail;
+
+import javax.mail.MessagingException;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ */
+public abstract class AbstractMailRepository extends AbstractLogEnabled
+ implements MailRepository, Serviceable, Configurable, Initializable {
+
+ /**
+ * Whether 'deep debugging' is turned on.
+ */
+ protected static final boolean DEEP_DEBUG = false;
+
+ /**
+ * A lock used to control access to repository elements, locking access
+ * based on the key
+ */
+ private Lock lock;
+
+ protected Store store; // variable is not used beyond initialization
+
+ void setStore(Store store) {
+ this.store = store;
+ }
+
+
+ public void initialize() throws Exception {
+ lock = new Lock();
+ }
+
+
+ /**
+ * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager )
+ */
+ public void service( final ServiceManager componentManager )
+ throws ServiceException {
+ setStore((Store)componentManager.lookup( Store.ROLE ));
+ }
+
+ /**
+ * Releases a lock on a message identified by a key
+ *
+ * @param key the key of the message to be unlocked
+ *
+ * @return true if successfully released the lock, false otherwise
+ */
+ public boolean unlock(String key) {
+ if (lock.unlock(key)) {
+ if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
+ StringBuffer debugBuffer =
+ new StringBuffer(256)
+ .append("Unlocked ")
+ .append(key)
+ .append(" for ")
+ .append(Thread.currentThread().getName())
+ .append(" @ ")
+ .append(new java.util.Date(System.currentTimeMillis()));
+ getLogger().debug(debugBuffer.toString());
+ }
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Obtains a lock on a message identified by a key
+ *
+ * @param key the key of the message to be locked
+ *
+ * @return true if successfully obtained the lock, false otherwise
+ */
+ public boolean lock(String key) {
+ if (lock.lock(key)) {
+ if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
+ StringBuffer debugBuffer =
+ new StringBuffer(256)
+ .append("Locked ")
+ .append(key)
+ .append(" for ")
+ .append(Thread.currentThread().getName())
+ .append(" @ ")
+ .append(new java.util.Date(System.currentTimeMillis()));
+ getLogger().debug(debugBuffer.toString());
+ }
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+
+ /**
+ * Store this message to the database. Optionally stores the message
+ * body to the filesystem and only writes the headers to the database.
+ */
+ public void store(Mail mc) throws MessagingException {
+ boolean wasLocked = true;
+ String key = mc.getName();
+ try {
+ synchronized(this) {
+ wasLocked = lock.isLocked(key);
+ if (!wasLocked) {
+ //If it wasn't locked, we want a lock during the store
+ lock(key);
+ }
+ }
+ internalStore(mc);
+ if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
+ StringBuffer logBuffer =
+ new StringBuffer(64)
+ .append("Mail ")
+ .append(key)
+ .append(" stored.");
+ getLogger().debug(logBuffer.toString());
+ }
+ } catch (MessagingException e) {
+ getLogger().error("Exception caught while storing mail "+key,e);
+ throw e;
+ } catch (Exception e) {
+ getLogger().error("Exception caught while storing mail "+key,e);
+ throw new MessagingException("Exception caught while storing mail "+key,e);
+ } finally {
+ if (!wasLocked) {
+ // If it wasn't locked, we need to unlock now
+ unlock(key);
+ synchronized (this) {
+ notify();
+ }
+ }
+ }
+ }
+
+
+ protected abstract void internalStore(Mail mc) throws MessagingException, IOException;
+
+
+ /**
+ * Removes a specified message
+ *
+ * @param mail the message to be removed from the repository
+ */
+ public void remove(Mail mail) throws MessagingException {
+ remove(mail.getName());
+ }
+
+
+ /**
+ * Removes a Collection of mails from the repository
+ * @param mails The Collection of <code>MailImpl</code>'s to delete
+ * @throws MessagingException
+ * @since 2.2.0
+ */
+ public void remove(Collection mails) throws MessagingException {
+ Iterator delList = mails.iterator();
+ while (delList.hasNext()) {
+ remove((Mail)delList.next());
+ }
+ }
+
+ /**
+ * Removes a message identified by key.
+ *
+ * @param key the key of the message to be removed from the repository
+ */
+ public void remove(String key) throws MessagingException {
+ if (lock(key)) {
+ try {
+ internalRemove(key);
+ } finally {
+ unlock(key);
+ }
+ } else {
+ StringBuffer exceptionBuffer =
+ new StringBuffer(64)
+ .append("Cannot lock ")
+ .append(key)
+ .append(" to remove it");
+ throw new MessagingException(exceptionBuffer.toString());
+ }
+ }
+
+
+ protected abstract void internalRemove(String key) throws MessagingException;
+
+
+}
Propchange: james/server/trunk/src/java/org/apache/james/mailrepository/AbstractMailRepository.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: james/server/trunk/src/java/org/apache/james/mailrepository/AvalonMailRepository.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/mailrepository/AvalonMailRepository.java?rev=425899&r1=425898&r2=425899&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/mailrepository/AvalonMailRepository.java (original)
+++ james/server/trunk/src/java/org/apache/james/mailrepository/AvalonMailRepository.java Wed Jul 26 16:06:49 2006
@@ -20,24 +20,18 @@
import org.apache.avalon.cornerstone.services.store.ObjectRepository;
import org.apache.avalon.cornerstone.services.store.Store;
import org.apache.avalon.cornerstone.services.store.StreamRepository;
-import org.apache.avalon.framework.activity.Initializable;
-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.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.ServiceException;
-import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.Serviceable;
import org.apache.james.core.MimeMessageCopyOnWriteProxy;
import org.apache.james.core.MimeMessageWrapper;
-import org.apache.james.services.MailRepository;
-import org.apache.james.util.Lock;
import org.apache.mailet.Mail;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
+import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
@@ -58,16 +52,8 @@
* @version 1.0.0, 24/04/1999
*/
public class AvalonMailRepository
- extends AbstractLogEnabled
- implements MailRepository, Configurable, Serviceable, Initializable {
+ extends AbstractMailRepository {
- /**
- * Whether 'deep debugging' is turned on.
- */
- protected final static boolean DEEP_DEBUG = false;
-
- private Lock lock;
- private Store store; // variable is not used beyond initialization
private StreamRepository streamRepository;
private ObjectRepository objectRepository;
private String destination;
@@ -75,18 +61,6 @@
private boolean fifo;
private boolean cacheKeys; // experimental: for use with write mostly repositories such as spam and error
- void setStore(Store store) {
- this.store = store;
- }
-
- /**
- * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager )
- */
- public void service( final ServiceManager componentManager )
- throws ServiceException {
- setStore((Store)componentManager.lookup( Store.ROLE ));
- }
-
/**
* @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
*/
@@ -114,11 +88,11 @@
*/
public void initialize()
throws Exception {
+ super.initialize();
try {
objectRepository = (ObjectRepository) selectRepository(store, "OBJECT");
streamRepository = (StreamRepository) selectRepository(store, "STREAM");
- lock = new Lock();
if (cacheKeys) keys = Collections.synchronizedSet(new HashSet());
//Finds non-matching pairs and deletes the extra files
@@ -180,151 +154,61 @@
}
/**
- * Releases a lock on a message identified by a key
- *
- * @param key the key of the message to be unlocked
- *
- * @return true if successfully released the lock, false otherwise
- */
- public boolean unlock(String key) {
- if (lock.unlock(key)) {
- if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
- StringBuffer debugBuffer =
- new StringBuffer(256)
- .append("Unlocked ")
- .append(key)
- .append(" for ")
- .append(Thread.currentThread().getName())
- .append(" @ ")
- .append(new java.util.Date(System.currentTimeMillis()));
- getLogger().debug(debugBuffer.toString());
- }
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Obtains a lock on a message identified by a key
- *
- * @param key the key of the message to be locked
- *
- * @return true if successfully obtained the lock, false otherwise
+ * @param mc
+ * @param key
+ * @throws MessagingException
+ * @throws IOException
*/
- public boolean lock(String key) {
- if (lock.lock(key)) {
- if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
+ protected void internalStore(Mail mc) throws MessagingException, IOException {
+ String key = mc.getName();
+ if (keys != null && !keys.contains(key)) {
+ keys.add(key);
+ }
+ boolean saveStream = true;
+
+ MimeMessage message = mc.getMessage();
+ // if the message is a Copy on Write proxy we check the wrapped message
+ // to optimize the behaviour in case of MimeMessageWrapper
+ if (message instanceof MimeMessageCopyOnWriteProxy) {
+ MimeMessageCopyOnWriteProxy messageCow = (MimeMessageCopyOnWriteProxy) message;
+ message = messageCow.getWrappedMessage();
+ }
+ if (message instanceof MimeMessageWrapper) {
+ MimeMessageWrapper wrapper = (MimeMessageWrapper) message;
+ if (DEEP_DEBUG) {
+ System.out.println("Retrieving from: " + wrapper.getSourceId());
StringBuffer debugBuffer =
- new StringBuffer(256)
- .append("Locked ")
- .append(key)
- .append(" for ")
- .append(Thread.currentThread().getName())
- .append(" @ ")
- .append(new java.util.Date(System.currentTimeMillis()));
- getLogger().debug(debugBuffer.toString());
- }
-// synchronized (this) {
-// notifyAll();
-// }
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Stores a message in this repository. Shouldn't this return the key
- * under which it is stored?
- *
- * @param mc the mail message to store
- */
- public void store(Mail mc) throws MessagingException {
- try {
- String key = mc.getName();
- //Remember whether this key was locked
- boolean wasLocked = true;
- synchronized (this) {
- wasLocked = lock.isLocked(key);
-
- if (!wasLocked) {
- //If it wasn't locked, we want a lock during the store
- lock(key);
- }
- }
- try {
- if (keys != null && !keys.contains(key)) {
- keys.add(key);
- }
- boolean saveStream = true;
-
- MimeMessage message = mc.getMessage();
- // if the message is a Copy on Write proxy we check the wrapped message
- // to optimize the behaviour in case of MimeMessageWrapper
- if (message instanceof MimeMessageCopyOnWriteProxy) {
- MimeMessageCopyOnWriteProxy messageCow = (MimeMessageCopyOnWriteProxy) message;
- message = messageCow.getWrappedMessage();
- }
- if (message instanceof MimeMessageWrapper) {
- MimeMessageWrapper wrapper = (MimeMessageWrapper) message;
- if (DEEP_DEBUG) {
- System.out.println("Retrieving from: " + wrapper.getSourceId());
- StringBuffer debugBuffer =
- new StringBuffer(64)
- .append("Saving to: ")
- .append(destination)
- .append("/")
- .append(mc.getName());
- System.out.println(debugBuffer.toString());
- System.out.println("Modified: " + wrapper.isModified());
- }
- StringBuffer destinationBuffer =
- new StringBuffer(128)
+ new StringBuffer(64)
+ .append("Saving to: ")
.append(destination)
.append("/")
.append(mc.getName());
- if (destinationBuffer.toString().equals(wrapper.getSourceId()) && !wrapper.isModified()) {
- //We're trying to save to the same place, and it's not modified... we shouldn't save.
- //More importantly, if we try to save, we will create a 0-byte file since we're
- //retrying to retrieve from a file we'll be overwriting.
- saveStream = false;
- }
- }
- if (saveStream) {
- OutputStream out = null;
- try {
- out = streamRepository.put(key);
- mc.getMessage().writeTo(out);
- } finally {
- if (out != null) out.close();
- }
- }
- //Always save the header information
- objectRepository.put(key, mc);
- } finally {
- if (!wasLocked) {
- // If it wasn't locked, we need to unlock now
- unlock(key);
- synchronized (this) {
- notify();
- }
- }
+ System.out.println(debugBuffer.toString());
+ System.out.println("Modified: " + wrapper.isModified());
}
-
- if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
- StringBuffer logBuffer =
- new StringBuffer(64)
- .append("Mail ")
- .append(key)
- .append(" stored.");
- getLogger().debug(logBuffer.toString());
+ StringBuffer destinationBuffer =
+ new StringBuffer(128)
+ .append(destination)
+ .append("/")
+ .append(mc.getName());
+ if (destinationBuffer.toString().equals(wrapper.getSourceId()) && !wrapper.isModified()) {
+ //We're trying to save to the same place, and it's not modified... we shouldn't save.
+ //More importantly, if we try to save, we will create a 0-byte file since we're
+ //retrying to retrieve from a file we'll be overwriting.
+ saveStream = false;
+ }
+ }
+ if (saveStream) {
+ OutputStream out = null;
+ try {
+ out = streamRepository.put(key);
+ mc.getMessage().writeTo(out);
+ } finally {
+ if (out != null) out.close();
}
-
- } catch (Exception e) {
- getLogger().error("Exception storing mail: " + e, e);
- throw new MessagingException("Exception caught while storing Message Container: " + e);
}
+ //Always save the header information
+ objectRepository.put(key, mc);
}
/**
@@ -368,50 +252,12 @@
}
/**
- * Removes a specified message
- *
- * @param mail the message to be removed from the repository
- */
- public void remove(Mail mail) throws MessagingException {
- remove(mail.getName());
- }
-
-
- /**
- * Removes a Collection of mails from the repository
- * @param mails The Collection of <code>MailImpl</code>'s to delete
- * @throws MessagingException
- * @since 2.2.0
- */
- public void remove(Collection mails) throws MessagingException {
- Iterator delList = mails.iterator();
- while (delList.hasNext()) {
- remove((Mail)delList.next());
- }
- }
-
- /**
* Removes a message identified by key.
- *
- * @param key the key of the message to be removed from the repository
*/
- public void remove(String key) throws MessagingException {
- if (lock(key)) {
- try {
- if (keys != null) keys.remove(key);
- streamRepository.remove(key);
- objectRepository.remove(key);
- } finally {
- unlock(key);
- }
- } else {
- StringBuffer exceptionBuffer =
- new StringBuffer(64)
- .append("Cannot lock ")
- .append(key)
- .append(" to remove it");
- throw new MessagingException(exceptionBuffer.toString());
- }
+ protected void internalRemove(String key) throws MessagingException {
+ if (keys != null) keys.remove(key);
+ streamRepository.remove(key);
+ objectRepository.remove(key);
}
/**
Modified: james/server/trunk/src/java/org/apache/james/mailrepository/JDBCMailRepository.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/mailrepository/JDBCMailRepository.java?rev=425899&r1=425898&r2=425899&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/mailrepository/JDBCMailRepository.java (original)
+++ james/server/trunk/src/java/org/apache/james/mailrepository/JDBCMailRepository.java Wed Jul 26 16:06:49 2006
@@ -18,28 +18,21 @@
package org.apache.james.mailrepository;
import org.apache.avalon.cornerstone.services.datasources.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.service.Serviceable;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.ServiceException;
-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.MimeMessageCopyOnWriteProxy;
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.Mail;
import org.apache.mailet.MailAddress;
@@ -61,7 +54,6 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -87,13 +79,8 @@
* @version CVS $Revision$ $Date$
*/
public class JDBCMailRepository
- extends AbstractLogEnabled
- implements MailRepository, Contextualizable, Serviceable, Configurable, Initializable {
-
- /**
- * Whether 'deep debugging' is turned on.
- */
- private static final boolean DEEP_DEBUG = false;
+ extends AbstractMailRepository
+ implements Contextualizable {
/**
* The Avalon context used by the instance
@@ -101,12 +88,6 @@
protected Context context;
/**
- * A lock used to control access to repository elements, locking access
- * based on the key
- */
- private Lock lock;
-
- /**
* The table name parsed from the destination URL
*/
protected String tableName;
@@ -137,11 +118,6 @@
protected DataSourceComponent datasource;
/**
- * The store where the repository is selected from
- */
- protected Store store;
-
- /**
* The name of the datasource used by this repository
*/
protected String datasourceName;
@@ -166,10 +142,6 @@
*/
private int inMemorySizeLimit;
- public void setStore(Store store) {
- this.store = store;
- }
-
public void setDatasources(DataSourceSelector datasources) {
this.datasources = datasources;
}
@@ -187,6 +159,7 @@
*/
public void service( final ServiceManager componentManager )
throws ServiceException {
+ super.service(componentManager);
StringBuffer logBuffer = null;
if (getLogger().isDebugEnabled()) {
logBuffer =
@@ -198,8 +171,6 @@
// Get the DataSourceSelector service
DataSourceSelector datasources = (DataSourceSelector)componentManager.lookup( DataSourceSelector.ROLE );
setDatasources(datasources);
- Store store = (Store)componentManager.lookup(Store.ROLE);
- setStore(store);
}
/**
@@ -292,7 +263,6 @@
}
}
- lock = new Lock();
if (getLogger().isDebugEnabled()) {
StringBuffer logBuffer =
new StringBuffer(128)
@@ -319,6 +289,7 @@
* @throws Exception if an error occurs
*/
public void initialize() throws Exception {
+ super.initialize();
StringBuffer logBuffer = null;
if (getLogger().isDebugEnabled()) {
getLogger().debug(this.getClass().getName() + ".initialize()");
@@ -490,81 +461,20 @@
}
/**
- * Releases a lock on a message identified by a key
- *
- * @param key the key of the message to be unlocked
- *
- * @return true if successfully released the lock, false otherwise
- */
- public boolean unlock(String key) {
- if (lock.unlock(key)) {
- if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
- StringBuffer debugBuffer =
- new StringBuffer(256)
- .append("Unlocked ")
- .append(key)
- .append(" for ")
- .append(Thread.currentThread().getName())
- .append(" @ ")
- .append(new java.util.Date(System.currentTimeMillis()));
- getLogger().debug(debugBuffer.toString());
- }
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Obtains a lock on a message identified by a key
- *
- * @param key the key of the message to be locked
- *
- * @return true if successfully obtained the lock, false otherwise
- */
- public boolean lock(String key) {
- if (lock.lock(key)) {
- if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
- StringBuffer debugBuffer =
- new StringBuffer(256)
- .append("Locked ")
- .append(key)
- .append(" for ")
- .append(Thread.currentThread().getName())
- .append(" @ ")
- .append(new java.util.Date(System.currentTimeMillis()));
- getLogger().debug(debugBuffer.toString());
- }
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Store this message to the database. Optionally stores the message
- * body to the filesystem and only writes the headers to the database.
+ * @param mc mail to be stored
+ * @throws SQLException
+ * @throws IOException
+ * @throws MessagingException
*/
- public void store(Mail mc) throws MessagingException {
+ protected void internalStore(Mail mc) throws IOException, MessagingException {
Connection conn = null;
- boolean wasLocked = true;
- String key = mc.getName();
try {
- synchronized(this) {
- wasLocked = lock.isLocked(key);
-
- if (!wasLocked) {
- //If it wasn't locked, we want a lock during the store
- lock(key);
- }
- }
conn = datasource.getConnection();
-
//Need to determine whether need to insert this record, or update it.
-
+
//Begin a transaction
conn.setAutoCommit(false);
-
+
PreparedStatement checkMessageExists = null;
ResultSet rsExists = null;
boolean exists = false;
@@ -579,11 +489,11 @@
theJDBCUtil.closeJDBCResultSet(rsExists);
theJDBCUtil.closeJDBCStatement(checkMessageExists);
}
-
+
if (exists) {
//Update the existing record
PreparedStatement updateMessage = null;
-
+
try {
updateMessage =
conn.prepareStatement(sqlQueries.getSqlString("updateMessageSQL", true));
@@ -614,7 +524,7 @@
updateMessage = null;
theJDBCUtil.closeJDBCStatement(localUpdateMessage);
}
-
+
//Determine whether attributes are used and available for storing
if (jdbcMailAttributesReady && mc.hasAttributes()) {
String updateMessageAttrSql =
@@ -659,7 +569,7 @@
theJDBCUtil.closeJDBCStatement(updateMessageAttr);
}
}
-
+
//Determine whether the message body has changed, and possibly avoid
// updating the database.
MimeMessage messageBody = mc.getMessage();
@@ -691,7 +601,7 @@
}
}
-
+
} else {
//Insert the record into the database
PreparedStatement insertMessage = null;
@@ -720,9 +630,9 @@
insertMessage.setString(7, mc.getRemoteHost());
insertMessage.setString(8, mc.getRemoteAddr());
insertMessage.setTimestamp(9, new java.sql.Timestamp(mc.getLastUpdated().getTime()));
-
+
MessageInputStream is = new MessageInputStream(mc, sr, inMemorySizeLimit);
-
+
insertMessage.setBinaryStream(10, is, (int) is.getSize());
//Store attributes
@@ -760,23 +670,14 @@
theJDBCUtil.closeJDBCStatement(insertMessage);
}
}
-
-
+
+
conn.commit();
conn.setAutoCommit(true);
-
- } catch (Exception e) {
- getLogger().error("Exception caught while storing mail Container",e);
- throw new MessagingException("Exception caught while storing mail Container: ",e);
+ } catch (SQLException e) {
+ throw new IOException(e.getMessage());
} finally {
theJDBCUtil.closeJDBCConnection(conn);
- if (!wasLocked) {
- // If it wasn't locked, we need to unlock now
- unlock(key);
- synchronized (this) {
- notify();
- }
- }
}
}
@@ -932,55 +833,28 @@
}
/**
- * Removes a specified message
- *
- * @param mail the message to be removed from the repository
- */
- public void remove(Mail mail) throws MessagingException {
- remove(mail.getName());
- }
-
- /**
- * Removes a Collection of mails from the repository
- * @param mails The Collection of <code>MailImpl</code>'s to delete
+ * @param key
* @throws MessagingException
- * @since 2.2.0
- */
- public void remove(Collection mails) throws MessagingException {
- Iterator delList = mails.iterator();
- while (delList.hasNext()) {
- remove((Mail)delList.next());
- }
- }
-
- /**
- * Removes a message identified by a key.
- *
- * @param key the key of the message to be removed from the repository
*/
- public void remove(String key) throws MessagingException {
- //System.err.println("removing " + key);
- if (lock(key)) {
- Connection conn = null;
- PreparedStatement removeMessage = null;
- try {
- conn = datasource.getConnection();
- removeMessage =
- conn.prepareStatement(sqlQueries.getSqlString("removeMessageSQL", true));
- removeMessage.setString(1, key);
- removeMessage.setString(2, repositoryName);
- removeMessage.execute();
+ protected void internalRemove(String key) throws MessagingException {
+ Connection conn = null;
+ PreparedStatement removeMessage = null;
+ try {
+ conn = datasource.getConnection();
+ removeMessage =
+ conn.prepareStatement(sqlQueries.getSqlString("removeMessageSQL", true));
+ removeMessage.setString(1, key);
+ removeMessage.setString(2, repositoryName);
+ removeMessage.execute();
- if (sr != null) {
- sr.remove(key);
- }
- } catch (Exception me) {
- throw new MessagingException("Exception while removing mail: " + me.getMessage());
- } finally {
- theJDBCUtil.closeJDBCStatement(removeMessage);
- theJDBCUtil.closeJDBCConnection(conn);
- unlock(key);
+ if (sr != null) {
+ sr.remove(key);
}
+ } catch (Exception me) {
+ throw new MessagingException("Exception while removing mail: " + me.getMessage());
+ } finally {
+ theJDBCUtil.closeJDBCStatement(removeMessage);
+ theJDBCUtil.closeJDBCConnection(conn);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org