james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r940260 - in /james/imap/trunk: jcr/src/main/java/org/apache/james/imap/jcr/ jcr/src/main/resources/org/apache/james/imap/jcr/ jpa/src/main/java/org/apache/james/imap/jpa/ memory/src/main/java/org/apache/james/imap/inmemory/ store/src/main/...
Date Sun, 02 May 2010 16:55:27 GMT
Author: norman
Date: Sun May  2 16:55:26 2010
New Revision: 940260

URL: http://svn.apache.org/viewvc?rev=940260&view=rev
Log:
Refactor api a bit to make it more elegant and flexible. We use now a Mapper per Request pattern

Modified:
    james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/AbstractJCRMapper.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMailbox.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMailboxManager.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRSubscriptionManager.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRUtils.java
    james/imap/trunk/jcr/src/main/resources/org/apache/james/imap/jcr/imap.cnd
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMailboxManager.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPATransactionalMapper.java
    james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailboxManager.java
    james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMailbox.java
    james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemorySubscriptionManager.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailbox.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MailboxMapper.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MessageMapper.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/transaction/AbstractTransactionalMapper.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/transaction/TransactionalMapper.java

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/AbstractJCRMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/AbstractJCRMapper.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/AbstractJCRMapper.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/AbstractJCRMapper.java Sun
May  2 16:55:26 2010
@@ -92,5 +92,10 @@ public abstract class AbstractJCRMapper 
     protected void rollback() throws MailboxException {
         // no rollback supported by level 1 jcr
     }
+
+    public void dispose() {
+        session.logout();
+    }
+    
     
 }

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMailbox.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMailbox.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMailbox.java Sun May 
2 16:55:26 2010
@@ -105,9 +105,7 @@ public class JCRMailbox extends StoreMai
 
     @Override
     protected MessageMapper<String> createMessageMapper(MailboxSession session) throws
MailboxException {
-        Session jcrSession = getSession(session);
-        JCRUtils.addJCRSession(session, jcrSession);
-        
+        Session jcrSession = getSession(session);        
         JCRMessageMapper messageMapper = new JCRMessageMapper(jcrSession, getMailboxId(),
getScaling(), log);
         
         return messageMapper;
@@ -122,9 +120,7 @@ public class JCRMailbox extends StoreMai
      * @throws MailboxException
      */
     protected JCRMailboxMapper createMailboxMapper(MailboxSession session) throws MailboxException
{
-        Session jcrSession = getSession(session);
-        JCRUtils.addJCRSession(session, jcrSession);
-        
+        Session jcrSession = getSession(session);        
         JCRMailboxMapper mapper = new JCRMailboxMapper(jcrSession, getScaling(), log);
         return mapper;
 

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMailboxManager.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMailboxManager.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMailboxManager.java Sun
May  2 16:55:26 2010
@@ -19,7 +19,6 @@
 package org.apache.james.imap.jcr;
 
 import java.util.ArrayList;
-import java.util.List;
 import java.util.Locale;
 
 import javax.jcr.LoginException;
@@ -90,8 +89,6 @@ public class JCRMailboxManager extends S
     protected MailboxMapper<String> createMailboxMapper(MailboxSession session) throws
MailboxException {
 
         Session jcrSession = getSession(session);
-
-        JCRUtils.addJCRSession(session, jcrSession);
         
         JCRMailboxMapper mapper = new JCRMailboxMapper(jcrSession, getScaling(), getLog());
         return mapper;
@@ -166,24 +163,6 @@ public class JCRMailboxManager extends S
     protected Repository getRepository() {
         return repository;
     }
-
-
-    /**
-     * Logout from all opened JCR Sessions
-     */
-    public void endProcessingRequest(MailboxSession session) {
-        List<Session> sessions = JCRUtils.getJCRSessions(session);
-        for (int i = 0 ; i < sessions.size(); i++) {
-            Session jcrSession = sessions.get(i);
-            if (jcrSession.isLive()) {
-                try {
-                    jcrSession.logout();
-                } catch (Exception e) {
-                    // just catch exceptions on logout
-                }
-            }
-        }
-    }
     
     
 }

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRSubscriptionManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRSubscriptionManager.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRSubscriptionManager.java
(original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRSubscriptionManager.java
Sun May  2 16:55:26 2010
@@ -73,9 +73,7 @@ public class JCRSubscriptionManager exte
     
     @Override
     protected SubscriptionMapper createMapper(MailboxSession session) throws SubscriptionException
{
-        Session jcrSession = getSession(session);
-        JCRUtils.addJCRSession(session, jcrSession);
-        
+        Session jcrSession = getSession(session);        
         
         JCRSubscriptionMapper mapper = new JCRSubscriptionMapper(jcrSession, getScaling(),
logger);
 

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRUtils.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRUtils.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRUtils.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRUtils.java Sun May  2
16:55:26 2010
@@ -20,8 +20,6 @@ package org.apache.james.imap.jcr;
 
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.util.ArrayList;
-import java.util.List;
 
 import javax.jcr.ItemExistsException;
 import javax.jcr.Node;
@@ -36,7 +34,6 @@ import javax.jcr.version.VersionExceptio
 
 import org.apache.jackrabbit.commons.cnd.CndImporter;
 import org.apache.jackrabbit.util.Text;
-import org.apache.james.imap.mailbox.MailboxSession;
 
 /**
  * Utilities used for JCR 
@@ -180,40 +177,6 @@ public class JCRUtils implements JCRImap
         return pathBuf.toString();
     }
 
-    /**
-     * Return a List of JCR Sessions for the given MailboxSession
-     * 
-     * @param session
-     * @return jcrSessionList
-     */
-    @SuppressWarnings("unchecked")
-    public static List<Session> getJCRSessions(MailboxSession session) {
-        List<Session> sessions = null;
-        if (session != null) {
-            sessions = (List<Session>) session.getAttributes().get(JCR_SESSIONS);
-        }
-        if (sessions == null) {
-            sessions = new ArrayList<Session>();
-        }
-        return sessions;
-    }
-
-	/**
-	 * Add the JCR Session to the MailboxSession
-	 * @param session
-	 * @param jcrSession
-	 */
-    @SuppressWarnings("unchecked")
-    public static void addJCRSession(MailboxSession session, Session jcrSession) {
-        if (session != null) {
-            List<Session> sessions = (List<Session>) session.getAttributes().get(JCR_SESSIONS);

-            if (sessions == null) {
-                sessions = new ArrayList<Session>();
-            }
-            sessions.add(jcrSession);
-            session.getAttributes().put(JCR_SESSIONS, sessions); 
-        }
-    }
     
 
     /**

Modified: james/imap/trunk/jcr/src/main/resources/org/apache/james/imap/jcr/imap.cnd
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/resources/org/apache/james/imap/jcr/imap.cnd?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/resources/org/apache/james/imap/jcr/imap.cnd (original)
+++ james/imap/trunk/jcr/src/main/resources/org/apache/james/imap/jcr/imap.cnd Sun May  2
16:55:26 2010
@@ -54,7 +54,7 @@
     + mailboxMemberships (imap:mailboxMemberships) 
  
 [imap:mailboxMembership] >  mix:referenceable, nt:base
-    - imap:mailboxUUID (REFERENCE) mandatory < 'imap:mailbox'
+    - imap:mailboxUUID (String) mandatory
     - imap:uid (LONG) mandatory
     - imap:size (LONG) mandatory
     - imap:answered (BOOLEAN) mandatory

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMailboxManager.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMailboxManager.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMailboxManager.java Sun
May  2 16:55:26 2010
@@ -50,7 +50,7 @@ public abstract class JPAMailboxManager 
     @Override
     protected void doCreate(String namespaceName, MailboxSession session) throws MailboxException
{
         final Mailbox<Long> mailbox = new org.apache.james.imap.jpa.mail.model.JPAMailbox(namespaceName,
randomUidValidity());
-        final MailboxMapper<Long> mapper = createMailboxMapper(session);
+        final MailboxMapper<Long> mapper = getMailboxMapperForRequest(session);
         mapper.execute(new TransactionalMapper.Transaction(){
 
             public void run() throws MailboxException {
@@ -66,8 +66,8 @@ public abstract class JPAMailboxManager 
      * @param maibloxSession
      * @throws MailboxException
      */
-    public void deleteEverything(MailboxSession maibloxSession) throws MailboxException {
-        final MailboxMapper<Long> mapper = createMailboxMapper(maibloxSession);
+    public void deleteEverything(MailboxSession mailboxSession) throws MailboxException {
+        final MailboxMapper<Long> mapper = getMailboxMapperForRequest(mailboxSession);
         mapper.execute(new TransactionalMapper.Transaction() {
 
             public void run() throws MailboxException {

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPATransactionalMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPATransactionalMapper.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPATransactionalMapper.java
(original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPATransactionalMapper.java
Sun May  2 16:55:26 2010
@@ -18,6 +18,7 @@
  ****************************************************************/
 package org.apache.james.imap.jpa;
 
+
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityTransaction;
@@ -32,11 +33,10 @@ import org.apache.james.imap.store.trans
  * JPA implementation of TransactionMapper. This class is not thread-safe!
  *
  */
-public class JPATransactionalMapper extends AbstractTransactionalMapper {
+public abstract class JPATransactionalMapper extends AbstractTransactionalMapper {
 
     private final EntityManagerFactory factory;
     private EntityManager entityManager;
-    
     public JPATransactionalMapper(final EntityManagerFactory factory) {
         this.factory = factory;
     }
@@ -72,7 +72,6 @@ public class JPATransactionalMapper exte
     protected void commit() throws MailboxException {
         try {
             getManager().getTransaction().commit();
-            getManager().close();
         } catch (PersistenceException e) {
             throw new StorageException(HumanReadableText.COMMIT_TRANSACTION_FAILED, e);
         }
@@ -89,5 +88,15 @@ public class JPATransactionalMapper exte
             getManager().getTransaction().rollback();
         }
     }
+    
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.imap.store.transaction.TransactionalMapper#dispose()
+     */
+    public void dispose() {
+        if (entityManager != null && entityManager.isOpen()) {
+            entityManager.close();
+        }
+    }
 
 }

Modified: james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailboxManager.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailboxManager.java
(original)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailboxManager.java
Sun May  2 16:55:26 2010
@@ -208,5 +208,17 @@ public class InMemoryMailboxManager exte
         storeMailboxByName.clear();
         idNameMap.clear();
     }
+
+    public void dispose() {
+        // do nothing
+        
+    }
+
+    @Override
+    protected MailboxMapper<Long> getMailboxMapperForRequest(MailboxSession session)
throws MailboxException {
+        return createMailboxMapper(session);
+    }
+    
+    
     
 }

Modified: james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMailbox.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMailbox.java
(original)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMailbox.java
Sun May  2 16:55:26 2010
@@ -246,4 +246,15 @@ public class InMemoryStoreMailbox extend
     public void execute(Transaction transaction) throws MailboxException {
         transaction.run();
     }
+
+
+    public void dispose() {
+        // do nothing
+        
+    }
+    
+    
+    public MessageMapper<Long> getMessageMapperForRequest(MailboxSession session) throws
MailboxException {
+        return createMessageMapper(session);
+    }
 }

Modified: james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemorySubscriptionManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemorySubscriptionManager.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemorySubscriptionManager.java
(original)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemorySubscriptionManager.java
Sun May  2 16:55:26 2010
@@ -174,4 +174,13 @@ public class InMemorySubscriptionManager
     public void execute(Transaction transaction) throws MailboxException {
         transaction.run();
     }
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.imap.store.transaction.TransactionalMapper#dispose()
+     */
+    public void dispose() {
+        // do nothing
+        
+    }
 }

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailbox.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailbox.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailbox.java Sun
May  2 16:55:26 2010
@@ -83,10 +83,29 @@ public abstract class StoreMailbox<Id> i
     
     private MailboxEventDispatcher dispatcher;
     
+    public final static String MESSAGE_MAPPER = "MESSAGE_MAPPER";
+    
     public StoreMailbox(final MailboxEventDispatcher dispatcher, final Mailbox<Id>
mailbox) {
         this.mailboxId = mailbox.getMailboxId();
         this.dispatcher = dispatcher;
     }
+    
+    /**
+     * Return the {@link MessageMapper} for the current Request. If none exists, it will
get created.
+     * 
+     * @param session
+     * @return mapper
+     * @throws MailboxException
+     */
+    @SuppressWarnings("unchecked")
+    public MessageMapper<Id> getMessageMapperForRequest(MailboxSession session) throws
MailboxException {
+        MessageMapper<Id> mapper = (MessageMapper<Id>) session.getAttributes().get(MESSAGE_MAPPER);
+        if (mapper == null) {
+            mapper = createMessageMapper(session);
+            session.getAttributes().put(MESSAGE_MAPPER, mapper);
+        }
+        return mapper;
+    }
 
     /**
      * Return the {@link MailboxEventDispatcher} for this Mailbox
@@ -107,7 +126,7 @@ public abstract class StoreMailbox<Id> i
     protected abstract MailboxMembership<Id> copyMessage(MailboxMembership<Id>
originalMessage, long uid, MailboxSession session) throws MailboxException;
     
     /**
-     * Create a new {@link MessageMapper} to use
+     * Create a new {@link MessageMapper} to use for the current Request. 
      * 
      * @return mapper
      */
@@ -137,7 +156,7 @@ public abstract class StoreMailbox<Id> i
      * @see org.apache.james.imap.mailbox.Mailbox#getMessageCount(org.apache.james.imap.mailbox.MailboxSession)
      */
     public int getMessageCount(MailboxSession mailboxSession) throws MailboxException {
-        final MessageMapper<Id> messageMapper = createMessageMapper(mailboxSession);
+        final MessageMapper<Id> messageMapper = getMessageMapperForRequest(mailboxSession);
         return (int) messageMapper.countMessagesInMailbox();
     }
 
@@ -149,7 +168,7 @@ public abstract class StoreMailbox<Id> i
             final MailboxSession mailboxSession,final boolean isRecent, final Flags flagsToBeSet)
     throws MailboxException {
         // this will hold the uid after the transaction was complete
-        final MessageMapper<Id> mapper = createMessageMapper(mailboxSession);
+        final MessageMapper<Id> mapper = getMessageMapperForRequest(mailboxSession);
         
         final Mailbox<Id> mailbox = reserveNextUid(mailboxSession);
         final long uid = mailbox.getLastUid();
@@ -388,7 +407,7 @@ public abstract class StoreMailbox<Id> i
     public Iterator<MessageResult> getMessages(final MessageRange set, FetchGroup fetchGroup,
             MailboxSession mailboxSession) throws MailboxException {
         UidRange range = uidRangeForMessageSet(set);
-        final MessageMapper<Id> messageMapper = createMessageMapper(mailboxSession);
+        final MessageMapper<Id> messageMapper = getMessageMapperForRequest(mailboxSession);
         final List<MailboxMembership<Id>> rows = new ArrayList<MailboxMembership<Id>>(messageMapper.findInMailbox(set));
         return getMessages(fetchGroup, range, rows);
     }
@@ -427,7 +446,7 @@ public abstract class StoreMailbox<Id> i
     }
 
     private long[] recent(final boolean reset, MailboxSession mailboxSession) throws MailboxException
{
-        final MessageMapper<Id> mapper = createMessageMapper(mailboxSession);
+        final MessageMapper<Id> mapper = getMessageMapperForRequest(mailboxSession);
         final List<Long> results = new ArrayList<Long>();
 
         mapper.execute(new TransactionalMapper.Transaction() {
@@ -451,7 +470,7 @@ public abstract class StoreMailbox<Id> i
 
     private Long getFirstUnseen(MailboxSession mailboxSession) throws MailboxException {
         try {
-            final MessageMapper<Id> messageMapper = createMessageMapper(mailboxSession);
+            final MessageMapper<Id> messageMapper = getMessageMapperForRequest(mailboxSession);
             final List<MailboxMembership<Id>> members = messageMapper.findUnseenMessagesInMailbox();
             final Iterator<MailboxMembership<Id>> it = members.iterator();
             final Long result;
@@ -468,7 +487,7 @@ public abstract class StoreMailbox<Id> i
     }
 
     private int getUnseenCount(MailboxSession mailboxSession) throws MailboxException {
-        final MessageMapper<Id> messageMapper = createMessageMapper(mailboxSession);
+        final MessageMapper<Id> messageMapper = getMessageMapperForRequest(mailboxSession);
         final int count = (int) messageMapper.countUnseenMessagesInMailbox();
         return count;
     }
@@ -483,7 +502,7 @@ public abstract class StoreMailbox<Id> i
 
     private Iterator<Long> doExpunge(final MessageRange set, MailboxSession mailboxSession)
     throws MailboxException {
-        final MessageMapper<Id> mapper = createMessageMapper(mailboxSession);
+        final MessageMapper<Id> mapper = getMessageMapperForRequest(mailboxSession);
         final Collection<Long> uids = new TreeSet<Long>();
         
         mapper.execute(new TransactionalMapper.Transaction() {
@@ -516,7 +535,7 @@ public abstract class StoreMailbox<Id> i
 
     private Map<Long, Flags> doSetFlags(final Flags flags, final boolean value, final
boolean replace,
             final MessageRange set, final MailboxSession mailboxSession) throws MailboxException
{
-        final MessageMapper<Id> mapper = createMessageMapper(mailboxSession);
+        final MessageMapper<Id> mapper = getMessageMapperForRequest(mailboxSession);
         final SortedMap<Long, Flags> newFlagsByUid = new TreeMap<Long, Flags>();
         final Map<Long, Flags> originalFlagsByUid = new HashMap<Long, Flags>(INITIAL_SIZE_FLAGS);
         mapper.execute(new TransactionalMapper.Transaction(){
@@ -576,7 +595,7 @@ public abstract class StoreMailbox<Id> i
      * @see org.apache.james.imap.mailbox.Mailbox#search(org.apache.james.imap.mailbox.SearchQuery,
org.apache.james.imap.mailbox.MailboxSession)
      */
     public Iterator<Long> search(SearchQuery query, MailboxSession mailboxSession)
throws MailboxException {
-        final MessageMapper<Id> messageMapper = createMessageMapper(mailboxSession);
+        final MessageMapper<Id> messageMapper = getMessageMapperForRequest(mailboxSession);
         final List<MailboxMembership<Id>> members = messageMapper.searchMailbox(query);
         final Set<Long> uids = new TreeSet<Long>();
         for (MailboxMembership<Id> member:members) {
@@ -611,7 +630,7 @@ public abstract class StoreMailbox<Id> i
     private void copy(final List<MailboxMembership<Id>> originalRows, final MailboxSession
session) throws MailboxException {
         try {
             final List<MailboxMembership<Id>> copiedRows = new ArrayList<MailboxMembership<Id>>();
-            final MessageMapper<Id> mapper = createMessageMapper(session);
+            final MessageMapper<Id> mapper = getMessageMapperForRequest(session);
             
 
             for (final MailboxMembership<Id> originalMessage:originalRows) {
@@ -655,7 +674,7 @@ public abstract class StoreMailbox<Id> i
      */
     public void copyTo(MessageRange set, StoreMailbox<Id> toMailbox, MailboxSession
session) throws MailboxException {
         try {
-            final MessageMapper<Id> mapper = createMessageMapper(session);
+            final MessageMapper<Id> mapper = getMessageMapperForRequest(session);
             
             final List<MailboxMembership<Id>> originalRows = mapper.findInMailbox(set);
             toMailbox.copy(originalRows, session);

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java
(original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java
Sun May  2 16:55:26 2010
@@ -46,6 +46,7 @@ import org.apache.james.imap.mailbox.Mai
 import org.apache.james.imap.mailbox.util.MailboxEventDispatcher;
 import org.apache.james.imap.mailbox.util.SimpleMailboxMetaData;
 import org.apache.james.imap.store.mail.MailboxMapper;
+import org.apache.james.imap.store.mail.MessageMapper;
 import org.apache.james.imap.store.mail.model.Mailbox;
 import org.apache.james.imap.store.transaction.TransactionalMapper;
 
@@ -71,7 +72,7 @@ public abstract class StoreMailboxManage
     private final DelegatingMailboxListener delegatingListener = new DelegatingMailboxListener();
     private final Authenticator authenticator;    
     private final Subscriber subscriber;    
-    
+    private final static String MAILBOX_MAPPER = "MAILBOX_MAPPER"; 
     private final char delimiter;
     
     public StoreMailboxManager(final Authenticator authenticator, final Subscriber subscriber)
{
@@ -86,6 +87,22 @@ public abstract class StoreMailboxManage
     }
 
     /**
+     * Return the {@link MailboxMapper} for the current Request. If none exists, it will
get created lazy
+     * 
+     * @param session
+     * @return mapper
+     * @throws MailboxException
+     */
+    @SuppressWarnings("unchecked")
+    protected MailboxMapper<Id> getMailboxMapperForRequest(MailboxSession session)
throws MailboxException {
+        MailboxMapper<Id> mapper = (MailboxMapper<Id>) session.getAttributes().get(MAILBOX_MAPPER);
+        if (mapper == null) {
+            mapper = createMailboxMapper(session);
+            session.getAttributes().put(MAILBOX_MAPPER, mapper);
+        }
+        return mapper;
+    }
+    /**
      * Create a StoreMailbox for the given Mailbox
      * 
      * @param mailboxRow
@@ -94,7 +111,7 @@ public abstract class StoreMailboxManage
     protected abstract StoreMailbox<Id> createMailbox(MailboxEventDispatcher dispatcher,
Mailbox<Id> mailboxRow);
     
     /**
-     * Create the MailboxMapper which should get used 
+     * Create the MailboxMapper
      * 
      * @return mailboxMapper
      */
@@ -127,7 +144,7 @@ public abstract class StoreMailboxManage
      */
     private StoreMailbox<Id> doGetMailbox(String mailboxName, MailboxSession session)
throws MailboxException {
         synchronized (mutex) {
-            final MailboxMapper<Id> mapper = createMailboxMapper(session);
+            final MailboxMapper<Id> mapper = getMailboxMapperForRequest(session);
             Mailbox<Id> mailboxRow = mapper.findMailboxByName(mailboxName);
             
             if (mailboxRow == null) {
@@ -196,7 +213,7 @@ public abstract class StoreMailboxManage
         synchronized (mutex) {
             // TODO put this into a serilizable transaction
             
-            final MailboxMapper<Id> mapper = createMailboxMapper(session);
+            final MailboxMapper<Id> mapper = getMailboxMapperForRequest(session);
             
             mapper.execute(new TransactionalMapper.Transaction() {
 
@@ -227,7 +244,7 @@ public abstract class StoreMailboxManage
                 throw new MailboxExistsException(to);
             }
 
-            final MailboxMapper<Id> mapper = createMailboxMapper(session);        
       
+            final MailboxMapper<Id> mapper = getMailboxMapperForRequest(session); 
              
             mapper.execute(new TransactionalMapper.Transaction() {
 
                 public void run() throws MailboxException {
@@ -313,7 +330,7 @@ public abstract class StoreMailboxManage
                 delimiter).replace(freeWildcard, SQL_WILDCARD_CHAR)
                 .replace(localWildcard, SQL_WILDCARD_CHAR);
 
-        final MailboxMapper<Id> mapper = createMailboxMapper(session);
+        final MailboxMapper<Id> mapper = getMailboxMapperForRequest(session);
         final List<Mailbox<Id>> mailboxes = mapper.findMailboxWithNameLike(search);
         final List<MailboxMetaData> results = new ArrayList<MailboxMetaData>(mailboxes.size());
         for (Mailbox<Id> mailbox: mailboxes) {
@@ -353,7 +370,7 @@ public abstract class StoreMailboxManage
      */
     public boolean mailboxExists(String mailboxName, MailboxSession session) throws MailboxException
{
         synchronized (mutex) {
-            final MailboxMapper<Id> mapper = createMailboxMapper(session);
+            final MailboxMapper<Id> mapper = getMailboxMapperForRequest(session);
             final long count = mapper.countMailboxesWithName(mailboxName);
             if (count == 0) {
                 return false;
@@ -496,13 +513,21 @@ public abstract class StoreMailboxManage
 
 
     /**
-     * End processing of Request for session. Default is to do nothing.
-     * 
-     * Implementations should override this if they need todo anything special
+     * End processing of Request for session. This will dispose all objects which belong
to the current
+     * Request
      */
+    @SuppressWarnings("unchecked")
     public void endProcessingRequest(MailboxSession session) {
-        // Default do nothing
-        
+        if (session != null) {
+            // dispose mapper
+            MailboxMapper<Id> mapper = (MailboxMapper<Id>) session.getAttributes().remove(MAILBOX_MAPPER);
+            if (mapper != null)
+                mapper.dispose();
+
+            MessageMapper<Id> messageMapper = (MessageMapper<Id>) session.getAttributes().remove(StoreMailbox.MESSAGE_MAPPER);
+            if (messageMapper != null)
+                messageMapper.dispose();
+        }
     }
 
 

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MailboxMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MailboxMapper.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MailboxMapper.java
(original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MailboxMapper.java
Sun May  2 16:55:26 2010
@@ -26,7 +26,8 @@ import org.apache.james.imap.store.mail.
 import org.apache.james.imap.store.transaction.TransactionalMapper;
 
 /**
- * Mapper for {@link Mailbox}
+ * Mapper for {@link Mailbox} actions. A {@link MailboxMapper} has a lifecycle from the start
of a request 
+ * to the end of the request.
  *
  */
 public interface MailboxMapper<Id> extends TransactionalMapper{

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MessageMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MessageMapper.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MessageMapper.java
(original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MessageMapper.java
Sun May  2 16:55:26 2010
@@ -29,7 +29,8 @@ import org.apache.james.imap.store.mail.
 import org.apache.james.imap.store.transaction.TransactionalMapper;
 
 /**
- * Maps {@link Document} in a {@link Mailbox}.
+ * Maps {@link Document} in a {@link Mailbox}. A {@link MessageMapper} has a lifecycle from
the start of a request 
+ * to the end of the request.
  */
 public interface MessageMapper<Id> extends TransactionalMapper {
 

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/transaction/AbstractTransactionalMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/transaction/AbstractTransactionalMapper.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/transaction/AbstractTransactionalMapper.java
(original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/transaction/AbstractTransactionalMapper.java
Sun May  2 16:55:26 2010
@@ -65,6 +65,6 @@ public abstract class AbstractTransactio
      * @throws StorageException
      */
     protected abstract void rollback() throws MailboxException;
-
+    
 
 }

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/transaction/TransactionalMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/transaction/TransactionalMapper.java?rev=940260&r1=940259&r2=940260&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/transaction/TransactionalMapper.java
(original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/transaction/TransactionalMapper.java
Sun May  2 16:55:26 2010
@@ -50,4 +50,8 @@ public interface TransactionalMapper {
         public void run() throws MailboxException;
     }
 
+    /**
+     * Dispose the mapper
+     */
+    public void dispose();
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message