cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r585753 - /cocoon/trunk/blocks/cocoon-naming/cocoon-naming-impl/src/main/java/org/apache/cocoon/components/naming/LDAPEntryManager.java
Date Wed, 17 Oct 2007 23:23:57 GMT
Author: vgritsenko
Date: Wed Oct 17 16:23:56 2007
New Revision: 585753

URL: http://svn.apache.org/viewvc?rev=585753&view=rev
Log:
fix error handling
fix logging
use commons logging

Modified:
    cocoon/trunk/blocks/cocoon-naming/cocoon-naming-impl/src/main/java/org/apache/cocoon/components/naming/LDAPEntryManager.java

Modified: cocoon/trunk/blocks/cocoon-naming/cocoon-naming-impl/src/main/java/org/apache/cocoon/components/naming/LDAPEntryManager.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-naming/cocoon-naming-impl/src/main/java/org/apache/cocoon/components/naming/LDAPEntryManager.java?rev=585753&r1=585752&r2=585753&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-naming/cocoon-naming-impl/src/main/java/org/apache/cocoon/components/naming/LDAPEntryManager.java
(original)
+++ cocoon/trunk/blocks/cocoon-naming/cocoon-naming-impl/src/main/java/org/apache/cocoon/components/naming/LDAPEntryManager.java
Wed Oct 17 16:23:56 2007
@@ -21,7 +21,6 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-
 import javax.naming.Context;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
@@ -35,11 +34,12 @@
 
 import org.apache.avalon.excalibur.pool.Recyclable;
 import org.apache.avalon.framework.activity.Disposable;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.avalon.framework.parameters.ParameterException;
 import org.apache.avalon.framework.parameters.Parameterizable;
 import org.apache.avalon.framework.parameters.Parameters;
+
 import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.util.AbstractLogEnabled;
 
 /**
  *	The <code>LDAPEntryManager</code> is an Avalon Component for managing Entries
in a Javax Naming Directory.
@@ -58,9 +58,9 @@
  *
  * @version $Id$
  */
-public class LDAPEntryManager
-	extends AbstractLogEnabled
-	implements EntryManager, Parameterizable, Disposable, Recyclable {
+public class LDAPEntryManager extends AbstractLogEnabled
+	                          implements EntryManager, Parameterizable, Disposable,
+                                         Recyclable {
 
 	/* congiguration parameter names */
 	protected final static String LDAP_HOST_PARAM = "ldap-host";
@@ -69,250 +69,220 @@
 	protected final static String LDAP_BASE_PARAM = "ldap-base";
 	
 	/* internal state */
-	private boolean disposed = false;
+	private boolean disposed;
 	
 	/* internal instance variables */
-	protected DirContext context = null;
-	protected Hashtable environment = null;
-
-	/* default constructor */
-	public LDAPEntryManager() {
-	}
+	protected DirContext context;
+	protected Hashtable environment;
 
+    
 	/** Avalon, Parameterize this Class */
 	public void parameterize(Parameters params) throws ParameterException {
-		if (getLogger ().isDebugEnabled ()) {
-			getLogger ().debug ("LDAPEntryManager parameterizing");
-		}
-		if (this.environment == null) {
-			String host = params.getParameter (LDAP_HOST_PARAM);
-			if (getLogger ().isDebugEnabled ()) {
-				getLogger ().debug ("LDAP using host: " + host);
-			}
-			String base = params.getParameter (LDAP_BASE_PARAM);
-			if (getLogger ().isDebugEnabled ()) {
-				getLogger ().debug ("LDAP using base: " + base);
-			}
-			String user = params.getParameter (LDAP_USER_PARAM);
-			if (getLogger ().isDebugEnabled ()) {
-				getLogger ().debug ("LDAP using user: " + user);
-			}
-			String pass = params.getParameter (LDAP_PASS_PARAM);
-			if (getLogger ().isDebugEnabled ()) {
-				getLogger ().debug ("LDAP using pass: " + pass);
-			}
-			this.environment = new Hashtable ();
-			this.environment.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
-			this.environment.put (Context.PROVIDER_URL, host + "/" + base);
-			if (user != null) {
-				this.environment.put (Context.SECURITY_AUTHENTICATION, "simple");
-				this.environment.put (Context.SECURITY_PRINCIPAL, user);
-				this.environment.put (Context.SECURITY_CREDENTIALS, pass);
-			}
-		}
+        String host = params.getParameter(LDAP_HOST_PARAM);
+        String base = params.getParameter(LDAP_BASE_PARAM);
+        String user = params.getParameter(LDAP_USER_PARAM);
+        String pass = params.getParameter(LDAP_PASS_PARAM);
+        if (getLogger().isDebugEnabled()) {
+            getLogger().debug("Using LDAP host: " + host + ", base: " + base +
+                              ", user: " + user + ", pass: " + pass);
+        }
+        this.environment = new Hashtable();
+        this.environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
+        this.environment.put(Context.PROVIDER_URL, host + "/" + base);
+        if (user != null) {
+            this.environment.put(Context.SECURITY_AUTHENTICATION, "simple");
+            this.environment.put(Context.SECURITY_PRINCIPAL, user);
+            this.environment.put(Context.SECURITY_CREDENTIALS, pass);
+        }
 	}
 
 	/* Avalon, Recycle this Class */
 	public final void recycle()	 {
-		if (getLogger ().isDebugEnabled ()) {
-			getLogger ().debug ("LDAPEntryManager recycling");
-		}
-		try {
-			this.context.close();
-		} catch ( Exception e) {
-			getLogger().error ("LDAPEntryManager.recycle() :" + e.getMessage());
-		} finally {
-			this.context = null;
-		}
-	}
+        try {
+            this.context.close();
+        } catch (Exception e) {
+            getLogger().error("Exception in recycle", e);
+        } finally {
+            this.context = null;
+        }
+    }
 
 	/* Avalon, Dispose of this Class */
 	public final void dispose()	 {
-		if (getLogger ().isDebugEnabled ()) {
-			getLogger ().debug ("LDAPEntryManager disposing");
-		}
-		try {
-			if (context != null) {
-				this.context.close();
-			}
-		} catch ( Exception e) {
-			getLogger().error ("LDAPEntryManager.recycle() :" + e.getMessage());
-		} finally {
-			this.context = null;
-			this.environment = null;
-			this.disposed = true;
-		}
-	}
+        try {
+            if (context != null) {
+                this.context.close();
+            }
+        } catch (Exception e) {
+            getLogger().error("Exception in dispose", e);
+        } finally {
+            this.context = null;
+            this.environment = null;
+            this.disposed = true;
+        }
+    }
 
 	/* lazy initialise this class */
 	protected void initialize() throws Exception {
-		if (getLogger ().isDebugEnabled ()) {
-			getLogger ().debug ("LDAPEntryManager initialising");
-		}
-		if (null == this.environment) {
-			throw new IllegalStateException("LDAPEntryManager.initialize() : Not Configured");
-		}
-		if (this.disposed) {
-			throw new IllegalStateException("LDAPEntryManager.initialize() : Already disposed");
-		}
-		try {
-			this.context = new InitialDirContext (this.environment);
-			if (getLogger ().isDebugEnabled ()) {
-				getLogger ().debug ("LDAPEntryManager new context: " + this.context.getNameInNamespace
());
-			}
-		} catch ( Exception e) {
-			getLogger().error ("LDAPEntryManager.initialize()" + e.getMessage());
-			return;
-		}
-	}
+        if (this.disposed) {
+            throw new IllegalStateException("initialize() : Already disposed");
+        }
+        this.context = new InitialDirContext(this.environment);
+        if (getLogger().isDebugEnabled()) {
+            getLogger().debug("New context: " + this.context.getNameInNamespace());
+        }
+    }
 
-	/**
-	 *	Creates a new Entry
-	 *
-	 * 	@param  name  The name of the Entry to create
-	 * 	@param  attributes  The Map of Attributes to create it with
-	 */
-	public void create(String name, Map attributes) throws ProcessingException {
-		Context newContext = null;
-		try {
-			if (this.context == null) initialize ();
-			if (getLogger ().isDebugEnabled ()) {
-				getLogger ().debug ("LDAPEntryManager creating new Context: " + name);
-			}
-			newContext = context.createSubcontext (name, map2Attributes (attributes));
-		} catch (Exception e) {
-			getLogger ().error ("LDAPEntryManager.create() :" + e.getMessage());
-			throw new ProcessingException (e);
-		} finally {
-			try {
-				if (newContext != null) newContext.close ();
-			} catch (NamingException ne) {
-				throw new ProcessingException (ne);
-			}
-		}
-	}
-	
-	/**
-	 *	Retrieves a named Entry's Attributes
-	 *
-	 * 	@param  name  The name of the Entry to modify
-	 *  @return a Map of the Attributes
-	 */
-	public Map get(String name) throws ProcessingException {
-		try {
-			if (this.context == null) initialize ();
-			if (getLogger ().isDebugEnabled ()) {
-				getLogger ().debug ("LDAPEntryManager retrieving Entry: " + name);
+
+    /**
+     * Creates a new Entry
+     *
+     * @param name       The name of the Entry to create
+     * @param attributes The Map of Attributes to create it with
+     */
+    public void create(String name, Map attributes) throws ProcessingException {
+        Context newContext = null;
+        try {
+            if (this.context == null) {
+                initialize();
+            }
+            if (getLogger().isDebugEnabled()) {
+                getLogger().debug("Creating new Context: " + name);
+            }
+            newContext = context.createSubcontext(name, map2Attributes(attributes));
+        } catch (Exception e) {
+            throw new ProcessingException(e);
+        } finally {
+            try {
+                if (newContext != null) {
+                    newContext.close();
+                }
+            } catch (NamingException e) {
+                throw new ProcessingException(e);
 			}
-			return attributes2Map (context.getAttributes (name));
-		} catch (Exception e) {
-			getLogger ().error ("LDAPEntryManager.get() :" + e.getMessage());
-			throw new ProcessingException (e);
 		}
 	}
 
-	/**
-	 *	Finds Entries based on matching their Attributes
-	 *
-	 * 	@param  attributes  The Attributes to match
-	 *  @return a Map of the results, each with a Map of their Attributes
-	 */
-	public Map find(Map attributes) throws ProcessingException {
-		return find("", attributes);
-	}
+    /**
+     * Retrieves a named Entry's Attributes
+     *
+     * @param name The name of the Entry to modify
+     * @return a Map of the Attributes
+     */
+    public Map get(String name) throws ProcessingException {
+        try {
+            if (this.context == null) {
+                initialize();
+            }
+            if (getLogger().isDebugEnabled()) {
+                getLogger().debug("Retrieving Entry: " + name);
+            }
+            return attributes2Map(context.getAttributes(name));
+        } catch (Exception e) {
+            throw new ProcessingException(e);
+        }
+    }
 
-	/**
-	 *	Finds Entries based on their Attributes
-	 *
-	 * 	@param  cntx  The sub-context to search
-	 * 	@param  attributes  The Attributes to match
-	 *  @return a Map of the results, each with a Map of their Attributes
-	 */
-	public Map find(String cntx, Map attributes) throws ProcessingException {
-		try {
-			if (this.context == null) initialize ();
-			if (getLogger ().isDebugEnabled ()) {
-				getLogger ().debug ("LDAPEntryManager finding Entries in: " + cntx);
-			}
-			return namingEnumeration2Map (context.search (cntx, map2Attributes (attributes)));
-		} catch (Exception e) {
-			getLogger ().error ("LDAPEntryManager.find() :" + e.getMessage());
-			throw new ProcessingException (e);
-		}
-	}
-	
-	/**
-	 *	Modifies an existing Entry
-	 *
-	 * 	@param  name  The name of the Entry to modify
-	 * 	@param  mod_op  The modification mode to use
-	 * 	@param  attributes  The Map of modifications
-	 */
-	public void modify(String name, int mod_op, Map attributes) throws ProcessingException {
-		try {
-			if (this.context == null) initialize ();
-			if (getLogger ().isDebugEnabled ()) {
-				getLogger ().debug ("LDAPEntryManager modifying Entry: " + name);
-			}
-			context.modifyAttributes (name, mod_op, map2Attributes (attributes));
-		} catch (Exception e) {
-			getLogger ().error ("LDAPEntryManager.modify() :" + e.getMessage());
-			throw new ProcessingException (e);
-		}
-	}
+    /**
+     * Finds Entries based on matching their Attributes
+     *
+     * @param attributes The Attributes to match
+     * @return a Map of the results, each with a Map of their Attributes
+     */
+    public Map find(Map attributes) throws ProcessingException {
+        return find("", attributes);
+    }
+
+    /**
+     * Finds Entries based on their Attributes
+     *
+     * @param cntx       The sub-context to search
+     * @param attributes The Attributes to match
+     * @return a Map of the results, each with a Map of their Attributes
+     */
+    public Map find(String cntx, Map attributes) throws ProcessingException {
+        try {
+            if (this.context == null) {
+                initialize();
+            }
+            if (getLogger().isDebugEnabled()) {
+                getLogger().debug("Finding Entries in: " + cntx);
+            }
+            return namingEnumeration2Map(context.search(cntx, map2Attributes(attributes)));
+        } catch (Exception e) {
+            throw new ProcessingException(e);
+        }
+    }
 
-	/* 
-		Converts an Attributes Enumeration into a Map of those Attributes 
-		Should be easier to manupulate in FlowScript and display in JXTemplate
-		Keep in mind that because there can be many entries for each Attribute
-		we store each value of the Map as an Array
-	*/
-	private Map attributes2Map (Attributes attributes) throws NamingException {
-		Map map = new HashMap ();
-		for (NamingEnumeration atts = attributes.getAll(); atts.hasMore();) {
-			Attribute attr = (Attribute)atts.next();
-			String id = attr.getID();
-			List val = new java.util.ArrayList ();
-			NamingEnumeration vals = attr.getAll();
-			while (vals.hasMore ()) {
-				val.add (vals.next ()); 
-			}
-			map.put (id, val);
-		}
-		return map;
-	}		
+    /**
+     * Modifies an existing Entry
+     *
+     * @param name       The name of the Entry to modify
+     * @param mod_op     The modification mode to use
+     * @param attributes The Map of modifications
+     */
+    public void modify(String name, int mod_op, Map attributes) throws ProcessingException
{
+        try {
+            if (this.context == null) initialize();
+            if (getLogger().isDebugEnabled()) {
+                getLogger().debug("Modifying Entry: " + name);
+            }
+            context.modifyAttributes(name, mod_op, map2Attributes(attributes));
+        } catch (Exception e) {
+            throw new ProcessingException(e);
+        }
+    }
 
-	/* 
-		Converts a Map into an Enumeration of Attributes 
-		Should be easier to provide from FlowScript
-	*/
-	private Attributes map2Attributes (Map map) {
-		Attributes attrs = new BasicAttributes (false);
-        Iterator keys = map.keySet ().iterator ();
-        while (keys.hasNext ()) {
-            String key = (String)keys.next ();
-            Iterator vals = ((List)map.get (key)).iterator ();
-            Attribute attr = new BasicAttribute (key);
-            while (vals.hasNext ()) {
-                attr.add (vals.next ());
+    /*
+         Converts an Attributes Enumeration into a Map of those Attributes
+         Should be easier to manupulate in FlowScript and display in JXTemplate
+         Keep in mind that because there can be many entries for each Attribute
+         we store each value of the Map as an Array
+     */
+    private Map attributes2Map(Attributes attributes) throws NamingException {
+        Map map = new HashMap();
+        for (NamingEnumeration atts = attributes.getAll(); atts.hasMore();) {
+            Attribute attr = (Attribute) atts.next();
+            String id = attr.getID();
+            List val = new java.util.ArrayList();
+            NamingEnumeration vals = attr.getAll();
+            while (vals.hasMore()) {
+                val.add(vals.next());
             }
-            attrs.put (attr);
+            map.put(id, val);
         }
-		return attrs;
-	}
+        return map;
+    }
+
+    /*
+         Converts a Map into an Enumeration of Attributes
+         Should be easier to provide from FlowScript
+     */
+    private Attributes map2Attributes(Map map) {
+        Attributes attrs = new BasicAttributes(false);
+        Iterator keys = map.keySet().iterator();
+        while (keys.hasNext()) {
+            String key = (String) keys.next();
+            Iterator vals = ((List) map.get(key)).iterator();
+            Attribute attr = new BasicAttribute(key);
+            while (vals.hasNext()) {
+                attr.add(vals.next());
+            }
+            attrs.put(attr);
+        }
+        return attrs;
+    }
 
-	/* 
-		Converts a NamingEnumeration into a Map of those Entries, with Attributes 
-		Should be easier to manupulate in FlowScript and display in JXTemplate
-	*/
-	private Map namingEnumeration2Map (NamingEnumeration enumeration) throws NamingException
{
-		Map map = new HashMap ();
-		while (enumeration.hasMore ()) {
-			SearchResult sr = (SearchResult)enumeration.next ();
-			map.put (sr.getName (), attributes2Map(sr.getAttributes ()));
+    /*
+         Converts a NamingEnumeration into a Map of those Entries, with Attributes
+         Should be easier to manupulate in FlowScript and display in JXTemplate
+     */
+    private Map namingEnumeration2Map(NamingEnumeration enumeration) throws NamingException
{
+        Map map = new HashMap();
+        while (enumeration.hasMore()) {
+            SearchResult sr = (SearchResult) enumeration.next();
+            map.put(sr.getName (), attributes2Map(sr.getAttributes ()));
 		}
 		return map;
 	}
 }
-
-



Mime
View raw message