SpamController.java :  » Mail-Clients » columba-1.4 » org » columba » mail » spam » Java Open Source

Java Open Source » Mail Clients » columba 1.4 
columba 1.4 » org » columba » mail » spam » SpamController.java
//The contents of this file are subject to the Mozilla Public License Version 1.1
//(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.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo
// Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.

package org.columba.mail.spam;

import java.util.logging.Logger;

import org.columba.api.plugin.IExtension;
import org.columba.api.plugin.IExtensionHandler;
import org.columba.api.plugin.PluginException;
import org.columba.api.plugin.PluginHandlerNotFoundException;
import org.columba.core.logging.Logging;
import org.columba.core.plugin.PluginManager;
import org.columba.mail.folder.IMailbox;
import org.columba.mail.plugin.IExtensionHandlerKeys;

/**
 * High-level wrapper for the spam filter.
 * <p>
 * Class should be used by Columba, to add ham or spam messages to the training
 * database. And to score messages using this training set.
 * <p>
 * 
 * @author fdietz
 */
public class SpamController implements ISpamPlugin {

  /** JDK 1.4+ logging framework logger, used for logging. */
  private static final Logger LOG = Logger
      .getLogger("org.columba.core.gui.htmlviewer");

  /**
   * singleton pattern instance of this class
   */
  private static SpamController instance;

  private ISpamPlugin spamPlugin;

  /**
   * private constructor
   */
  private SpamController() {

    try {
      IExtensionHandler handler =  PluginManager
          .getInstance().getExtensionHandler(IExtensionHandlerKeys.ORG_COLUMBA_MAIL_SPAM);

      IExtension extension = handler.getExtension("SpamAssassin");

      spamPlugin = (ISpamPlugin) extension.instanciateExtension(null);

    } catch (PluginHandlerNotFoundException e) {
      LOG.severe(e.getMessage());
      if (Logging.DEBUG)
        e.printStackTrace();

    } catch (PluginException e) {
      LOG.severe(e.getMessage());
      if (Logging.DEBUG)
        e.printStackTrace();
    }
  }

  /**
   * Get instance of class.
   * 
   * @return spam controller
   */
  public static SpamController getInstance() {
    if (instance == null)
      instance = new SpamController();

    return instance;
  }

  public boolean scoreMessage(IMailbox mailbox, Object uid) throws Exception {

    return spamPlugin.scoreMessage(mailbox, uid);
  }

  public void trainMessageAsSpam(IMailbox mailbox, Object uid)
      throws Exception {
    spamPlugin.trainMessageAsSpam(mailbox, uid);
  }

  public void trainMessageAsHam(IMailbox mailbox, Object uid)
      throws Exception {
    spamPlugin.trainMessageAsHam(mailbox, uid);
  }

  /**
   * Save frequency DB to file.
   */
  public void save() {
    spamPlugin.save();
  }

  public void load() {
    spamPlugin.load();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.