1 package net.sf.jldapbeans.lang; 2 3 import java.beans.PropertyChangeListener; 4 5 import javax.naming.NamingException; 6 import javax.naming.directory.Attributes; 7 import javax.naming.event.EventDirContext; 8 import javax.naming.event.NamespaceChangeListener; 9 import javax.naming.event.NamingEvent; 10 import javax.naming.event.NamingExceptionEvent; 11 import javax.naming.event.ObjectChangeListener; 12 import javax.naming.ldap.LdapContext; 13 import javax.naming.ldap.LdapName; 14 15 import org.apache.commons.lang.builder.EqualsBuilder; 16 import org.apache.commons.lang.builder.HashCodeBuilder; 17 import org.apache.commons.lang.builder.ToStringBuilder; 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 21 public abstract class LdapObject { 22 private class LdapNamingListener implements NamespaceChangeListener, ObjectChangeListener { 23 24 public void namingExceptionThrown(NamingExceptionEvent evt) { 25 log.error("Exception thrown during event dispatching.", evt.getException()); 26 } 27 28 public void objectAdded(NamingEvent evt) { 29 log.trace("Object added. OldBinding [" 30 + evt.getOldBinding() 31 + "], NewBinding [" 32 + evt.getNewBinding()); 33 } 34 35 public void objectChanged(NamingEvent evt) { 36 log.trace("Object changed. OldBinding [" 37 + evt.getOldBinding() 38 + "], NewBinding [" 39 + evt.getNewBinding()); 40 } 41 42 public void objectRemoved(NamingEvent evt) { 43 log.trace("Object removed. OldBinding [" 44 + evt.getOldBinding() 45 + "], NewBinding [" 46 + evt.getNewBinding()); 47 } 48 49 public void objectRenamed(NamingEvent evt) { 50 log.trace("Object renamed. OldBinding [" 51 + evt.getOldBinding() 52 + "], NewBinding [" 53 + evt.getNewBinding()); 54 } 55 56 } 57 58 private static final Log log = LogFactory.getLog(LdapObject.class); 59 60 private LdapNamingListener m_eventListener = new LdapNamingListener(); 61 62 private LdapContext m_context = null; 63 private EventDirContext m_eventContext = null; 64 private LdapName m_name; 65 66 public abstract void addPropertyChangeListener(PropertyChangeListener listener); 67 68 public boolean equals(Object o) { 69 return EqualsBuilder.reflectionEquals(this, o); 70 } 71 72 protected final Attributes getAttributes() { 73 try { 74 return m_context.getAttributes(m_name); 75 } 76 catch(Exception e) { 77 return null; 78 } 79 } 80 81 protected final void uncontextualize() throws NamingException { 82 m_eventContext.removeNamingListener(m_eventListener); 83 m_eventContext = null; 84 m_context = null; 85 m_name = null; 86 } 87 88 protected final LdapContext getContext() { 89 return m_context; 90 } 91 92 public final LdapClass getLdapClass() throws LdapClassException { 93 return LdapClass.classOf(this); 94 } 95 96 public final LdapName getName() { 97 return m_name; 98 } 99 100 public int hashCode() { 101 return HashCodeBuilder.reflectionHashCode(this); 102 } 103 104 protected void contextualize(LdapName name, LdapContext context) throws NamingException { 105 m_name = name; 106 m_context = context; 107 m_eventContext = (EventDirContext) context; 108 m_eventContext.addNamingListener(m_name, EventDirContext.OBJECT_SCOPE, m_eventListener); 109 } 110 111 public abstract void removePropertyChangeListener(PropertyChangeListener listener); 112 113 public String toString() { 114 return ToStringBuilder.reflectionToString(this); 115 } 116 117 }