danny       2002/09/24 08:36:30

  Added:       src/java/org/apache/james/fetchpop ReaderInputStream.java
                        FetchScheduler.xinfo FetchScheduler.java
                        FetchPOP.java
               phoenix-bin/lib commons-net-1.0.0-dev.jar
  Log:
  A new block providing fetchpop functionality using commons-net POP3 library
  Fetch inserts fetched mail into the spoolmanager spool
  
  Revision  Changes    Path
  1.1                  
jakarta-james/src/java/org/apache/james/fetchpop/ReaderInputStream.java
  
  Index: ReaderInputStream.java
  ===================================================================
  /**
   * ReaderInputStream.java
   * 
   * Copyright (C) 24-Sep-2002 The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file. 
   *
   * Danny Angus
   */
  package org.apache.james.fetchpop;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.Reader;
  /**
   * 
   * 
   * Simple class to allow a cast from a java.io.Reader to a java.io.InputStream<br>
   * <br>$Id: ReaderInputStream.java,v 1.1 2002/09/24 15:36:30 danny Exp $
   * @author <A href="mailto:[EMAIL PROTECTED]";>Danny Angus</a>
   * 
   */
  public class ReaderInputStream extends InputStream {
      private Reader reader = null;
      public ReaderInputStream(Reader reader) {
          this.reader = reader;
      }
      /**
       * @see java.io.InputStream#read()
       */
      public int read() throws IOException {
          return reader.read();
      }
  }
  
  
  
  1.1                  
jakarta-james/src/java/org/apache/james/fetchpop/FetchScheduler.xinfo
  
  Index: FetchScheduler.xinfo
  ===================================================================
  <?xml version="1.0"?>
  
  <blockinfo>
  
    <!-- section to describe block -->
    <block>
      <version>1.0</version>
    </block>
  
    <!-- services that are offered by this block -->
    <services>
      <service name="org.apache.avalon.framework.component.Component" version="1.0"/>
    </services>
  
    <dependencies>
      <dependency>
        <service name="org.apache.james.services.MailServer" version="1.0"/>
      </dependency>
      <dependency> 
        <service name="org.apache.avalon.cornerstone.services.scheduler.TimeScheduler" 
version="1.0"/>
      </dependency> 
    </dependencies>  
  </blockinfo>
  
  
  
  1.1                  
jakarta-james/src/java/org/apache/james/fetchpop/FetchScheduler.java
  
  Index: FetchScheduler.java
  ===================================================================
  /**
   * FetchScheduler.java
   * 
   * Copyright (C) 24-Sep-2002 The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file. 
   *
   * Danny Angus
   */
  package org.apache.james.fetchpop;
  import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
  import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.component.DefaultComponentManager;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.james.services.MailServer;
  /**
   *  A class to instantiate and schedule a set of POP mail fetching tasks<br>
   * <br>$Id: FetchScheduler.java,v 1.1 2002/09/24 15:36:30 danny Exp $
   *  @author <A href="mailto:[EMAIL PROTECTED]";>Danny Angus</a>
   *  @see org.apache.james.fetchpop.FetchPOP#configure(Configuration) FetchPOP
   *  
   */
  public class FetchScheduler
      extends AbstractLogEnabled
      implements Component, Configurable, Initializable, Composable {
      private Configuration conf;
      private MailServer server;
      private DefaultComponentManager compMgr;
      /**
       * @see 
org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
       */
      public void configure(Configuration conf) throws ConfigurationException {
          this.conf = conf;
      }
      /**
       * @see org.apache.avalon.framework.activity.Initializable#initialize()
       */
      public void initialize() throws Exception {
          if (conf.getAttribute("enabled").equalsIgnoreCase("true")) {
              TimeScheduler scheduler = (TimeScheduler) 
compMgr.lookup(TimeScheduler.ROLE);
              Configuration[] fetchConfs = conf.getChildren("fetch");
              for (int i = 0; i < fetchConfs.length; i++) {
                  FetchPOP fp = new FetchPOP();
                  Configuration fetchConf = fetchConfs[i];
                  fp.configure(
                      fetchConf,
                      (MailServer) compMgr.lookup(MailServer.ROLE),
                      getLogger().getChildLogger(fetchConf.getAttribute("name")));
                  Integer interval = new 
Integer(fetchConf.getChild("interval").getValue());
                  PeriodicTimeTrigger fetchTrigger = new PeriodicTimeTrigger(0, 
interval.intValue());
                  scheduler.addTrigger(fetchConf.getAttribute("name"), fetchTrigger, 
fp);
              }
              System.out.println("Fetch POP Started ");
          } else {
              getLogger().info("Fetch POP Disabled");
              System.out.println("Fetch POP Disabled");
          }
      }
      /**
       * @see 
org.apache.avalon.framework.component.Composable#compose(ComponentManager)
       */
      public void compose(ComponentManager comp) throws ComponentException {
          compMgr = new DefaultComponentManager(comp);
      }
  }
  
  
  
  1.1                  jakarta-james/src/java/org/apache/james/fetchpop/FetchPOP.java
  
  Index: FetchPOP.java
  ===================================================================
  /**
   * FetchPOP.java
   * 
   * Copyright (C) 24-Sep-2002 The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file. 
   *
   * Danny Angus
   */
  package org.apache.james.fetchpop;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.SocketException;
  import java.util.Enumeration;
  import java.util.Vector;
  
  import javax.mail.MessagingException;
  import javax.mail.internet.MimeMessage;
  
  import org.apache.avalon.cornerstone.services.scheduler.Target;
  import org.apache.avalon.framework.component.DefaultComponentManager;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.commons.net.pop3.POP3Client;
  import org.apache.commons.net.pop3.POP3MessageInfo;
  import org.apache.james.services.MailServer;
  /**
   *
   * A class which fetches mail from a single POP account and inserts it into the 
incoming spool<br>
   * <br>$Id: FetchPOP.java,v 1.1 2002/09/24 15:36:30 danny Exp $
   * @author <A href="mailto:[EMAIL PROTECTED]";>Danny Angus</a>
   * 
   */
  public class FetchPOP implements Target {
      private Configuration conf;
      private DefaultComponentManager compMgr;
      private MailServer server;
      private String popHost;
      private String popUser;
      private String popPass;
      private String popName;
      private Logger logger;
      /**
       * @see 
org.apache.avalon.cornerstone.services.scheduler.Target#targetTriggered(String)
       */
      public void targetTriggered(String arg0) {
          getLogger().debug(popName + " fetcher starting fetch");
          POP3Client pop = new POP3Client();
          try {
              pop.connect(popHost);
              pop.login(popUser, popPass);
              getLogger().debug("login:" + pop.getReplyString());
              pop.setState(POP3Client.TRANSACTION_STATE);
              POP3MessageInfo[] messages = pop.listMessages();
              getLogger().debug("list:" + pop.getReplyString());
              Vector recieved = new Vector();
              for (int i = 0; i < messages.length; i++) {
                  InputStream in = new 
ReaderInputStream(pop.retrieveMessage(messages[i].number));
                  getLogger().debug("retrieve:" + pop.getReplyString());
                  MimeMessage message = new MimeMessage(null, in);
                  in.close();
                  message.addHeader("X-fetchpop", "fetched by james");
                  message.saveChanges();
                  logger.debug("sent message " + message.toString());
                  server.sendMail(message);
                  recieved.add(messages[i]);
              }
              Enumeration enum = recieved.elements();
              while (enum.hasMoreElements()) {
                  POP3MessageInfo element = (POP3MessageInfo) enum.nextElement();
                  pop.deleteMessage(element.number);
                  getLogger().debug("delete:" + pop.getReplyString());
              }
              pop.logout();
              getLogger().debug("logout:" + pop.getReplyString());
              pop.disconnect();
          } catch (SocketException e) {
              getLogger().error(e.getMessage());
          } catch (IOException e) {
              getLogger().error(e.getMessage());
          } catch (MessagingException e) {
              getLogger().error(e.getMessage());
          }
      }
      /**
       * Method configure.
       * &lt;fetchpop enabled="false"&gt;<br>
       *  &lt;!-- you can have as many fetch tasks as you want to        --&gt;<br>
       *  &lt;!-- but each must have a unique name to identify itself by --&gt;<br>
       *  &lt;fetch name="mydomain.com"&gt;<br>
       *      &lt;!-- host name or IP address --&gt;<br>
       *      &lt;host&gt;mail.mydomain.com&lt;/host&gt;<br>
       *      &lt;!-- acount login username --&gt;<br>
       *      &lt;user&gt;username&lt;/user&gt;<br>
       *      &lt;!-- account login password --&gt;<br>
       *      &lt;password&gt;pass&lt;/password&gt;<br>
       *      &lt;!-- Interval to check this account in milliseconds, 60000 is every 
ten minutes --&gt;<br>
       *      &lt;interval&gt;600000&lt;/interval&gt;<br>
       *  &lt;/fetch&gt;<br>
       *  &lt;/fetchpop&gt;<br>
       *  @param conf configuration element for this fetcher:<br>
       * @param server mailserver which can spool fetched mail
       * @param logger child logger of FetchScheduler
       * @throws ConfigurationException
       */
      public void configure(Configuration conf, MailServer server, Logger logger)
          throws ConfigurationException {
          logger.debug("configured fetch");
          this.conf = conf;
          this.server = server;
          this.logger = logger;
          this.popHost = conf.getChild("host").getValue();
          this.popUser = conf.getChild("user").getValue();
          this.popPass = conf.getChild("password").getValue();
          this.popName = conf.getAttribute("name");
      }
      private Logger getLogger() {
          return logger;
      }
  }
  
  
  
  1.1                  jakarta-james/phoenix-bin/lib/commons-net-1.0.0-dev.jar
  
        <<Binary file>>
  
  

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

Reply via email to