Author: eric
Date: Tue Jan 18 18:07:31 2011
New Revision: 1060495

URL: http://svn.apache.org/viewvc?rev=1060495&view=rev
Log:
Create users and mails - Only one configuration file - Add JMX support 
(JAMES-1052)

Added:
    
james/server/trunk/container-spring/src/main/config/james/usersrepository23.xml
      - copied unchanged from r1060227, 
james/server/trunk/container-spring/src/main/config/james/importjames23-usersrepository.xml
    
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23ImporterManagement.java
    
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23ImporterManagementMBean.java
Removed:
    
james/server/trunk/container-spring/src/main/config/james/importjames23-usersrepository.xml
    james/server/trunk/container-spring/src/main/config/james/importjames23.xml
Modified:
    james/server/trunk/container-spring/pom.xml
    
james/server/trunk/container-spring/src/main/config/james/context/james-server-context.xml
    
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23Importer.java

Modified: james/server/trunk/container-spring/pom.xml
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/pom.xml?rev=1060495&r1=1060494&r2=1060495&view=diff
==============================================================================
--- james/server/trunk/container-spring/pom.xml (original)
+++ james/server/trunk/container-spring/pom.xml Tue Jan 18 18:07:31 2011
@@ -60,7 +60,7 @@
           <configurationDirectory>conf</configurationDirectory>
           <target>${project.build.directory}/appassembler</target>
           <repositoryLayout>flat</repositoryLayout>
-                 <repositoryName>lib</repositoryName>
+          <repositoryName>lib</repositoryName>
           
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
           <environmentSetupFileName>setenv</environmentSetupFileName>
           <!--  set some sane defaults for memory -->
@@ -251,7 +251,7 @@
         </executions>
       </plugin>
       
-         <plugin>
+      <plugin>
         <artifactId>maven-assembly-plugin</artifactId>
         <configuration>
           <descriptorSourceDirectory>src/assemble/</descriptorSourceDirectory>
@@ -549,7 +549,27 @@
     </dependency>
     <dependency>
       <groupId>org.apache.james</groupId>
-      <artifactId>james-server-data-library</artifactId>
+      <artifactId>james-server-user-library</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
+      <artifactId>james-server-user-file</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
+      <artifactId>james-server-user-jcr</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
+      <artifactId>james-server-user-jdbc</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
+      <artifactId>james-server-user-jpa</artifactId>
       <scope>runtime</scope>
     </dependency>
     <dependency>
@@ -564,6 +584,21 @@
     </dependency>
     <dependency>
       <groupId>org.apache.james</groupId>
+      <artifactId>james-server-domainlist-library</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
+      <artifactId>james-server-domainlist-xml</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
+      <artifactId>james-server-domainlist-jpa</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
       <artifactId>james-server-smtpserver</artifactId>
       <scope>runtime</scope>
     </dependency>
@@ -642,17 +677,22 @@
     </dependency>
     <dependency>
       <groupId>org.apache.james</groupId>
-      <artifactId>james-server-file</artifactId>
+      <artifactId>james-server-mail-library</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
+      <artifactId>james-server-mail-file</artifactId>
       <scope>runtime</scope>
     </dependency>
     <dependency>
       <groupId>org.apache.james</groupId>
-      <artifactId>james-server-jdbc</artifactId>
+      <artifactId>james-server-mail-jdbc</artifactId>
       <scope>runtime</scope>
     </dependency>
     <dependency>
       <groupId>org.apache.james</groupId>
-      <artifactId>james-server-jcr</artifactId>
+      <artifactId>james-server-mail-jcr</artifactId>
       <scope>runtime</scope>
     </dependency>
     <dependency>

Modified: 
james/server/trunk/container-spring/src/main/config/james/context/james-server-context.xml
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/config/james/context/james-server-context.xml?rev=1060495&r1=1060494&r2=1060495&view=diff
==============================================================================
--- 
james/server/trunk/container-spring/src/main/config/james/context/james-server-context.xml
 (original)
+++ 
james/server/trunk/container-spring/src/main/config/james/context/james-server-context.xml
 Tue Jan 18 18:07:31 2011
@@ -83,7 +83,7 @@
             </entry>
             <entry>
               <key>
-                <value>importjames23-usersrepository</value>
+                <value>usersrepository23</value>
               </key>
 <!--               no alias needed -->
               <value></value>
@@ -307,7 +307,7 @@
     <!-- 
       Mailbox Import
      -->
-    <bean id="importjames23" 
class="org.apache.james.container.spring.tool.James23Importer"/>
+    <bean id="james23importer" 
class="org.apache.james.container.spring.tool.James23Importer"/>
           
     <!-- 
     ===========================================================================
@@ -384,6 +384,7 @@
           <entry 
key="org.apache.james:type=component,component=mailetcontainer,name=mailspooler"
 value-ref="mailspooler"/>
           <entry key="org.apache.james:type=mailbox,name=mailboxcopier" 
value-ref="mailboxcopiermanagement"/>
           <entry key="org.apache.james:type=container,name=logprovider" 
value-ref="logprovider"/>
+          <entry key="org.apache.james:type=mailbox,name=james23importer" 
value-ref="james23importermanagement"/>
         </map>
       </property>
       <property name="assembler">
@@ -397,5 +398,6 @@
     <bean id="domainlistmanagement" 
class="org.apache.james.domainlist.lib.DomainListManagement" />
     <bean id="mailboxmanagermanagement" 
class="org.apache.james.adapter.mailbox.MailboxManagerManagement" />
     <bean id="mailboxcopiermanagement" 
class="org.apache.james.container.spring.mailbox.MailboxCopierManagement" />
+    <bean id="james23importermanagement" 
class="org.apache.james.container.spring.tool.James23ImporterManagement" />
 
 </beans>

Modified: 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23Importer.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23Importer.java?rev=1060495&r1=1060494&r2=1060495&view=diff
==============================================================================
--- 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23Importer.java
 (original)
+++ 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23Importer.java
 Tue Jan 18 18:07:31 2011
@@ -18,17 +18,24 @@
  ****************************************************************/
 package org.apache.james.container.spring.tool;
 
+import java.io.IOException;
+import java.util.Date;
 import java.util.Iterator;
 
 import javax.annotation.Resource;
+import javax.mail.Flags;
 import javax.mail.MessagingException;
 
-import org.apache.commons.configuration.ConfigurationException;
-import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.commons.logging.Log;
-import org.apache.james.lifecycle.api.Configurable;
+import org.apache.james.domainlist.api.DomainList;
+import org.apache.james.domainlist.api.DomainListException;
 import org.apache.james.lifecycle.api.LogEnabled;
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxExistsException;
 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.mailrepository.api.MailRepository;
 import org.apache.james.mailrepository.api.MailRepositoryStore;
 import 
org.apache.james.mailrepository.api.MailRepositoryStore.MailRepositoryStoreException;
@@ -36,7 +43,10 @@ import org.apache.james.user.api.UsersRe
 import org.apache.james.user.api.UsersRepositoryException;
 import org.apache.mailet.Mail;
 
-public class James23Importer implements Configurable, LogEnabled {
+/**
+ * Tool to import James 2.3 users and mails into James 3.0.
+ */
+public class James23Importer implements LogEnabled {
     
     /**
      * The logger.
@@ -44,65 +54,122 @@ public class James23Importer implements 
     private Log log;
     
     /**
-     * The configuration.
-     */
-    private HierarchicalConfiguration config;
-
-    /**
-     * James 2.3 user repository defined by configuration.
-     */
-    @Resource(name = "importjames23-usersrepository")
-    private UsersRepository james23UsersRepository;
-    
-    /**
      * James 3.0 users repository.
      */
     @Resource(name="usersrepository")
-    private UsersRepository usersRepository;
+    private UsersRepository james30UsersRepository;
 
     /**
-     * The mail repository store needed to select the james 2.3 mail 
repository.
+     * James 3.0 users repository.
      */
     @Resource(name="mailrepositorystore")
     private MailRepositoryStore mailRepositoryStore;
 
     /**
+     * James 3.0 domain list.
+     */
+    @Resource(name="domainlist")
+    private DomainList domainList;
+
+    /**
      * The mailbox manager needed to copy the mails to.
      */
     @Resource(name="mailboxmanager")
     private MailboxManager mailboxManager;
 
     /**
-     * Copy 2.3 users to 3.0 users (taking virtualDomains into account)
-     * Copy 2.3 mails to 3.0 mails.
-     * 
-     * TODO: This is just a skeleton to talk about the architecture (conf,...)
+     * James 2.3 user repository defined by configuration.
+     */
+    @Resource(name = "usersrepository23")
+    private UsersRepository james23UsersRepository;
+    
+    /**
+     * Import 2.3 users to 3.0 users (taking virtualDomains into account)
+     * Import 2.3 mails to 3.0 mails.
      * 
      * @throws MailRepositoryStoreException
      * @throws MessagingException
+     * @throws UsersRepositoryException 
+     * @throws DomainListException 
+     * @throws IOException 
+     * @throws MailboxException 
+     */
+    public void importUsersAndMailsFromJames23(String 
james23MailRepositoryPath, String defaultPassword) throws 
MailRepositoryStoreException, MessagingException, UsersRepositoryException, 
DomainListException, MailboxException, IOException {
+        importUsersFromJames23(defaultPassword);
+        importMailsFromJames23(james23MailRepositoryPath);
+    }
+    
+    /**
+     * Import 2.3 users to 3.0 users (taking virtualDomains into account)
+     * 
+     * @param defaultPassword
+     * @throws MessagingException
+     * @throws UsersRepositoryException
+     * @throws DomainListException
      */
-    public void importFromJames23() throws MailRepositoryStoreException, 
MessagingException {
+    public void importUsersFromJames23(String defaultPassword) throws 
MessagingException, UsersRepositoryException, DomainListException {
+        Iterator<String> j23uIt = james23UsersRepository.list();
+        while (j23uIt.hasNext()) {
+            String userName23 = j23uIt.next();
+            String userName30 = convert23UserTo30(userName23);
+            james30UsersRepository.addUser(userName30, defaultPassword);
+            log.info("New user is copied from 2.3 to 3.0 with username=" + 
userName30);
+        }
+    }
+    
+    /**
+     * Import 2.3 mails to 3.0 mails.
+     * 
+     * @param james23MailRepositoryPath the 2.3 mail repository path to import 
from e.g. file://var/mail/inboxes
+     * @throws MessagingException
+     * @throws MailRepositoryStoreException
+     * @throws UsersRepositoryException
+     * @throws IOException 
+     * @throws MailboxException 
+     * @throws DomainListException 
+     */
+    public void importMailsFromJames23(String james23MailRepositoryPath) 
throws MessagingException, MailRepositoryStoreException, 
UsersRepositoryException, MailboxException, IOException, DomainListException {
         
-        String james23MailRepositoryPath = config.getString("repositoryPath");
+        Flags flags = new Flags();
+        boolean isRecent = false;
         
-        Iterator<String> j23uIt;
-        try {
-            j23uIt = james23UsersRepository.list();
-        } catch (UsersRepositoryException e) {
-            throw new MessagingException("Unable to access UsersRepository", 
e);
-        }
+        Iterator<String> james23userRepositoryIterator = 
james23UsersRepository.list();
         
-        while (j23uIt.hasNext()) {
-            String user = j23uIt.next();
-            System.out.println("James 2.3 user:" + user);
-            MailRepository mailRepository = 
mailRepositoryStore.select(james23MailRepositoryPath + "/" + user);
-            Iterator<String> sr = mailRepository.list();
-            while (sr.hasNext()) {
-                Mail mail = mailRepository.retrieve(sr.next());
-                System.out.println(mail.getMessage().getSubject() 
-                        + ": " + mail.getMessage().getSize() 
-                        + " - " + mail.getMessage().getLineCount());
+        while (james23userRepositoryIterator.hasNext()) {
+            
+            String userName23 = james23userRepositoryIterator.next();
+            MailRepository mailRepository = 
mailRepositoryStore.select(james23MailRepositoryPath + "/" + userName23);
+            Iterator<String> mailRepositoryIterator = mailRepository.list();
+            
+            String userName30 = convert23UserTo30(userName23);
+            
+            MailboxPath mailboxPath = MailboxPath.inbox(userName30);
+
+            MailboxSession mailboxSession = 
mailboxManager.createSystemSession(userName30, log);
+
+            mailboxManager.startProcessingRequest(mailboxSession);
+            try {
+                mailboxManager.createMailbox(mailboxPath, mailboxSession);
             }
+            catch (MailboxExistsException e) {
+                // Do nothing.
+            }
+            mailboxManager.endProcessingRequest(mailboxSession);
+
+            MessageManager messageManager = 
mailboxManager.getMailbox(mailboxPath, mailboxSession);
+
+            while (mailRepositoryIterator.hasNext()) {
+                Mail mail = 
mailRepository.retrieve(mailRepositoryIterator.next());
+                mailboxManager.startProcessingRequest(mailboxSession);
+                // TODO We need to build the full message, not only the body.
+                
messageManager.appendMessage(mail.getMessage().getInputStream(), 
+                        mail.getMessage().getReceivedDate(), 
+                        mailboxSession, 
+                        isRecent, 
+                        flags);
+                mailboxManager.endProcessingRequest(mailboxSession);
+            }
+            
         }
         
     }
@@ -113,12 +180,18 @@ public class James23Importer implements 
     public void setLog(Log log) {
         this.log = log;
     }
-
-    /* (non-Javadoc)
-     * @see 
org.apache.james.lifecycle.api.Configurable#configure(org.apache.commons.configuration.HierarchicalConfiguration)
+    
+    /**
+     * Utility method to convert a James 2.3 username to a 
+     * James 3.0 username. To achieve this, we need to add 
+     * the default James 3.0 domain because 2.3 users have no domains.
+     * 
+     * @param userName23
+     * @return
+     * @throws DomainListException
      */
-    public void configure(HierarchicalConfiguration config) throws 
ConfigurationException {
-        this.config = config;
+    private String convert23UserTo30(String userName23) throws 
DomainListException {
+        return new String(userName23 + "@" + domainList.getDefaultDomain());
     }
-    
+
 }

Added: 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23ImporterManagement.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23ImporterManagement.java?rev=1060495&view=auto
==============================================================================
--- 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23ImporterManagement.java
 (added)
+++ 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23ImporterManagement.java
 Tue Jan 18 18:07:31 2011
@@ -0,0 +1,103 @@
+/****************************************************************
+ * 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.container.spring.tool;
+
+import java.io.IOException;
+
+import javax.annotation.Resource;
+import javax.mail.MessagingException;
+
+import org.apache.james.domainlist.api.DomainListException;
+import org.apache.james.mailbox.MailboxException;
+import 
org.apache.james.mailrepository.api.MailRepositoryStore.MailRepositoryStoreException;
+import org.apache.james.user.api.UsersRepositoryException;
+
+/**
+ * {@link James23Importer} support via JMX.
+ */
+public class James23ImporterManagement implements 
James23ImporterManagementMBean {
+
+    /**
+     * 
+     */
+    @Resource(name="james23importer")
+    private James23Importer james23Importer;
+    
+    /* (non-Javadoc)
+     * @see 
org.apache.james.container.spring.tool.James23ImporterManagementMBean#importUsersAndMailsFromJames23(java.lang.String,
 java.lang.String)
+     */
+    public void importUsersAndMailsFromJames23(String 
james23MailRepositoryPath, String defaultPassword) throws Exception {
+        try {
+            
james23Importer.importUsersAndMailsFromJames23(james23MailRepositoryPath, 
defaultPassword);
+        } catch (MailRepositoryStoreException e) {
+            throw new Exception(e.getMessage());
+        } catch (MessagingException e) {
+            throw new Exception(e.getMessage());
+        } catch (UsersRepositoryException e) {
+            throw new Exception(e.getMessage());
+        } catch (DomainListException e) {
+            throw new Exception(e.getMessage());
+        } catch (IOException e) {
+            throw new Exception(e.getMessage());
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.james.container.spring.tool.James23ImporterManagementMBean#importUsersFromJames23(java.lang.String)
+     */
+    public void importUsersFromJames23(String defaultPassword) throws 
Exception {
+        try {
+            james23Importer.importUsersFromJames23(defaultPassword);
+        } catch (MessagingException e) {
+            throw new Exception(e.getMessage());
+        } catch (UsersRepositoryException e) {
+            throw new Exception(e.getMessage());
+        } catch (DomainListException e) {
+            throw new Exception(e.getMessage());
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.james.container.spring.tool.James23ImporterManagementMBean#importMailsFromJames23(java.lang.String)
+     */
+    public void importMailsFromJames23(String james23MailRepositoryPath) 
throws Exception {
+        try {
+            james23Importer.importMailsFromJames23(james23MailRepositoryPath);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new Exception(e.getMessage());
+//        } catch (MailboxException e) {
+//            e.printStackTrace();
+//            throw new Exception(e.getMessage());
+//        } catch (MailRepositoryStoreException e) {
+//            e.printStackTrace();
+//            throw new Exception(e.getMessage());
+//        } catch (MessagingException e) {
+//            e.printStackTrace();
+//            throw new Exception(e.getMessage());
+//        } catch (UsersRepositoryException e) {
+//            e.printStackTrace();
+//            throw new Exception(e.getMessage());
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//            throw new Exception(e.getMessage());
+        }
+    }
+
+}

Added: 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23ImporterManagementMBean.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23ImporterManagementMBean.java?rev=1060495&view=auto
==============================================================================
--- 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23ImporterManagementMBean.java
 (added)
+++ 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/tool/James23ImporterManagementMBean.java
 Tue Jan 18 18:07:31 2011
@@ -0,0 +1,48 @@
+/****************************************************************
+ * 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.container.spring.tool;
+
+import org.apache.james.mailbox.MailboxManager;
+
+/**
+ * Allow to copy {@link MailboxManager} contents from one to the other via JMX
+ * 
+ *
+ */
+public interface James23ImporterManagementMBean {
+    
+    /**
+     * @param james23MailRepositoryPath
+     * @param defaultPassword
+     */
+    void importUsersAndMailsFromJames23(String james23MailRepositoryPath, 
String defaultPassword) throws Exception;
+
+    /**
+     * @param defaultPassword
+     * @throws Exception
+     */
+    void importUsersFromJames23(String defaultPassword) throws Exception;
+    
+    /**
+     * @param james23MailRepositoryPath
+     * @throws Exception
+     */
+    void importMailsFromJames23(String james23MailRepositoryPath) throws 
Exception;
+    
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to