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 2008/12/31 20:43:53 UTC

svn commit: r730452 - in /james/protocols/imap/trunk: deployment/src/test/java/org/apache/james/imap/functional/jpa/ jpa/ jpa/src/main/java/org/apache/james/imap/jpa/ jpa/src/main/java/org/apache/james/imap/jpa/om/ jpa/src/main/java/org/apache/james/im...

Author: rdonkin
Date: Wed Dec 31 11:43:53 2008
New Revision: 730452

URL: http://svn.apache.org/viewvc?rev=730452&view=rev
Log:
JPA backed subscriber implementation

Added:
    james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/SubscriptionManager.java
    james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/
    james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/SubscriptionMapper.java   (with props)
    james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/model/
    james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/model/Subscription.java
Removed:
    james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/om/
Modified:
    james/protocols/imap/trunk/deployment/src/test/java/org/apache/james/imap/functional/jpa/JPAHostSystem.java
    james/protocols/imap/trunk/jpa/build.xml
    james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMailboxManager.java

Modified: james/protocols/imap/trunk/deployment/src/test/java/org/apache/james/imap/functional/jpa/JPAHostSystem.java
URL: http://svn.apache.org/viewvc/james/protocols/imap/trunk/deployment/src/test/java/org/apache/james/imap/functional/jpa/JPAHostSystem.java?rev=730452&r1=730451&r2=730452&view=diff
==============================================================================
--- james/protocols/imap/trunk/deployment/src/test/java/org/apache/james/imap/functional/jpa/JPAHostSystem.java (original)
+++ james/protocols/imap/trunk/deployment/src/test/java/org/apache/james/imap/functional/jpa/JPAHostSystem.java Wed Dec 31 11:43:53 2008
@@ -30,6 +30,7 @@
 import org.apache.james.imap.functional.SimpleMailboxManagerProvider;
 import org.apache.james.imap.functional.jpa.user.InMemoryUserManager;
 import org.apache.james.imap.jpa.JPAMailboxManager;
+import org.apache.james.imap.jpa.SubscriptionManager;
 import org.apache.james.imap.main.DefaultImapDecoderFactory;
 import org.apache.james.imap.processor.main.DefaultImapProcessorFactory;
 import org.apache.james.test.functional.HostSystem;
@@ -54,11 +55,13 @@
         properties.put("openjpa.Log", "JDBC=WARN, SQL=WARN, Runtime=WARN");
         properties.put("openjpa.ConnectionFactoryProperties", "PrettyPrint=true, PrettyPrintLineLength=72");
         properties.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
-        properties.put("openjpa.MetaDataFactory", "jpa(Types=org.apache.james.imap.jpa.mail.model.Header;org.apache.james.imap.jpa.mail.model.Mailbox;org.apache.james.imap.jpa.mail.model.Message)");
+        properties.put("openjpa.MetaDataFactory", "jpa(Types=org.apache.james.imap.jpa.mail.model.Header;" +
+                "org.apache.james.imap.jpa.mail.model.Mailbox;org.apache.james.imap.jpa.mail.model.Message;" +
+                "org.apache.james.imap.jpa.user.model.Subscription)");
         
         userManager = new InMemoryUserManager();
         final EntityManagerFactory entityManagerFactory = OpenJPAPersistence.getEntityManagerFactory(properties);
-        mailboxManager = new JPAMailboxManager(userManager, userManager, entityManagerFactory);
+        mailboxManager = new JPAMailboxManager(userManager, new SubscriptionManager(entityManagerFactory), entityManagerFactory);
         
         SimpleMailboxManagerProvider provider = new SimpleMailboxManagerProvider();
         final DefaultImapProcessorFactory defaultImapProcessorFactory = new DefaultImapProcessorFactory();

Modified: james/protocols/imap/trunk/jpa/build.xml
URL: http://svn.apache.org/viewvc/james/protocols/imap/trunk/jpa/build.xml?rev=730452&r1=730451&r2=730452&view=diff
==============================================================================
--- james/protocols/imap/trunk/jpa/build.xml (original)
+++ james/protocols/imap/trunk/jpa/build.xml Wed Dec 31 11:43:53 2008
@@ -64,7 +64,7 @@
             </classpath>   
             <config
                log="TOOL=TRACE"   
-               metaDataFactory="jpa(Types=org.apache.james.imap.jpa.mail.model.Header;org.apache.james.imap.jpa.mail.model.Mailbox;org.apache.james.imap.jpa.mail.model.Message)"/>   
+               metaDataFactory="jpa(Types=org.apache.james.imap.jpa.mail.model.Header;org.apache.james.imap.jpa.mail.model.Mailbox;org.apache.james.imap.jpa.mail.model.Message;org.apache.james.imap.jpa.user.model.Subscription)"/>   
           </openjpac>
     </target> 
 </project>

Modified: james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMailboxManager.java
URL: http://svn.apache.org/viewvc/james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMailboxManager.java?rev=730452&r1=730451&r2=730452&view=diff
==============================================================================
--- james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMailboxManager.java (original)
+++ james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/JPAMailboxManager.java Wed Dec 31 11:43:53 2008
@@ -48,17 +48,14 @@
 
 public class JPAMailboxManager extends AbstractLogEnabled implements MailboxManager {
 
-    
     private static final char SQL_WILDCARD_CHAR = '%';
 
     private final static Random random = new Random();
 
     private final Map<String, JPAMailbox> mailboxes;
 
-    private final Authenticator authenticator;
-    
+    private final Authenticator authenticator;    
     private final Subscriber subscriber;
-    
     private final EntityManagerFactory entityManagerFactory;
 
     public JPAMailboxManager(final Authenticator authenticator, final Subscriber subscriber, final EntityManagerFactory entityManagerFactory) {

Added: james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/SubscriptionManager.java
URL: http://svn.apache.org/viewvc/james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/SubscriptionManager.java?rev=730452&view=auto
==============================================================================
--- james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/SubscriptionManager.java (added)
+++ james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/SubscriptionManager.java Wed Dec 31 11:43:53 2008
@@ -0,0 +1,90 @@
+/****************************************************************
+ * 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;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceException;
+
+import org.apache.james.imap.jpa.user.SubscriptionMapper;
+import org.apache.james.imap.jpa.user.model.Subscription;
+import org.apache.james.imap.mailbox.SubscriptionException;
+
+/**
+ * Manages subscriptions.
+ */
+public class SubscriptionManager implements Subscriber {
+
+    private static final int INITIAL_SIZE = 32;
+    private final EntityManagerFactory factory;
+    
+    public SubscriptionManager(final EntityManagerFactory factory) {
+        super();
+        this.factory = factory;
+    }
+
+    public void subscribe(final String user, final String mailbox) throws SubscriptionException {
+        try {
+            final SubscriptionMapper mapper = new SubscriptionMapper(factory.createEntityManager());
+            mapper.begin();
+            
+            final Subscription subscription = mapper.findFindMailboxSubscriptionForUser(user, mailbox);
+            if (subscription == null) {
+                final Subscription newSubscription = new Subscription(user, mailbox);
+                mapper.save(newSubscription);
+                mapper.commit();
+            }
+        } catch (PersistenceException e) {
+            throw new SubscriptionException(e);
+        }
+    }
+
+    public Collection<String> subscriptions(final String user) throws SubscriptionException {
+        try {
+            final SubscriptionMapper mapper = new SubscriptionMapper(factory.createEntityManager());
+            final List<Subscription> subscriptions = mapper.findSubscriptionsForUser(user);
+            final Collection<String> results = new HashSet<String>(INITIAL_SIZE);
+            for (Subscription subscription:subscriptions) {
+                results.add(subscription.getMailbox());
+            }
+            return results;
+        }  catch (PersistenceException e) {
+            throw new SubscriptionException(e);
+        }
+    }
+
+    public void unsubscribe(final String user, final String mailbox) throws SubscriptionException {
+        try {
+            final SubscriptionMapper mapper = new SubscriptionMapper(factory.createEntityManager());
+            mapper.begin();
+            
+            final Subscription subscription = mapper.findFindMailboxSubscriptionForUser(user, mailbox);
+            if (subscription != null) {
+                mapper.delete(subscription);
+                mapper.commit();
+            }
+        } catch (PersistenceException e) {
+            throw new SubscriptionException(e);
+        }
+    }
+
+}

Added: james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/SubscriptionMapper.java
URL: http://svn.apache.org/viewvc/james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/SubscriptionMapper.java?rev=730452&view=auto
==============================================================================
--- james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/SubscriptionMapper.java (added)
+++ james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/SubscriptionMapper.java Wed Dec 31 11:43:53 2008
@@ -0,0 +1,88 @@
+/****************************************************************
+ * 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.user;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.NoResultException;
+
+import org.apache.james.imap.jpa.user.model.Subscription;
+
+/**
+ * Maps data access logic to JPA operations.
+ */
+public class SubscriptionMapper {
+    private final EntityManager entityManager;
+    
+    public SubscriptionMapper(final EntityManager entityManager) {
+        super();
+        this.entityManager = entityManager;
+    }
+    
+    public void begin() {
+        entityManager.getTransaction().begin();
+    }
+    
+    public void commit() {
+        entityManager.getTransaction().commit();
+    }
+
+    /**
+     * Finds any subscriptions for a given user to the given mailbox.
+     * @param user not null
+     * @param mailbox not null
+     * @return <code>Subscription</code>, 
+     * or null when the user is not subscribed to the given mailbox
+     */
+    public Subscription findFindMailboxSubscriptionForUser(final String user, final String mailbox) {
+        try {
+            return (Subscription) entityManager.createNamedQuery("findFindMailboxSubscriptionForUser")
+                .setParameter("userParam", user).setParameter("mailboxParam", mailbox).getSingleResult();
+        } catch (NoResultException e) {
+            return null;
+        }
+    }
+    
+    /**
+     * Saves the given subscription.
+     * @param subscription not null
+     */
+    public void save(Subscription subscription) {
+        entityManager.persist(subscription);
+    }
+
+    /**
+     * Finds subscriptions for the given user.
+     * @param user not null
+     * @return not null
+     */
+    @SuppressWarnings("unchecked")
+    public List<Subscription> findSubscriptionsForUser(String user) {
+        return (List<Subscription>) entityManager.createNamedQuery("findSubscriptionsForUser").setParameter("userParam", user).getResultList();
+    }
+
+    /**
+     * Deletes the given subscription.
+     * @param subscription not null
+     */
+    public void delete(Subscription subscription) {
+        entityManager.remove(subscription);
+    }
+}

Propchange: james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/SubscriptionMapper.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Added: james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/model/Subscription.java
URL: http://svn.apache.org/viewvc/james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/model/Subscription.java?rev=730452&view=auto
==============================================================================
--- james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/model/Subscription.java (added)
+++ james/protocols/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/user/model/Subscription.java Wed Dec 31 11:43:53 2008
@@ -0,0 +1,128 @@
+/****************************************************************
+ * 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.user.model;
+
+import javax.persistence.Basic;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+import javax.persistence.UniqueConstraint;
+
+/**
+ * A subscription to a mailbox by a user.
+ */
+@Entity
+@Table(uniqueConstraints=@UniqueConstraint(columnNames={"USER", "MAILBOX"}))
+@NamedQueries({
+    @NamedQuery(name="findFindMailboxSubscriptionForUser",
+        query="SELECT subscription FROM Subscription subscription WHERE subscription.user = :userParam AND subscription.mailbox = :mailboxParam"),          
+    @NamedQuery(name="findSubscriptionsForUser",
+        query="SELECT subscription FROM Subscription subscription WHERE subscription.user = :userParam")                  
+})
+public class Subscription {
+
+    private static final String TO_STRING_SEPARATOR = "  ";
+    /** Primary key */
+    @GeneratedValue
+    @Id private long id;
+    /** Name of the subscribed user */
+    @Basic(optional=false) private String user;
+    /** Subscribed mailbox */
+    @Basic(optional=false) private String mailbox;
+    
+    /**
+     * Used by JPA
+     */
+    @Deprecated
+    public Subscription() {}
+    
+    /**
+     * Constructs a user subscription.
+     * @param user not null
+     * @param mailbox not null
+     */
+    public Subscription(String user, String mailbox) {
+        super();
+        this.user = user;
+        this.mailbox = mailbox;
+    }
+
+    /**
+     * Gets the name of the mailbox to which 
+     * the user is subscribed.
+     * Note that subscriptions must be maintained
+     * beyond the lifetime of a particular instance
+     * of a mailbox.
+     * @return not null
+     */
+    public String getMailbox() {
+        return mailbox;
+    }
+    
+    /**
+     * Gets the name of the subscribed user.
+     * @return not null
+     */
+    public String getUser() {
+        return user;
+    }
+
+    @Override
+    public int hashCode() {
+        final int PRIME = 31;
+        int result = 1;
+        result = PRIME * result + (int) (id ^ (id >>> 32));
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        final Subscription other = (Subscription) obj;
+        if (id != other.id)
+            return false;
+        return true;
+    }
+
+    /**
+     * Renders output suitable for debugging.
+     *
+     * @return output suitable for debugging
+     */
+    public String toString()
+    {
+        final String result = "Subscription ( "
+            + "id = " + this.id + TO_STRING_SEPARATOR
+            + "user = " + this.user + TO_STRING_SEPARATOR
+            + "mailbox = " + this.mailbox + TO_STRING_SEPARATOR
+            + " )";
+    
+        return result;
+    }
+    
+    
+}



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