From ce222b113f944118487e3cb9f5f0a5a738d310c2 Mon Sep 17 00:00:00 2001 From: takuya-o Date: Mon, 21 Feb 2005 16:11:06 +0000 Subject: [PATCH] BUGFIX: Therefor LDAP initialize flag invarted, LDAP can not initialize. When org.jent.checksmtp.ladp=true, error occurred. FAILSAFE: Unexpected Exception catch and close Sockets. FAILSAFE: When LDAP NamingException, InitialDirContext refresh. --- src/org/jent/checksmtp/LDAPSearch.java | 55 +++++++++++++++++++++------------- src/org/jent/checksmtp/Processer.java | 22 +++++++++++++- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/org/jent/checksmtp/LDAPSearch.java b/src/org/jent/checksmtp/LDAPSearch.java index 0a7a3ab..e309b2b 100644 --- a/src/org/jent/checksmtp/LDAPSearch.java +++ b/src/org/jent/checksmtp/LDAPSearch.java @@ -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; } diff --git a/src/org/jent/checksmtp/Processer.java b/src/org/jent/checksmtp/Processer.java index a86b0de..a540ce4 100644 --- a/src/org/jent/checksmtp/Processer.java +++ b/src/org/jent/checksmtp/Processer.java @@ -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 + } + } } } -- 2.11.0