james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r983076 - in /james/imap/trunk: jcr/src/main/java/org/apache/james/imap/jcr/ jcr/src/main/java/org/apache/james/imap/jcr/mail/ jcr/src/main/java/org/apache/james/imap/jcr/mail/model/ jpa/src/main/java/org/apache/james/imap/jpa/ jpa/src/main...
Date Fri, 06 Aug 2010 18:02:36 GMT
Author: norman
Date: Fri Aug  6 18:02:34 2010
New Revision: 983076

URL: http://svn.apache.org/viewvc?rev=983076&view=rev
Log:
Next uid reserve get now done when saving the Message (IMAP-186)

Removed:
    james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRUidConsumer.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAUidConsumer.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/UidConsumer.java
Modified:
    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/JCRMessageManager.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/JCRMessageMapper.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/model/JCRMailbox.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/model/JCRMessage.java
    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/JPAMessageManager.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMessageMapper.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/JPAMailbox.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/AbstractJPAMailboxMembership.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/JPAMailboxMembership.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/JPAStreamingMailboxMembership.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/openjpa/OpenJPAMailboxManager.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/openjpa/OpenJPAMessageManager.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/InMemoryStoreMessageManager.java
    james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/mail/InMemoryMessageMapper.java
    james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/mail/model/SimpleMailboxMembership.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/StoreMessageManager.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/mail/model/Mailbox.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/model/MailboxMembership.java

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=983076&r1=983075&r2=983076&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 Fri Aug  6 18:02:34 2010
@@ -30,7 +30,6 @@ import org.apache.james.imap.store.Authe
 import org.apache.james.imap.store.StoreMailboxManager;
 import org.apache.james.imap.store.StoreMessageManager;
 import org.apache.james.imap.store.Subscriber;
-import org.apache.james.imap.store.UidConsumer;
 import org.apache.james.imap.store.mail.model.Mailbox;
 import org.apache.james.imap.store.transaction.TransactionalMapper;
 
@@ -44,18 +43,18 @@ public class JCRMailboxManager extends S
     private final Log logger = LogFactory.getLog(JCRMailboxManager.class);
     
     public JCRMailboxManager(JCRMailboxSessionMapperFactory mapperFactory, final Authenticator authenticator, final Subscriber subscriber) {
-	    this(mapperFactory, authenticator, subscriber, new JCRVmNodeLocker(), new JCRUidConsumer(mapperFactory.getRepository(),  new JCRVmNodeLocker()));
+	    this(mapperFactory, authenticator, subscriber, new JCRVmNodeLocker());
     }
 
-    public JCRMailboxManager(JCRMailboxSessionMapperFactory mapperFactory, final Authenticator authenticator, final Subscriber subscriber, final NodeLocker locker, final UidConsumer<String> consumer) {
-        super(mapperFactory, authenticator, subscriber, consumer);
+    public JCRMailboxManager(JCRMailboxSessionMapperFactory mapperFactory, final Authenticator authenticator, final Subscriber subscriber, final NodeLocker locker) {
+        super(mapperFactory, authenticator, subscriber);
         this.mapperFactory = mapperFactory;
     }
 
     
     @Override
-    protected StoreMessageManager<String> createMessageManager(MailboxEventDispatcher dispatcher, UidConsumer<String> consumer, Mailbox<String> mailboxEntity, MailboxSession session) throws MailboxException{
-        return new JCRMessageManager(mapperFactory, dispatcher, consumer, (JCRMailbox) mailboxEntity, logger, getDelimiter(), session);
+    protected StoreMessageManager<String> createMessageManager(MailboxEventDispatcher dispatcher, Mailbox<String> mailboxEntity, MailboxSession session) throws MailboxException{
+        return new JCRMessageManager(mapperFactory, dispatcher, (JCRMailbox) mailboxEntity, logger, getDelimiter(), session);
     }
 
     @Override

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMessageManager.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMessageManager.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/JCRMessageManager.java Fri Aug  6 18:02:34 2010
@@ -33,7 +33,6 @@ import org.apache.james.imap.mailbox.Mai
 import org.apache.james.imap.mailbox.MailboxSession;
 import org.apache.james.imap.mailbox.util.MailboxEventDispatcher;
 import org.apache.james.imap.store.StoreMessageManager;
-import org.apache.james.imap.store.UidConsumer;
 import org.apache.james.imap.store.mail.model.Header;
 import org.apache.james.imap.store.mail.model.MailboxMembership;
 import org.apache.james.imap.store.mail.model.PropertyBuilder;
@@ -47,9 +46,8 @@ public class JCRMessageManager extends S
     private final Log log;
 
     public JCRMessageManager(JCRMailboxSessionMapperFactory mapperFactory,
-            final MailboxEventDispatcher dispatcher, UidConsumer<String> consumer,
-            final JCRMailbox mailbox, final Log log, final char delimiter, MailboxSession session) throws MailboxException {
-        super(mapperFactory, dispatcher, consumer, mailbox, session);
+            final MailboxEventDispatcher dispatcher, final JCRMailbox mailbox, final Log log, final char delimiter, MailboxSession session) throws MailboxException {
+        super(mapperFactory, dispatcher, mailbox, session);
         this.log = log;
     }
 
@@ -59,12 +57,12 @@ public class JCRMessageManager extends S
     }
 
     @Override
-    protected MailboxMembership<String> createMessage(Date internalDate, long uid, int size, int bodyStartOctet, InputStream document, Flags flags, List<Header> headers, PropertyBuilder propertyBuilder) {
+    protected MailboxMembership<String> createMessage(Date internalDate, int size, int bodyStartOctet, InputStream document, Flags flags, List<Header> headers, PropertyBuilder propertyBuilder) {
         final List<JCRHeader> jcrHeaders = new ArrayList<JCRHeader>(headers.size());
         for (Header header: headers) {
             jcrHeaders.add((JCRHeader) header);
         }
-        final MailboxMembership<String> message = new JCRMessage(mailbox.getMailboxId(), uid, internalDate, 
+        final MailboxMembership<String> message = new JCRMessage(mailbox.getMailboxId(), internalDate, 
                 size, flags, document, bodyStartOctet, jcrHeaders, propertyBuilder, log);
         return message;
     }

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/JCRMessageMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/JCRMessageMapper.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/JCRMessageMapper.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/JCRMessageMapper.java Fri Aug  6 18:02:34 2010
@@ -27,6 +27,7 @@ import java.util.List;
 import javax.jcr.ItemNotFoundException;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
+import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
@@ -41,6 +42,7 @@ import org.apache.james.imap.jcr.Abstrac
 import org.apache.james.imap.jcr.MailboxSessionJCRRepository;
 import org.apache.james.imap.jcr.NodeLocker;
 import org.apache.james.imap.jcr.NodeLocker.NodeLockedExecution;
+import org.apache.james.imap.jcr.mail.model.JCRMailbox;
 import org.apache.james.imap.jcr.mail.model.JCRMessage;
 import org.apache.james.imap.mailbox.MailboxSession;
 import org.apache.james.imap.mailbox.MessageRange;
@@ -467,12 +469,9 @@ public class JCRMessageMapper extends Ab
 
     /*
      * (non-Javadoc)
-     * 
-     * @see
-     * org.apache.james.imap.store.mail.MessageMapper#save(org.apache.james.
-     * imap.store.mail.model.MailboxMembership)
+     * @see org.apache.james.imap.store.mail.MessageMapper#save(org.apache.james.imap.store.mail.model.Mailbox, org.apache.james.imap.store.mail.model.MailboxMembership)
      */
-    public void save(Mailbox<String> mailbox, MailboxMembership<String> message) throws StorageException {
+    public long save(Mailbox<String> mailbox, MailboxMembership<String> message) throws StorageException {
         final JCRMessage membership = (JCRMessage) message;
         try {
 
@@ -501,6 +500,8 @@ public class JCRMessageMapper extends Ab
                 Node node = null;
                 Node mailboxNode = getSession().getNodeByIdentifier(mailbox.getMailboxId());
 
+                final long nextUid = reserveNextUid((JCRMailbox) mailbox);
+                
                 NodeLocker locker = getNodeLocker();
 
                 if (scaleType > MESSAGE_SCALE_NONE) {
@@ -555,10 +556,12 @@ public class JCRMessageMapper extends Ab
                 locker.execute(new NodeLockedExecution<Void>() {
 
                     public Void execute(Node node) throws RepositoryException {
-                        Node messageNode = node.addNode(String.valueOf(membership.getUid()), "nt:file");
+                        Node messageNode = node.addNode(String.valueOf(nextUid), "nt:file");
                         messageNode.addMixin("jamesMailbox:message");
                         try {
                             membership.merge(messageNode);
+                            messageNode.setProperty(JCRMessage.UID_PROPERTY, nextUid);
+
                         } catch (IOException e) {
                             throw new RepositoryException("Unable to merge message in to tree", e);
                         }
@@ -572,9 +575,11 @@ public class JCRMessageMapper extends Ab
                         return true;
                     }
                 }, node, Void.class);
+                return nextUid;
 
             } else {
                 membership.merge(messageNode);
+                return membership.getUid();
             }
         } catch (RepositoryException e) {
             throw new StorageException(HumanReadableText.SAVE_FAILED, e);
@@ -685,8 +690,9 @@ public class JCRMessageMapper extends Ab
      * (non-Javadoc)
      * @see org.apache.james.imap.store.mail.MessageMapper#copy(java.lang.Object, long, org.apache.james.imap.store.mail.model.MailboxMembership)
      */
-    public MailboxMembership<String> copy(Mailbox<String> mailbox, long uid, MailboxMembership<String> oldmessage) throws StorageException{
+    public MailboxMembership<String> copy(Mailbox<String> mailbox, MailboxMembership<String> oldmessage) throws StorageException{
         try {
+            long uid = reserveNextUid((JCRMailbox) mailbox);
             String newMessagePath = getSession().getNodeByIdentifier(mailbox.getMailboxId()).getPath() + NODE_DELIMITER + String.valueOf(uid);
             getSession().getWorkspace().copy(((JCRMessage)oldmessage).getNode().getPath(), getSession().getNodeByIdentifier(mailbox.getMailboxId()).getPath() + NODE_DELIMITER + String.valueOf(uid));
             Node node = getSession().getNode(newMessagePath);
@@ -695,7 +701,30 @@ public class JCRMessageMapper extends Ab
             return new JCRMessage(node,getLogger());
         } catch (RepositoryException e) {
             throw new StorageException(HumanReadableText.SAVE_FAILED, e);
+        } catch (InterruptedException e) {
+            throw new StorageException(HumanReadableText.SAVE_FAILED, e);
         }
     }
+    
+    protected long reserveNextUid(Mailbox<String> mailbox) throws RepositoryException, InterruptedException {
+        long result = getNodeLocker().execute(new NodeLocker.NodeLockedExecution<Long>() {
+
+            public Long execute(Node node) throws RepositoryException {
+                Property uidProp = node.getProperty(JCRMailbox.LASTUID_PROPERTY);
+                long uid = uidProp.getLong();
+                uid++;
+                uidProp.setValue(uid);
+                uidProp.getSession().save();
+                return uid;
+            }
+
+            public boolean isDeepLocked() {
+                return true;
+            }
+            
+        }, getSession().getNodeByIdentifier(mailbox.getMailboxId()), Long.class);
+        return result;
+        
+    }
 
 }

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/model/JCRMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/model/JCRMailbox.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/model/JCRMailbox.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/model/JCRMailbox.java Fri Aug  6 18:02:34 2010
@@ -75,24 +75,6 @@ public class JCRMailbox implements Mailb
     public Log getLog() {
         return logger;
     }
-    
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.imap.store.mail.model.Mailbox#consumeUid()
-     */
-    public void consumeUid() {
-        if (isPersistent()) {
-            try {
-                long uid = node.getProperty(LASTUID_PROPERTY).getLong();
-                uid++;
-                node.setProperty(LASTUID_PROPERTY, uid);
-            } catch (RepositoryException e) {
-                logger.error("Unable to access property " + LASTUID_PROPERTY, e);
-            }
-        } else {
-            lastUid++;
-        }
-    }
 
     /*
      * (non-Javadoc)
@@ -311,5 +293,4 @@ public class JCRMailbox implements Mailb
             this.user = user;
         }        
     }
-
 }

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/model/JCRMessage.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/model/JCRMessage.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/model/JCRMessage.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/imap/jcr/mail/model/JCRMessage.java Fri Aug  6 18:02:34 2010
@@ -99,13 +99,11 @@ public class JCRMessage extends Abstract
         this.node = node;
     }
     
-    public JCRMessage(String mailboxUUID, long uid,
-            Date internalDate, int size, Flags flags, InputStream content,
+    public JCRMessage(String mailboxUUID, Date internalDate, int size, Flags flags, InputStream content,
             int bodyStartOctet, final List<JCRHeader> headers,
             final PropertyBuilder propertyBuilder, Log logger) {
         super();
         this.mailboxUUID = mailboxUUID;
-        this.uid = uid;
         this.internalDate = internalDate;
         this.size = size;
         this.logger = logger;
@@ -134,9 +132,8 @@ public class JCRMessage extends Abstract
      * @param message
      * @throws IOException 
      */
-    public JCRMessage(String mailboxUUID, long uid, JCRMessage message, Log logger) throws MailboxException {
+    public JCRMessage(String mailboxUUID, JCRMessage message, Log logger) throws MailboxException {
         this.mailboxUUID = mailboxUUID;
-        this.uid = uid;
         this.internalDate = message.getInternalDate();
         this.size = message.getFullContentOctets();
         this.answered = message.isAnswered();

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=983076&r1=983075&r2=983076&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 Fri Aug  6 18:02:34 2010
@@ -36,7 +36,7 @@ public abstract class JPAMailboxManager 
     
     public JPAMailboxManager(JPAMailboxSessionMapperFactory mailboxSessionMapperFactory,
             final Authenticator authenticator, final Subscriber subscriber) {
-        super(mailboxSessionMapperFactory, authenticator, subscriber, new JPAUidConsumer(mailboxSessionMapperFactory));
+        super(mailboxSessionMapperFactory, authenticator, subscriber);
     }
     
     @Override

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMessageManager.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMessageManager.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMessageManager.java Fri Aug  6 18:02:34 2010
@@ -31,7 +31,6 @@ import org.apache.james.imap.mailbox.Mai
 import org.apache.james.imap.mailbox.MailboxSession;
 import org.apache.james.imap.mailbox.util.MailboxEventDispatcher;
 import org.apache.james.imap.store.StoreMessageManager;
-import org.apache.james.imap.store.UidConsumer;
 import org.apache.james.imap.store.mail.model.Header;
 import org.apache.james.imap.store.mail.model.Mailbox;
 import org.apache.james.imap.store.mail.model.MailboxMembership;
@@ -43,19 +42,18 @@ import org.apache.james.imap.store.mail.
 public class JPAMessageManager extends StoreMessageManager<Long> {
     
     public JPAMessageManager(JPAMailboxSessionMapperFactory mapperFactory,
-            final MailboxEventDispatcher dispatcher, final UidConsumer<Long> consumer,
-            final Mailbox<Long> mailbox, MailboxSession session) throws MailboxException {
-        super(mapperFactory, dispatcher, consumer, mailbox, session);     
+            final MailboxEventDispatcher dispatcher,final Mailbox<Long> mailbox, MailboxSession session) throws MailboxException {
+        super(mapperFactory, dispatcher, mailbox, session);     
     }
     
     @Override
-    protected MailboxMembership<Long> createMessage(Date internalDate, final long uid, final int size, int bodyStartOctet, final InputStream document, 
+    protected MailboxMembership<Long> createMessage(Date internalDate, final int size, int bodyStartOctet, final InputStream document, 
             final Flags flags, final List<Header> headers, PropertyBuilder propertyBuilder) throws MailboxException{
         final List<JPAHeader> jpaHeaders = new ArrayList<JPAHeader>(headers.size());
         for (Header header: headers) {
             jpaHeaders.add((JPAHeader) header);
         }
-        final MailboxMembership<Long> message = new JPAMailboxMembership(mailbox.getMailboxId(), uid, internalDate, size, flags, document, bodyStartOctet, jpaHeaders, propertyBuilder);
+        final MailboxMembership<Long> message = new JPAMailboxMembership(mailbox.getMailboxId(), internalDate, size, flags, document, bodyStartOctet, jpaHeaders, propertyBuilder);
         return message;
     }
 

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMessageMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMessageMapper.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMessageMapper.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMessageMapper.java Fri Aug  6 18:02:34 2010
@@ -20,12 +20,15 @@ package org.apache.james.imap.jpa.mail;
 
 import java.util.List;
 
+import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
+import javax.persistence.LockModeType;
 import javax.persistence.PersistenceException;
 import javax.persistence.Query;
 
 import org.apache.james.imap.api.display.HumanReadableText;
 import org.apache.james.imap.jpa.JPATransactionalMapper;
+import org.apache.james.imap.jpa.mail.model.JPAMailbox;
 import org.apache.james.imap.jpa.mail.model.openjpa.AbstractJPAMailboxMembership;
 import org.apache.james.imap.jpa.mail.model.openjpa.JPAMailboxMembership;
 import org.apache.james.imap.jpa.mail.model.openjpa.JPAStreamingMailboxMembership;
@@ -291,9 +294,17 @@ public class JPAMessageMapper extends JP
     /**
      * @see org.apache.james.imap.store.mail.MessageMapper#save(MailboxMembership)
      */
-    public void save(Mailbox<Long> mailbox, MailboxMembership<Long> message) throws StorageException {
+    public long save(Mailbox<Long> mailbox, MailboxMembership<Long> message) throws StorageException {
         try {
+            
+            if (message.getUid() == 0) {
+                
+                ((AbstractJPAMailboxMembership) message).setUid(reserveUid((JPAMailbox)mailbox));
+            }
+            
+            
             getEntityManager().persist(message);
+            return message.getUid();
         } catch (PersistenceException e) {
             throw new StorageException(HumanReadableText.SAVE_FAILED, e);
         }
@@ -303,13 +314,13 @@ public class JPAMessageMapper extends JP
      * (non-Javadoc)
      * @see org.apache.james.imap.store.mail.MessageMapper#copy(java.lang.Object, long, org.apache.james.imap.store.mail.model.MailboxMembership)
      */
-    public MailboxMembership<Long> copy(Mailbox<Long> mailbox, long uid, MailboxMembership<Long> original) throws StorageException {
+    public MailboxMembership<Long> copy(Mailbox<Long> mailbox, MailboxMembership<Long> original) throws StorageException {
         try {
             MailboxMembership<Long> copy;
             if (original instanceof JPAStreamingMailboxMembership) {
-                copy = new JPAStreamingMailboxMembership(mailbox.getMailboxId(), uid, (AbstractJPAMailboxMembership) original);
+                copy = new JPAStreamingMailboxMembership(mailbox.getMailboxId(), (AbstractJPAMailboxMembership) original);
             } else {
-                copy = new JPAMailboxMembership(mailbox.getMailboxId(), uid, (AbstractJPAMailboxMembership)original);
+                copy = new JPAMailboxMembership(mailbox.getMailboxId(), (AbstractJPAMailboxMembership)original);
             }
             save(mailbox, copy);
             return copy;
@@ -317,4 +328,22 @@ public class JPAMessageMapper extends JP
             throw new StorageException(e.getKey(),e);
         }
     }
+    
+    /**
+     * Reserve the uid for the next {@link MailboxMembership} in the given {@link JPAMailbox}.
+     * This is done by using a row lock.
+     * 
+     * @param mailbox
+     * @return uid
+     */
+    protected long reserveUid(JPAMailbox mailbox) {
+        EntityManager manager = getEntityManager();
+
+        // we need to set a pessimistic write lock to be sure we don't get any problems with dirty reads etc.
+        JPAMailbox m = manager.find(JPAMailbox.class, mailbox.getMailboxId(), LockModeType.PESSIMISTIC_WRITE);
+        manager.refresh(m);
+        m.consumeUid();
+        manager.persist(m);
+        return m.getLastUid();
+    }
 }

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/JPAMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/JPAMailbox.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/JPAMailbox.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/JPAMailbox.java Fri Aug  6 18:02:34 2010
@@ -193,4 +193,8 @@ public class JPAMailbox implements Mailb
     public void setUser(String user) {
         this.user = user;
     }
+
+    public void setLastUid(long uid) {
+        this.lastUid = uid;
+    }
 }

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/AbstractJPAMailboxMembership.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/AbstractJPAMailboxMembership.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/AbstractJPAMailboxMembership.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/AbstractJPAMailboxMembership.java Fri Aug  6 18:02:34 2010
@@ -142,10 +142,9 @@ public abstract class AbstractJPAMailbox
     @Deprecated
     public AbstractJPAMailboxMembership() {}
 
-    public AbstractJPAMailboxMembership(long mailboxId, long uid, Date internalDate, Flags flags, int bodyStartOctet, final List<JPAHeader> headers, final PropertyBuilder propertyBuilder) throws MailboxException {
+    public AbstractJPAMailboxMembership(long mailboxId, Date internalDate, Flags flags, int bodyStartOctet, final List<JPAHeader> headers, final PropertyBuilder propertyBuilder) throws MailboxException {
         super();
         this.mailboxId = mailboxId;
-        this.uid = uid;
         this.internalDate = internalDate;
        
         setFlags(flags);
@@ -159,10 +158,9 @@ public abstract class AbstractJPAMailbox
      * @param original message to be copied, not null
      * @throws IOException 
      */
-    public AbstractJPAMailboxMembership(long mailboxId, long uid, MailboxMembership<?> original) throws MailboxException {
+    public AbstractJPAMailboxMembership(long mailboxId, MailboxMembership<?> original) throws MailboxException {
         super();
         this.mailboxId = mailboxId;
-        this.uid = uid;
         this.internalDate = original.getInternalDate();
         this.answered = original.isAnswered();
         this.deleted = original.isDeleted();
@@ -299,4 +297,8 @@ public abstract class AbstractJPAMailbox
 
         return retValue;
     }
+    
+    public void setUid(long uid) {
+        this.uid = uid;
+    }
 }

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/JPAMailboxMembership.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/JPAMailboxMembership.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/JPAMailboxMembership.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/JPAMailboxMembership.java Fri Aug  6 18:02:34 2010
@@ -1,80 +1,80 @@
-/****************************************************************
- * 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.imap.jpa.mail.model.openjpa;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Date;
-import java.util.List;
-
-import javax.mail.Flags;
-import javax.persistence.CascadeType;
-import javax.persistence.Entity;
-import javax.persistence.FetchType;
-import javax.persistence.ManyToOne;
-
-import org.apache.james.imap.api.display.HumanReadableText;
-import org.apache.james.imap.jpa.mail.model.JPAHeader;
-import org.apache.james.imap.mailbox.MailboxException;
-import org.apache.james.imap.store.mail.model.Document;
-import org.apache.james.imap.store.mail.model.PropertyBuilder;
-
-@Entity(name="Membership")
-public class JPAMailboxMembership extends AbstractJPAMailboxMembership{
-
-
-    /** The value for the body field. Lazy loaded */
-    @ManyToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY) private JPAMessage message;
-  
-    
-    /**
-     * For enhancement only.
-     */
-    @Deprecated
-    public JPAMailboxMembership() {}
-
-    public JPAMailboxMembership(long mailboxId, long uid, Date internalDate, int size, Flags flags, 
-            InputStream content, int bodyStartOctet, final List<JPAHeader> headers, final PropertyBuilder propertyBuilder) throws MailboxException {
-        super(mailboxId, uid, internalDate, flags, bodyStartOctet, headers, propertyBuilder);  
-        try {
-            this.message = new JPAMessage(content, size, bodyStartOctet, headers, propertyBuilder);
-        } catch (IOException e) {
-            throw new MailboxException(HumanReadableText.FAILURE_MAIL_PARSE,e);
-        }
-    }
-
-    public JPAMailboxMembership(long mailboxId, long uid, AbstractJPAMailboxMembership original) throws MailboxException {
-        super(mailboxId, uid, original);
-        try {
-            this.message = new JPAMessage((JPAMessage) original.getDocument());
-        } catch (IOException e) {
-            throw new MailboxException(HumanReadableText.FAILURE_MAIL_PARSE,e);
-        }
-    }
-    
-    /**
-     * Gets the message member.
-     * @return message, not null
-     */
-    public Document getDocument() {
-        return message;
-    }
-    
-
-}
+/****************************************************************
+ * 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.imap.jpa.mail.model.openjpa;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Date;
+import java.util.List;
+
+import javax.mail.Flags;
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.ManyToOne;
+
+import org.apache.james.imap.api.display.HumanReadableText;
+import org.apache.james.imap.jpa.mail.model.JPAHeader;
+import org.apache.james.imap.mailbox.MailboxException;
+import org.apache.james.imap.store.mail.model.Document;
+import org.apache.james.imap.store.mail.model.PropertyBuilder;
+
+@Entity(name="Membership")
+public class JPAMailboxMembership extends AbstractJPAMailboxMembership{
+
+
+    /** The value for the body field. Lazy loaded */
+    @ManyToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY) private JPAMessage message;
+  
+    
+    /**
+     * For enhancement only.
+     */
+    @Deprecated
+    public JPAMailboxMembership() {}
+
+    public JPAMailboxMembership(long mailboxId, Date internalDate, int size, Flags flags, 
+            InputStream content, int bodyStartOctet, final List<JPAHeader> headers, final PropertyBuilder propertyBuilder) throws MailboxException {
+        super(mailboxId, internalDate, flags, bodyStartOctet, headers, propertyBuilder);  
+        try {
+            this.message = new JPAMessage(content, size, bodyStartOctet, headers, propertyBuilder);
+        } catch (IOException e) {
+            throw new MailboxException(HumanReadableText.FAILURE_MAIL_PARSE,e);
+        }
+    }
+
+    public JPAMailboxMembership(long mailboxId, AbstractJPAMailboxMembership original) throws MailboxException {
+        super(mailboxId, original);
+        try {
+            this.message = new JPAMessage((JPAMessage) original.getDocument());
+        } catch (IOException e) {
+            throw new MailboxException(HumanReadableText.FAILURE_MAIL_PARSE,e);
+        }
+    }
+    
+    /**
+     * Gets the message member.
+     * @return message, not null
+     */
+    public Document getDocument() {
+        return message;
+    }
+    
+
+}

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/JPAStreamingMailboxMembership.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/JPAStreamingMailboxMembership.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/JPAStreamingMailboxMembership.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/openjpa/JPAStreamingMailboxMembership.java Fri Aug  6 18:02:34 2010
@@ -1,79 +1,79 @@
-/****************************************************************
- * 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.imap.jpa.mail.model.openjpa;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Date;
-import java.util.List;
-
-import javax.mail.Flags;
-import javax.persistence.CascadeType;
-import javax.persistence.Entity;
-import javax.persistence.FetchType;
-import javax.persistence.ManyToOne;
-
-import org.apache.james.imap.api.display.HumanReadableText;
-import org.apache.james.imap.jpa.mail.model.JPAHeader;
-import org.apache.james.imap.mailbox.MailboxException;
-import org.apache.james.imap.store.mail.model.Document;
-import org.apache.james.imap.store.mail.model.MailboxMembership;
-import org.apache.james.imap.store.mail.model.PropertyBuilder;
-
-@Entity(name="Membership")
-public class JPAStreamingMailboxMembership extends AbstractJPAMailboxMembership{
-
-
-    /** The value for the body field. Lazy loaded */
-    @ManyToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY) private JPAStreamingMessage message;
-  
-    
-    /**
-     * For enhancement only.
-     */
-    @Deprecated
-    public JPAStreamingMailboxMembership() {}
-
-    public JPAStreamingMailboxMembership(long mailboxId, long uid, Date internalDate, int size, Flags flags, 
-            InputStream content, int bodyStartOctet, final List<JPAHeader> headers, final PropertyBuilder propertyBuilder) throws MailboxException {
-        super(mailboxId, uid, internalDate, flags, bodyStartOctet, headers, propertyBuilder);  
-        this.message = new JPAStreamingMessage(content, size, bodyStartOctet, headers, propertyBuilder);
-        
-       
-    }
-
-    public JPAStreamingMailboxMembership(long mailboxId, long uid, MailboxMembership<?> original) throws MailboxException {
-        super(mailboxId, uid, original);
-        try {
-            this.message = new JPAStreamingMessage(original.getDocument());
-        } catch (IOException e) {
-            throw new MailboxException(HumanReadableText.FAILURE_MAIL_PARSE,e);
-        }
-    }
-    
-    /**
-     * Gets the message member.
-     * @return message, not null
-     */
-    public Document getDocument() {
-        return message;
-    }
-    
-
-}
+/****************************************************************
+ * 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.imap.jpa.mail.model.openjpa;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Date;
+import java.util.List;
+
+import javax.mail.Flags;
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.ManyToOne;
+
+import org.apache.james.imap.api.display.HumanReadableText;
+import org.apache.james.imap.jpa.mail.model.JPAHeader;
+import org.apache.james.imap.mailbox.MailboxException;
+import org.apache.james.imap.store.mail.model.Document;
+import org.apache.james.imap.store.mail.model.MailboxMembership;
+import org.apache.james.imap.store.mail.model.PropertyBuilder;
+
+@Entity(name="Membership")
+public class JPAStreamingMailboxMembership extends AbstractJPAMailboxMembership{
+
+
+    /** The value for the body field. Lazy loaded */
+    @ManyToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY) private JPAStreamingMessage message;
+  
+    
+    /**
+     * For enhancement only.
+     */
+    @Deprecated
+    public JPAStreamingMailboxMembership() {}
+
+    public JPAStreamingMailboxMembership(long mailboxId, Date internalDate, int size, Flags flags, 
+            InputStream content, int bodyStartOctet, final List<JPAHeader> headers, final PropertyBuilder propertyBuilder) throws MailboxException {
+        super(mailboxId, internalDate, flags, bodyStartOctet, headers, propertyBuilder);  
+        this.message = new JPAStreamingMessage(content, size, bodyStartOctet, headers, propertyBuilder);
+        
+       
+    }
+
+    public JPAStreamingMailboxMembership(long mailboxId, MailboxMembership<?> original) throws MailboxException {
+        super(mailboxId, original);
+        try {
+            this.message = new JPAStreamingMessage(original.getDocument());
+        } catch (IOException e) {
+            throw new MailboxException(HumanReadableText.FAILURE_MAIL_PARSE,e);
+        }
+    }
+    
+    /**
+     * Gets the message member.
+     * @return message, not null
+     */
+    public Document getDocument() {
+        return message;
+    }
+    
+
+}

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/openjpa/OpenJPAMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/openjpa/OpenJPAMailboxManager.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/openjpa/OpenJPAMailboxManager.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/openjpa/OpenJPAMailboxManager.java Fri Aug  6 18:02:34 2010
@@ -28,7 +28,6 @@ import org.apache.james.imap.mailbox.uti
 import org.apache.james.imap.store.Authenticator;
 import org.apache.james.imap.store.StoreMessageManager;
 import org.apache.james.imap.store.Subscriber;
-import org.apache.james.imap.store.UidConsumer;
 import org.apache.james.imap.store.mail.model.Mailbox;
 
 /**
@@ -49,8 +48,8 @@ public class OpenJPAMailboxManager exten
     }
 
     @Override
-    protected StoreMessageManager<Long> createMessageManager(MailboxEventDispatcher dispatcher, UidConsumer<Long> consumer, Mailbox<Long> mailboxRow, MailboxSession session) throws MailboxException {
-        StoreMessageManager<Long> result =  new OpenJPAMessageManager((JPAMailboxSessionMapperFactory) mailboxSessionMapperFactory, dispatcher, consumer, mailboxRow, session, useStreaming);
+    protected StoreMessageManager<Long> createMessageManager(MailboxEventDispatcher dispatcher, Mailbox<Long> mailboxRow, MailboxSession session) throws MailboxException {
+        StoreMessageManager<Long> result =  new OpenJPAMessageManager((JPAMailboxSessionMapperFactory) mailboxSessionMapperFactory, dispatcher, mailboxRow, session, useStreaming);
         return result;
     }
 }

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/openjpa/OpenJPAMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/openjpa/OpenJPAMessageManager.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/openjpa/OpenJPAMessageManager.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/openjpa/OpenJPAMessageManager.java Fri Aug  6 18:02:34 2010
@@ -33,7 +33,6 @@ import org.apache.james.imap.jpa.mail.mo
 import org.apache.james.imap.mailbox.MailboxException;
 import org.apache.james.imap.mailbox.MailboxSession;
 import org.apache.james.imap.mailbox.util.MailboxEventDispatcher;
-import org.apache.james.imap.store.UidConsumer;
 import org.apache.james.imap.store.mail.model.Header;
 import org.apache.james.imap.store.mail.model.Mailbox;
 import org.apache.james.imap.store.mail.model.MailboxMembership;
@@ -48,28 +47,26 @@ public class OpenJPAMessageManager exten
     private final boolean useStreaming;
 
     public OpenJPAMessageManager(JPAMailboxSessionMapperFactory mapperFactory,
-            MailboxEventDispatcher dispatcher, UidConsumer<Long> consumer,
-            Mailbox<Long> mailbox, MailboxSession session) throws MailboxException {
-        this(mapperFactory, dispatcher, consumer, mailbox, session, false);
+            MailboxEventDispatcher dispatcher, Mailbox<Long> mailbox, MailboxSession session) throws MailboxException {
+        this(mapperFactory, dispatcher, mailbox, session, false);
     }
 
     public OpenJPAMessageManager(JPAMailboxSessionMapperFactory mapperFactory,
-            MailboxEventDispatcher dispatcher, UidConsumer<Long> consumer,
-            Mailbox<Long> mailbox, MailboxSession session, final boolean useStreaming) throws MailboxException {
-        super(mapperFactory, dispatcher, consumer, mailbox, session);
+            MailboxEventDispatcher dispatcher, Mailbox<Long> mailbox, MailboxSession session, final boolean useStreaming) throws MailboxException {
+        super(mapperFactory, dispatcher, mailbox, session);
         this.useStreaming = useStreaming;
     }
 
     @Override
-    protected MailboxMembership<Long> createMessage(Date internalDate, long uid, int size, int bodyStartOctet, InputStream document, Flags flags, List<Header> headers, PropertyBuilder propertyBuilder) throws MailboxException {
+    protected MailboxMembership<Long> createMessage(Date internalDate, int size, int bodyStartOctet, InputStream document, Flags flags, List<Header> headers, PropertyBuilder propertyBuilder) throws MailboxException {
         if (useStreaming) {
             final List<JPAHeader> jpaHeaders = new ArrayList<JPAHeader>(headers.size());
             for (Header header: headers) {
                 jpaHeaders.add((JPAHeader) header);
             }
-            return new JPAStreamingMailboxMembership(mailbox.getMailboxId(), uid, internalDate, size, flags, document, bodyStartOctet, jpaHeaders, propertyBuilder);
+            return new JPAStreamingMailboxMembership(mailbox.getMailboxId(), internalDate, size, flags, document, bodyStartOctet, jpaHeaders, propertyBuilder);
         } else {
-            return super.createMessage(internalDate, uid, size, bodyStartOctet, document, flags, headers, propertyBuilder);
+            return super.createMessage(internalDate, size, bodyStartOctet, document, flags, headers, propertyBuilder);
         }
     }
 

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=983076&r1=983075&r2=983076&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 Fri Aug  6 18:02:34 2010
@@ -30,24 +30,17 @@ import org.apache.james.imap.store.Mailb
 import org.apache.james.imap.store.StoreMailboxManager;
 import org.apache.james.imap.store.StoreMessageManager;
 import org.apache.james.imap.store.Subscriber;
-import org.apache.james.imap.store.UidConsumer;
 import org.apache.james.imap.store.mail.model.Mailbox;
 
 public class InMemoryMailboxManager extends StoreMailboxManager<Long> {
 
     public InMemoryMailboxManager(MailboxSessionMapperFactory<Long> mapperFactory, Authenticator authenticator, Subscriber subscriber) {
-        super(mapperFactory, authenticator, subscriber, new UidConsumer<Long>() {
-
-            public long reserveNextUid(Mailbox<Long> mailbox, MailboxSession session) throws MailboxException {
-                mailbox.consumeUid();
-                return mailbox.getLastUid();
-            }
-        });
+        super(mapperFactory, authenticator, subscriber);
     }
 
     @Override
-    protected StoreMessageManager<Long> createMessageManager(MailboxEventDispatcher dispatcher, UidConsumer<Long> consumer, Mailbox<Long> mailboxRow, MailboxSession session) throws MailboxException {
-        return new InMemoryStoreMessageManager(mailboxSessionMapperFactory, dispatcher, consumer, (InMemoryMailbox)mailboxRow, session);
+    protected StoreMessageManager<Long> createMessageManager(MailboxEventDispatcher dispatcher, Mailbox<Long> mailboxRow, MailboxSession session) throws MailboxException {
+        return new InMemoryStoreMessageManager(mailboxSessionMapperFactory, dispatcher, (InMemoryMailbox)mailboxRow, session);
     }
 
     @Override

Modified: james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMessageManager.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMessageManager.java (original)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMessageManager.java Fri Aug  6 18:02:34 2010
@@ -34,7 +34,6 @@ import org.apache.james.imap.mailbox.Mai
 import org.apache.james.imap.mailbox.util.MailboxEventDispatcher;
 import org.apache.james.imap.store.MailboxSessionMapperFactory;
 import org.apache.james.imap.store.StoreMessageManager;
-import org.apache.james.imap.store.UidConsumer;
 import org.apache.james.imap.store.mail.model.Header;
 import org.apache.james.imap.store.mail.model.MailboxMembership;
 import org.apache.james.imap.store.mail.model.PropertyBuilder;
@@ -42,9 +41,9 @@ import org.apache.james.imap.store.mail.
 public class InMemoryStoreMessageManager extends StoreMessageManager<Long> {
 
     public InMemoryStoreMessageManager(MailboxSessionMapperFactory<Long> mapperFactory,
-            MailboxEventDispatcher dispatcher, UidConsumer<Long> consumer, InMemoryMailbox mailbox,
+            MailboxEventDispatcher dispatcher, InMemoryMailbox mailbox,
             MailboxSession session) throws MailboxException {
-        super(mapperFactory, dispatcher, consumer, mailbox, session);
+        super(mapperFactory, dispatcher,mailbox, session);
     }
     
     @Override
@@ -53,7 +52,7 @@ public class InMemoryStoreMessageManager
     }
 
     @Override
-    protected MailboxMembership<Long> createMessage(Date internalDate, long uid, int size, int bodyStartOctet, 
+    protected MailboxMembership<Long> createMessage(Date internalDate, int size, int bodyStartOctet, 
             InputStream  document, Flags flags, List<Header> headers, PropertyBuilder propertyBuilder) {
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         byte[] byteContent;
@@ -71,7 +70,8 @@ public class InMemoryStoreMessageManager
             e.printStackTrace();
             byteContent = new byte[0];
         }
-        return new SimpleMailboxMembership(internalDate, uid, size, bodyStartOctet, byteContent, flags, headers, propertyBuilder, mailbox.getMailboxId());
+        ((InMemoryMailbox) mailbox).consumeUid();
+        return new SimpleMailboxMembership(internalDate, mailbox.getLastUid(), size, bodyStartOctet, byteContent, flags, headers, propertyBuilder, mailbox.getMailboxId());
     }
     
 }

Modified: james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/mail/InMemoryMessageMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/mail/InMemoryMessageMapper.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/mail/InMemoryMessageMapper.java (original)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/mail/InMemoryMessageMapper.java Fri Aug  6 18:02:34 2010
@@ -7,6 +7,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.james.imap.inmemory.mail.model.InMemoryMailbox;
 import org.apache.james.imap.inmemory.mail.model.SimpleMailboxMembership;
 import org.apache.james.imap.mailbox.MailboxException;
 import org.apache.james.imap.mailbox.MessageRange;
@@ -163,8 +164,9 @@ public class InMemoryMessageMapper imple
      * (non-Javadoc)
      * @see org.apache.james.imap.store.mail.MessageMapper#save(org.apache.james.imap.store.mail.model.MailboxMembership)
      */
-    public void save(Mailbox<Long> mailbox, MailboxMembership<Long> message) throws StorageException {
+    public long save(Mailbox<Long> mailbox, MailboxMembership<Long> message) throws StorageException {
         getMembershipByUidForMailbox(mailbox).put(message.getUid(), message);
+        return message.getUid();
     }
 
     /*
@@ -193,12 +195,14 @@ public class InMemoryMessageMapper imple
         // Do nothing
     }
 
+  
     /*
      * (non-Javadoc)
-     * @see org.apache.james.imap.store.mail.MessageMapper#copy(java.lang.Object, long, org.apache.james.imap.store.mail.model.MailboxMembership)
+     * @see org.apache.james.imap.store.mail.MessageMapper#copy(org.apache.james.imap.store.mail.model.Mailbox, org.apache.james.imap.store.mail.model.MailboxMembership)
      */
-    public MailboxMembership<Long> copy(Mailbox<Long> mailbox, long uid, MailboxMembership<Long> original) throws StorageException {
-        SimpleMailboxMembership membership = new SimpleMailboxMembership(mailbox.getMailboxId(), uid, (SimpleMailboxMembership) original);
+    public MailboxMembership<Long> copy(Mailbox<Long> mailbox, MailboxMembership<Long> original) throws StorageException {
+        ((InMemoryMailbox) mailbox).consumeUid();
+        SimpleMailboxMembership membership = new SimpleMailboxMembership(mailbox.getMailboxId(), mailbox.getLastUid(), (SimpleMailboxMembership) original);
         save(mailbox, membership);
         return membership;
     }

Modified: james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/mail/model/SimpleMailboxMembership.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/mail/model/SimpleMailboxMembership.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/mail/model/SimpleMailboxMembership.java (original)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/mail/model/SimpleMailboxMembership.java Fri Aug  6 18:02:34 2010
@@ -54,7 +54,6 @@ public class SimpleMailboxMembership ext
     private Long lineCount;
     private byte[] document;
     private int bodyStartOctet;
-    
     public SimpleMailboxMembership(long mailboxId, long uid, final SimpleMailboxMembership original) {
         this.uid = uid;
         this.mailboxId = mailboxId;
@@ -75,7 +74,7 @@ public class SimpleMailboxMembership ext
         this.bodyStartOctet = original.bodyStartOctet;
     }
     
-    public SimpleMailboxMembership(Date internalDate, long uid, int size, int bodyStartOctet, byte[] document, 
+    public SimpleMailboxMembership(Date internalDate, long uid,  int size, int bodyStartOctet, byte[] document, 
             Flags flags, List<Header> headers, PropertyBuilder propertyBuilder, final long mailboxId) {
         this.uid = uid;
         this.document = document;

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=983076&r1=983075&r2=983076&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 Fri Aug  6 18:02:34 2010
@@ -59,13 +59,10 @@ public abstract class StoreMailboxManage
     private final MailboxEventDispatcher dispatcher = new MailboxEventDispatcher();
     private final DelegatingMailboxListener delegatingListener = new DelegatingMailboxListener();   
     private final MailboxPathLock lock = new MailboxPathLock();
-    protected final MailboxSessionMapperFactory<Id> mailboxSessionMapperFactory;
-    private UidConsumer<Id> consumer;
+    protected final MailboxSessionMapperFactory<Id> mailboxSessionMapperFactory;    
     
-    
-    public StoreMailboxManager(MailboxSessionMapperFactory<Id> mailboxSessionMapperFactory, final Authenticator authenticator, final Subscriber subscriber, final UidConsumer<Id> consumer) {
+    public StoreMailboxManager(MailboxSessionMapperFactory<Id> mailboxSessionMapperFactory, final Authenticator authenticator, final Subscriber subscriber) {
         super(authenticator, subscriber);
-        this.consumer = consumer;
         this.mailboxSessionMapperFactory = mailboxSessionMapperFactory;
         
         // The dispatcher need to have the delegating listener added
@@ -78,7 +75,7 @@ public abstract class StoreMailboxManage
      * @param mailboxRow
      * @return storeMailbox
      */
-    protected abstract StoreMessageManager<Id> createMessageManager(MailboxEventDispatcher dispatcher, UidConsumer<Id> consumer, Mailbox<Id> mailboxRow, MailboxSession session) throws MailboxException;
+    protected abstract StoreMessageManager<Id> createMessageManager(MailboxEventDispatcher dispatcher, Mailbox<Id> mailboxRow, MailboxSession session) throws MailboxException;
 
     /**
      * Create a Mailbox for the given namespace and store it to the underlying storage
@@ -104,7 +101,7 @@ public abstract class StoreMailboxManage
         } else {
             getLog().debug("Loaded mailbox " + mailboxPath);
 
-            return createMessageManager(dispatcher, consumer, mailboxRow, session);
+            return createMessageManager(dispatcher, mailboxRow, session);
         }
     }
 

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMessageManager.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMessageManager.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMessageManager.java Fri Aug  6 18:02:34 2010
@@ -84,13 +84,11 @@ public abstract class StoreMessageManage
     
     private MailboxEventDispatcher dispatcher;
 
-    private UidConsumer<Id> consumer;
     
     
-    public StoreMessageManager(MailboxSessionMapperFactory<Id> mapperFactory, final MailboxEventDispatcher dispatcher, UidConsumer<Id> consumer, final Mailbox<Id> mailbox, MailboxSession session) throws MailboxException {
+    public StoreMessageManager(MailboxSessionMapperFactory<Id> mapperFactory, final MailboxEventDispatcher dispatcher, final Mailbox<Id> mailbox, MailboxSession session) throws MailboxException {
         this.mailbox = mailbox;
         this.dispatcher = dispatcher;
-        this.consumer = consumer;
         this.messageMapper = mapperFactory.getMessageMapper(session);
     }
 
@@ -134,9 +132,6 @@ public abstract class StoreMessageManage
     public long appendMessage(final InputStream msgIn, final Date internalDate,
             final MailboxSession mailboxSession,final boolean isRecent, final Flags flagsToBeSet)
     throws MailboxException {
-        // this will hold the uid after the transaction was complete
-        final long uid = consumer.reserveNextUid(getMailboxEntity(), mailboxSession);
-        
         File file = null;
         SharedFileInputStream tmpMsgIn = null;
         try {
@@ -154,14 +149,7 @@ public abstract class StoreMessageManage
             out.close();
             
             tmpMsgIn = new SharedFileInputStream(file);
-            // To be thread safe, we first get our own copy and the
-            // exclusive
-            // Uid
-            // TODO create own message_id and assign uid later
-            // at the moment it could lead to the situation that uid 5
-            // is
-            // inserted long before 4, when
-            // mail 4 is big and comes over a slow connection.
+           
             final int size = tmpMsgIn.available();
             final int bodyStartOctet = bodyStartOctet(tmpMsgIn);
 
@@ -256,15 +244,15 @@ public abstract class StoreMessageManage
             if (isRecent) {
                 flags.add(Flags.Flag.RECENT);
             }
-            final MailboxMembership<Id> message = createMessage(internalDate, uid, size, bodyStartOctet, tmpMsgIn.newStream(0, -1), flags, headers, propertyBuilder);
-
+            final MailboxMembership<Id> message = createMessage(internalDate, size, bodyStartOctet, tmpMsgIn.newStream(0, -1), flags, headers, propertyBuilder);
+            final List<Long> uids = new ArrayList<Long>();
             messageMapper.execute(new TransactionalMapper.Transaction() {
                 
                 public void run() throws MailboxException {
-                    messageMapper.save(mailbox, message);
+                    uids.add(messageMapper.save(mailbox, message));
                 }
             });
-           
+            long uid = uids.get(0);
             dispatcher.added(uid, mailboxSession.getSessionId(), new StoreMailboxPath<Id>(getMailboxEntity()));
             return uid;
         } catch (IOException e) {
@@ -336,7 +324,6 @@ public abstract class StoreMessageManage
      * Create a new {@link MailboxMembership} for the given data
      * 
      * @param internalDate
-     * @param uid
      * @param size
      * @param bodyStartOctet
      * @param documentIn
@@ -346,7 +333,7 @@ public abstract class StoreMessageManage
      * @return membership
      * @throws MailboxException 
      */
-    protected abstract MailboxMembership<Id> createMessage(Date internalDate, final long uid, final int size, int bodyStartOctet, 
+    protected abstract MailboxMembership<Id> createMessage(Date internalDate, final int size, int bodyStartOctet, 
             final InputStream documentIn, final Flags flags, final List<Header> headers, PropertyBuilder propertyBuilder) throws MailboxException;
     
     /**
@@ -550,11 +537,10 @@ public abstract class StoreMessageManage
             final List<MailboxMembership<Id>> copiedRows = new ArrayList<MailboxMembership<Id>>();
 
             for (final MailboxMembership<Id> originalMessage:originalRows) {
-                final long uid = consumer.reserveNextUid(getMailboxEntity(),session);
                 messageMapper.execute(new TransactionalMapper.Transaction() {
 
                     public void run() throws MailboxException {
-                        final MailboxMembership<Id> newRow = messageMapper.copy(mailbox, uid, originalMessage);
+                        final MailboxMembership<Id> newRow = messageMapper.copy(mailbox, originalMessage);
                         copiedRows.add(newRow);
                     }
                     

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=983076&r1=983075&r2=983076&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 Fri Aug  6 18:02:34 2010
@@ -125,13 +125,15 @@ public interface MessageMapper<Id> exten
 
 
     /**
-     * Save the given {@link MailboxMembership} to the underlying storage
+     * Save the given {@link MailboxMembership} to the underlying storage and return the uid of it. the uid needs to be unique and sequential. Howto
+     * archive that is up to the implementation
+     * 
      * 
      * @param mailbox
      * @param message
      * @throws StorageException
      */
-    public abstract void save(Mailbox<Id> mailbox, MailboxMembership<Id> message) throws StorageException;
+    public abstract long save(Mailbox<Id> mailbox, MailboxMembership<Id> message) throws StorageException;
     
     
     /**
@@ -143,6 +145,6 @@ public interface MessageMapper<Id> exten
      * @return The copied instance
      * @throws StorageException
      */
-    public abstract MailboxMembership<Id> copy(Mailbox<Id> mailbox, long uid, MailboxMembership<Id> original) throws StorageException;
+    public abstract MailboxMembership<Id> copy(Mailbox<Id> mailbox, MailboxMembership<Id> original) throws StorageException;
 
 }
\ No newline at end of file

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/model/Mailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/model/Mailbox.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/model/Mailbox.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/model/Mailbox.java Fri Aug  6 18:02:34 2010
@@ -28,12 +28,7 @@ public interface Mailbox<Id> {
      * @return uid
      */
     public abstract long getLastUid();
-
-    /**
-     * Consumes the current UID.
-     */
-    public abstract void consumeUid();
-    
+        
     /**
      * Gets the unique mailbox ID.
      * @return mailbox id

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/model/MailboxMembership.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/model/MailboxMembership.java?rev=983076&r1=983075&r2=983076&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/model/MailboxMembership.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/model/MailboxMembership.java Fri Aug  6 18:02:34 2010
@@ -112,4 +112,5 @@ public interface MailboxMembership<Id> {
      * @return new instance, not null
      */
     public abstract Flags createFlags();
+
 }



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