manifoldcf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwri...@apache.org
Subject svn commit: r1552715 - in /manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email: EmailConnector.java EmailSession.java
Date Fri, 20 Dec 2013 16:10:30 GMT
Author: kwright
Date: Fri Dec 20 16:10:30 2013
New Revision: 1552715

URL: http://svn.apache.org/r1552715
Log:
Separate session management into a new class.

Added:
    manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailSession.java
  (with props)
Modified:
    manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailConnector.java

Modified: manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailConnector.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailConnector.java?rev=1552715&r1=1552714&r2=1552715&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailConnector.java
(original)
+++ manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailConnector.java
Fri Dec 20 16:10:30 2013
@@ -70,14 +70,18 @@ public class EmailConnector extends org.
 
   // Local variables.
   protected long sessionExpiration = -1L;
+  
+  // Parameters for establishing a session
+  
   protected String server = null;
   protected String port = null;
   protected String username = null;
   protected String password = null;
   protected String protocol = null;
-  protected Map<String, String> properties = new HashMap<String,String>();
-  private Session session = null;
-  private Store store = null;
+  protected Properties properties = new Properties();
+  
+  // Local session handle
+  protected EmailSession session = null;
 
   private static Map<String,String> providerMap;
   static
@@ -111,7 +115,7 @@ public class EmailConnector extends org.
       if (cn.getType().equals(EmailConfig.NODE_PROPERTIES)) {
         String findParameterName = cn.getAttributeValue(EmailConfig.ATTRIBUTE_NAME);
         String findParameterValue = cn.getAttributeValue(EmailConfig.ATTRIBUTE_VALUE);
-        this.properties.put(findParameterName, findParameterValue);
+        this.properties.setProperty(findParameterName, findParameterValue);
       }
     }
   }
@@ -169,10 +173,11 @@ public class EmailConnector extends org.
     finalizeConnection();
     getSession();
     try {
-      Folder defaultFolder = store.getDefaultFolder();
+      Folder defaultFolder = session.openDefaultFolder();
       if (defaultFolder == null) {
         throw new ManifoldCFException("Error checking the connection: No default folder.");
       }
+      session.closeFolder(defaultFolder);
     } catch (MessagingException e) {
       handleMessagingException(e,"checking the connection");
     }
@@ -256,8 +261,10 @@ public class EmailConnector extends org.
   public void addSeedDocuments(ISeedingActivity activities,
     DocumentSpecification spec, long startTime, long endTime, int jobMode)
     throws ManifoldCFException, ServiceInterruption {
+
     getSession();
-    int i = 0, j = 0;
+
+    int i = 0;
     Map<String,String> findMap = new HashMap<String,String>();
     List<String> folderNames = new ArrayList<String>();
     while (i < spec.getChildCount()) {
@@ -277,7 +284,7 @@ public class EmailConnector extends org.
     for (String folderName : folderNames)
     {
       try {
-        Folder folder = openFolder(folderName);
+        Folder folder = session.openFolder(folderName);
         try
         {
           Message[] messages = findMessages(folder, startTime, endTime, findMap);
@@ -288,7 +295,7 @@ public class EmailConnector extends org.
         }
         finally
         {
-          folder.close(false);
+          session.closeFolder(folder);
         }
       } catch (MessagingException e) {
         handleMessagingException(e, "finding emails");
@@ -297,14 +304,6 @@ public class EmailConnector extends org.
 
   }
 
-  private Folder openFolder(String folderName)
-    throws MessagingException
-  {
-    Folder thisFolder = store.getFolder(folderName);
-    thisFolder.open(Folder.READ_ONLY);
-    return thisFolder;
-  }
-
   /*
   This method will return the list of messages which matches the given criteria
   */
@@ -348,27 +347,18 @@ public class EmailConnector extends org.
     
     Message[] result;
     if (searchTerm == null)
-      result = folder.getMessages();
+      result = session.getMessages(folder);
     else
-      result = folder.search(searchTerm);
+      result = session.search(folder, searchTerm);
     return result;
   }
 
-  private void getSession()
+  protected void getSession()
     throws ManifoldCFException, ServiceInterruption {
     if (session == null) {
-      // Create empty properties
-      Properties props = new Properties();
-      // Get session
       try {
-        Session thisSession = Session.getDefaultInstance(props, null);
-        Store thisStore = thisSession.getStore(providerMap.get(protocol));
-        thisStore.connect(server, username, password);
-
-        
-        session = thisSession;
-        store = thisStore;
-        
+        session = new EmailSession(server, port, username, password,
+          providerMap.get(protocol), properties);
       } catch (MessagingException e) {
         handleMessagingException(e, "connecting");
       }
@@ -376,15 +366,15 @@ public class EmailConnector extends org.
     sessionExpiration = System.currentTimeMillis() + EmailConfig.SESSION_EXPIRATION_MILLISECONDS;
   }
 
-  private void finalizeConnection() {
-    try {
-      if (store != null)
-        store.close();
-    } catch (MessagingException e) {
-      Logging.connectors.warn("Error while closing connection to server: " + e.getMessage(),e);
-    } finally {
-      store = null;
-      session = null;
+  protected void finalizeConnection() {
+    if (session != null) {
+      try {
+        session.close();
+      } catch (MessagingException e) {
+        Logging.connectors.warn("Error while closing connection to server: " + e.getMessage(),e);
+      } finally {
+        session = null;
+      }
     }
   }
 
@@ -475,7 +465,7 @@ public class EmailConnector extends org.
           Folder folder = openFolders.get(folderName);
           if (folder == null)
           {
-            folder = openFolder(folderName);
+            folder = session.openFolder(folderName);
             openFolders.put(folderName,folder);
           }
           
@@ -487,7 +477,8 @@ public class EmailConnector extends org.
           MessageIDTerm messageIDTerm = new MessageIDTerm(id);
           Message[] message = null;
 
-          message = folder.search(messageIDTerm);
+          message = session.search(folder, messageIDTerm);
+
           for (Message msg : message) {
             RepositoryDocument rd = new RepositoryDocument();
             Date setDate = msg.getSentDate();
@@ -585,7 +576,7 @@ public class EmailConnector extends org.
       {
         try
         {
-          f.close(false);
+          session.closeFolder(f);
         }
         catch (MessagingException e)
         {
@@ -1031,23 +1022,15 @@ public class EmailConnector extends org.
     throws ManifoldCFException, ServiceInterruption
   {
     getSession();
-    List<String> folderList = new ArrayList<String>();
     try
     {
-      Folder[] folders = store.getDefaultFolder().list("*");
-      for (Folder folder : folders)
-      {
-        if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0)
-          folderList.add(folder.getFullName());
-      }
+      return session.listFolders();
     }
     catch (MessagingException e)
     {
       handleMessagingException(e,"getting folder list");
+      return null;
     }
-    String[] rval = folderList.toArray(new String[0]);
-    java.util.Arrays.sort(rval);
-    return rval;
   }
 
 

Added: manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailSession.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailSession.java?rev=1552715&view=auto
==============================================================================
--- manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailSession.java
(added)
+++ manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailSession.java
Fri Dec 20 16:10:30 2013
@@ -0,0 +1,134 @@
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (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.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+package org.apache.manifoldcf.crawler.connectors.email;
+
+import java.io.*;
+import java.util.*;
+import javax.mail.*;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
+import javax.mail.search.*;
+
+/** This class represents a raw email session, without any protection
+* from threads waiting on sockets, etc.
+*/
+public class EmailSession
+{
+  protected final String server;
+  protected final String port;
+  protected final String username;
+  protected final String password;
+  protected final String protocol;
+  protected final Properties properties;
+
+  private Session session = null;
+  private Store store = null;
+
+  /** Create a session */
+  public EmailSession(String server, String port, String username, String password,
+    String protocol, Properties properties)
+    throws MessagingException
+  {
+    this.server = server;
+    this.port = port;
+    this.username = username;
+    this.password = password;
+    this.protocol = protocol;
+    this.properties = properties;
+    
+    // Now, try to connect
+    Session thisSession = Session.getDefaultInstance(properties, null);
+    Store thisStore = thisSession.getStore(protocol);
+    thisStore.connect(server, username, password);
+
+    session = thisSession;
+    store = thisStore;
+  }
+  
+  public String[] listFolders()
+    throws MessagingException
+  {
+    if (store != null)
+    {
+      List<String> folderList = new ArrayList<String>();
+      Folder[] folders = store.getDefaultFolder().list("*");
+      for (Folder folder : folders)
+      {
+        if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0)
+          folderList.add(folder.getFullName());
+      }
+      String[] rval = folderList.toArray(new String[0]);
+      java.util.Arrays.sort(rval);
+      return rval;
+    }
+    return null;
+  }
+  
+  public Folder openDefaultFolder()
+    throws MessagingException
+  {
+    if (store != null)
+    {
+      Folder thisFolder = store.getDefaultFolder();
+      thisFolder.open(Folder.READ_ONLY);
+      return thisFolder;
+    }
+    return null;
+  }
+
+  public Folder openFolder(String folderName)
+    throws MessagingException
+  {
+    if (store != null)
+    {
+      Folder thisFolder = store.getFolder(folderName);
+      thisFolder.open(Folder.READ_ONLY);
+      return thisFolder;
+    }
+    return null;
+  }
+  
+  public void closeFolder(Folder folder)
+    throws MessagingException
+  {
+    folder.close(false);
+  }
+  
+  public Message[] getMessages(Folder folder)
+    throws MessagingException
+  {
+    return folder.getMessages();
+  }
+  
+  public Message[] search(Folder folder, SearchTerm searchTerm)
+    throws MessagingException
+  {
+    return folder.search(searchTerm);
+  }
+  
+  public void close()
+    throws MessagingException
+  {
+    if (store != null)
+    {
+      store.close();
+      store = null;
+    }
+    session = null;
+  }
+}
\ No newline at end of file

Propchange: manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailSession.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: manifoldcf/branches/CONNECTORS-553/connectors/email/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/email/EmailSession.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message