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 ba...@apache.org on 2006/11/18 15:46:10 UTC

svn commit: r476531 - in /james/server/trunk/src/java/org/apache/james: management/SpoolManagement.java management/SpoolManagementMBean.java remotemanager/RemoteManagerHandler.java services/SpoolManagementService.java

Author: bago
Date: Sat Nov 18 06:46:10 2006
New Revision: 476531

URL: http://svn.apache.org/viewvc?view=rev&rev=476531
Log:
Added methods to move mails from a src spool to a destination spool (supporting filters and state changes) 
Published the command in RemoteManager as MOVEMAILS (see JAMES-704)

Modified:
    james/server/trunk/src/java/org/apache/james/management/SpoolManagement.java
    james/server/trunk/src/java/org/apache/james/management/SpoolManagementMBean.java
    james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
    james/server/trunk/src/java/org/apache/james/services/SpoolManagementService.java

Modified: james/server/trunk/src/java/org/apache/james/management/SpoolManagement.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/management/SpoolManagement.java?view=diff&rev=476531&r1=476530&r2=476531
==============================================================================
--- james/server/trunk/src/java/org/apache/james/management/SpoolManagement.java (original)
+++ james/server/trunk/src/java/org/apache/james/management/SpoolManagement.java Sat Nov 18 06:46:10 2006
@@ -69,6 +69,89 @@
     }
 
     /**
+     * Move all mails from the given repository to another repository matching the given filter criteria
+     *
+     * @param srcspoolRepositoryURL the spool whose item are listed
+     * @param srcstate if not NULL, only mails with matching state are returned
+     * @param dstSpoolRepositoryURL the destination spool
+     * @param dstState if not NULL, the state will be changed before storing the message to the new repository.
+     * @param header if not NULL, only mails with at least one header with a value matching headerValueRegex are returned
+     * @param headerValueRegex the regular expression the header must match
+     * @return a counter of moved mails
+     * @throws SpoolManagementException
+     */
+    public int moveSpoolItems(String srcSpoolRepositoryURL, String srcState, String dstSpoolRepositoryURL, String dstState, String header, String headerValueRegex) 
+            throws SpoolManagementException {
+        SpoolFilter filter = new SpoolFilter(srcState, header, headerValueRegex);
+        try {
+            return moveSpoolItems(srcSpoolRepositoryURL, dstSpoolRepositoryURL, dstState, filter);
+        } catch (Exception e) {
+            throw new SpoolManagementException(e);
+        }
+    }
+
+    /**
+     * Move all mails from the given repository to another repository matching the given filter criteria
+     *
+     * @param srcspoolRepositoryURL the spool whose item are listed
+     * @param dstSpoolRepositoryURL the destination spool
+     * @param dstState if not NULL, the state will be changed before storing the message to the new repository.
+     * @param spoolFilter the filter to select messages from the source repository
+     * @return a counter of moved mails
+     * @throws ServiceException 
+     * @throws MessagingException 
+     * @throws SpoolManagementException
+     */
+    public int moveSpoolItems(String srcSpoolRepositoryURL, String dstSpoolRepositoryURL, String dstState, SpoolFilter filter)
+            throws ServiceException, MessagingException, SpoolManagementException {
+        
+        SpoolRepository srcSpoolRepository;
+        SpoolRepository dstSpoolRepository;
+        srcSpoolRepository = getSpoolRepository(srcSpoolRepositoryURL);
+        dstSpoolRepository = getSpoolRepository(dstSpoolRepositoryURL);
+        
+        // get an iterator of all keys
+        Iterator spoolR = srcSpoolRepository.list();
+
+        int count = 0;
+        while (spoolR.hasNext()) {
+            String key = spoolR.next().toString();
+            boolean locked = false;
+            try {
+                locked = srcSpoolRepository.lock(key);
+            } catch (MessagingException e) {
+                // unable to lock
+            }
+            if (locked) {
+                Mail m = null;
+                try {
+                    m = srcSpoolRepository.retrieve(key);
+                    if (filterMatches(m, filter)) {
+                        if (dstState != null) {
+                            m.setState(dstState);
+                        }
+                        dstSpoolRepository.store(m);
+                        srcSpoolRepository.remove(m);
+                        count++;
+                    }
+                } catch (MessagingException e) {
+                    // unable to retrieve message
+                } finally {
+                    try {
+                        srcSpoolRepository.unlock(key);
+                    } catch (MessagingException e) {
+                        // unable to unlock
+                    }
+                    ContainerUtil.dispose(m);
+                }
+            }
+        }
+        
+        return count;
+
+    }
+
+    /**
      * Lists all mails from the given repository matching the given filter criteria 
      * 
      * @param spoolRepositoryURL the spool whose item are listed

Modified: james/server/trunk/src/java/org/apache/james/management/SpoolManagementMBean.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/management/SpoolManagementMBean.java?view=diff&rev=476531&r1=476530&r2=476531
==============================================================================
--- james/server/trunk/src/java/org/apache/james/management/SpoolManagementMBean.java (original)
+++ james/server/trunk/src/java/org/apache/james/management/SpoolManagementMBean.java Sat Nov 18 06:46:10 2006
@@ -30,6 +30,24 @@
 public interface SpoolManagementMBean {
 
     /**
+     * Move all mails from the given repository to another repository matching the given filter criteria
+     * 
+     * @phoenix:mx-operation
+     * @phoenix:mx-description Move mails from a spool to another
+     * 
+     * @param srcspoolRepositoryURL the spool whose item are listed
+     * @param srcstate if not NULL, only mails with matching state are returned
+     * @param dstSpoolRepositoryURL the destination spool
+     * @param dstState if not NULL, the state will be changed before storing the message to the new repository.
+     * @param header if not NULL, only mails with at least one header with a value matching headerValueRegex are returned
+     * @param headerValueRegex the regular expression the header must match
+     * @return a counter of moved mails
+     * @throws SpoolManagementException
+     */
+    public int moveSpoolItems(String srcSpoolRepositoryURL, String srcState, String dstSpoolRepositoryURL, String dstState, String header, String headerValueRegex) 
+            throws SpoolManagementException;
+
+    /**
      * List mails on the spool matching the given criteria
      *
      * @phoenix:mx-operation

Modified: james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java?view=diff&rev=476531&r1=476530&r2=476531
==============================================================================
--- james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java (original)
+++ james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java Sat Nov 18 06:46:10 2006
@@ -77,6 +77,7 @@
         "LISTSPOOL",
         "FLUSHSPOOL",
         "DELETESPOOL",
+        "MOVEMAILS",
         "ADDHAM",
         "ADDSPAM",
         "EXPORTBAYESIANDATA",
@@ -499,6 +500,8 @@
         out.println("listspool [spoolrepositoryname] ([header=name] [regex=value])           list all mails which reside in the spool and have an error state");
         out.println("flushspool [spoolrepositoryname] ([key] | [header=name] [regex=value])  try to resend the mail assing to the given key. If no key is given all mails get resend");
         out.println("deletespool [spoolrepositoryname] ([key] | [header=name] [regex=value]) delete the mail assign to the given key. If no key is given all mails get deleted");
+        out.println("movemails [srcSpoolrepositoryname] [dstSpoolrepositoryname] ([header=headername] [regex=regexValue])");
+        out.println("    [srcstate=sourcestate] [dststate=destinationstate]                  move mails from the source repository to the destination repository.");
         out.println("listprocessors [processorname]                                          list names of all processors");
         out.println("listmailets [processorname]                                             list names of all mailets for specified processor");
         out.println("listmatchers [processorname]                                            list names of all mailets for specified processor");
@@ -980,6 +983,75 @@
             out.flush();
 
             out.println("Number of deleted mails: " + count);
+            out.flush();
+
+        } catch (Exception e) {
+            out.println("Error opening the spoolrepository " + e.getMessage());
+            out.flush();
+            getLogger().error("Error opeing the spoolrepository " + e.getMessage());
+        }
+        return true;
+    }
+
+
+    /**
+     * Handler method called upon receipt of a MOVEMAILS command. Returns
+     * whether further commands should be read off the wire.
+     * 
+     * @param argument
+     *            the argument passed in with the command
+     */
+    private boolean doMOVEMAILS(String argument) {
+        String[] args = null;
+
+        if (argument != null)
+            args = argument.split(" ");
+
+        // check if the command was called correct
+        if ((argument == null || argument.trim().equals(""))
+                || (args.length < 2 || args.length > 6)) {
+            writeLoggedFlushedResponse("Usage: MOVEMAILS [srcSpoolrepositoryname] [dstSpoolrepositoryname] ([header=headername] [regex=regexValue]) [srcstate=sourcestate] [dststate=destinationstate]");
+            return true;
+        }
+
+        String srcUrl = args[0];
+        String dstUrl = args[1];
+        
+        String dstState = null;
+        String srcState = null;
+        String header = null;
+        String regex = null;
+
+        for (int i = 2; i < args.length; i++) {
+            if (args[i].startsWith(HEADER_IDENTIFIER)) {
+                header = args[i].substring(HEADER_IDENTIFIER.length());
+            } else if (args[i].startsWith(REGEX_IDENTIFIER)) {
+                header = args[i].substring(REGEX_IDENTIFIER.length());
+            } else if (args[i].startsWith("srcstate=")) {
+                header = args[i].substring("srcstate=".length());
+            } else if (args[i].startsWith("dststate=")) {
+                header = args[i].substring("dststate=".length());
+            } else {
+                writeLoggedResponse("Unexpected parameter "+args[i]);
+                writeLoggedFlushedResponse("Usage: MOVEMAILS [srcSpoolrepositoryname] [dstSpoolrepositoryname] ([header=headername] [regex=regexValue]) [srcstate=sourcestate] [dststate=destinationstate]");
+                return true;
+            }
+        }
+        
+        if ((header != null && regex == null) || (header == null && regex != null)) {
+            if (regex == null) {
+                writeLoggedResponse("Bad parameters: used header without regex");
+            } else {
+                writeLoggedResponse("Bad parameters: used regex without header");
+            }
+            writeLoggedFlushedResponse("Usage: MOVEMAILS [srcSpoolrepositoryname] [dstSpoolrepositoryname] ([header=headername] [regex=regexValue]) [srcstate=sourcestate] [dststate=destinationstate]");
+            return true;
+        }
+
+        try {
+            int count = theConfigData.getSpoolManagement().moveSpoolItems(srcUrl, dstUrl, dstState, new SpoolFilter(srcState,header,regex));
+            
+            out.println("Number of moved mails: " + count);
             out.flush();
 
         } catch (Exception e) {

Modified: james/server/trunk/src/java/org/apache/james/services/SpoolManagementService.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/services/SpoolManagementService.java?view=diff&rev=476531&r1=476530&r2=476531
==============================================================================
--- james/server/trunk/src/java/org/apache/james/services/SpoolManagementService.java (original)
+++ james/server/trunk/src/java/org/apache/james/services/SpoolManagementService.java Sat Nov 18 06:46:10 2006
@@ -33,6 +33,21 @@
     String ROLE = "org.apache.james.services.SpoolManagementService";
 
     /**
+     * Move all mails from the given repository to another repository matching the given filter criteria
+     *
+     * @param srcspoolRepositoryURL the spool whose item are listed
+     * @param dstSpoolRepositoryURL the destination spool
+     * @param dstState if not NULL, the state will be changed before storing the message to the new repository.
+     * @param spoolFilter the filter to select messages from the source repository
+     * @return a counter of moved mails
+     * @throws ServiceException 
+     * @throws MessagingException 
+     * @throws SpoolManagementException
+     */
+    public int moveSpoolItems(String srcSpoolRepositoryURL, String dstSpoolRepositoryURL, String dstState, SpoolFilter filter)
+            throws ServiceException, MessagingException, SpoolManagementException;
+
+    /**
      * Removes all mails from the given repository matching the filter
      *  
      * @param spoolRepositoryURL the spool whose item are listed



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