Author: norman
Date: Fri Aug 18 12:34:02 2006
New Revision: 432695

URL: http://svn.apache.org/viewvc?rev=432695&view=rev
Log:
Add commands to RemoteManager to train BayesianAnalyzer. Part of JAMES-590

Modified:
    james/server/trunk/src/conf/james-assembly.xml
    james/server/trunk/src/conf/james-config.xml
    
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.java
    
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.xinfo
    
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
    
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandlerConfigurationData.java
    
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
    
james/server/trunk/src/java/org/apache/james/transport/mailets/BayesianAnalysis.java
    
james/server/trunk/src/java/org/apache/james/transport/mailets/BayesianAnalysisFeeder.java
    james/server/trunk/src/java/org/apache/james/util/JDBCBayesianAnalyzer.java

Modified: james/server/trunk/src/conf/james-assembly.xml
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/conf/james-assembly.xml?rev=432695&r1=432694&r2=432695&view=diff
==============================================================================
--- james/server/trunk/src/conf/james-assembly.xml (original)
+++ james/server/trunk/src/conf/james-assembly.xml Fri Aug 18 12:34:02 2006
@@ -70,7 +70,10 @@
     <provide name="James" role="org.apache.james.services.MailServer"/>
     <provide name="thread-manager"
              
role="org.apache.avalon.cornerstone.services.threads.ThreadManager" />
-    <provide name="spoolmanagement" 
role="org.apache.james.services.SpoolManagementService"/>
+    <provide name="spoolmanagement" 
+             role="org.apache.james.services.SpoolManagementService"/>
+    <provide name="database-connections"
+             
role="org.apache.avalon.cornerstone.services.datasources.DataSourceSelector" />
   </block>
 
   <!-- The User Management block  -->

Modified: james/server/trunk/src/conf/james-config.xml
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/conf/james-config.xml?rev=432695&r1=432694&r2=432695&view=diff
==============================================================================
--- james/server/trunk/src/conf/james-config.xml (original)
+++ james/server/trunk/src/conf/james-config.xml Fri Aug 18 12:34:02 2006
@@ -802,6 +802,9 @@
          <!-- 
          <prompt>james&gt;</prompt>
          -->
+         
+         <!-- The repository -->
+         <repositoryPath> db://maildb </repositoryPath>
       </handler>
    </remotemanager>
 

Modified: 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.java?rev=432695&r1=432694&r2=432695&view=diff
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.java 
(original)
+++ 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.java 
Fri Aug 18 12:34:02 2006
@@ -21,9 +21,11 @@
 
 package org.apache.james.remotemanager;
 
+import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector;
 import org.apache.avalon.cornerstone.services.store.Store;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.context.Contextualizable;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.james.core.AbstractJamesService;
@@ -43,7 +45,7 @@
  * @version 1.0.0, 24/04/1999
  */
 public class RemoteManager
-    extends AbstractJamesService implements RemoteManagerMBean {
+    extends AbstractJamesService implements RemoteManagerMBean, 
Contextualizable{
 
     /**
      * A HashMap of (user id, passwords) for James administrators
@@ -76,10 +78,20 @@
     private MailServer mailServer;
 
     /**
-     * There reference to the Store
+     * The reference to the Store
      */
     private Store store;
+    
+    /**
+     * The reference to the DataSourceSelector
+     */
+    private DataSourceSelector dataSourceSelector;
 
+    /**
+     * The reference to the repositorPath
+     */
+    private String repositoryPath;
+    
     public void setUsersStore(UsersStore usersStore) {
         this.usersStore = usersStore;
     }
@@ -99,7 +111,7 @@
     public void setStore(Store store) {
         this.store = store;
     }
-
+    
     /**
      * The configuration data to be passed to the handler
      */
@@ -127,6 +139,8 @@
         SpoolManagementService spoolManagement = 
             (SpoolManagementService) 
componentManager.lookup(SpoolManagementService.ROLE);
         setSpoolManagement(spoolManagement);
+        dataSourceSelector = (DataSourceSelector) 
componentManager.lookup(DataSourceSelector.ROLE);
+               
     }
 
     /**
@@ -148,9 +162,15 @@
             if (promtConfiguration != null) prompt = 
promtConfiguration.getValue();
             if (prompt == null) prompt = ""; 
             else if (!prompt.equals("") && !prompt.endsWith(" ")) prompt += " 
"; 
+            
+            Configuration reposConfiguration = 
handlerConfiguration.getChild("repositoryPath", false);
+            if (reposConfiguration == null) {
+                throw new ConfigurationException("Please configure the 
repositoryPath");
+            }
+            repositoryPath = reposConfiguration.getValue();
         }
     }
-
+  
     /**
      * @see org.apache.james.core.AbstractJamesService#getDefaultPort()
      */
@@ -207,7 +227,6 @@
             return RemoteManager.this.store;
         }
         
-
         /**
          * @see 
org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getUsersRepository()
          */
@@ -239,7 +258,20 @@
         public String getPrompt() {
             return RemoteManager.this.prompt;
         }
+        
+        /**
+         * @see 
org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getRepositoryPath()
+         */
+        public String getRepositoryPath() {
+            return RemoteManager.this.repositoryPath;
+        }
 
+        /**
+         * @see 
org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getDataSourceSelector()
+         */
+        public DataSourceSelector getDataSourceSelector(){
+            return RemoteManager.this.dataSourceSelector;
+        }
     }
 
     /**

Modified: 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.xinfo
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.xinfo?rev=432695&r1=432694&r2=432695&view=diff
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.xinfo 
(original)
+++ 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.xinfo 
Fri Aug 18 12:34:02 2006
@@ -37,5 +37,8 @@
     <dependency>
       <service name="org.apache.james.services.SpoolManagementService" 
version="1.0"/>
     </dependency>
+    <dependency>
+      <service 
name="org.apache.avalon.cornerstone.services.datasources.DataSourceSelector" 
version="1.0"/>
+    </dependency>
   </dependencies>
 </blockinfo>

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?rev=432695&r1=432694&r2=432695&view=diff
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
 Fri Aug 18 12:34:02 2006
@@ -21,15 +21,26 @@
 
 package org.apache.james.remotemanager;
 
+import org.apache.avalon.excalibur.datasource.DataSourceComponent;
+import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
 import org.apache.james.Constants;
 import org.apache.james.management.SpoolFilter;
+import org.apache.james.context.AvalonContextUtilities;
 import org.apache.james.core.AbstractJamesHandler;
 import org.apache.james.services.JamesUser;
 import org.apache.james.services.User;
 import org.apache.james.services.UsersRepository;
+import org.apache.james.util.JDBCBayesianAnalyzer;
 import org.apache.mailet.MailAddress;
 
 import javax.mail.internet.ParseException;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
 import java.net.Socket;
 import java.util.ArrayList;
@@ -48,7 +59,7 @@
  *
  */
 public class RemoteManagerHandler
-    extends AbstractJamesHandler {
+    extends AbstractJamesHandler implements Contextualizable,Initializable {
 
     /**
      * The text string for the ADDUSER command
@@ -136,6 +147,16 @@
     private static final String COMMAND_DELETESPOOL = "DELETESPOOL";
 
     /**
+     * The text string for the ADDHAM command
+     */
+    private static final String COMMAND_ADDHAM = "ADDHAM";
+    
+    /**
+     * The text string for the ADDSPAM command
+     */
+    private static final String COMMAND_ADDSPAM = "ADDSPAM";
+    
+    /**
      * The text string for the QUIT command
      */
     private static final String COMMAND_QUIT = "QUIT";
@@ -144,6 +165,10 @@
      * The text string for the SHUTDOWN command
      */
     private static final String COMMAND_SHUTDOWN = "SHUTDOWN";
+    
+    private static final String HAM = "HAM";
+    
+    private static final String SPAM = "SPAM";
 
     /**
      * The per-service configuration data that applies to all handlers
@@ -154,6 +179,12 @@
      * The current UsersRepository being managed/viewed/modified
      */
     private UsersRepository users;
+    
+    private Context context;
+    
+    private String sqlFileUrl = "file://conf/sqlResources.xml";
+    
+    DataSourceComponent datasource;
 
     /**
      * Set the configuration data for the handler.
@@ -316,6 +347,10 @@
             return doFLUSHSPOOL(argument);
         } else if (command.equals(COMMAND_DELETESPOOL)) {
             return doDELETESPOOL(argument);
+        } else if (command.equals(COMMAND_ADDHAM)) {
+            return doADDHAM(argument);
+        } else if (command.equals(COMMAND_ADDSPAM)) {
+                return doADDSPAM(argument);
         } else if (command.equals(COMMAND_QUIT)) {
             return doQUIT(argument);
         } else if (command.equals(COMMAND_SHUTDOWN)) {
@@ -553,6 +588,8 @@
         out.println("listspool [spoolrepositoryname]                list all 
mails which reside in the spool and have an error state");
         out.println("flushspool [spoolrepositoryname] ([key])       try to 
resend the mail assing to the given key. If no key is given all mails get 
resend");
         out.println("deletespool [spoolrepositoryname] ([key])      delete the 
mail assign to the given key. If no key is given all mails get deleted");
+        out.println("addham [directory]                             feed the 
BayesianAnalysisFeeder with the content of the directory as HAM. One mail per 
file");
+        out.println("addspam [directory]                            feed the 
BayesianAnalysisFeeder with the content of the directory as SPAM. One mail per 
file");
         out.println("shutdown                                       kills the 
current JVM (convenient when James is run as a daemon)");
         out.println("quit                                           close 
connection");
         out.flush();
@@ -1020,5 +1057,140 @@
         writeLoggedFlushedResponse("Unknown command " + argument);
         return true;
     }
+    
+    /**
+     * Handler method called upon receipt of a ADDHAM command. Returns
+     * whether further commands should be read off the wire.
+     * 
+     * @param argument
+     *            the argument passed in with the command
+     */
+    private boolean doADDHAM(String argument) {
+    
+        // check if the command was called correct
+        if (argument == null || argument.trim().equals("")) {
+            writeLoggedFlushedResponse("Usage: ADDHAM [hamdir]");
+            return true;
+        }
+
+        try {
+
+            int count = feedBayesianAnalyzer(argument,HAM);
+            out.println("Feed the BayesianAnalysis with " + count + " HAM");
+            out.flush();
+        
+        } catch (Exception e) {
+            getLogger().error("Error on feeding BayesianAnalysis: " + 
e.getMessage());
+            out.println("Error on feeding BayesianAnalysis: " + 
e.getMessage());
+            out.flush();
+        }
+        return true;
+    }
+    
+    /**
+     * Handler method called upon receipt of a ADDSPAM command. Returns
+     * whether further commands should be read off the wire.
+     * 
+     * @param argument
+     *            the argument passed in with the command
+     */
+    private boolean doADDSPAM(String argument) {
+    
+        // check if the command was called correct
+        if (argument == null || argument.trim().equals("")) {
+            writeLoggedFlushedResponse("Usage: ADDSPAM [spamdir]");
+            return true;
+        }
+
+        try {
+
+            int count = feedBayesianAnalyzer(argument, SPAM);
+            out.println("Feed the BayesianAnalysis with " + count + " SPAM");
+            out.flush();
+            
+        } catch (Exception e) {
+            getLogger().error("Error on feeding BayesianAnalysis: " + 
e.getMessage());
+            out.println("Error on feeding BayesianAnalysis: " + 
e.getMessage());
+            out.flush();
+        }
+        return true;
+    }
+    
+    /**
+     * Helper method to train the BayesianAnalysis
+     *
+     * @param dir The directory which contains the emails which should be used 
to feed the BayesianAnalysis
+     * @param type The type to train. HAM or SPAM
+     * @return count The count of trained messages
+     * @throws IllegalArgumentException Get thrown if the directory is not 
valid
+     */
+    private int feedBayesianAnalyzer(String dir, String type) throws Exception 
{
+    
+        //Clear out any existing word/counts etc..
+        analyzer.clear();
+        
+        File tmpFile = new File(dir);
+        int count = 0;
+    
+        // stop the watchdog temporary 
+        theWatchdog.stop();
+        
+        synchronized(JDBCBayesianAnalyzer.DATABASE_LOCK) {
+
+            // check if the provided dir is really a directory
+            // TODO: Support mbox files
+            if (tmpFile.isDirectory()) {
+                File[] files = tmpFile.listFiles();
+        
+                for (int i = 0; i < files.length; i++) {
+                    if (type.equalsIgnoreCase(HAM)) {
+                        analyzer.addHam(new BufferedReader(new 
FileReader(files[i])));
+                        count++;
+                    } else if (type.equalsIgnoreCase(SPAM)) {
+                        analyzer.addSpam(new BufferedReader(new 
FileReader(files[i])));
+                        count++;
+                    }  
+                }
+              
+                //Update storage statistics.
+                if (type.equalsIgnoreCase(HAM)) {
+                    analyzer.updateHamTokens(datasource.getConnection());
+                } else if (type.equalsIgnoreCase(SPAM)) {
+                    analyzer.updateSpamTokens(datasource.getConnection());
+                } 
+    
+            } else {
+               throw new IllegalArgumentException("Please provide an valid 
directory");
+            }
+        }
+        
+        // start the watchdog again
+        theWatchdog.start();
+        
+        return count;
+    }
+    
+    private JDBCBayesianAnalyzer analyzer = new JDBCBayesianAnalyzer() {
+        protected void delegatedLog(String logString) {
+            getLogger().debug("BayesianAnalysisFeeder: " + logString);
+        }
+    };
+    
+    
+    /**
+     * @see 
org.apache.avalon.framework.context.Contextualizable#contextualize(Context)
+     */
+    public void contextualize(final Context context) throws ContextException {
+        this.context = context;
+    }
 
+    /**
+     * @see org.apache.avalon.framework.activity.Initializable#initialize()
+     */
+    public void initialize() throws Exception {
+        String repos = theConfigData.getRepositoryPath().substring(5);
+        datasource = (DataSourceComponent) 
theConfigData.getDataSourceSelector().select(repos);
+        File sqlFile = AvalonContextUtilities.getFile(context, sqlFileUrl);
+        analyzer.initSqlQueries(datasource.getConnection(), 
sqlFile.getAbsolutePath());
+    }
 }

Modified: 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandlerConfigurationData.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandlerConfigurationData.java?rev=432695&r1=432694&r2=432695&view=diff
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandlerConfigurationData.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandlerConfigurationData.java
 Fri Aug 18 12:34:02 2006
@@ -21,6 +21,7 @@
 
 package org.apache.james.remotemanager;
 
+import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector;
 import org.apache.avalon.cornerstone.services.store.Store;
 import org.apache.james.services.MailServer;
 import org.apache.james.services.SpoolManagementService;
@@ -93,5 +94,19 @@
      * @return the Store
      */
     Store getStore();
+    
+    /**
+     * Returns the RepositoryPath
+     * 
+     * @return the RepositoryPath
+     */
+    String getRepositoryPath();
+    
+    /**
+     * Returns the DataSourceSelector
+     * 
+     * @return the DataSourceSelector
+     */
+    DataSourceSelector getDataSourceSelector();
 
 }

Modified: 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java?rev=432695&r1=432694&r2=432695&view=diff
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
 Fri Aug 18 12:34:02 2006
@@ -74,7 +74,7 @@
         } else {
             throw new ConfigurationException(
                 "Please configure at least on invalid MX network");
-            }
+        }
     }
 
     /**

Modified: 
james/server/trunk/src/java/org/apache/james/transport/mailets/BayesianAnalysis.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/mailets/BayesianAnalysis.java?rev=432695&r1=432694&r2=432695&view=diff
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/transport/mailets/BayesianAnalysis.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/transport/mailets/BayesianAnalysis.java
 Fri Aug 18 12:34:02 2006
@@ -250,7 +250,7 @@
         }
         
         try {
-            analyzer.initSqlQueries(datasource.getConnection(), 
getMailetContext());
+            analyzer.initSqlQueries(datasource.getConnection(), 
getMailetContext().getAttribute("confDir") + "sqlResources.xml");
         } catch (Exception e) {
             throw new MessagingException("Exception initializing queries", e);
         }        

Modified: 
james/server/trunk/src/java/org/apache/james/transport/mailets/BayesianAnalysisFeeder.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/mailets/BayesianAnalysisFeeder.java?rev=432695&r1=432694&r2=432695&view=diff
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/transport/mailets/BayesianAnalysisFeeder.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/transport/mailets/BayesianAnalysisFeeder.java
 Fri Aug 18 12:34:02 2006
@@ -209,7 +209,7 @@
         }
         
         try {
-            analyzer.initSqlQueries(datasource.getConnection(), 
getMailetContext());
+            analyzer.initSqlQueries(datasource.getConnection(), 
getMailetContext().getAttribute("confDir") + "sqlResources.xml");
         } catch (Exception e) {
             throw new MessagingException("Exception initializing queries", e);
         }        

Modified: 
james/server/trunk/src/java/org/apache/james/util/JDBCBayesianAnalyzer.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/JDBCBayesianAnalyzer.java?rev=432695&r1=432694&r2=432695&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/util/JDBCBayesianAnalyzer.java 
(original)
+++ james/server/trunk/src/java/org/apache/james/util/JDBCBayesianAnalyzer.java 
Fri Aug 18 12:34:02 2006
@@ -350,13 +350,13 @@
      * for finding the conf/sqlResources.xml file
      * @throws Exception If any error occurs
      */
-    public void initSqlQueries(Connection conn, 
org.apache.mailet.MailetContext mailetContext) throws Exception {
+    public void initSqlQueries(Connection conn, String file) throws Exception {
         try {
             if (conn.getAutoCommit()) {
                 conn.setAutoCommit(false);
             }
             
-            this.sqlFile = new File((String) 
mailetContext.getAttribute("confDir"), "sqlResources.xml").getCanonicalFile();
+            this.sqlFile = new File(file).getCanonicalFile();
             sqlQueries.init(this.sqlFile, JDBCBayesianAnalyzer.class.getName() 
, conn, getSqlParameters());
             
             checkTables(conn);



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to