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.api.entry.ClonedServerEntry;
33 import org.apache.directory.server.core.api.entry.ServerEntryUtils;
34 import org.apache.directory.server.core.api.event.DirectoryListener;
35 import org.apache.directory.server.core.api.interceptor.context.AddOperationContext;
36 import org.apache.directory.server.core.api.interceptor.context.DeleteOperationContext;
37 import org.apache.directory.server.core.api.interceptor.context.ModifyOperationContext;
38 import org.apache.directory.server.core.api.interceptor.context.MoveAndRenameOperationContext;
39 import org.apache.directory.server.core.api.interceptor.context.MoveOperationContext;
40 import org.apache.directory.server.core.api.interceptor.context.RenameOperationContext;
41 import org.apache.directory.server.i18n.I18n;
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 public class EventListenerAdapter implements DirectoryListener
66 {
67 private static final Logger LOG = LoggerFactory.getLogger( EventListenerAdapter.class );
68 private final NamingListener listener;
69 private final ServerLdapContext source;
70
71
72 public EventListenerAdapter( ServerLdapContext source, NamingListener listener )
73 {
74 this( source, listener, new SearchControls() );
75 }
76
77
78 public EventListenerAdapter( ServerLdapContext source, NamingListener listener, SearchControls controls )
79 {
80 this.source = source;
81 this.listener = listener;
82 }
83
84
85 @Override
86 public boolean isSynchronous()
87 {
88 return false;
89 }
90
91
92 private void deliverNamingExceptionEvent( Exception e )
93 {
94 LOG.error( I18n.err( I18n.ERR_118 ), e );
95 NamingExceptionEvent evt = null;
96
97 if ( e instanceof NamingException )
98 {
99 evt = new NamingExceptionEvent( source, ( NamingException ) e );
100 }
101 else
102 {
103 NamingException ne = new NamingException( I18n.err( I18n.ERR_119 ) );
104 ne.setRootCause( e );
105 evt = new NamingExceptionEvent( source, ne );
106 }
107
108 listener.namingExceptionThrown( evt );
109 }
110
111
112
113
114
115 public void entryAdded( AddOperationContext addContext )
116 {
117 try
118 {
119 Binding binding = new Binding( addContext.getDn().getName(),
120 ServerEntryUtils.toBasicAttributes( addContext.getEntry() ), false );
121 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_ADDED,
122 binding, null, addContext );
123
124 if ( listener instanceof NamespaceChangeListener )
125 {
126 ( ( NamespaceChangeListener ) listener ).objectAdded( evt );
127 }
128 }
129 catch ( Exception e )
130 {
131 deliverNamingExceptionEvent( e );
132 }
133 }
134
135
136
137
138
139 public void entryDeleted( DeleteOperationContext deleteContext )
140 {
141 try
142 {
143 if ( listener instanceof NamespaceChangeListener )
144 {
145 Binding binding = new Binding( deleteContext.getDn().getName(),
146 ServerEntryUtils.toBasicAttributes( deleteContext.getEntry() ), false );
147 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_REMOVED, null,
148 binding, deleteContext );
149 ( ( NamespaceChangeListener ) listener ).objectAdded( evt );
150 }
151 }
152 catch ( Exception e )
153 {
154 deliverNamingExceptionEvent( e );
155 }
156 }
157
158
159
160
161
162 public void entryModified( ModifyOperationContext modifyContext )
163 {
164 try
165 {
166 Binding newBinding = new Binding( modifyContext.getDn().getName(),
167 ServerEntryUtils.toBasicAttributes( modifyContext.getEntry() ), false );
168 Binding oldBinding = new Binding( modifyContext.getDn().getName(),
169 ServerEntryUtils.toBasicAttributes( ( ( ClonedServerEntry ) modifyContext.getEntry() )
170 .getOriginalEntry() ), false );
171 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_CHANGED,
172 newBinding, oldBinding, modifyContext );
173
174 if ( listener instanceof ObjectChangeListener )
175 {
176 ( ( ObjectChangeListener ) listener ).objectChanged( evt );
177 }
178 }
179 catch ( Exception e )
180 {
181 deliverNamingExceptionEvent( e );
182 }
183 }
184
185
186
187
188
189 public void entryMoved( MoveOperationContext moveContext )
190 {
191 try
192 {
193 if ( listener instanceof NamespaceChangeListener )
194 {
195 Binding newBinding = new Binding( moveContext.getDn().getName(),
196 ServerEntryUtils.toBasicAttributes( moveContext.getEntry() ), false );
197 Binding oldBinding = new Binding( moveContext.getDn().getName(),
198 ServerEntryUtils.toBasicAttributes( ( ( ClonedServerEntry ) moveContext.getEntry() )
199 .getOriginalEntry() ), false );
200 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_RENAMED,
201 newBinding, oldBinding, moveContext );
202 ( ( NamespaceChangeListener ) listener ).objectRenamed( evt );
203 }
204 }
205 catch ( Exception e )
206 {
207 deliverNamingExceptionEvent( e );
208 }
209 }
210
211
212
213
214
215 public void entryMovedAndRenamed( MoveAndRenameOperationContext moveAndRenameContext )
216 {
217 try
218 {
219 if ( listener instanceof NamespaceChangeListener )
220 {
221 Binding newBinding = new Binding( moveAndRenameContext.getDn().getName(),
222 ServerEntryUtils.toBasicAttributes( moveAndRenameContext.getEntry() ), false );
223 Binding oldBinding = new Binding( moveAndRenameContext.getDn().getName(),
224 ServerEntryUtils.toBasicAttributes( ( ( ClonedServerEntry ) moveAndRenameContext.getEntry() )
225 .getOriginalEntry() ), false );
226 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_RENAMED,
227 newBinding, oldBinding, moveAndRenameContext );
228 ( ( NamespaceChangeListener ) listener ).objectRenamed( evt );
229 }
230 }
231 catch ( Exception e )
232 {
233 deliverNamingExceptionEvent( e );
234 }
235 }
236
237
238
239
240
241 public void entryRenamed( RenameOperationContext renameContext )
242 {
243 try
244 {
245 if ( listener instanceof NamespaceChangeListener )
246 {
247 Binding newBinding = new Binding( renameContext.getDn().getName(),
248 ServerEntryUtils.toBasicAttributes( renameContext.getEntry() ), false );
249 Binding oldBinding = new Binding( renameContext.getDn().getName(),
250 ServerEntryUtils.toBasicAttributes( ( ( ClonedServerEntry ) renameContext.getEntry() )
251 .getOriginalEntry() ), false );
252 NamingEvent evt = new NamingEvent( source, NamingEvent.OBJECT_RENAMED,
253 newBinding, oldBinding, null );
254 ( ( NamespaceChangeListener ) listener ).objectRenamed( evt );
255 }
256 }
257 catch ( Exception e )
258 {
259 deliverNamingExceptionEvent( e );
260 }
261 }
262 }