Пример #1
0
 private void logProblem(final MessagingException e) {
   logProblem(e.getMessage());
   final Exception nextException = e.getNextException();
   if (nextException != null) {
     if (nextException instanceof MessagingException) {
       logProblem((MessagingException) nextException);
     } else {
       logProblem(nextException.getMessage());
     }
   }
 }
  /** @see org.apache.avalon.framework.activity.Initializable#initialize() */
  public void initialize() throws Exception {

    getLogger().info("JamesSpoolManager init...");
    spool = (SpoolRepository) compMgr.lookup(SpoolRepository.ROLE);

    MailetLoader mailetLoader = (MailetLoader) compMgr.lookup(MailetLoader.ROLE);
    MatcherLoader matchLoader = (MatcherLoader) compMgr.lookup(MatcherLoader.ROLE);

    // A processor is a Collection of
    processors = new HashMap();

    final Configuration[] processorConfs = conf.getChildren("processor");
    for (int i = 0; i < processorConfs.length; i++) {
      Configuration processorConf = processorConfs[i];
      String processorName = processorConf.getAttribute("name");
      try {
        LinearProcessor processor = new LinearProcessor();
        setupLogger(processor, processorName);
        processor.setSpool(spool);
        processor.initialize();
        processors.put(processorName, processor);

        final Configuration[] mailetConfs = processorConf.getChildren("mailet");
        // Loop through the mailet configuration, load
        // all of the matcher and mailets, and add
        // them to the processor.
        for (int j = 0; j < mailetConfs.length; j++) {
          Configuration c = mailetConfs[j];
          String mailetClassName = c.getAttribute("class");
          String matcherName = c.getAttribute("match");
          Mailet mailet = null;
          Matcher matcher = null;
          try {
            matcher = matchLoader.getMatcher(matcherName);
            // The matcher itself should log that it's been inited.
            if (getLogger().isInfoEnabled()) {
              StringBuffer infoBuffer =
                  new StringBuffer(64)
                      .append("Matcher ")
                      .append(matcherName)
                      .append(" instantiated.");
              getLogger().info(infoBuffer.toString());
            }
          } catch (MessagingException ex) {
            // **** Do better job printing out exception
            if (getLogger().isErrorEnabled()) {
              StringBuffer errorBuffer =
                  new StringBuffer(256)
                      .append("Unable to init matcher ")
                      .append(matcherName)
                      .append(": ")
                      .append(ex.toString());
              getLogger().error(errorBuffer.toString(), ex);
              if (ex.getNextException() != null) {
                getLogger().error("Caused by nested exception: ", ex.getNextException());
              }
            }
            System.err.println("Unable to init matcher " + matcherName);
            System.err.println("Check spool manager logs for more details.");
            // System.exit(1);
            throw ex;
          }
          try {
            mailet = mailetLoader.getMailet(mailetClassName, c);
            if (getLogger().isInfoEnabled()) {
              StringBuffer infoBuffer =
                  new StringBuffer(64)
                      .append("Mailet ")
                      .append(mailetClassName)
                      .append(" instantiated.");
              getLogger().info(infoBuffer.toString());
            }
          } catch (MessagingException ex) {
            // **** Do better job printing out exception
            if (getLogger().isErrorEnabled()) {
              StringBuffer errorBuffer =
                  new StringBuffer(256)
                      .append("Unable to init mailet ")
                      .append(mailetClassName)
                      .append(": ")
                      .append(ex.toString());
              getLogger().error(errorBuffer.toString(), ex);
              if (ex.getNextException() != null) {
                getLogger().error("Caused by nested exception: ", ex.getNextException());
              }
            }
            System.err.println("Unable to init mailet " + mailetClassName);
            System.err.println("Check spool manager logs for more details.");
            // System.exit(1);
            throw ex;
          }
          // Add this pair to the processor
          processor.add(matcher, mailet);
        }

        // Close the processor matcher/mailet lists.
        //
        // Please note that this is critical to the proper operation
        // of the LinearProcessor code.  The processor will not be
        // able to service mails until this call is made.
        processor.closeProcessorLists();

        if (getLogger().isInfoEnabled()) {
          StringBuffer infoBuffer =
              new StringBuffer(64)
                  .append("Processor ")
                  .append(processorName)
                  .append(" instantiated.");
          getLogger().info(infoBuffer.toString());
        }
      } catch (Exception ex) {
        if (getLogger().isErrorEnabled()) {
          StringBuffer errorBuffer =
              new StringBuffer(256)
                  .append("Unable to init processor ")
                  .append(processorName)
                  .append(": ")
                  .append(ex.toString());
          getLogger().error(errorBuffer.toString(), ex);
        }
        throw ex;
      }
    }
    if (getLogger().isInfoEnabled()) {
      StringBuffer infoBuffer =
          new StringBuffer(64)
              .append("Spooler Manager uses ")
              .append(numThreads)
              .append(" Thread(s)");
      getLogger().info(infoBuffer.toString());
    }

    active = true;
    numActive = 0;
    spoolThreads = new java.util.ArrayList(numThreads);
    for (int i = 0; i < numThreads; i++) {
      Thread reader = new Thread(this, "Spool Thread #" + i);
      spoolThreads.add(reader);
      reader.start();
    }
  }
Пример #3
0
  /* (non-Javadoc)
   * @see org.claros.commons.mail.protocols.Protocol#connect(int)
   */
  public ConnectionMetaHandler connect(int connectType)
      throws SystemException, ConnectionException, ServerDownException {
    Folder fold = null;
    try {
      if (handler == null || handler.getStore() == null || !handler.getStore().isConnected()) {
        Properties props = new Properties();
        //				props.setProperty("mail.imap.separatestoreconnection", "true");
        //				props.setProperty("mail.imap.connectionpoolsize", "0");
        //				props.setProperty("mail.imap.connectionpooltimeout", "100");

        //				props.setProperty("mail.debug", "true");
        Session session = Session.getDefaultInstance(props);
        log.debug("session instance initiated");
        handler = new ConnectionMetaHandler();
        handler.setStore(session.getStore(profile.getProtocol()));
        log.debug("session store set. protocol is: " + profile.getProtocol());
        handler
            .getStore()
            .connect(
                profile.getFetchServer(),
                profile.getIFetchPort(),
                auth.getUsername(),
                auth.getPassword());
        if (handler.getStore().isConnected()) {
          log.debug("Store has been connected... Successful");
        } else {
          log.warn("Connection unsuccessfull...!!");
        }
      }
      fold = handler.getStore().getFolder(Constants.FOLDER_INBOX(profile));

      HashMap imapUserFolders = (HashMap) imapFolders.get(auth.getUsername());
      imapUserFolders.put("INBOX", fold);
      imapFolders.put(auth.getUsername(), imapUserFolders);

      handler.setMbox(fold);
      log.debug("Got mailbox folder. Folder is: " + fold.getFullName());
      handler.setTotalMessagesCount(fold.getMessageCount());
      log.debug("Message Count:" + handler.getTotalMessagesCount());
    } catch (FolderNotFoundException e) {
      log.fatal(
          profile.getProtocol()
              + " cannot identify the INBOX folder. Please check your folder-namespace variable at config.xml.");
      throw new SystemException(e);
    } catch (NoSuchProviderException e) {
      log.fatal(profile.getProtocol() + " provider could not be found.");
      throw new SystemException(e);
    } catch (MessagingException e) {
      Exception ne = e.getNextException();
      if (ne != null) {
        if (ne instanceof ConnectException || ne instanceof IOException) {
          throw new ServerDownException("Server is unreachable.");
        }
      }
      log.error("Connection could not be established." + e.getMessage());
      //			throw new ConnectionException(e);
    } catch (Exception e) {
      log.error("An unknown exception while connect.", e);
    }
    return handler;
  }