1 17 package org.apache.cocoon.components.naming; 18 19 import java.util.HashMap ; 20 import java.util.Hashtable ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.naming.Context ; 26 import javax.naming.NamingEnumeration ; 27 import javax.naming.NamingException ; 28 import javax.naming.directory.Attribute ; 29 import javax.naming.directory.Attributes ; 30 import javax.naming.directory.BasicAttribute ; 31 import javax.naming.directory.BasicAttributes ; 32 import javax.naming.directory.DirContext ; 33 import javax.naming.directory.InitialDirContext ; 34 import javax.naming.directory.SearchResult ; 35 36 import org.apache.avalon.excalibur.pool.Recyclable; 37 import org.apache.avalon.framework.activity.Disposable; 38 import org.apache.avalon.framework.logger.AbstractLogEnabled; 39 import org.apache.avalon.framework.parameters.ParameterException; 40 import org.apache.avalon.framework.parameters.Parameterizable; 41 import org.apache.avalon.framework.parameters.Parameters; 42 import org.apache.cocoon.ProcessingException; 43 44 60 61 public class LDAPEntryManager 62 extends AbstractLogEnabled 63 implements EntryManager, Parameterizable, Disposable, Recyclable { 64 65 66 protected final static String LDAP_HOST_PARAM = "ldap-host"; 67 protected final static String LDAP_USER_PARAM = "ldap-user"; 68 protected final static String LDAP_PASS_PARAM = "ldap-pass"; 69 protected final static String LDAP_BASE_PARAM = "ldap-base"; 70 71 72 private boolean disposed = false; 73 private boolean recycled = false; 74 75 76 protected DirContext context = null; 77 protected Hashtable environment = null; 78 79 80 public LDAPEntryManager() { 81 } 82 83 84 public void parameterize(Parameters params) throws ParameterException { 85 if (getLogger ().isDebugEnabled ()) { 86 getLogger ().debug ("LDAPEntryManager parameterizing"); 87 } 88 if (this.environment == null) { 89 String host = params.getParameter (LDAP_HOST_PARAM); 90 if (getLogger ().isDebugEnabled ()) { 91 getLogger ().debug ("LDAP using host: " + host); 92 } 93 String base = params.getParameter (LDAP_BASE_PARAM); 94 if (getLogger ().isDebugEnabled ()) { 95 getLogger ().debug ("LDAP using base: " + base); 96 } 97 String user = params.getParameter (LDAP_USER_PARAM); 98 if (getLogger ().isDebugEnabled ()) { 99 getLogger ().debug ("LDAP using user: " + user); 100 } 101 String pass = params.getParameter (LDAP_PASS_PARAM); 102 if (getLogger ().isDebugEnabled ()) { 103 getLogger ().debug ("LDAP using pass: " + pass); 104 } 105 this.environment = new Hashtable (); 106 this.environment.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 107 this.environment.put (Context.PROVIDER_URL, host + "/" + base); 108 if (user != null) { 109 this.environment.put (Context.SECURITY_AUTHENTICATION, "simple"); 110 this.environment.put (Context.SECURITY_PRINCIPAL, user); 111 this.environment.put (Context.SECURITY_CREDENTIALS, pass); 112 } 113 } 114 } 115 116 117 public final void recycle() { 118 if (getLogger ().isDebugEnabled ()) { 119 getLogger ().debug ("LDAPEntryManager recycling"); 120 } 121 try { 122 this.context.close(); 123 } catch ( Exception e) { 124 getLogger().error ("LDAPEntryManager.recycle() :" + e.getMessage()); 125 } finally { 126 this.context = null; 127 } 128 this.recycled = true; 129 } 130 131 132 public final void dispose() { 133 if (getLogger ().isDebugEnabled ()) { 134 getLogger ().debug ("LDAPEntryManager disposing"); 135 } 136 try { 137 if (context != null) { 138 this.context.close(); 139 } 140 } catch ( Exception e) { 141 getLogger().error ("LDAPEntryManager.recycle() :" + e.getMessage()); 142 } finally { 143 this.context = null; 144 this.environment = null; 145 this.disposed = true; 146 } 147 } 148 149 150 protected void initialize() throws Exception { 151 if (getLogger ().isDebugEnabled ()) { 152 getLogger ().debug ("LDAPEntryManager initialising"); 153 } 154 if (null == this.environment) { 155 throw new IllegalStateException ("LDAPEntryManager.initialize() : Not Configured"); 156 } 157 if (this.disposed) { 158 throw new IllegalStateException ("LDAPEntryManager.initialize() : Already disposed"); 159 } 160 try { 161 this.context = new InitialDirContext (this.environment); 162 if (getLogger ().isDebugEnabled ()) { 163 getLogger ().debug ("LDAPEntryManager new context: " + this.context.getNameInNamespace ()); 164 } 165 } catch ( Exception e) { 166 getLogger().error ("LDAPEntryManager.initialize()" + e.getMessage()); 167 return; 168 } 169 } 170 171 177 public void create(String name, Map attributes) throws ProcessingException { 178 Context newContext = null; 179 try { 180 if (this.context == null) initialize (); 181 if (getLogger ().isDebugEnabled ()) { 182 getLogger ().debug ("LDAPEntryManager creating new Context: " + name); 183 } 184 newContext = context.createSubcontext (name, map2Attributes (attributes)); 185 } catch (Exception e) { 186 getLogger ().error ("LDAPEntryManager.create() :" + e.getMessage()); 187 throw new ProcessingException (e); 188 } finally { 189 try { 190 if (newContext != null) newContext.close (); 191 } catch (NamingException ne) { 192 throw new ProcessingException (ne); 193 } 194 } 195 } 196 197 203 public Map get(String name) throws ProcessingException { 204 try { 205 if (this.context == null) initialize (); 206 if (getLogger ().isDebugEnabled ()) { 207 getLogger ().debug ("LDAPEntryManager retrieving Entry: " + name); 208 } 209 return attributes2Map (context.getAttributes (name)); 210 } catch (Exception e) { 211 getLogger ().error ("LDAPEntryManager.get() :" + e.getMessage()); 212 throw new ProcessingException (e); 213 } 214 } 215 216 222 public Map find(Map attributes) throws ProcessingException { 223 return find("", attributes); 224 } 225 226 233 public Map find(String cntx, Map attributes) throws ProcessingException { 234 try { 235 if (this.context == null) initialize (); 236 if (getLogger ().isDebugEnabled ()) { 237 getLogger ().debug ("LDAPEntryManager finding Entries in: " + cntx); 238 } 239 return namingEnumeration2Map (context.search (cntx, map2Attributes (attributes))); 240 } catch (Exception e) { 241 getLogger ().error ("LDAPEntryManager.find() :" + e.getMessage()); 242 throw new ProcessingException (e); 243 } 244 } 245 246 253 public void modify(String name, int mod_op, Map attributes) throws ProcessingException { 254 try { 255 if (this.context == null) initialize (); 256 if (getLogger ().isDebugEnabled ()) { 257 getLogger ().debug ("LDAPEntryManager modifying Entry: " + name); 258 } 259 context.modifyAttributes (name, mod_op, map2Attributes (attributes)); 260 } catch (Exception e) { 261 getLogger ().error ("LDAPEntryManager.modify() :" + e.getMessage()); 262 throw new ProcessingException (e); 263 } 264 } 265 266 272 private Map attributes2Map (Attributes attributes) throws NamingException { 273 Map map = new HashMap (); 274 for (NamingEnumeration atts = attributes.getAll(); atts.hasMore();) { 275 Attribute attr = (Attribute )atts.next(); 276 String id = attr.getID(); 277 List val = new java.util.ArrayList (); 278 NamingEnumeration vals = attr.getAll(); 279 while (vals.hasMore ()) { 280 val.add (vals.next ()); 281 } 282 map.put (id, val); 283 } 284 return map; 285 } 286 287 291 private Attributes map2Attributes (Map map) { 292 Attributes attrs = new BasicAttributes (false); 293 for (Iterator i = map.entrySet().iterator(); i.hasNext(); ) { 294 Map.Entry me = (Map.Entry )i.next(); 295 String key = (String )me.getKey(); 296 Attribute attr = new BasicAttribute (key); 297 for (Iterator vals = ((List )me.getValue()).iterator(); vals.hasNext(); ) { 298 attr.add(vals.next()); 299 } 300 attrs.put(attr); 301 } 302 return attrs; 303 } 304 305 309 private Map namingEnumeration2Map (NamingEnumeration enumeration) throws NamingException { 310 Map map = new HashMap (); 311 while (enumeration.hasMore ()) { 312 SearchResult sr = (SearchResult )enumeration.next (); 313 map.put (sr.getName (), attributes2Map(sr.getAttributes ())); 314 } 315 return map; 316 } 317 } 318 319 320 | Popular Tags |