1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.jndi;
21
22
23 import javax.naming.Binding;
24 import javax.naming.NamingException;
25 import javax.naming.directory.SearchControls;
26 import javax.naming.event.NamespaceChangeListener;
27 import javax.naming.event.NamingEvent;
28 import javax.naming.event.NamingExceptionEvent;
29 import javax.naming.event.NamingListener;
30 import javax.naming.event.ObjectChangeListener;
31
32 import org.apache.directory.server.core.entry.ServerEntryUtils;
33 import org.apache.directory.server.core.event.DirectoryListener;
34 import org.apache.directory.server.core.interceptor.context.AddOperationContext;
35 import org.apache.directory.server.core.interceptor.context.DeleteOperationContext;
36 import org.apache.directory.server.core.interceptor.context.ModifyOperationContext;
37 import org.apache.directory.server.core.interceptor.context.MoveAndRenameOperationContext;
38 import org.apache.directory.server.core.interceptor.context.MoveOperationContext;
39 import org.apache.directory.server.core.interceptor.context.OperationContext;
40 import org.apache.directory.server.core.interceptor.context.RenameOperationContext;
41
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class EventListenerAdapter implements DirectoryListener
67 {
68 private static final Logger LOG = LoggerFactory.getLogger( EventListenerAdapter.class );
69 private final NamingListener listener;
70 private final ServerLdapContext source;
71
72
73
74
75
76 private final SearchControls controls;
77
78
79 public EventListenerAdapter( ServerLdapContext source, NamingListener listener )
80 {
81 this( source, listener, new SearchControls() );
82 }
83
84
85 public EventListenerAdapter( ServerLdapContext source, NamingListener listener, SearchControls controls )
86 {
87 this.source = source;
88 this.controls = controls;
89 this.listener = listener;
90 }
91
92
93 private void deliverNamingExceptionEvent( Exception e, OperationContext opContext )
94 {
95 LOG.error( "Error encountered while delivering notifications.", e );
96 NamingExceptionEvent evt = null;
97
98 if ( e instanceof NamingException )
99 {
100 evt = new NamingExceptionEvent( source, ( NamingException ) e );
101 }
102 else
103 {
104 NamingException ne = new NamingException( "Encountered exception during event handling." );
105 ne.setRootCause( e );
106 evt = new NamingExceptionEvent( source, ne );
107 }
108
109 listener.namingExceptionThrown( evt );
110 }
111
112
113
114
115
116 public void entryAdded( AddOperationContext opContext )
117 {
118 try
119 {
120 Binding binding = new Binding( opContext.getDn().getUpName(),
121 ServerEntryUtils.toBasicAttributes( opContext.getEntry() ), false );
122 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_ADDED,
123 binding, null, opContext );
124
125 if ( listener instanceof NamespaceChangeListener )
126 {
127 ( ( NamespaceChangeListener ) listener ).objectAdded( evt );
128 }
129 }
130 catch ( Exception e )
131 {
132 deliverNamingExceptionEvent( e, opContext );
133 }
134 }
135
136
137
138
139
140 public void entryDeleted( DeleteOperationContext opContext )
141 {
142 try
143 {
144 if ( listener instanceof NamespaceChangeListener )
145 {
146 Binding binding = new Binding( opContext.getDn().getUpName(),
147 ServerEntryUtils.toBasicAttributes( opContext.getEntry() ), false );
148 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_REMOVED, null,
149 binding, opContext );
150 ( ( NamespaceChangeListener ) listener ).objectAdded( evt );
151 }
152 }
153 catch ( Exception e )
154 {
155 deliverNamingExceptionEvent( e, opContext );
156 }
157 }
158
159
160
161
162
163 public void entryModified( ModifyOperationContext opContext )
164 {
165 try
166 {
167 Binding newBinding = new Binding( opContext.getDn().getUpName(),
168 ServerEntryUtils.toBasicAttributes( opContext.getEntry() ), false );
169 Binding oldBinding = new Binding( opContext.getDn().getUpName(),
170 ServerEntryUtils.toBasicAttributes( opContext.getEntry().getOriginalEntry() ), false );
171 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_CHANGED,
172 newBinding, oldBinding, opContext );
173
174 if ( listener instanceof ObjectChangeListener )
175 {
176 ( ( ObjectChangeListener ) listener ).objectChanged( evt );
177 }
178 }
179 catch ( Exception e )
180 {
181 deliverNamingExceptionEvent( e, opContext );
182 }
183 }
184
185
186
187
188
189 public void entryMoved( MoveOperationContext opContext )
190 {
191 try
192 {
193 if ( listener instanceof NamespaceChangeListener )
194 {
195 Binding newBinding = new Binding( opContext.getDn().getUpName(),
196 ServerEntryUtils.toBasicAttributes( opContext.getEntry() ), false );
197 Binding oldBinding = new Binding( opContext.getDn().getUpName(),
198 ServerEntryUtils.toBasicAttributes( opContext.getEntry().getOriginalEntry() ), false );
199 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_RENAMED,
200 newBinding, oldBinding, opContext );
201 ( ( NamespaceChangeListener ) listener ).objectRenamed( evt );
202 }
203 }
204 catch ( Exception e )
205 {
206 deliverNamingExceptionEvent( e, opContext );
207 }
208 }
209
210
211
212
213
214 public void entryMovedAndRenamed( MoveAndRenameOperationContext opContext )
215 {
216 try
217 {
218 if ( listener instanceof NamespaceChangeListener )
219 {
220 Binding newBinding = new Binding( opContext.getDn().getUpName(),
221 ServerEntryUtils.toBasicAttributes( opContext.getEntry() ), false );
222 Binding oldBinding = new Binding( opContext.getDn().getUpName(),
223 ServerEntryUtils.toBasicAttributes( opContext.getEntry().getOriginalEntry() ), false );
224 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_RENAMED,
225 newBinding, oldBinding, opContext );
226 ( ( NamespaceChangeListener ) listener ).objectRenamed( evt );
227 }
228 }
229 catch ( Exception e )
230 {
231 deliverNamingExceptionEvent( e, opContext );
232 }
233 }
234
235
236
237
238
239 public void entryRenamed( RenameOperationContext opContext )
240 {
241 try
242 {
243 if ( listener instanceof NamespaceChangeListener )
244 {
245 Binding newBinding = new Binding( opContext.getDn().getUpName(),
246 ServerEntryUtils.toBasicAttributes( opContext.getEntry() ), false );
247 Binding oldBinding = new Binding( opContext.getDn().getUpName(),
248 ServerEntryUtils.toBasicAttributes( opContext.getEntry().getOriginalEntry() ), false );
249 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_RENAMED,
250 newBinding, oldBinding, null );
251 ( ( NamespaceChangeListener ) listener ).objectRenamed( evt );
252 }
253 }
254 catch ( Exception e )
255 {
256 deliverNamingExceptionEvent( e, opContext );
257 }
258 }
259 }