OSDN Git Service

BUGFIX: When SMTP connection was closed, LDAP connection close. It avoid LDAP server...
authorTakuya Ono <takuya-o@users.sourceforge.jp>
Wed, 23 Feb 2005 13:27:17 +0000 (13:27 +0000)
committerTakuya Ono <takuya-o@users.sourceforge.jp>
Wed, 23 Feb 2005 13:27:17 +0000 (13:27 +0000)
MOD: Change properties name "org.jent.checksmtp.ldap.root" to "org.jent.checksmtp.ldap.baseDn".
ADD: Add properties "org.jent.checksmtp.enableRemoteConnect". It control permit connect from remote host.
ADD: Catch LDAP Time Limit and Not Found Exception

src/org/jent/checksmtp/ApplicationProperties.java
src/org/jent/checksmtp/LDAPSearch.java
src/org/jent/checksmtp/Processer.java
src/org/jent/checksmtp/SMTPclient.java

index 522e33f..7e42ca4 100644 (file)
@@ -15,11 +15,12 @@ import java.util.Properties;
  * <H3>Properties</H3>
  * <DL>
  * <DT> -.port       <DD>Service port number.       (Default:8725)
+ * <DT> -.enableRemoteConnect <DD>Permit connect from remote host. (Default:false)
  * <DT> -.serverHost <DD>STMP Server host name.     (Default:mail)
  * <DT> -.serverPort <DD>STMP Server port.          (Default:25)
  * <DT> -.ladp       <DD>If "true" use LDAP search. (Default:false)
  * <DT> -.ldap.providerUrl <DD>LDAP Provider URL    (Default:ldap://localhost:389
- * <DT> -.ldap.root  <DD>LDAP Search root           (Default:C=JP)
+ * <DT> -.ldap.baseDn <DD>LDAP Search root           (Default:C=JP)
  * <DT> -.ldap.isSjis<DD>Addhoc patch: When LDAP Server use SJIS, this propertiy set "true" (Default:false)
  * <DT> -.ldap.attribues<DD>LDAP Search attribues separate by space (Defaut:cn)
  * </DL>
@@ -27,12 +28,13 @@ import java.util.Properties;
 public class ApplicationProperties
 {
   private static final String PREFIX = "org.jent.checksmtp";
+  private static final String SMTP_PORT = PREFIX + ".port";
+  private static final String SMTP_ENEBLE_REMOTE_CONNECT = PREFIX + ".enableRemoteConnect";
   private static final String SMTP_SERVER_HOST = PREFIX + ".serverHost";
   private static final String SMTP_SERVER_PORT = PREFIX + ".serverPort";
-  private static final String SMTP_PORT = PREFIX + ".port";
   private static final String LDAP_PREFIX = PREFIX + ".ldap";
   private static final String LDAP_PROVIDER_URL = LDAP_PREFIX + ".providerUrl";
-  private static final String LDAP_ROOT = LDAP_PREFIX + ".root";
+  private static final String LDAP_ROOT = LDAP_PREFIX + ".baseDn";
   private static final String LDAP_IS_SJIS = LDAP_PREFIX + ".isSjis";
   private static final String LDAP_IS_ATTRIBUTES = LDAP_PREFIX + ".attributes";
 
@@ -109,6 +111,17 @@ public class ApplicationProperties
   {
     applicationProperties.setProperty(SMTP_PORT, new Integer(port).toString());
   }
+  
+  public static boolean getSmtpEnebleRemoteConnect()
+  {
+    String str = applicationProperties.getProperty(SMTP_ENEBLE_REMOTE_CONNECT);
+    return new Boolean(str).booleanValue();
+  }
+
+  public static void setSmtpEnableRemoteConnect(boolean obj)
+  {
+    applicationProperties.setProperty(SMTP_ENEBLE_REMOTE_CONNECT, Boolean.toString(obj));
+  }
 
   public static String getLdapProviderURL()
   {
index e309b2b..8262a6a 100644 (file)
@@ -6,8 +6,10 @@ import java.util.Properties;
 import java.util.regex.Pattern;
 
 import javax.naming.Context;
+import javax.naming.NameNotFoundException;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.TimeLimitExceededException;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.InitialDirContext;
 import javax.naming.directory.SearchControls;
@@ -27,8 +29,9 @@ public class LDAPSearch {
       if ( ctx==null 
       || (!lastProviderUrl.equals(ApplicationProperties.getLdapProviderURL())) ) {
         try {
-          if ( ctx != null ) { ctx.close(); }
-           //InitialDirContext
+          if ( ctx != null ) { close(); }
+          System.out.println("init  LDAP connection.");
+          //InitialDirContext
           lastProviderUrl = ApplicationProperties.getLdapProviderURL();
           Properties env = new Properties();
           env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
@@ -38,12 +41,36 @@ public class LDAPSearch {
           ctx = new InitialDirContext(env);
         } catch (NamingException e) {
           System.err.println("LDAPSearch init error.");
+          lastProviderUrl = null;
           e.printStackTrace();
         }
       }
     }
   }
   
+  /**
+   * LDAP connection close.
+   * <P>LDAP connection open is automatical.</P>
+   * <P>If you hope aboid LDAP Server cconnection timeout close, you call close().</P>
+   */
+  public static synchronized void close()
+  {
+    try
+    {
+      if (ctx != null)
+      {
+        System.out.println("close LDAP connection.");
+        ctx.close();
+        ctx = null;
+      }
+    }
+    catch (NamingException e)
+    {
+      System.err.println("LDAPSearch init error.");
+      e.printStackTrace();
+    }
+  }
+  
   public static String search(String mail) {
     String ans = "";
 
@@ -51,7 +78,10 @@ public class LDAPSearch {
         return ans;
     }
     if ( ctx==null
-    || (!lastProviderUrl.equals(ApplicationProperties.getLdapProviderURL())) ) {
+      || ( (lastProviderUrl != null) 
+          && (!lastProviderUrl.equals(ApplicationProperties.getLdapProviderURL()))
+      )
+    ) {
          init();
     }
     
@@ -94,16 +124,17 @@ public class LDAPSearch {
         System.err.println("ANS=" + ans);
       }
       enum.close();
+    } catch (TimeLimitExceededException tlEx) {
+      System.out.println("LDAP Search Time Limit.");
+      //IGNORE Exception
+    } catch (NameNotFoundException nnfEx) {
+      System.out.println("LDAP Search Name Not Found.");
+      //IGNORE Exception
     } catch (NamingException e) {
       System.err.println("LDAP Search Error.");
       e.printStackTrace();
       //ctx reflesh
-      try {
-        ctx.close();
-      } catch (NamingException nEx) {
-        //IGNORE Exception
-      }
-      ctx = null;
+      close();
     } catch (UnsupportedEncodingException ueEx) {
       UnsupportedEncodingException e = ueEx;
       System.err.println("LDAP SJIS Error.");
index a540ce4..ec9f84f 100644 (file)
@@ -116,6 +116,8 @@ public class Processer implements Runnable {
           //IGNORE close Exception
         }
       }
+      //LDAP Connection close.
+      LDAPSearch.close();
     }
   }
 
index 9fd92b8..43e445e 100644 (file)
@@ -23,9 +23,14 @@ public class SMTPclient implements Runnable {
                 System.err.println("Open SMTP waiting port. " + serverport);
 
                 try {
+                  if ( ApplicationProperties.getSmtpEnebleRemoteConnect() ) {
+                    System.out.println("Enable remote connection.");
+                    server = new ServerSocket(serverport, 10);
+                  } else {
                     server = new ServerSocket(serverport, 10,
                             InetAddress.getByAddress(
                                 new byte[] { 127, 0, 0, 1 }));
+                  }
                 } catch (IOException e) {
                     e.printStackTrace(); //Unexpected!!
                 }