You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rd...@apache.org on 2009/06/02 14:39:09 UTC

svn commit: r781020 - in /james/imap/trunk: jpa/src/main/java/org/apache/james/imap/jpa/mail/ memory/src/main/java/org/apache/james/imap/inmemory/ store/src/main/java/org/apache/james/imap/store/ store/src/main/java/org/apache/james/imap/store/mail/

Author: rdonkin
Date: Tue Jun  2 12:39:09 2009
New Revision: 781020

URL: http://svn.apache.org/viewvc?rev=781020&view=rev
Log:
IMAP-95 Implement MailboxManager. https://issues.apache.org/jira/browse/IMAP-95

Added:
    james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/SimpleHeader.java   (with props)
Modified:
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMailboxMapper.java
    james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailbox.java
    james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailboxManager.java
    james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMailbox.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MailboxMapper.java

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMailboxMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMailboxMapper.java?rev=781020&r1=781019&r2=781020&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMailboxMapper.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMailboxMapper.java Tue Jun  2 12:39:09 2009
@@ -46,9 +46,9 @@
     /**
      * @see org.apache.james.imap.store.mail.MailboxMapper#hasChildren
      */
-    public boolean hasChildren(String mailboxName, char hierarchyDeliminator) throws StorageException {
+    public boolean existsMailboxStartingWith(String mailboxName) throws StorageException {
         
-        final String name = mailboxName + hierarchyDeliminator + SQL_WILDCARD_CHAR; 
+        final String name = mailboxName + SQL_WILDCARD_CHAR; 
         final Long numberOfChildMailboxes = (Long) entityManager.createNamedQuery("countMailboxesWithNameLike").setParameter("nameParam", name).getSingleResult();
         return numberOfChildMailboxes != null && numberOfChildMailboxes > 0;
     }

Modified: james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailbox.java?rev=781020&r1=781019&r2=781020&view=diff
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailbox.java (original)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryMailbox.java Tue Jun  2 12:39:09 2009
@@ -29,13 +29,15 @@
 public class InMemoryMailbox implements Mailbox {
 
     private final long id;    
+    private final long uidValidity;
     private AtomicLong nextUid;
     private String name;
     
-    public InMemoryMailbox(final long id, final String name) {
+    public InMemoryMailbox(final long id, final String name, final long uidValidity) {
         super();
         this.id = id;
         this.name = name;
+        this.uidValidity = uidValidity;
     }
 
     public void consumeUid() {
@@ -56,8 +58,7 @@
 
 
     public long getUidValidity() {
-        // TODO Auto-generated method stub
-        return 0;
+        return uidValidity;
     }
 
     public void setName(String name) {

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=781020&r1=781019&r2=781020&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 Tue Jun  2 12:39:09 2009
@@ -19,7 +19,10 @@
 
 package org.apache.james.imap.inmemory;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.james.imap.mailbox.MailboxNotFoundException;
 import org.apache.james.imap.mailbox.StorageException;
@@ -32,74 +35,92 @@
 
 public class InMemoryMailboxManager extends StoreMailboxManager implements MailboxMapper {
 
+    private static final int INITIAL_SIZE = 128;
+    private Map<Long, InMemoryMailbox> mailboxesById;
+    
     public InMemoryMailboxManager(Authenticator authenticator, Subscriber subscriber) {
         super(authenticator, subscriber);
+        mailboxesById = new ConcurrentHashMap<Long, InMemoryMailbox>(INITIAL_SIZE);
     }
 
     @Override
     protected StoreMailbox createMailbox(Mailbox mailboxRow) {
-        return null;
+        final InMemoryStoreMailbox storeMailbox = new InMemoryStoreMailbox((InMemoryMailbox)mailboxRow);
+        return storeMailbox;
     }
 
     @Override
     protected MailboxMapper createMailboxMapper() {
-        // TODO Auto-generated method stub
-        return null;
+        return this;
     }
 
     @Override
-    protected void doCreate(String namespaceName) {
-        // TODO Auto-generated method stub
-
+    protected void doCreate(String namespaceName) throws StorageException {
+        InMemoryMailbox mailbox = new InMemoryMailbox(random.nextLong(), namespaceName, random.nextInt());
+        save(mailbox);
     }
 
     public void begin() throws StorageException {}
 
     public void commit() throws StorageException {}
 
-    public Mailbox consumeNextUid(long mailboxId) throws StorageException, MailboxNotFoundException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
     public long countMailboxesWithName(String name) throws StorageException {
-        // TODO Auto-generated method stub
-        return 0;
+        int total = 0;
+        for (final InMemoryMailbox mailbox:mailboxesById.values()) {
+            if (mailbox.getName().equals(name)) {
+                total++;
+            }
+        }
+        return total;
     }
 
     public void delete(Mailbox mailbox) throws StorageException {
-        // TODO Auto-generated method stub
-        
+        mailboxesById.remove(mailbox.getMailboxId());
     }
 
     public void deleteAll() throws StorageException {
-        // TODO Auto-generated method stub
-        
+        mailboxesById.clear();
     }
 
     public Mailbox findMailboxById(long mailboxId) throws StorageException, MailboxNotFoundException {
-        // TODO Auto-generated method stub
-        return null;
+        return mailboxesById.get(mailboxesById);
     }
 
     public Mailbox findMailboxByName(String name) throws StorageException, MailboxNotFoundException {
-        // TODO Auto-generated method stub
-        return null;
+        Mailbox result = null;
+        for (final InMemoryMailbox mailbox:mailboxesById.values()) {
+            if (mailbox.getName().equals(name)) {
+                result = mailbox;
+                break;
+            }
+        }
+        return result;
     }
 
     public List<Mailbox> findMailboxWithNameLike(String name) throws StorageException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    public boolean hasChildren(String mailboxName, char hierarchyDeliminator) throws StorageException {
-        // TODO Auto-generated method stub
-        return false;
+        final String regex = name.replace("%", ".*");
+        List<Mailbox> results = new ArrayList<Mailbox>();
+        for (final InMemoryMailbox mailbox:mailboxesById.values()) {
+            if (mailbox.getName().matches(regex)) {
+                results.add(mailbox);
+            }
+        }
+        return results;
+    }
+
+    public boolean existsMailboxStartingWith(String mailboxName) throws StorageException {
+        boolean result = false;
+        for (final InMemoryMailbox mailbox:mailboxesById.values()) {
+            if (mailbox.getName().startsWith(mailboxName)) {
+                result = true;
+                break;
+            }
+        }
+        return result;
     }
 
     public void save(Mailbox mailbox) throws StorageException {
-        // TODO Auto-generated method stub
-        
+        mailboxesById.put(mailbox.getMailboxId(), (InMemoryMailbox) mailbox);
     }
 
 }

Modified: james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMailbox.java?rev=781020&r1=781019&r2=781020&view=diff
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMailbox.java (original)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/InMemoryStoreMailbox.java Tue Jun  2 12:39:09 2009
@@ -33,9 +33,12 @@
 import org.apache.james.imap.store.mail.model.PropertyBuilder;
 
 public class InMemoryStoreMailbox extends StoreMailbox {
+    
+    private InMemoryMailbox mailbox;
 
-    public InMemoryStoreMailbox(Mailbox mailbox) {
+    public InMemoryStoreMailbox(InMemoryMailbox mailbox) {
         super(mailbox);
+        this.mailbox = mailbox;
     }
 
     @Override
@@ -46,8 +49,7 @@
 
     @Override
     protected Header createHeader(int lineNumber, String name, String value) {
-        // TODO Auto-generated method stub
-        return null;
+        return new SimpleHeader(name, lineNumber, value);
     }
 
     @Override

Added: james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/SimpleHeader.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/SimpleHeader.java?rev=781020&view=auto
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/SimpleHeader.java (added)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/SimpleHeader.java Tue Jun  2 12:39:09 2009
@@ -0,0 +1,60 @@
+/****************************************************************
+ * 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.inmemory;
+
+import org.apache.james.imap.store.mail.model.Header;
+
+public class SimpleHeader implements Header {
+
+    public String field;
+    public int lineNumber;
+    public String value;
+    
+    public SimpleHeader() {}
+    
+    public SimpleHeader(SimpleHeader header) {
+        this.field = header.field;
+        this.lineNumber = header.lineNumber;
+        this.value = header.value;
+    }
+    
+    public SimpleHeader(String field, int lineNumber, String value) {
+        super();
+        this.field = field;
+        this.lineNumber = lineNumber;
+        this.value = value;
+    }
+
+    public String getFieldName() {
+        return field;
+    }
+
+    public int getLineNumber() {
+        return lineNumber;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public int compareTo(Header o) {
+        return getLineNumber() - o.getLineNumber();
+    }
+}
\ No newline at end of file

Propchange: james/imap/trunk/memory/src/main/java/org/apache/james/imap/inmemory/SimpleHeader.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=781020&r1=781019&r2=781020&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 Tue Jun  2 12:39:09 2009
@@ -51,7 +51,7 @@
 public abstract class StoreMailboxManager extends AbstractLogEnabled implements MailboxManager {
     public static final String USER_NAMESPACE_PREFIX = "#mail";
     
-    private static final char SQL_WILDCARD_CHAR = '%';
+    public static final char SQL_WILDCARD_CHAR = '%';
 
     protected final static Random random = new Random();
 
@@ -271,7 +271,7 @@
      * @throws TorqueException
      */
     private boolean hasChildren(String name, final MailboxMapper mapper) throws StorageException {
-        return mapper.hasChildren(name, delimiter);
+        return mapper.existsMailboxStartingWith(name + delimiter);
     }
 
     public boolean mailboxExists(String mailboxName, MailboxSession session) throws MailboxException {

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MailboxMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MailboxMapper.java?rev=781020&r1=781019&r2=781020&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MailboxMapper.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/mail/MailboxMapper.java Tue Jun  2 12:39:09 2009
@@ -37,11 +37,10 @@
     /**
      * Does the given mailbox have children?
      * @param mailboxName not null
-     * @param hierarchyDeliminator character used to separate individual mailboxes in the hierarchy
      * @return true when the mailbox has children, false otherwise
      * @throws StorageException
      */
-    public abstract boolean hasChildren(String mailboxName, char hierarchyDeliminator) throws StorageException;
+    public abstract boolean existsMailboxStartingWith(String mailboxName) throws StorageException;
     
     public abstract void delete(Mailbox mailbox) throws StorageException;
 



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