OSDN Git Service

BUGFIX: Therefor LDAP initialize flag invarted, LDAP can not initialize. When org...
authortakuya-o <takuya-o>
Mon, 21 Feb 2005 16:11:06 +0000 (16:11 +0000)
committertakuya-o <takuya-o>
Mon, 21 Feb 2005 16:11:06 +0000 (16:11 +0000)
FAILSAFE: Unexpected Exception catch and close Sockets.
FAILSAFE: When LDAP NamingException, InitialDirContext refresh.

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

index 0a7a3ab..e309b2b 100644 (file)
@@ -21,20 +21,25 @@ public class LDAPSearch {
   private LDAPSearch() {
   }
 
-  static private void init() {
-    if ( !ApplicationProperties.getLdap() ) {
-      //InitialDirContext
-      lastProviderUrl = ApplicationProperties.getLdapProviderURL();
-      Properties env = new Properties();
-      env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
-                      "com.sun.jndi.ldap.LdapCtxFactory");
-      env.put(Context.PROVIDER_URL, lastProviderUrl);
-  
-      try {
-        ctx = new InitialDirContext(env);
-      } catch (NamingException e) {
-        System.err.println("LDAPSearch init error.");
-        e.printStackTrace();
+  static synchronized private void init() {
+    if ( ApplicationProperties.getLdap() ) {
+      //not initialized ctx or LDAP Provider URL was changed.
+      if ( ctx==null 
+      || (!lastProviderUrl.equals(ApplicationProperties.getLdapProviderURL())) ) {
+        try {
+          if ( ctx != null ) { ctx.close(); }
+           //InitialDirContext
+          lastProviderUrl = ApplicationProperties.getLdapProviderURL();
+          Properties env = new Properties();
+          env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
+                          "com.sun.jndi.ldap.LdapCtxFactory");
+          env.put(Context.PROVIDER_URL, lastProviderUrl);
+    
+          ctx = new InitialDirContext(env);
+        } catch (NamingException e) {
+          System.err.println("LDAPSearch init error.");
+          e.printStackTrace();
+        }
       }
     }
   }
@@ -45,12 +50,9 @@ public class LDAPSearch {
     if (!ApplicationProperties.getLdap()) {
         return ans;
     }
-    synchronized (LDAPSearch.class ) {
-      //not initialized ctx or LDAP Provider URL was changed.
-      if ( ctx==null 
-      || (!lastProviderUrl.equals(ApplicationProperties.getLdapProviderURL())) ) {
-        init();
-      }
+    if ( ctx==null
+    || (!lastProviderUrl.equals(ApplicationProperties.getLdapProviderURL())) ) {
+         init();
     }
     
     Pattern pattern = Pattern.compile("[ \t]+"); //Attributes separate by space.
@@ -95,11 +97,22 @@ public class LDAPSearch {
     } catch (NamingException e) {
       System.err.println("LDAP Search Error.");
       e.printStackTrace();
+      //ctx reflesh
+      try {
+        ctx.close();
+      } catch (NamingException nEx) {
+        //IGNORE Exception
+      }
+      ctx = null;
     } catch (UnsupportedEncodingException ueEx) {
       UnsupportedEncodingException e = ueEx;
       System.err.println("LDAP SJIS Error.");
       e.printStackTrace();
-    }
+    } catch (NullPointerException npEx) { //for failsafe.
+      NullPointerException e = npEx;
+      System.err.println("LDAP Unexpected Error.");
+      e.printStackTrace();
+    } 
 
     return ans;
   }
index a86b0de..a540ce4 100644 (file)
@@ -57,6 +57,8 @@ public class Processer implements Runnable {
     BufferedReader clientReader;
     PrintWriter clientWriter;
 
+    Socket server = null;
+      
     try {
       //Connect to SMTP Server host 
       String servername = ApplicationProperties.getSmtpServerHost();
@@ -65,7 +67,7 @@ public class Processer implements Runnable {
       int serverport = ApplicationProperties.getSmtpServerPort();
 
       //Connection to true SMTP server.
-      Socket server = new Socket(servername, serverport);
+      server = new Socket(servername, serverport);
       serverInput = server.getInputStream();
       serverOutput = server.getOutputStream();
       clientInput = client.getInputStream();
@@ -92,10 +94,28 @@ public class Processer implements Runnable {
       //forServerThread.start();
       ////Use this ThreadforClientThread.run();
       server.close();
+      server = null;
       client.close();
+      client = null;
     } catch (IOException e) {
       System.err.println("Execption occurred in Processer.");
       e.printStackTrace();
+    } finally {
+      //for failsafe Socket close.
+      if (server != null) { 
+        try { 
+          server.close();
+        } catch (IOException ioEx ) {
+          //IGNORE close Exception
+        }
+      }
+      if (client != null) { 
+        try {
+          client.close();
+        } catch (IOException ioEx ) {
+          //IGNORE close Exception
+        }
+      }
     }
   }