Author: norman
Date: Fri Sep 1 14:02:51 2006
New Revision: 439481
URL: http://svn.apache.org/viewvc?rev=439481&view=rev
Log:
Add methods to BayesianAnalyterManagment and RemoteManager to import and export
bayesian data. See JAMES-602
Modified:
james/server/trunk/src/java/org/apache/james/management/BayesianAnalyzerManagement.java
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
james/server/trunk/src/java/org/apache/james/services/BayesianAnalyzerManagementService.java
Modified:
james/server/trunk/src/java/org/apache/james/management/BayesianAnalyzerManagement.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/management/BayesianAnalyzerManagement.java?rev=439481&r1=439480&r2=439481&view=diff
==============================================================================
---
james/server/trunk/src/java/org/apache/james/management/BayesianAnalyzerManagement.java
(original)
+++
james/server/trunk/src/java/org/apache/james/management/BayesianAnalyzerManagement.java
Fri Sep 1 14:02:51 2006
@@ -25,10 +25,14 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.io.PrintWriter;
import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
import net.fortuna.mstor.data.MboxFile;
@@ -48,6 +52,9 @@
import org.apache.james.services.BayesianAnalyzerManagementService;
import org.apache.james.util.JDBCBayesianAnalyzer;
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
/**
* Management for BayesianAnalyzer
*/
@@ -61,7 +68,6 @@
private Context context;
private String sqlFileUrl = "file://conf/sqlResources.xml";
-
/**
* @see
org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
*/
@@ -147,16 +153,16 @@
* @see
org.apache.james.services.BayesianAnalyzerManagementService#addHamFromMbox(String)
*/
public int addHamFromMbox(String file) throws FileNotFoundException,
IllegalArgumentException, IOException, SQLException,
BayesianAnalyzerManagementException {
- if (repos == null) throw new
BayesianAnalyzerManagementException("RepositoryPath not configured");
- return feedBayesianAnalyzerFromMbox(file,HAM);
+ if (repos == null) throw new
BayesianAnalyzerManagementException("RepositoryPath not configured");
+ return feedBayesianAnalyzerFromMbox(file,HAM);
}
/**
* @see
org.apache.james.services.BayesianAnalyzerManagementService#addSpamFromMbox(String)
*/
public int addSpamFromMbox(String file) throws FileNotFoundException,
IllegalArgumentException, IOException, SQLException,
BayesianAnalyzerManagementException {
- if (repos == null) throw new
BayesianAnalyzerManagementException("RepositoryPath not configured");
- return feedBayesianAnalyzerFromMbox(file,SPAM);
+ if (repos == null) throw new
BayesianAnalyzerManagementException("RepositoryPath not configured");
+ return feedBayesianAnalyzerFromMbox(file,SPAM);
}
/**
@@ -258,9 +264,93 @@
return count;
}
+ /**
+ * @see
org.apache.james.services.BayesianAnalyzerManagementService#exportData(String)
+ */
+ public void exportData(String file) throws IOException,
BayesianAnalyzerManagementException, SQLException {
+ if (repos == null) throw new
BayesianAnalyzerManagementException("RepositoryPath not configured");
+
+ synchronized(JDBCBayesianAnalyzer.DATABASE_LOCK) {
+ analyzer.loadHamNSpam(component.getConnection());
+
+ int hamMessageCount = analyzer.getHamMessageCount();
+ int spamMessageCount = analyzer.getSpamMessageCount();
+ Map hamTokenCounts = analyzer.getHamTokenCounts();
+ Map spamTokenCounts = analyzer.getSpamTokenCounts();
+
+ XStream xstream = new XStream(new DomDriver());
+ xstream.alias("bayesianAnalyzer", BayesianAnalyzerXml.class);
+ PrintWriter printwriter = new PrintWriter(new
FileOutputStream(file));
+ printwriter.println(xstream.toXML(new
BayesianAnalyzerXml(hamMessageCount,spamMessageCount,hamTokenCounts,spamTokenCounts)));
+ printwriter.close();
+ }
+ }
+
+ /**
+ * @see
org.apache.james.services.BayesianAnalyzerManagementService#importData(String)
+ */
+ public void importData(String file) throws IOException,
BayesianAnalyzerManagementException, SQLException, FileNotFoundException {
+ if (repos == null) throw new
BayesianAnalyzerManagementException("RepositoryPath not configured");
+
+ synchronized(JDBCBayesianAnalyzer.DATABASE_LOCK){
+ XStream xstream = new XStream(new DomDriver());
+
+ BayesianAnalyzerXml bAnalyzerXml = (BayesianAnalyzerXml)
xstream.fromXML(new FileReader(file));
+
+ // clear old data
+ analyzer.clear();
+ analyzer.tokenCountsClear();
+
+ //TODO: Drop old corpus in database;
+
+ // add the new data
+ analyzer.setHamMessageCount(bAnalyzerXml.getHamMessageCount());
+ analyzer.setSpamMessageCount(bAnalyzerXml.getSpamMessageCount());
+ analyzer.setHamTokenCounts(bAnalyzerXml.getHamTokenCounts());
+ analyzer.setSpamTokenCounts(bAnalyzerXml.getSpamTokenCounts());
+ analyzer.updateHamTokens(component.getConnection());
+ analyzer.updateSpamTokens(component.getConnection());
+ }
+
+ }
+
private JDBCBayesianAnalyzer analyzer = new JDBCBayesianAnalyzer() {
protected void delegatedLog(String logString) {
// no logging
}
};
+
+ /**
+ * Inner class to represent the data in an xml file
+ */
+ private static class BayesianAnalyzerXml {
+ private int hamMessageCount = 0;
+ private int spamMessageCount = 0;
+ private Map hamTokenCounts = new HashMap();
+ private Map spamTokenCounts = new HashMap();
+
+ public BayesianAnalyzerXml(int hamMessageCount, int spamMessageCount,
Map hamTokenCounts, Map spamTokenCounts) {
+ this.hamMessageCount = hamMessageCount;
+ this.spamMessageCount = spamMessageCount;
+ this.hamTokenCounts = hamTokenCounts;
+ this.spamTokenCounts = spamTokenCounts;
+ }
+
+ public int getHamMessageCount() {
+ return hamMessageCount;
+ }
+
+ public int getSpamMessageCount() {
+ return spamMessageCount;
+ }
+
+ public Map getHamTokenCounts() {
+ return hamTokenCounts;
+ }
+
+ public Map getSpamTokenCounts() {
+ return spamTokenCounts;
+ }
+
+ }
}
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=439481&r1=439480&r2=439481&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 Sep 1 14:02:51 2006
@@ -157,6 +157,10 @@
*/
private static final String COMMAND_ADDSPAM = "ADDSPAM";
+ private static final String COMMAND_EXPORTBAYESIANDATA =
"EXPORTBAYESIANDATA";
+
+ private static final String COMMAND_IMPORTBAYESIANDATA =
"IMPORTBAYESIANDATA";
+
/**
* The text string for the QUIT command
*/
@@ -343,7 +347,11 @@
} else if (command.equals(COMMAND_ADDHAM)) {
return doADDHAM(argument);
} else if (command.equals(COMMAND_ADDSPAM)) {
- return doADDSPAM(argument);
+ return doADDSPAM(argument);
+ } else if (command.equals(COMMAND_EXPORTBAYESIANDATA)) {
+ return doEXPORTBAYESIANDATA(argument);
+ } else if (command.equals(COMMAND_IMPORTBAYESIANDATA)) {
+ return doIMPORTBAYESIANDATA(argument);
} else if (command.equals(COMMAND_QUIT)) {
return doQUIT(argument);
} else if (command.equals(COMMAND_SHUTDOWN)) {
@@ -606,6 +614,8 @@
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 dir/mbox [directory/mbox] feed the
BayesianAnalysisFeeder with the content of the directory or mbox file as HAM");
out.println("addspam dir/mbox [directory/mbox] feed the
BayesianAnalysisFeeder with the content of the directory or mbox file as SPAM");
+ out.println("exportbayesiandata [file] export the
BayesianAnalysis data to a xml file");
+ out.println("importbayesiandata [file] import the
BayesianAnalysis data from a xml file");
out.println("shutdown kills the
current JVM (convenient when James is run as a daemon)");
out.println("quit close
connection");
out.flush();
@@ -1192,6 +1202,92 @@
if (exception != null) {
getLogger().error("Error on feeding BayesianAnalysis: " +
exception);
out.println("Error on feeding BayesianAnalysis: " + exception);
+ out.flush();
+ }
+ return true;
+ }
+
+
+
+ private boolean doEXPORTBAYESIANDATA(String argument) {
+ String exception = null;
+
+ // check if the command was called correct
+ if (argument == null || argument.trim().equals("")) {
+ writeLoggedFlushedResponse("Usage: EXPORTBAYESIANALYZERDATA
[dir]");
+ return true;
+ }
+
+ try {
+
+ // stop watchdog cause feeding can take some time
+ theWatchdog.stop();
+
+ theConfigData.getBayesianAnalyzerManagement().exportData(argument);
+ out.println("Exported the BayesianAnalysis data");
+ out.flush();
+
+ } catch (SQLException e) {
+ exception = e.getMessage();
+ } catch (FileNotFoundException e) {
+ exception = e.getMessage();
+ } catch (IllegalArgumentException e) {
+ exception = e.getMessage();
+ } catch (IOException e) {
+ exception = e.getMessage();
+ } catch (BayesianAnalyzerManagementException e) {
+ writeLoggedFlushedResponse("Command disabled. Configure
BayesianAnalyzerMangement to enable it");
+ return true;
+ } finally {
+ theWatchdog.start();
+ }
+
+ // check if any exception was thrown
+ if (exception != null) {
+ getLogger().error("Error on exporting BayesianAnalysis data: " +
exception);
+ out.println("Error on exporting BayesianAnalysis data: " +
exception);
+ out.flush();
+ }
+ return true;
+ }
+
+ private boolean doIMPORTBAYESIANDATA(String argument) {
+ String exception = null;
+
+ // check if the command was called correct
+ if (argument == null || argument.trim().equals("")) {
+ writeLoggedFlushedResponse("Usage: IMPORTBAYESIANALYZERDATA
[dir]");
+ return true;
+ }
+
+ try {
+
+ // stop watchdog cause feeding can take some time
+ theWatchdog.stop();
+
+ theConfigData.getBayesianAnalyzerManagement().importData(argument);
+ out.println("Imported the BayesianAnalysis data");
+ out.flush();
+
+ } catch (SQLException e) {
+ exception = e.getMessage();
+ } catch (FileNotFoundException e) {
+ exception = e.getMessage();
+ } catch (IllegalArgumentException e) {
+ exception = e.getMessage();
+ } catch (IOException e) {
+ exception = e.getMessage();
+ } catch (BayesianAnalyzerManagementException e) {
+ writeLoggedFlushedResponse("Command disabled. Configure
BayesianAnalyzerMangement to enable it");
+ return true;
+ } finally {
+ theWatchdog.start();
+ }
+
+ // check if any exception was thrown
+ if (exception != null) {
+ getLogger().error("Error on importing BayesianAnalysis data: " +
exception);
+ out.println("Error on imporitng BayesianAnalysis data: " +
exception);
out.flush();
}
return true;
Modified:
james/server/trunk/src/java/org/apache/james/services/BayesianAnalyzerManagementService.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/services/BayesianAnalyzerManagementService.java?rev=439481&r1=439480&r2=439481&view=diff
==============================================================================
---
james/server/trunk/src/java/org/apache/james/services/BayesianAnalyzerManagementService.java
(original)
+++
james/server/trunk/src/java/org/apache/james/services/BayesianAnalyzerManagementService.java
Fri Sep 1 14:02:51 2006
@@ -82,4 +82,25 @@
* @throws BayesianAnalyzerManagementException If the service is not
configured
*/
public int addHamFromMbox(String file) throws FileNotFoundException,
IllegalArgumentException, IOException, SQLException,
BayesianAnalyzerManagementException;
+
+ /**
+ * Export the data to a xml file
+ *
+ * @param file The filename to store the data
+ * @throws IOException
+ * @throws BayesianAnalyzerManagementException If the service is not
configured
+ * @throws SQLException
+ */
+ public void exportData(String file) throws IOException,
BayesianAnalyzerManagementException, SQLException;
+
+ /**
+ * Import the data from a xml file
+ *
+ * @param file The filename to export data from
+ *
+ * @throws IOException
+ * @throws BayesianAnalyzerManagementException IF the service is not
configured
+ * @throws SQLException
+ */
+ public void importData(String file) throws IOException,
BayesianAnalyzerManagementException, SQLException;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]