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 er...@apache.org on 2010/12/13 16:49:45 UTC

svn commit: r1045186 - in /james/mailbox/trunk: ./ copier/ copier/src/ copier/src/main/ copier/src/main/java/ copier/src/main/java/org/ copier/src/main/java/org/apache/ copier/src/main/java/org/apache/james/ copier/src/main/java/org/apache/james/mailbo...

Author: eric
Date: Mon Dec 13 15:49:44 2010
New Revision: 1045186

URL: http://svn.apache.org/viewvc?rev=1045186&view=rev
Log:
Add mailbox copier module (MAILBOX-19)

Added:
    james/mailbox/trunk/copier/   (with props)
    james/mailbox/trunk/copier/src/
    james/mailbox/trunk/copier/src/main/
    james/mailbox/trunk/copier/src/main/java/
    james/mailbox/trunk/copier/src/main/java/org/
    james/mailbox/trunk/copier/src/main/java/org/apache/
    james/mailbox/trunk/copier/src/main/java/org/apache/james/
    james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/
    james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/
    james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopier.java
    james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierImpl.java
    james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierManagement.java
    james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierManagementMBean.java
    james/mailbox/trunk/copier/src/main/resources/
    james/mailbox/trunk/copier/src/test/
    james/mailbox/trunk/copier/src/test/java/
    james/mailbox/trunk/copier/src/test/java/org/
    james/mailbox/trunk/copier/src/test/java/org/apache/
    james/mailbox/trunk/copier/src/test/java/org/apache/james/
    james/mailbox/trunk/copier/src/test/java/org/apache/james/mailbox/
    james/mailbox/trunk/copier/src/test/java/org/apache/james/mailbox/copier/
    james/mailbox/trunk/copier/src/test/java/org/apache/james/mailbox/copier/MailboxCopierTest.java
    james/mailbox/trunk/copier/src/test/resources/
Modified:
    james/mailbox/trunk/pom.xml

Propchange: james/mailbox/trunk/copier/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Dec 13 15:49:44 2010
@@ -0,0 +1,2 @@
+target
+.*

Added: james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopier.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopier.java?rev=1045186&view=auto
==============================================================================
--- james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopier.java (added)
+++ james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopier.java Mon Dec 13 15:49:44 2010
@@ -0,0 +1,25 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.mailbox.copier;
+
+public interface MailboxCopier {
+
+	public Boolean copyMailboxes();
+
+}

Added: james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierImpl.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierImpl.java?rev=1045186&view=auto
==============================================================================
--- james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierImpl.java (added)
+++ james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierImpl.java Mon Dec 13 15:49:44 2010
@@ -0,0 +1,154 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.mailbox.copier;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.mail.Flags.Flag;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.james.mailbox.BadCredentialsException;
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxPath;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.MessageRange;
+import org.apache.james.mailbox.MessageResult;
+import org.apache.james.mailbox.store.streaming.InputStreamContent;
+import org.apache.james.mailbox.util.FetchGroupImpl;
+
+public class MailboxCopierImpl implements MailboxCopier {
+	
+    private Log log = LogFactory.getLog("org.apache.james.mailbox.copier");
+
+    private MailboxManager srcMailboxManager;
+	
+	private MailboxManager dstMailboxManager;
+		
+	public Boolean copyMailboxes() {
+	    
+        MailboxSession srcMailboxSession;
+        MailboxSession dstMailboxSession;
+	    
+	    try {
+            srcMailboxSession = srcMailboxManager.createSystemSession("manager", log);
+	    } catch (BadCredentialsException e) {
+	        log.error(e.getMessage());
+	        e.printStackTrace();
+	        return false;
+	    } catch (MailboxException e) {
+	        log.error(e.getMessage());
+	        e.printStackTrace();
+	        return false;
+	    }
+	    
+        srcMailboxManager.startProcessingRequest(srcMailboxSession);
+		
+        try {
+        	
+	        List<MailboxPath> mailboxPathList = srcMailboxManager.list(srcMailboxSession);
+	        
+	        for (MailboxPath mailboxPath: mailboxPathList) {
+	            
+	            try {
+	                dstMailboxSession = dstMailboxManager.createSystemSession(mailboxPath.getUser(), log);
+	            } catch (BadCredentialsException e) {
+	                log.error(e.getMessage());
+	                e.printStackTrace();
+	                return false;
+	            } catch (MailboxException e) {
+	                log.error(e.getMessage());
+	                e.printStackTrace();
+	                return false;
+	            }
+	            
+	            dstMailboxManager.startProcessingRequest(dstMailboxSession);
+	            dstMailboxManager.createMailbox(mailboxPath, dstMailboxSession);
+	            dstMailboxManager.endProcessingRequest(dstMailboxSession);
+	            
+	            MessageManager srcMessageManager = srcMailboxManager.getMailbox(mailboxPath, srcMailboxSession);
+	            
+	        	Iterator<MessageResult> messageResultIterator = srcMessageManager.getMessages(MessageRange.all(), FetchGroupImpl.FULL_CONTENT, srcMailboxSession);
+	        
+	        	while (messageResultIterator.hasNext()) {
+	        	    
+	        	    MessageResult messageResult = messageResultIterator.next();
+	        	    InputStreamContent content = (InputStreamContent) messageResult.getFullContent();
+	        	    
+	                try {
+	                    dstMailboxSession = dstMailboxManager.createSystemSession(mailboxPath.getUser(), log);
+	                } catch (BadCredentialsException e) {
+	                    log.error(e.getMessage());
+	                    e.printStackTrace();
+	                    return false;
+	                } catch (MailboxException e) {
+	                    log.error(e.getMessage());
+	                    e.printStackTrace();
+	                    return false;
+	                }
+	                
+                    dstMailboxManager.startProcessingRequest(dstMailboxSession);
+                    MessageManager dstMessageManager = dstMailboxManager.getMailbox(mailboxPath, dstMailboxSession);
+	                dstMessageManager.appendMessage(content.getInputStream(), 
+                            messageResult.getInternalDate(),
+	                        dstMailboxSession, 
+	                        messageResult.getFlags().contains(Flag.RECENT),
+	                        messageResult.getFlags());
+	                dstMailboxManager.endProcessingRequest(dstMailboxSession);
+	                
+	            }
+	        	    
+	        }
+        
+        } catch (MailboxException e) {
+            log.error(e.getMessage());
+	        e.printStackTrace();
+            return false;
+        } catch (IOException e) {
+            log.error(e.getMessage());
+            e.printStackTrace();
+            return false;
+        }
+		
+        srcMailboxManager.endProcessingRequest(srcMailboxSession);
+		
+        try {
+	        srcMailboxManager.logout(srcMailboxSession, true);
+        } catch (MailboxException e) {
+	        e.printStackTrace();
+	        return false;
+        }
+        
+        return true;
+
+	}
+	
+    public void setSrcMailboxManager(MailboxManager srcMailboxManager) {
+        this.srcMailboxManager = srcMailboxManager;
+    }
+
+    public void setDstMailboxManager(MailboxManager dstMailboxManager) {
+        this.dstMailboxManager = dstMailboxManager;
+    }
+    
+}

Added: james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierManagement.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierManagement.java?rev=1045186&view=auto
==============================================================================
--- james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierManagement.java (added)
+++ james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierManagement.java Mon Dec 13 15:49:44 2010
@@ -0,0 +1,38 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.mailbox.copier;
+
+import javax.annotation.Resource;
+import javax.management.NotCompliantMBeanException;
+import javax.management.StandardMBean;
+
+public class MailboxCopierManagement extends StandardMBean implements MailboxCopierManagementMBean {
+    
+    @Resource(name="mailboxCopier")
+    private MailboxCopier mailboxCopier;
+    
+    public MailboxCopierManagement() throws NotCompliantMBeanException {
+        super(MailboxCopierManagementMBean.class);
+    }
+    
+    public Boolean copyMailboxes() {
+        return mailboxCopier.copyMailboxes();
+    }
+
+}

Added: james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierManagementMBean.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierManagementMBean.java?rev=1045186&view=auto
==============================================================================
--- james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierManagementMBean.java (added)
+++ james/mailbox/trunk/copier/src/main/java/org/apache/james/mailbox/copier/MailboxCopierManagementMBean.java Mon Dec 13 15:49:44 2010
@@ -0,0 +1,23 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.mailbox.copier;
+
+public interface MailboxCopierManagementMBean extends MailboxCopier {
+
+}

Added: james/mailbox/trunk/copier/src/test/java/org/apache/james/mailbox/copier/MailboxCopierTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/copier/src/test/java/org/apache/james/mailbox/copier/MailboxCopierTest.java?rev=1045186&view=auto
==============================================================================
--- james/mailbox/trunk/copier/src/test/java/org/apache/james/mailbox/copier/MailboxCopierTest.java (added)
+++ james/mailbox/trunk/copier/src/test/java/org/apache/james/mailbox/copier/MailboxCopierTest.java Mon Dec 13 15:49:44 2010
@@ -0,0 +1,160 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.mailbox.copier;
+
+import java.io.ByteArrayInputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Calendar;
+import java.util.List;
+
+import javax.mail.Flags;
+
+import junit.framework.Assert;
+
+import org.apache.commons.logging.impl.SimpleLog;
+import org.apache.james.mailbox.BadCredentialsException;
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxPath;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.inmemory.InMemoryMailboxManager;
+import org.apache.james.mailbox.inmemory.InMemoryMailboxSessionMapperFactory;
+import org.apache.james.mailbox.inmemory.mail.InMemoryCachingUidProvider;
+import org.apache.james.mailbox.store.Authenticator;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class MailboxCopierTest {
+    
+    private static final int MAILBOX_COUNT = 100;
+    
+    private static final int MESSAGE_PER_MAILBOX_COUNT = 10;
+    
+    private MailboxCopierImpl mailboxCopier;
+    
+    private MailboxManager srcMemMailboxManager;
+    
+    private MailboxManager dstMemMailboxManager;
+    
+    @Before
+    public void setup() throws BadCredentialsException, MailboxException {
+        
+        mailboxCopier = new MailboxCopierImpl();
+        
+        srcMemMailboxManager = newInMemoryMailboxManager();
+        dstMemMailboxManager = newInMemoryMailboxManager();
+        
+        mailboxCopier.setSrcMailboxManager(srcMemMailboxManager);
+        mailboxCopier.setDstMailboxManager(dstMemMailboxManager);
+        
+    }
+    
+    @After
+    public void tearDown() {
+    }
+
+    /**
+     * @param args
+     * @throws MailboxException 
+     * @throws UnsupportedEncodingException 
+     */
+    @Test
+    public void testMailboxCopy() throws MailboxException, UnsupportedEncodingException {
+        
+        feedSrcMailboxManager();
+
+        assertMailboxManagerSize(srcMemMailboxManager);
+        
+        mailboxCopier.copyMailboxes();
+
+        assertMailboxManagerSize(dstMemMailboxManager);
+        
+    }
+    
+    /**
+     * @throws MailboxException 
+     * @throws BadCredentialsException 
+     * 
+     */
+    private void assertMailboxManagerSize(MailboxManager mailboxManager) throws BadCredentialsException, MailboxException {
+        
+        MailboxSession mailboxSession = mailboxManager.createSystemSession("manager", new SimpleLog("src-mailbox-copier"));        
+        mailboxManager.startProcessingRequest(mailboxSession);
+        List<MailboxPath> mailboxPathList = mailboxManager.list(mailboxSession);
+        Assert.assertEquals(MAILBOX_COUNT, mailboxPathList.size());
+        for (MailboxPath mailboxPath: mailboxPathList) {
+            MessageManager messageManager = mailboxManager.getMailbox(mailboxPath, mailboxSession);
+            Assert.assertEquals(MESSAGE_PER_MAILBOX_COUNT, messageManager.getMessageCount(mailboxSession));
+        }
+        mailboxManager.endProcessingRequest(mailboxSession);
+        mailboxManager.logout(mailboxSession, true);
+        
+    }
+    
+    /**
+     * @throws MailboxException
+     * @throws UnsupportedEncodingException
+     */
+    private void feedSrcMailboxManager() throws MailboxException, UnsupportedEncodingException {
+
+        MessageManager messageManager = null;
+        MailboxPath mailboxPath = null;
+        
+        for (int i=0; i < MAILBOX_COUNT; i++) {
+
+            MailboxSession srcMailboxSession = srcMemMailboxManager.createSystemSession("user" + i, new SimpleLog("src-mailbox-copier"));        
+
+            srcMemMailboxManager.startProcessingRequest(srcMailboxSession);
+            mailboxPath = new MailboxPath("#private", "user" + i, "INBOX");
+            srcMemMailboxManager.createMailbox(mailboxPath, srcMailboxSession);
+            messageManager = srcMemMailboxManager.getMailbox(mailboxPath, srcMailboxSession);
+            for (int j=0; j < MESSAGE_PER_MAILBOX_COUNT; j++) {
+                messageManager.appendMessage(new ByteArrayInputStream(new String("fake message" + i).getBytes("UTF-8")), 
+                        Calendar.getInstance().getTime(), 
+                        srcMailboxSession, 
+                        true, 
+                        new Flags(Flags.Flag.RECENT));
+            }
+            
+            srcMemMailboxManager.endProcessingRequest(srcMailboxSession);
+            srcMemMailboxManager.logout(srcMailboxSession, true);
+        
+        }
+        
+    }
+    
+    /**
+     * @return
+     */
+    private MailboxManager newInMemoryMailboxManager() {
+    
+        return new InMemoryMailboxManager(
+            new InMemoryMailboxSessionMapperFactory(), 
+            new Authenticator() {
+                public boolean isAuthentic(String userid, CharSequence passwd) {
+                    return true;
+                }
+            }, 
+            new InMemoryCachingUidProvider());
+    
+    }
+
+}

Modified: james/mailbox/trunk/pom.xml
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/pom.xml?rev=1045186&r1=1045185&r2=1045186&view=diff
==============================================================================
--- james/mailbox/trunk/pom.xml (original)
+++ james/mailbox/trunk/pom.xml Mon Dec 13 15:49:44 2010
@@ -41,6 +41,7 @@
     <module>jcr</module>
     <module>maildir</module>
     <module>torque</module>
+    <module>copier</module>
   </modules>
   <ciManagement>
     <system>hudson</system>



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