From 3a4c89cc6491cba5273bac42509d71d23335af2c Mon Sep 17 00:00:00 2001 From: takuya-o Date: Wed, 23 Feb 2005 13:27:17 +0000 Subject: [PATCH] BUGFIX: When SMTP connection was closed, LDAP connection close. It avoid LDAP server connection timeout. 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 | 19 +++++++-- src/org/jent/checksmtp/LDAPSearch.java | 49 ++++++++++++++++++----- src/org/jent/checksmtp/Processer.java | 2 + src/org/jent/checksmtp/SMTPclient.java | 5 +++ 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/org/jent/checksmtp/ApplicationProperties.java b/src/org/jent/checksmtp/ApplicationProperties.java index 522e33f..7e42ca4 100644 --- a/src/org/jent/checksmtp/ApplicationProperties.java +++ b/src/org/jent/checksmtp/ApplicationProperties.java @@ -15,11 +15,12 @@ import java.util.Properties; *

Properties

*
*
-.port
Service port number. (Default:8725) + *
-.enableRemoteConnect
Permit connect from remote host. (Default:false) *
-.serverHost
STMP Server host name. (Default:mail) *
-.serverPort
STMP Server port. (Default:25) *
-.ladp
If "true" use LDAP search. (Default:false) *
-.ldap.providerUrl
LDAP Provider URL (Default:ldap://localhost:389 - *
-.ldap.root
LDAP Search root (Default:C=JP) + *
-.ldap.baseDn
LDAP Search root (Default:C=JP) *
-.ldap.isSjis
Addhoc patch: When LDAP Server use SJIS, this propertiy set "true" (Default:false) *
-.ldap.attribues
LDAP Search attribues separate by space (Defaut:cn) *
@@ -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() { diff --git a/src/org/jent/checksmtp/LDAPSearch.java b/src/org/jent/checksmtp/LDAPSearch.java index e309b2b..8262a6a 100644 --- a/src/org/jent/checksmtp/LDAPSearch.java +++ b/src/org/jent/checksmtp/LDAPSearch.java @@ -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. + *

LDAP connection open is automatical.

+ *

If you hope aboid LDAP Server cconnection timeout close, you call close().

+ */ + 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."); diff --git a/src/org/jent/checksmtp/Processer.java b/src/org/jent/checksmtp/Processer.java index a540ce4..ec9f84f 100644 --- a/src/org/jent/checksmtp/Processer.java +++ b/src/org/jent/checksmtp/Processer.java @@ -116,6 +116,8 @@ public class Processer implements Runnable { //IGNORE close Exception } } + //LDAP Connection close. + LDAPSearch.close(); } } diff --git a/src/org/jent/checksmtp/SMTPclient.java b/src/org/jent/checksmtp/SMTPclient.java index 9fd92b8..43e445e 100644 --- a/src/org/jent/checksmtp/SMTPclient.java +++ b/src/org/jent/checksmtp/SMTPclient.java @@ -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!! } -- 2.11.0