james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1139535 [1/2] - in /james/mailbox/trunk: jcr/src/main/java/org/apache/james/mailbox/jcr/ jcr/src/main/java/org/apache/james/mailbox/jcr/mail/ jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/ jcr/src/main/resources/org/apache/jame...
Date Sat, 25 Jun 2011 12:29:26 GMT
Author: norman
Date: Sat Jun 25 12:29:24 2011
New Revision: 1139535

URL: http://svn.apache.org/viewvc?rev=1139535&view=rev
Log:
Add back UidProvider and also add ModSeqProvider. I think I'm now done for refactoring and we are ready for 0.3. The locking of the Mailbox now happens via MailboxPathLocker which make it easy to wrie a clustered version of it.

Added:
    james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRModSeqProvider.java
    james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRUidProvider.java
    james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAModSeqProvider.java
    james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAUidProvider.java
    james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryModSeqProvider.java
    james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryUidProvider.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractLockingModSeqProvider.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractLockingUidProvider.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/ModSeqProvider.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/UidProvider.java
Modified:
    james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxSessionMapperFactory.java
    james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java
    james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java
    james/mailbox/trunk/jcr/src/main/resources/org/apache/james/mailbox/jcr/imap.cnd
    james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRMailboxManagerTest.java
    james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java
    james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRSubscriptionManagerTest.java
    james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxSessionMapperFactory.java
    james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java
    james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/JPAMailbox.java
    james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxManagerTest.java
    james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java
    james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPASubscriptionManagerTest.java
    james/mailbox/trunk/lucene/src/test/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndexTest.java
    james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java
    james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java
    james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryMailboxSessionMapperFactory.java
    james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractMessageMapper.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/model/Mailbox.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/SimpleMailbox.java
    james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/MailboxEventDispatcherFlagsTest.java

Modified: james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxSessionMapperFactory.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxSessionMapperFactory.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxSessionMapperFactory.java (original)
+++ james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxSessionMapperFactory.java Sat Jun 25 12:29:24 2011
@@ -27,6 +27,8 @@ import org.apache.james.mailbox.jcr.user
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper;
+import org.apache.james.mailbox.store.mail.ModSeqProvider;
+import org.apache.james.mailbox.store.mail.UidProvider;
 import org.apache.james.mailbox.store.user.SubscriptionMapper;
 
 /**
@@ -40,15 +42,19 @@ public class JCRMailboxSessionMapperFact
     private final static int DEFAULT_SCALING = 2;
     private final int scaling;
     private int messageScaling;
+    private UidProvider<String> uidProvider;
+    private ModSeqProvider<String> modSeqProvider;
 
-    public JCRMailboxSessionMapperFactory(final MailboxSessionJCRRepository repository) {
-        this(repository, DEFAULT_SCALING, JCRMessageMapper.MESSAGE_SCALE_DAY);
+    public JCRMailboxSessionMapperFactory(final MailboxSessionJCRRepository repository, final UidProvider<String> uidProvider, final ModSeqProvider<String> modSeqProvider) {
+        this(repository, uidProvider, modSeqProvider, DEFAULT_SCALING, JCRMessageMapper.MESSAGE_SCALE_DAY);
     }
 
-    public JCRMailboxSessionMapperFactory(final MailboxSessionJCRRepository repository,  final int scaling, final int messageScaling) {
+    public JCRMailboxSessionMapperFactory(final MailboxSessionJCRRepository repository,  final UidProvider<String> uidProvider, final ModSeqProvider<String> modSeqProvider, final int scaling, final int messageScaling) {
         this.repository = repository;
         this.scaling = scaling;
         this.messageScaling = messageScaling;
+        this.uidProvider= uidProvider;
+        this.modSeqProvider = modSeqProvider;
     }
     
     @Override
@@ -59,7 +65,7 @@ public class JCRMailboxSessionMapperFact
 
     @Override
     public MessageMapper<String> createMessageMapper(MailboxSession session) throws MailboxException {
-        JCRMessageMapper messageMapper = new JCRMessageMapper(repository, session, messageScaling);
+        JCRMessageMapper messageMapper = new JCRMessageMapper(repository, session, uidProvider, modSeqProvider,  messageScaling);
         return messageMapper;
     }
 

Modified: james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java (original)
+++ james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java Sat Jun 25 12:29:24 2011
@@ -44,11 +44,12 @@ import org.apache.james.mailbox.MessageR
 import org.apache.james.mailbox.MessageRange.Type;
 import org.apache.james.mailbox.jcr.JCRImapConstants;
 import org.apache.james.mailbox.jcr.MailboxSessionJCRRepository;
-import org.apache.james.mailbox.jcr.mail.model.JCRMailbox;
 import org.apache.james.mailbox.jcr.mail.model.JCRMessage;
 import org.apache.james.mailbox.store.SimpleMessageMetaData;
 import org.apache.james.mailbox.store.mail.AbstractMessageMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper;
+import org.apache.james.mailbox.store.mail.ModSeqProvider;
+import org.apache.james.mailbox.store.mail.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
 
@@ -106,14 +107,14 @@ public class JCRMessageMapper extends Ab
      * @param session {@link MailboxSession} to which the mapper is bound
      * @param logger Log
      */
-    public JCRMessageMapper(final MailboxSessionJCRRepository repository, MailboxSession mSession, int scaleType) {
-        super(mSession);
+    public JCRMessageMapper(final MailboxSessionJCRRepository repository, MailboxSession mSession, UidProvider<String> uidProvider, ModSeqProvider<String> modSeqProvider, int scaleType) {
+        super(mSession, uidProvider, modSeqProvider);
         this.repository = repository;        
         this.scaleType = scaleType;
     }
     
-    public JCRMessageMapper(final MailboxSessionJCRRepository repos, MailboxSession session) {
-        this(repos, session, MESSAGE_SCALE_DAY);
+    public JCRMessageMapper(final MailboxSessionJCRRepository repos, MailboxSession session, UidProvider<String> uidProvider, ModSeqProvider<String> modSeqProvider) {
+        this(repos, session, uidProvider, modSeqProvider, MESSAGE_SCALE_DAY);
     }
 
     
@@ -425,44 +426,6 @@ public class JCRMessageMapper extends Ab
 
 
     /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#expungeMarkedForDeletionInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange)
-     */
-    public Map<Long, MessageMetaData> expungeMarkedForDeletion(Mailbox<String> mailbox, MessageRange set) throws MailboxException {
-        try {
-            final List<Message<String>> results;
-            final long from = set.getUidFrom();
-            final long to = set.getUidTo();
-            final Type type = set.getType();
-            switch (type) {
-                default:
-                case ALL:
-                    results = findDeletedMessagesInMailbox(mailbox);
-                    break;
-                case FROM:
-                    results = findDeletedMessagesInMailboxAfterUID(mailbox, from);
-                    break;
-                case ONE:
-                    results = findDeletedMessageInMailboxWithUID(mailbox, from);
-                    break;
-                case RANGE:
-                    results = findDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
-                    break;       
-            }
-            Map<Long, MessageMetaData> uids = new HashMap<Long, MessageMetaData>();
-            for (int i = 0; i < results.size(); i++) {
-                Message<String> m = results.get(i);
-                long uid = m.getUid();
-                uids.put(uid, new SimpleMessageMetaData(m));
-                delete(mailbox, m);
-            }
-            return uids;
-        } catch (RepositoryException e) {
-            throw new MailboxException("Unable to search MessageRange " + set + " in mailbox " + mailbox, e);
-        }
-    }
-
-    /*
      * 
      * TODO: Maybe we should better use an ItemVisitor and just traverse through the child nodes. This could be a way faster
      * 
@@ -548,53 +511,7 @@ public class JCRMessageMapper extends Ab
        repository.logout(mailboxSession);
     }
     
-
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#calculateHigestModSeq(org.apache.james.mailbox.store.mail.model.Mailbox)
-     */
-    protected long calculateHigestModSeq(Mailbox<String> mailbox) throws MailboxException {
-        try {
-            Session s = getSession();
-            // we use order by because without it count will always be 0 in jackrabbit
-            String queryString = "/jcr:root/" + ISO9075.encodePath(s.getNodeByIdentifier(mailbox.getMailboxId()).getPath()) + "//element(*,jamesMailbox:message) order by @" + JCRMessage.MODSEQ_PROPERTY + " descending";
-            QueryManager manager = s.getWorkspace().getQueryManager();
-            Query q = manager.createQuery(queryString, Query.XPATH);
-            q.setLimit(1);
-            QueryResult result = q.execute();
-            NodeIterator nodes = result.getNodes();
-            if (nodes.hasNext()) {
-                return nodes.nextNode().getProperty(JCRMessage.UID_PROPERTY).getLong();
-            }
-            return 0;
-        } catch (RepositoryException e) {
-            throw new MailboxException("Unable to count unseen messages in mailbox " + mailbox, e);
-        }
-    }
-
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#calculateLastUid(org.apache.james.mailbox.store.mail.model.Mailbox)
-     */
-    protected long calculateLastUid(Mailbox<String> mailbox) throws MailboxException {
-        try {
-            Session s = getSession();
-            // we use order by because without it count will always be 0 in jackrabbit
-            String queryString = "/jcr:root/" + ISO9075.encodePath(s.getNodeByIdentifier(mailbox.getMailboxId()).getPath()) + "//element(*,jamesMailbox:message) order by @" + JCRMessage.UID_PROPERTY + " descending";
-            QueryManager manager = s.getWorkspace().getQueryManager();
-            Query q = manager.createQuery(queryString, Query.XPATH);
-            q.setLimit(1);
-            QueryResult result = q.execute();
-            NodeIterator nodes = result.getNodes();
-            if (nodes.hasNext()) {
-                return nodes.nextNode().getProperty(JCRMessage.UID_PROPERTY).getLong();
-            }
-            return 0;
-        } catch (RepositoryException e) {
-            throw new MailboxException("Unable to count unseen messages in mailbox " + mailbox, e);
-        }
-    }
-
+    
     @Override
     protected MessageMetaData copy(Mailbox<String> mailbox, long uid, long modSeq, Message<String> original) throws MailboxException {
         try {
@@ -689,19 +606,39 @@ public class JCRMessageMapper extends Ab
             throw new MailboxException("Unable to save message " + message + " in mailbox " + mailbox, e);
         }        
     }
-
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#saveSequences(org.apache.james.mailbox.store.mail.model.Mailbox, long, long)
-     */
-    protected void saveSequences(Mailbox<String> mailbox, long lastUid, long highestModSeq) throws MailboxException {
+    
+    @Override
+    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox<String> mailbox, MessageRange set) throws MailboxException {
         try {
-            Node mailboxNode = getSession().getNodeByIdentifier(mailbox.getMailboxId());
-            mailboxNode.setProperty(JCRMailbox.HIGHESTKNOWNMODSEQ_PROPERTY, highestModSeq);
-            mailboxNode.setProperty(JCRMailbox.LASTKNOWNUID_PROPERTY, lastUid);
-           
+            final List<Message<String>> results;
+            final long from = set.getUidFrom();
+            final long to = set.getUidTo();
+            final Type type = set.getType();
+            switch (type) {
+                default:
+                case ALL:
+                    results = findDeletedMessagesInMailbox(mailbox);
+                    break;
+                case FROM:
+                    results = findDeletedMessagesInMailboxAfterUID(mailbox, from);
+                    break;
+                case ONE:
+                    results = findDeletedMessageInMailboxWithUID(mailbox, from);
+                    break;
+                case RANGE:
+                    results = findDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
+                    break;       
+            }
+            Map<Long, MessageMetaData> uids = new HashMap<Long, MessageMetaData>();
+            for (int i = 0; i < results.size(); i++) {
+                Message<String> m = results.get(i);
+                long uid = m.getUid();
+                uids.put(uid, new SimpleMessageMetaData(m));
+                delete(mailbox, m);
+            }
+            return uids;
         } catch (RepositoryException e) {
-            throw new MailboxException("Unable to save sequences", e);
+            throw new MailboxException("Unable to search MessageRange " + set + " in mailbox " + mailbox, e);
         }
     }
 

Added: james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRModSeqProvider.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRModSeqProvider.java?rev=1139535&view=auto
==============================================================================
--- james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRModSeqProvider.java (added)
+++ james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRModSeqProvider.java Sat Jun 25 12:29:24 2011
@@ -0,0 +1,68 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.mailbox.jcr.mail;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxPathLocker;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.jcr.MailboxSessionJCRRepository;
+import org.apache.james.mailbox.jcr.mail.model.JCRMailbox;
+import org.apache.james.mailbox.store.mail.AbstractLockingModSeqProvider;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+public class JCRModSeqProvider extends AbstractLockingModSeqProvider<String>{
+
+    private MailboxSessionJCRRepository repository;
+
+    public JCRModSeqProvider(MailboxPathLocker locker, MailboxSessionJCRRepository repository) {
+        super(locker);
+        this.repository = repository;
+    }
+
+    @Override
+    public long highestModSeq(MailboxSession session, Mailbox<String> mailbox) throws MailboxException {
+        try {
+            Session s = repository.login(session);
+            Node node = s.getNodeByIdentifier(mailbox.getMailboxId());
+            return node.getProperty(JCRMailbox.HIGHESTMODSEQ_PROPERTY).getLong();
+        } catch (RepositoryException e) {
+            throw new MailboxException("Unable to get highest mod-sequence for mailbox " + mailbox, e);
+        } 
+    }
+
+    @Override
+    protected long lockedNextModSeq(MailboxSession session, Mailbox<String> mailbox) throws MailboxException {
+        try {
+            Session s = repository.login(session);
+            Node node = s.getNodeByIdentifier(mailbox.getMailboxId());
+            long modseq = node.getProperty(JCRMailbox.HIGHESTMODSEQ_PROPERTY).getLong();
+            modseq++;
+            node.setProperty(JCRMailbox.HIGHESTMODSEQ_PROPERTY, modseq);
+            s.save();
+            return modseq;
+        } catch (RepositoryException e) {
+            throw new MailboxException("Unable to consume next uid for mailbox " + mailbox, e);
+        }
+    }
+
+}

Added: james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRUidProvider.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRUidProvider.java?rev=1139535&view=auto
==============================================================================
--- james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRUidProvider.java (added)
+++ james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRUidProvider.java Sat Jun 25 12:29:24 2011
@@ -0,0 +1,71 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.mailbox.jcr.mail;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxPathLocker;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.jcr.MailboxSessionJCRRepository;
+import org.apache.james.mailbox.jcr.mail.model.JCRMailbox;
+import org.apache.james.mailbox.store.mail.AbstractLockingUidProvider;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+public class JCRUidProvider extends AbstractLockingUidProvider<String>{
+
+    private MailboxSessionJCRRepository repository;
+
+    public JCRUidProvider(MailboxPathLocker locker, MailboxSessionJCRRepository repository) {
+        super(locker);
+        this.repository = repository;
+    }
+
+    @Override
+    public long lastUid(MailboxSession mailboxSession, Mailbox<String> mailbox) throws MailboxException {
+        try {
+            Session s = repository.login(mailboxSession);
+            Node node = s.getNodeByIdentifier(mailbox.getMailboxId());
+            return node.getProperty(JCRMailbox.LASTUID_PROPERTY).getLong();
+        } catch (RepositoryException e) {
+            throw new MailboxException("Unable to get last uid for mailbox " + mailbox, e);
+        }
+        
+    }
+
+    @Override
+    protected long lockedNextUid(MailboxSession session, Mailbox<String> mailbox) throws MailboxException {
+        try {
+            Session s = repository.login(session);
+            Node node = s.getNodeByIdentifier(mailbox.getMailboxId());
+            long uid = node.getProperty(JCRMailbox.LASTUID_PROPERTY).getLong();
+            uid++;
+            node.setProperty(JCRMailbox.LASTUID_PROPERTY, uid);
+            s.save();
+            return uid;
+        } catch (RepositoryException e) {
+            throw new MailboxException("Unable to consume next uid for mailbox " + mailbox, e);
+        }
+    }
+    
+
+
+}

Modified: james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java (original)
+++ james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java Sat Jun 25 12:29:24 2011
@@ -42,8 +42,8 @@ public class JCRMailbox implements Mailb
     public final static String NAMESPACE_PROPERTY = "jamesMailbox:mailboxNamespace";
     public final static String NAME_PROPERTY = "jamesMailbox:mailboxName";
     public final static String UIDVALIDITY_PROPERTY = "jamesMailbox:mailboxUidValidity";
-    public final static String LASTKNOWNUID_PROPERTY = "jamesMailbox:mailboxLastKnownUid";
-    public final static String HIGHESTKNOWNMODSEQ_PROPERTY = "jamesMailbox:mailboxHighestKnownModSeq";
+    public final static String LASTUID_PROPERTY = "jamesMailbox:mailboxLastUid";
+    public final static String HIGHESTMODSEQ_PROPERTY = "jamesMailbox:mailboxHighestModSeq";
 
     private String name;
     private long uidValidity;
@@ -154,8 +154,8 @@ public class JCRMailbox implements Mailb
         }
         node.setProperty(USER_PROPERTY, user);
         node.setProperty(NAMESPACE_PROPERTY, getNamespace());
-        node.setProperty(HIGHESTKNOWNMODSEQ_PROPERTY, getHighestKnownModSeq());
-        node.setProperty(LASTKNOWNUID_PROPERTY, getLastKnownUid());
+        node.setProperty(HIGHESTMODSEQ_PROPERTY, getHighestModSeq());
+        node.setProperty(LASTUID_PROPERTY, getLastUid());
         this.node = node;
     }
     
@@ -282,33 +282,23 @@ public class JCRMailbox implements Mailb
         }        
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.model.Mailbox#getLastKnownUid()
-     */
-    public long getLastKnownUid() {
+    private long getLastUid() {
         if (isPersistent()) {
             try {
-                return node.getProperty(LASTKNOWNUID_PROPERTY).getLong();
+                return node.getProperty(LASTUID_PROPERTY).getLong();
             } catch (RepositoryException e) {
-                logger.error("Unable to access property " + LASTKNOWNUID_PROPERTY, e);
+                logger.error("Unable to access property " + LASTUID_PROPERTY, e);
             }
         }
         return lastKnownUid;
     }
 
-    
-    
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.model.Mailbox#getHighestKnownModSeq()
-     */
-    public long getHighestKnownModSeq() {
+    private long getHighestModSeq() {
         if (isPersistent()) {
             try {
-                return node.getProperty(HIGHESTKNOWNMODSEQ_PROPERTY).getLong();
+                return node.getProperty(HIGHESTMODSEQ_PROPERTY).getLong();
             } catch (RepositoryException e) {
-                logger.error("Unable to access property " + HIGHESTKNOWNMODSEQ_PROPERTY, e);
+                logger.error("Unable to access property " + HIGHESTMODSEQ_PROPERTY, e);
             }
         }
         return highestKnownModSeq;

Modified: james/mailbox/trunk/jcr/src/main/resources/org/apache/james/mailbox/jcr/imap.cnd
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/main/resources/org/apache/james/mailbox/jcr/imap.cnd?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jcr/src/main/resources/org/apache/james/mailbox/jcr/imap.cnd (original)
+++ james/mailbox/trunk/jcr/src/main/resources/org/apache/james/mailbox/jcr/imap.cnd Sat Jun 25 12:29:24 2011
@@ -56,6 +56,6 @@
     - jamesMailbox:mailboxLastUid (LONG) 
     - jamesMailbox:mailboxNamespace (STRING) 
     - jamesMailbox:mailboxUser (STRING) 
-    - jamesMailbox:mailboxLastKnownUid (LONG) 
-    - jamesMailbox:mailboxHighestKnownModSeq (LONG) 
+    - jamesMailbox:mailboxLastUid (LONG) 
+    - jamesMailbox:mailboxHighestModSeq (LONG) 
     + * (nt:unstructured) multiple

Modified: james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRMailboxManagerTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRMailboxManagerTest.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRMailboxManagerTest.java (original)
+++ james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRMailboxManagerTest.java Sat Jun 25 12:29:24 2011
@@ -31,6 +31,9 @@ import org.apache.james.mailbox.BadCrede
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MailboxManagerTest;
 import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.jcr.mail.JCRModSeqProvider;
+import org.apache.james.mailbox.jcr.mail.JCRUidProvider;
+import org.apache.james.mailbox.store.JVMMailboxPathLocker;
 import org.junit.After;
 import org.junit.Before;
 import org.slf4j.LoggerFactory;
@@ -94,9 +97,11 @@ public class JCRMailboxManagerTest exten
         // Register imap cnd file
         JCRUtils.registerCnd(repository, workspace, user, pass);
         MailboxSessionJCRRepository sessionRepos = new GlobalMailboxSessionJCRRepository(repository, workspace, user, pass);
-
-        JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos);
-        JCRMailboxManager manager = new JCRMailboxManager(mf, null);
+        JVMMailboxPathLocker locker = new JVMMailboxPathLocker();
+        JCRUidProvider uidProvider = new JCRUidProvider(locker, sessionRepos);
+        JCRModSeqProvider modSeqProvider= new JCRModSeqProvider(locker, sessionRepos);
+        JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos, uidProvider, modSeqProvider);
+        JCRMailboxManager manager = new JCRMailboxManager(mf, null, locker);
         manager.init();
         setMailboxManager(manager);
     }

Modified: james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java (original)
+++ james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java Sat Jun 25 12:29:24 2011
@@ -28,6 +28,9 @@ import org.apache.james.mailbox.Abstract
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MailboxManager;
 import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.jcr.mail.JCRModSeqProvider;
+import org.apache.james.mailbox.jcr.mail.JCRUidProvider;
+import org.apache.james.mailbox.store.JVMMailboxPathLocker;
 import org.junit.After;
 import org.junit.Before;
 import org.slf4j.LoggerFactory;
@@ -53,9 +56,11 @@ public class JCRStressTest extends Abstr
         // Register imap cnd file
         JCRUtils.registerCnd(repository, workspace, user, pass);
         MailboxSessionJCRRepository sessionRepos = new GlobalMailboxSessionJCRRepository(repository, workspace, user, pass);
-
-        JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos);
-        mailboxManager = new JCRMailboxManager(mf, null);
+        JVMMailboxPathLocker locker = new JVMMailboxPathLocker();
+        JCRUidProvider uidProvider = new JCRUidProvider(locker, sessionRepos);
+        JCRModSeqProvider modSeqProvider= new JCRModSeqProvider(locker, sessionRepos);
+        JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos, uidProvider, modSeqProvider);
+        mailboxManager = new JCRMailboxManager(mf, null, locker);
         mailboxManager.init();
 
     }

Modified: james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRSubscriptionManagerTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRSubscriptionManagerTest.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRSubscriptionManagerTest.java (original)
+++ james/mailbox/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRSubscriptionManagerTest.java Sat Jun 25 12:29:24 2011
@@ -63,7 +63,7 @@ public class JCRSubscriptionManagerTest 
 
         new File(JACKRABBIT_HOME).delete();
         MailboxSessionJCRRepository sessionRepos = new GlobalMailboxSessionJCRRepository(repository, workspace, user, pass);
-        JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos);
+        JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos, null, null);
         JCRSubscriptionManager sm = new JCRSubscriptionManager(mf);
         return sm;
     }

Modified: james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxSessionMapperFactory.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxSessionMapperFactory.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxSessionMapperFactory.java (original)
+++ james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxSessionMapperFactory.java Sat Jun 25 12:29:24 2011
@@ -28,6 +28,8 @@ import org.apache.james.mailbox.jpa.user
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper;
+import org.apache.james.mailbox.store.mail.ModSeqProvider;
+import org.apache.james.mailbox.store.mail.UidProvider;
 import org.apache.james.mailbox.store.user.SubscriptionMapper;
 
 /**
@@ -37,9 +39,13 @@ import org.apache.james.mailbox.store.us
 public class JPAMailboxSessionMapperFactory extends MailboxSessionMapperFactory<Long> {
 
     private final EntityManagerFactory entityManagerFactory;
+    private final UidProvider<Long> uidProvider;
+    private final ModSeqProvider<Long> modSeqProvider;
 
-    public JPAMailboxSessionMapperFactory(EntityManagerFactory entityManagerFactory) {
+    public JPAMailboxSessionMapperFactory(EntityManagerFactory entityManagerFactory, UidProvider<Long> uidProvider, ModSeqProvider<Long> modSeqProvider) {
         this.entityManagerFactory = entityManagerFactory;
+        this.uidProvider = uidProvider;
+        this.modSeqProvider = modSeqProvider;
         createEntityManager().close();   
     }
     
@@ -50,7 +56,7 @@ public class JPAMailboxSessionMapperFact
 
     @Override
     public MessageMapper<Long> createMessageMapper(MailboxSession session) {
-        return new JPAMessageMapper(session, entityManagerFactory);
+        return new JPAMessageMapper(session, uidProvider, modSeqProvider, entityManagerFactory);
     }
 
     @Override

Modified: james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java (original)
+++ james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java Sat Jun 25 12:29:24 2011
@@ -25,7 +25,6 @@ import java.util.Map;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityTransaction;
-import javax.persistence.NoResultException;
 import javax.persistence.PersistenceException;
 import javax.persistence.Query;
 
@@ -41,6 +40,8 @@ import org.apache.james.mailbox.jpa.mail
 import org.apache.james.mailbox.store.SimpleMessageMetaData;
 import org.apache.james.mailbox.store.mail.AbstractMessageMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper;
+import org.apache.james.mailbox.store.mail.ModSeqProvider;
+import org.apache.james.mailbox.store.mail.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
 import org.apache.openjpa.persistence.ArgumentException;
@@ -52,8 +53,8 @@ public class JPAMessageMapper extends Ab
     protected EntityManagerFactory entityManagerFactory;
     protected EntityManager entityManager;
     
-    public JPAMessageMapper(final MailboxSession session, final EntityManagerFactory entityManagerFactory) {
-        super(session);
+    public JPAMessageMapper(final MailboxSession session, final UidProvider<Long> uidProvider, ModSeqProvider<Long> modSeqProvider, final EntityManagerFactory entityManagerFactory) {
+        super(session, uidProvider, modSeqProvider);
         this.entityManagerFactory = entityManagerFactory;
     }
 
@@ -199,46 +200,6 @@ public class JPAMessageMapper extends Ab
     }
 
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#expungeMarkedForDeletion(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange)
-     */
-    public Map<Long, MessageMetaData> expungeMarkedForDeletion(Mailbox<Long> mailbox, final MessageRange set) throws MailboxException {
-        try {
-            final Map<Long, MessageMetaData> data;
-            final List<Message<Long>> results;
-            final long from = set.getUidFrom();
-            final long to = set.getUidTo();
-            
-            switch (set.getType()) {
-                case ONE:
-                    results = findDeletedMessagesInMailboxWithUID(mailbox, from);
-                    data = createMetaData(results);
-                    deleteDeletedMessagesInMailboxWithUID(mailbox, from);
-                    break;
-                case RANGE:
-                    results = findDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
-                    data = createMetaData(results);
-                    deleteDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
-                    break;
-                case FROM:
-                    results = findDeletedMessagesInMailboxAfterUID(mailbox, from);
-                    data = createMetaData(results);
-                    deleteDeletedMessagesInMailboxAfterUID(mailbox, from);
-                    break;
-                default:
-                case ALL:
-                    results = findDeletedMessagesInMailbox(mailbox);
-                    data = createMetaData(results);
-                    deleteDeletedMessagesInMailbox(mailbox);
-                    break;
-            }
-            
-            return data;
-        } catch (PersistenceException e) {
-            throw new MailboxException("Search of MessageRange " + set + " failed in mailbox " + mailbox, e);
-        }
-    }
     
     private Map<Long, MessageMetaData> createMetaData(List<Message<Long>> uids) {
         final Map<Long, MessageMetaData> data = new HashMap<Long, MessageMetaData>();
@@ -373,38 +334,6 @@ public class JPAMessageMapper extends Ab
 
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#calculateHigestModSeq(org.apache.james.mailbox.store.mail.model.Mailbox)
-     */
-    protected long calculateHigestModSeq(Mailbox<Long> mailbox) throws MailboxException {
-        try {
-            final long modSeq = (Long) entityManager.createNamedQuery("findHighestModSeqInMailbox").setParameter("idParam", mailbox.getMailboxId()).setMaxResults(1).getSingleResult();
-            return modSeq;
-        } catch (NoResultException e) {
-            return 0;
-        } catch (PersistenceException e) {
-            throw new MailboxException("Unable to retrieve higehst mod-sequence for mailbox " + mailbox);
-        }
-    }
-
-
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#calculateLastUid(org.apache.james.mailbox.store.mail.model.Mailbox)
-     */
-    protected long calculateLastUid(Mailbox<Long> mailbox) throws MailboxException {
-        try {
-            final long uid = (Long) entityManager.createNamedQuery("findLastUidInMailbox").setParameter("idParam", mailbox.getMailboxId()).setMaxResults(1).getSingleResult();
-            return uid;
-        } catch (NoResultException e) {
-            return 0;
-        } catch (PersistenceException e) {
-            throw new MailboxException("Unable to retrieve last uid for mailbox " + mailbox);
-        }
-    }
-
-
-    /*
-     * (non-Javadoc)
      * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#copy(org.apache.james.mailbox.store.mail.model.Mailbox, long, long, org.apache.james.mailbox.store.mail.model.Message)
      */
     protected MessageMetaData copy(Mailbox<Long> mailbox, long uid, long modSeq, Message<Long> original) throws MailboxException {
@@ -440,18 +369,41 @@ public class JPAMessageMapper extends Ab
     }
 
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#saveSequences(org.apache.james.mailbox.store.mail.model.Mailbox, long, long)
-     */
-    protected void saveSequences(Mailbox<Long> mailbox, long lastUid, long highestModSeq) throws MailboxException {
+    @Override
+    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox<Long> mailbox, MessageRange set) throws MailboxException {
         try {
-            getEntityManager().createNamedQuery("updateSequences")
-            .setParameter("idParam", mailbox.getMailboxId())
-            .setParameter("lastKnownUidParam", lastUid)
-            .setParameter("lastKnownHighestModSeqParam", highestModSeq).executeUpdate();      
+            final Map<Long, MessageMetaData> data;
+            final List<Message<Long>> results;
+            final long from = set.getUidFrom();
+            final long to = set.getUidTo();
+            
+            switch (set.getType()) {
+                case ONE:
+                    results = findDeletedMessagesInMailboxWithUID(mailbox, from);
+                    data = createMetaData(results);
+                    deleteDeletedMessagesInMailboxWithUID(mailbox, from);
+                    break;
+                case RANGE:
+                    results = findDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
+                    data = createMetaData(results);
+                    deleteDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
+                    break;
+                case FROM:
+                    results = findDeletedMessagesInMailboxAfterUID(mailbox, from);
+                    data = createMetaData(results);
+                    deleteDeletedMessagesInMailboxAfterUID(mailbox, from);
+                    break;
+                default:
+                case ALL:
+                    results = findDeletedMessagesInMailbox(mailbox);
+                    data = createMetaData(results);
+                    deleteDeletedMessagesInMailbox(mailbox);
+                    break;
+            }
+            
+            return data;
         } catch (PersistenceException e) {
-            throw new MailboxException("Save of sequences for mailbox " + mailbox + " failed", e);
+            throw new MailboxException("Search of MessageRange " + set + " failed in mailbox " + mailbox, e);
         }
     }
 }

Added: james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAModSeqProvider.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAModSeqProvider.java?rev=1139535&view=auto
==============================================================================
--- james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAModSeqProvider.java (added)
+++ james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAModSeqProvider.java Sat Jun 25 12:29:24 2011
@@ -0,0 +1,85 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.mailbox.jpa.mail;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceException;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxPathLocker;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
+import org.apache.james.mailbox.store.mail.AbstractLockingModSeqProvider;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+public class JPAModSeqProvider extends AbstractLockingModSeqProvider<Long>{
+
+    private EntityManagerFactory factory;
+
+    public JPAModSeqProvider(MailboxPathLocker locker, EntityManagerFactory factory) {
+        super(locker);
+        this.factory = factory;
+    }
+
+    @Override
+    public long highestModSeq(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
+        EntityManager manager = null;
+        try {
+            manager = factory.createEntityManager();
+            manager.getTransaction().begin();
+            long highest = (Long) manager.createNamedQuery("findHighestModSeq").setParameter("idParam", mailbox.getMailboxId()).getSingleResult();
+            manager.getTransaction().commit();
+            return highest;
+        } catch (PersistenceException e) {
+            if (manager != null && manager.getTransaction().isActive()) {
+                manager.getTransaction().rollback();
+            }
+            throw new MailboxException("Unable to get highest mod-sequence for mailbox " + mailbox, e);
+        } finally {
+            if (manager != null) {
+                manager.close();
+            }
+        }
+    }
+
+    @Override
+    protected long lockedNextModSeq(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
+        EntityManager manager = null;
+        try {
+            manager = factory.createEntityManager();
+            manager.getTransaction().begin();
+            JPAMailbox m = manager.find(JPAMailbox.class, mailbox.getMailboxId());
+            long modSeq = m.consumeModSeq();
+            manager.persist(m);
+            manager.getTransaction().commit();
+            return modSeq;
+        } catch (PersistenceException e) {
+            if (manager != null && manager.getTransaction().isActive()) {
+                manager.getTransaction().rollback();
+            }
+            throw new MailboxException("Unable to save highest mod-sequence for mailbox " + mailbox, e);
+        } finally {
+            if (manager != null) {
+                manager.close();
+            }
+        }
+    }
+
+}

Added: james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAUidProvider.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAUidProvider.java?rev=1139535&view=auto
==============================================================================
--- james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAUidProvider.java (added)
+++ james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAUidProvider.java Sat Jun 25 12:29:24 2011
@@ -0,0 +1,86 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.mailbox.jpa.mail;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceException;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxPathLocker;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
+import org.apache.james.mailbox.store.mail.AbstractLockingUidProvider;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+public class JPAUidProvider extends AbstractLockingUidProvider<Long>{
+
+    private EntityManagerFactory factory;
+
+    public JPAUidProvider(MailboxPathLocker locker, EntityManagerFactory factory) {
+        super(locker);
+        this.factory = factory;
+    }
+    
+    
+    @Override
+    public long lastUid(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
+        EntityManager manager = null;
+        try {
+            manager = factory.createEntityManager();
+            manager.getTransaction().begin();
+            long uid = (Long) manager.createNamedQuery("findLastUid").setParameter("idParam", mailbox.getMailboxId()).getSingleResult();
+            manager.getTransaction().commit();
+            return uid;
+        } catch (PersistenceException e) {
+            if (manager != null && manager.getTransaction().isActive()) {
+                manager.getTransaction().rollback();
+            }
+            throw new MailboxException("Unable to get last uid for mailbox " + mailbox, e);
+        } finally {
+            if (manager != null) {
+                manager.close();
+            }
+        }
+    }
+
+    @Override
+    protected long lockedNextUid(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
+        EntityManager manager = null;
+        try {
+            manager = factory.createEntityManager();
+            manager.getTransaction().begin();
+            JPAMailbox m = manager.find(JPAMailbox.class, mailbox.getMailboxId());
+            long uid = m.consumeUid();
+            manager.persist(m);
+            manager.getTransaction().commit();
+            return uid;
+        } catch (PersistenceException e) {
+            if (manager != null && manager.getTransaction().isActive()) {
+                manager.getTransaction().rollback();
+            }
+            throw new MailboxException("Unable to save next uid for mailbox " + mailbox, e);
+        } finally {
+            if (manager != null) {
+                manager.close();
+            }
+        }
+    }
+
+}

Modified: james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/JPAMailbox.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/JPAMailbox.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/JPAMailbox.java (original)
+++ james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/JPAMailbox.java Sat Jun 25 12:29:24 2011
@@ -51,9 +51,10 @@ import org.apache.james.mailbox.store.ma
         query="SELECT COUNT(mailbox) FROM Mailbox mailbox WHERE mailbox.name LIKE :nameParam and mailbox.user is NULL and mailbox.namespace= :namespaceParam"),
     @NamedQuery(name="listMailboxes",
         query="SELECT mailbox FROM Mailbox mailbox"),
-    @NamedQuery(name="updateSequences", 
-        query= "UPDATE Mailbox mailbox SET mailbox.lastKnownUid = :lastKnownUidParam, mailbox.highestKnownModSeq = :lastKnownHighestModSeqParam  WHERE mailbox.mailboxId = :idParam")
-    
+    @NamedQuery(name="findHighestModSeq",
+        query="SELECT mailbox.highestModSeq FROM Mailbox mailbox WHERE mailbox.mailbox.mailboxId = :idParam"),
+    @NamedQuery(name="findLastUid",
+        query="SELECT mailbox.lastUid FROM Mailbox mailbox WHERE mailbox.mailbox.mailboxId = :idParam")
 })
 public class JPAMailbox implements Mailbox<Long> {
     
@@ -84,12 +85,12 @@ public class JPAMailbox implements Mailb
     private String namespace;
 
     @Basic(optional = false)
-    @Column(name = "MAILBOX_LAST_KNOWN_UID", nullable = false)
-    private long lastKnownUid;
+    @Column(name = "MAILBOX_LAST_UID", nullable = false)
+    private long lastUid;
     
     @Basic(optional = false)
-    @Column(name = "MAILBOX_HIGHEST_KNOWN_MODSEQ", nullable = false)
-    private long highestKnownModSeq;
+    @Column(name = "MAILBOX_HIGHEST_MODSEQ", nullable = false)
+    private long highestModSeq;
     
     /**
      * JPA only
@@ -200,21 +201,20 @@ public class JPAMailbox implements Mailb
     }
 
     
-
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.model.Mailbox#getLastKnownUid()
-     */
-    public long getLastKnownUid() {
-        return lastKnownUid;
+    public long getLastUid() {
+        return lastUid;
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.model.Mailbox#getHighestKnownModSeq()
-     */
-    public long getHighestKnownModSeq() {
-        return highestKnownModSeq;
+    public long getHighestModSeq() {
+        return highestModSeq;
+    }
+    
+    public long consumeUid() {
+        return ++lastUid;
+    }
+    
+    public long consumeModSeq() {
+        return ++highestModSeq;
     }
     
 }

Modified: james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxManagerTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxManagerTest.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxManagerTest.java (original)
+++ james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxManagerTest.java Sat Jun 25 12:29:24 2011
@@ -26,6 +26,8 @@ import org.apache.james.mailbox.BadCrede
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MailboxManagerTest;
 import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.jpa.mail.JPAModSeqProvider;
+import org.apache.james.mailbox.jpa.mail.JPAUidProvider;
 import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
 import org.apache.james.mailbox.jpa.mail.model.JPAProperty;
 import org.apache.james.mailbox.jpa.mail.model.JPAUserFlag;
@@ -33,6 +35,7 @@ import org.apache.james.mailbox.jpa.mail
 import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMessage;
 import org.apache.james.mailbox.jpa.openjpa.OpenJPAMailboxManager;
 import org.apache.james.mailbox.jpa.user.model.JPASubscription;
+import org.apache.james.mailbox.store.JVMMailboxPathLocker;
 import org.apache.openjpa.persistence.OpenJPAPersistence;
 import org.junit.After;
 import org.junit.Before;
@@ -93,7 +96,8 @@ public class JPAMailboxManagerTest exten
                 JPASubscription.class.getName() + ")");
        
         entityManagerFactory = OpenJPAPersistence.getEntityManagerFactory(properties);
-        JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory);
+        JVMMailboxPathLocker locker = new JVMMailboxPathLocker();
+        JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory, new JPAUidProvider(locker, entityManagerFactory), new JPAModSeqProvider(locker, entityManagerFactory));
 
         JPAMailboxManager mailboxManager = new OpenJPAMailboxManager(mf, null);
         mailboxManager.init();

Modified: james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java (original)
+++ james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java Sat Jun 25 12:29:24 2011
@@ -27,6 +27,8 @@ import org.apache.james.mailbox.Abstract
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MailboxManager;
 import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.jpa.mail.JPAModSeqProvider;
+import org.apache.james.mailbox.jpa.mail.JPAUidProvider;
 import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
 import org.apache.james.mailbox.jpa.mail.model.JPAProperty;
 import org.apache.james.mailbox.jpa.mail.model.JPAUserFlag;
@@ -34,6 +36,7 @@ import org.apache.james.mailbox.jpa.mail
 import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMessage;
 import org.apache.james.mailbox.jpa.openjpa.OpenJPAMailboxManager;
 import org.apache.james.mailbox.jpa.user.model.JPASubscription;
+import org.apache.james.mailbox.store.JVMMailboxPathLocker;
 import org.apache.openjpa.persistence.OpenJPAPersistence;
 import org.junit.After;
 import org.junit.Before;
@@ -69,7 +72,9 @@ public class JPAStressTest extends Abstr
         properties.put("openjpa.LockTimeout", locktimeout + "");
        
         entityManagerFactory = OpenJPAPersistence.getEntityManagerFactory(properties);
-        JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory);
+        JVMMailboxPathLocker locker = new JVMMailboxPathLocker();
+        JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory, new JPAUidProvider(locker, entityManagerFactory), new JPAModSeqProvider(locker, entityManagerFactory));
+
         mailboxManager = new OpenJPAMailboxManager(mf, null);
         mailboxManager.init();
 

Modified: james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPASubscriptionManagerTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPASubscriptionManagerTest.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPASubscriptionManagerTest.java (original)
+++ james/mailbox/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPASubscriptionManagerTest.java Sat Jun 25 12:29:24 2011
@@ -22,12 +22,15 @@ import java.util.HashMap;
 
 import org.apache.james.mailbox.AbstractSubscriptionManagerTest;
 import org.apache.james.mailbox.SubscriptionManager;
+import org.apache.james.mailbox.jpa.mail.JPAModSeqProvider;
+import org.apache.james.mailbox.jpa.mail.JPAUidProvider;
 import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
 import org.apache.james.mailbox.jpa.mail.model.JPAProperty;
 import org.apache.james.mailbox.jpa.mail.model.JPAUserFlag;
 import org.apache.james.mailbox.jpa.mail.model.openjpa.AbstractJPAMessage;
 import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMessage;
 import org.apache.james.mailbox.jpa.user.model.JPASubscription;
+import org.apache.james.mailbox.store.JVMMailboxPathLocker;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
 import org.apache.openjpa.persistence.OpenJPAPersistence;
 import org.junit.After;
@@ -59,7 +62,10 @@ public class JPASubscriptionManagerTest 
     
     @Override
     public SubscriptionManager createSubscriptionManager() {
-        JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory);
+        JVMMailboxPathLocker locker = new JVMMailboxPathLocker();
+
+        JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory, new JPAUidProvider(locker, entityManagerFactory), new JPAModSeqProvider(locker, entityManagerFactory));
+
         JPASubscriptionManager sm = new JPASubscriptionManager(mf);
         
         return sm;

Modified: james/mailbox/trunk/lucene/src/test/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndexTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/lucene/src/test/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndexTest.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/lucene/src/test/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndexTest.java (original)
+++ james/mailbox/trunk/lucene/src/test/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndexTest.java Sat Jun 25 12:29:24 2011
@@ -742,16 +742,5 @@ public class LuceneMessageSearchIndexTes
             return 0;
         }
 
-        @Override
-        public long getLastKnownUid() {
-            // TODO Auto-generated method stub
-            return 0;
-        }
-
-        @Override
-        public long getHighestKnownModSeq() {
-            // TODO Auto-generated method stub
-            return 0;
-        }
     }
 }

Modified: james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java (original)
+++ james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java Sat Jun 25 12:29:24 2011
@@ -29,10 +29,12 @@ import org.apache.james.mailbox.MailboxP
 import org.apache.james.mailbox.MailboxPathLocker;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.store.JVMMailboxPathLocker;
+import org.apache.james.mailbox.store.mail.ModSeqProvider;
+import org.apache.james.mailbox.store.mail.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
 
-public class MaildirStore {
+public class MaildirStore implements UidProvider<Integer>, ModSeqProvider<Integer>{
 
     public static final String PATH_USER = "%user";
     public static final String PATH_DOMAIN = "%domain";
@@ -122,7 +124,7 @@ public class MaildirStore {
         try {
             uidValidity = folder.getUidValidity();
             lastUid = folder.getLastUid(session);
-            return new SimpleMailbox<Integer>(mailboxPath, uidValidity, lastUid, folder.getHighestModSeq());
+            return new SimpleMailbox<Integer>(mailboxPath, uidValidity);
 
         } catch (IOException e) {
             throw new MailboxException("Unable to load Mailbox " + mailboxPath, e);
@@ -244,4 +246,23 @@ public class MaildirStore {
             throw new MailboxException("Unable to generate next uid", e);
         }
     }
+
+    @Override
+    public long nextModSeq(MailboxSession session, Mailbox<Integer> mailbox) throws MailboxException {
+        return System.currentTimeMillis();
+    }
+
+    @Override
+    public long highestModSeq(MailboxSession session, Mailbox<Integer> mailbox) throws MailboxException {
+        try {
+            return createMaildirFolder(mailbox).getHighestModSeq();
+        } catch (IOException e) {
+            throw new MailboxException("Unable to get highest mod-sequence for mailbox", e);
+        }
+    }
+
+    @Override
+    public long lastUid(MailboxSession session, Mailbox<Integer> mailbox) throws MailboxException {
+       return createMaildirFolder(mailbox).getLastUid(session);
+    }
 }

Modified: james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java (original)
+++ james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java Sat Jun 25 12:29:24 2011
@@ -57,8 +57,8 @@ public class MaildirMessageMapper extend
     private final MaildirStore maildirStore;
     private final int BUF_SIZE = 2048;
 
-    public MaildirMessageMapper(MailboxSession session,MaildirStore  maildirStore) {
-        super(session);
+    public MaildirMessageMapper(MailboxSession session, MaildirStore  maildirStore) {
+        super(session, maildirStore, maildirStore);
         this.maildirStore = maildirStore;
     }
     
@@ -257,48 +257,6 @@ public class MaildirMessageMapper extend
         
     }
 
-    /**
-     * Call {@link #calculateHigestModSeq(Mailbox)} as we do no caching here
-     */
-    public long getHighestModSeq(Mailbox<Integer> mailbox) throws MailboxException {
-        return calculateHigestModSeq(mailbox);
-    }
-
-    /**
-     * Call {@link #calculateLastUid(Mailbox)} as we do no caching here
-     */
-    public long getLastUid(Mailbox<Integer> mailbox) throws MailboxException {
-        return calculateLastUid(mailbox);
-    }
-
-
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#calculateHigestModSeq(org.apache.james.mailbox.store.mail.model.Mailbox)
-     */
-    protected long calculateHigestModSeq(Mailbox<Integer> mailbox) throws MailboxException {
-        MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
-        try {
-            return folder.getHighestModSeq();
-        } catch (IOException e) {
-            throw new MailboxException("Unable to get highest mod-seq for mailbox " + mailbox, e);
-        }
-    }
-
-
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#calculateLastUid(org.apache.james.mailbox.store.mail.model.Mailbox)
-     */
-    protected long calculateLastUid(Mailbox<Integer> mailbox) throws MailboxException {
-        MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
-        try {
-            return folder.getLastUid(mailboxSession);
-        } catch (MailboxException e) {
-            throw new MailboxException("Unable to get last uid for mailbox " + mailbox, e);
-        }
-    }
-
 
     /*
      * (non-Javadoc)
@@ -309,43 +267,6 @@ public class MaildirMessageMapper extend
         return save(mailbox, theCopy);        
     }
 
-
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#expungeMarkedForDeletion(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange)
-     */
-    protected Map<Long, MessageMetaData> expungeMarkedForDeletion(Mailbox<Integer> mailbox, MessageRange set) throws MailboxException {
-        List<Message<Integer>> results = new ArrayList<Message<Integer>>();
-        final long from = set.getUidFrom();
-        final long to = set.getUidTo();
-        final Type type = set.getType();
-        switch (type) {
-        default:
-        case ALL:
-            results = findMessagesInMailbox(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, -1);
-            break;
-        case FROM:
-            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, -1);
-            break;
-        case ONE:
-            results = findDeletedMessageInMailboxWithUID(mailbox, from);
-            break;
-        case RANGE:
-            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, to);
-            break;       
-        }
-        Map<Long, MessageMetaData> uids = new HashMap<Long, MessageMetaData>();
-        for (int i = 0; i < results.size(); i++) {
-            Message<Integer> m = results.get(i);
-            long uid = m.getUid();
-            uids.put(uid, new SimpleMessageMetaData(m));
-            delete(mailbox, m);
-        }
-        
-        return uids;
-    }
-
-
     /*
      * (non-Javadoc)
      * 
@@ -430,14 +351,6 @@ public class MaildirMessageMapper extend
 
     }
 
-    /**
-     * Do nothing as maildir store the uid and modseq everytime by it own
-     */
-    protected void saveSequences(Mailbox<Integer> mailbox, long lastUid, long highestModSeq) throws MailboxException {
-        // Nothing todo as maildir does its own sequence-keeping
-        
-    }
-
 
     /*
      * (non-Javadoc)
@@ -537,4 +450,37 @@ public class MaildirMessageMapper extend
         
     }
 
+
+    @Override
+    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox<Integer> mailbox, MessageRange set) throws MailboxException {
+        List<Message<Integer>> results = new ArrayList<Message<Integer>>();
+        final long from = set.getUidFrom();
+        final long to = set.getUidTo();
+        final Type type = set.getType();
+        switch (type) {
+        default:
+        case ALL:
+            results = findMessagesInMailbox(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, -1);
+            break;
+        case FROM:
+            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, -1);
+            break;
+        case ONE:
+            results = findDeletedMessageInMailboxWithUID(mailbox, from);
+            break;
+        case RANGE:
+            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, to);
+            break;       
+        }
+        Map<Long, MessageMetaData> uids = new HashMap<Long, MessageMetaData>();
+        for (int i = 0; i < results.size(); i++) {
+            Message<Integer> m = results.get(i);
+            long uid = m.getUid();
+            uids.put(uid, new SimpleMessageMetaData(m));
+            delete(mailbox, m);
+        }
+        
+        return uids;
+    }
+
 }

Modified: james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryMailboxSessionMapperFactory.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryMailboxSessionMapperFactory.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryMailboxSessionMapperFactory.java (original)
+++ james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryMailboxSessionMapperFactory.java Sat Jun 25 12:29:24 2011
@@ -23,6 +23,8 @@ import org.apache.james.mailbox.MailboxS
 import org.apache.james.mailbox.SubscriptionException;
 import org.apache.james.mailbox.inmemory.mail.InMemoryMailboxMapper;
 import org.apache.james.mailbox.inmemory.mail.InMemoryMessageMapper;
+import org.apache.james.mailbox.inmemory.mail.InMemoryModSeqProvider;
+import org.apache.james.mailbox.inmemory.mail.InMemoryUidProvider;
 import org.apache.james.mailbox.inmemory.user.InMemorySubscriptionMapper;
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
@@ -37,7 +39,7 @@ public class InMemoryMailboxSessionMappe
     
     public InMemoryMailboxSessionMapperFactory() {
         mailboxMapper = new InMemoryMailboxMapper();
-        messageMapper = new InMemoryMessageMapper(null);
+        messageMapper = new InMemoryMessageMapper(null, new InMemoryUidProvider(), new InMemoryModSeqProvider());
         subscriptionMapper = new InMemorySubscriptionMapper();
     }
     

Modified: james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java (original)
+++ james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java Sat Jun 25 12:29:24 2011
@@ -36,6 +36,8 @@ import org.apache.james.mailbox.MessageM
 import org.apache.james.mailbox.MessageRange;
 import org.apache.james.mailbox.store.SimpleMessageMetaData;
 import org.apache.james.mailbox.store.mail.AbstractMessageMapper;
+import org.apache.james.mailbox.store.mail.ModSeqProvider;
+import org.apache.james.mailbox.store.mail.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMessage;
@@ -45,8 +47,8 @@ public class InMemoryMessageMapper exten
     private Map<Long, Map<Long, Message<Long>>> mailboxByUid;
     private static final int INITIAL_SIZE = 256;
     
-    public InMemoryMessageMapper(MailboxSession session) {
-        super(session);
+    public InMemoryMessageMapper(MailboxSession session, UidProvider<Long> uidProvider, ModSeqProvider<Long> modSeqProvider) {
+        super(session, uidProvider, modSeqProvider);
         this.mailboxByUid = new ConcurrentHashMap<Long, Map<Long, Message<Long>>>(INITIAL_SIZE);
     }
     
@@ -145,30 +147,6 @@ public class InMemoryMessageMapper exten
 
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#expungeMarkedForDeletionInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange)
-     */
-    public Map<Long, MessageMetaData> expungeMarkedForDeletion(final Mailbox<Long> mailbox, MessageRange set) throws MailboxException {
-        final Map<Long, MessageMetaData> filteredResult = new HashMap<Long, MessageMetaData>();
-
-        findInMailbox(mailbox, set, FetchType.Metadata, new MessageCallback<Long>() {
-
-            public void onMessages(List<Message<Long>> results) throws MailboxException {
-                for (final Iterator<Message<Long>> it = results.iterator(); it.hasNext();) {
-                    Message<Long> member = it.next();
-                    if (member.isDeleted()) {
-                        filteredResult.put(member.getUid(), new SimpleMessageMetaData(member));
-
-                        delete(mailbox, member);
-                    }
-                }
-            }
-        });
-
-        return filteredResult;
-    }
-
-    /*
-     * (non-Javadoc)
      * @see org.apache.james.mailbox.store.mail.MessageMapper#findRecentMessagesInMailbox()
      */
     public List<Long> findRecentMessageUidsInMailbox(Mailbox<Long> mailbox) throws MailboxException {
@@ -230,23 +208,6 @@ public class InMemoryMessageMapper exten
 
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#calculateHigestModSeq(org.apache.james.mailbox.store.mail.model.Mailbox)
-     */
-    protected long calculateHigestModSeq(Mailbox<Long> mailbox) throws MailboxException {
-        return 0;
-    }
-
-
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#calculateLastUid(org.apache.james.mailbox.store.mail.model.Mailbox)
-     */
-    protected long calculateLastUid(Mailbox<Long> mailbox) throws MailboxException {
-        return 0;
-    }
-
-    /*
-     * (non-Javadoc)
      * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#save(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.store.mail.model.Message)
      */
     protected MessageMetaData save(Mailbox<Long> mailbox, Message<Long> message) throws MailboxException {
@@ -279,11 +240,26 @@ public class InMemoryMessageMapper exten
     protected void rollback() throws MailboxException {        
     }
 
-    /**
-     * Do nothing
-     */
-    protected void saveSequences(Mailbox<Long> mailbox, long lastUid, long highestModSeq) throws MailboxException {
-        // Nothing todo as its a in memory implementation
-        
+
+
+    @Override
+    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(final Mailbox<Long> mailbox, MessageRange set) throws MailboxException {
+        final Map<Long, MessageMetaData> filteredResult = new HashMap<Long, MessageMetaData>();
+
+        findInMailbox(mailbox, set, FetchType.Metadata, new MessageCallback<Long>() {
+
+            public void onMessages(List<Message<Long>> results) throws MailboxException {
+                for (final Iterator<Message<Long>> it = results.iterator(); it.hasNext();) {
+                    Message<Long> member = it.next();
+                    if (member.isDeleted()) {
+                        filteredResult.put(member.getUid(), new SimpleMessageMetaData(member));
+
+                        delete(mailbox, member);
+                    }
+                }
+            }
+        });
+
+        return filteredResult;
     }
 }

Added: james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryModSeqProvider.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryModSeqProvider.java?rev=1139535&view=auto
==============================================================================
--- james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryModSeqProvider.java (added)
+++ james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryModSeqProvider.java Sat Jun 25 12:29:24 2011
@@ -0,0 +1,55 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.mailbox.inmemory.mail;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.store.mail.ModSeqProvider;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+public class InMemoryModSeqProvider implements ModSeqProvider<Long>{
+    private final ConcurrentMap<Long, AtomicLong> map = new ConcurrentHashMap<Long, AtomicLong>();
+
+    @Override
+    public long nextModSeq(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
+        return getHighest(mailbox.getMailboxId()).incrementAndGet();
+
+    }
+
+    @Override
+    public long highestModSeq(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
+        return getHighest(mailbox.getMailboxId()).get();
+    }
+    private AtomicLong getHighest(Long id) {
+        AtomicLong uid = map.get(id);
+        if (uid == null) {
+            uid = new AtomicLong(0);
+            AtomicLong u = map.putIfAbsent(id, uid);
+            if (u != null) {
+                uid = u;
+            }
+        }
+        return uid;
+    }
+}

Added: james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryUidProvider.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryUidProvider.java?rev=1139535&view=auto
==============================================================================
--- james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryUidProvider.java (added)
+++ james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryUidProvider.java Sat Jun 25 12:29:24 2011
@@ -0,0 +1,57 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.mailbox.inmemory.mail;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.store.mail.UidProvider;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+public class InMemoryUidProvider implements UidProvider<Long>{
+
+    private final ConcurrentMap<Long, AtomicLong> map = new ConcurrentHashMap<Long, AtomicLong>();
+    
+    @Override
+    public long nextUid(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
+        return getLast(mailbox.getMailboxId()).incrementAndGet();
+    }
+
+    @Override
+    public long lastUid(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
+        return getLast(mailbox.getMailboxId()).get();
+    }
+    
+    private AtomicLong getLast(Long id) {
+        AtomicLong uid = map.get(id);
+        if (uid == null) {
+            uid = new AtomicLong(0);
+            AtomicLong u = map.putIfAbsent(id, uid);
+            if (u != null) {
+                uid = u;
+            }
+        }
+        return uid;
+    }
+
+}

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java?rev=1139535&r1=1139534&r2=1139535&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java Sat Jun 25 12:29:24 2011
@@ -270,7 +270,7 @@ public class StoreMailboxManager<Id> imp
      * @throws MailboxException
      */
     protected org.apache.james.mailbox.store.mail.model.Mailbox<Id> doCreateMailbox(MailboxPath mailboxPath, final MailboxSession session) throws MailboxException {
-        return new SimpleMailbox<Id>(mailboxPath, randomUidValidity(), 0, 0);
+        return new SimpleMailbox<Id>(mailboxPath, randomUidValidity());
     }
     
     /*

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractLockingModSeqProvider.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractLockingModSeqProvider.java?rev=1139535&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractLockingModSeqProvider.java (added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractLockingModSeqProvider.java Sat Jun 25 12:29:24 2011
@@ -0,0 +1,49 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.mailbox.store.mail;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxPathLocker;
+import org.apache.james.mailbox.MailboxPathLocker.LockAwareExecution;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.store.StoreMailboxPath;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+public abstract class AbstractLockingModSeqProvider<Id> implements ModSeqProvider<Id>{
+
+    private final MailboxPathLocker locker;
+
+    public AbstractLockingModSeqProvider(MailboxPathLocker locker) {
+        this.locker = locker;
+    }
+    
+    @Override
+    public long nextModSeq(final MailboxSession session, final Mailbox<Id> mailbox) throws MailboxException {
+        return locker.executeWithLock(session, new StoreMailboxPath<Id>(mailbox), new LockAwareExecution<Long>() {
+
+            @Override
+            public Long execute() throws MailboxException {
+                return lockedNextModSeq(session, mailbox);
+            }
+        });
+    }
+    
+    protected abstract long lockedNextModSeq(MailboxSession session, Mailbox<Id> mailbox) throws MailboxException;
+
+}



---------------------------------------------------------------------
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