View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.jndi;
18  
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import javax.naming.Context;
23  import javax.naming.Name;
24  import javax.naming.NamingEnumeration;
25  import javax.naming.NamingException;
26  import javax.naming.directory.Attributes;
27  import javax.naming.directory.ModificationItem;
28  import javax.naming.directory.SearchControls;
29  import javax.naming.ldap.LdapContext;
30  
31  import org.apache.ldap.common.filter.ExprNode;
32  import org.apache.ldap.server.configuration.ContextPartitionConfiguration;
33  import org.apache.ldap.server.interceptor.InterceptorChain;
34  import org.apache.ldap.server.invocation.Invocation;
35  import org.apache.ldap.server.invocation.InvocationStack;
36  import org.apache.ldap.server.partition.ContextPartition;
37  import org.apache.ldap.server.partition.ContextPartitionNexus;
38  
39  /***
40   * A decorator that wraps other {@link ContextPartitionNexus} to enable
41   * {@link InterceptorChain} and {@link InvocationStack} support.
42   * All {@link Invocation}s made to this nexus is automatically pushed to
43   * {@link InvocationStack} of the current thread, and popped when
44   * the operation ends.  All invocations are filtered by {@link InterceptorChain}.
45   *
46   * @author The Apache Directory Project
47   * @version $Rev: 226451 $, $Date: 2005-07-29 20:54:58 -0400 (Fri, 29 Jul 2005) $
48   */
49  class ContextPartitionNexusProxy extends ContextPartitionNexus
50  {
51      private final Context caller;
52      private final ContextFactoryService service;
53      private final ContextFactoryConfiguration configuration;
54  
55      /***
56       * Creates a new instance.
57       * 
58       * @param caller a JNDI {@link Context} object that will call this proxy
59       * @param service a JNDI service
60       */
61      ContextPartitionNexusProxy( Context caller, ContextFactoryService service )
62      {
63          this.caller = caller;
64          this.service = service;
65          this.configuration = service.getConfiguration();
66      }
67      
68      public LdapContext getLdapContext() {
69          return this.configuration.getPartitionNexus().getLdapContext();
70      }
71  
72      public void init( ContextFactoryConfiguration factoryCfg, ContextPartitionConfiguration cfg )
73      {
74      }
75  
76      public void destroy()
77      {
78      }
79  
80      public ContextPartition getSystemPartition()
81      {
82          return this.configuration.getPartitionNexus().getSystemPartition();
83      }
84  
85      public Name getSuffix( boolean normalized ) throws NamingException
86      {
87          return this.configuration.getPartitionNexus().getSuffix( normalized );
88      }
89  
90      public void sync() throws NamingException {
91          this.service.sync();
92      }
93  
94      public void close() throws NamingException {
95          this.service.shutdown();
96      }
97  
98      public boolean isInitialized() {
99          return this.service.isStarted();
100     }
101 
102     public Name getMatchedName(Name dn, boolean normalized) throws NamingException {
103         InvocationStack stack = InvocationStack.getInstance();
104         stack.push( new Invocation(
105                 caller, "getMatchedDn",
106                 new Object[] { dn, normalized? Boolean.TRUE : Boolean.FALSE } ) );
107         try
108         {
109             return this.configuration.getInterceptorChain().getMatchedName( dn, normalized );
110         }
111         finally
112         {
113             stack.pop();
114         }
115     }
116 
117     public Name getSuffix(Name dn, boolean normalized) throws NamingException {
118         InvocationStack stack = InvocationStack.getInstance();
119         stack.push( new Invocation(
120                 caller, "getSuffix",
121                 new Object[] { dn, normalized? Boolean.TRUE : Boolean.FALSE } ) );
122         try
123         {
124             return this.configuration.getInterceptorChain().getSuffix( dn, normalized );
125         }
126         finally
127         {
128             stack.pop();
129         }
130     }
131 
132     public Iterator listSuffixes(boolean normalized) throws NamingException {
133         InvocationStack stack = InvocationStack.getInstance();
134         stack.push( new Invocation(
135                 caller, "listSuffixes",
136                 new Object[] { normalized? Boolean.TRUE : Boolean.FALSE } ) );
137         try
138         {
139             return this.configuration.getInterceptorChain().listSuffixes( normalized );
140         }
141         finally
142         {
143             stack.pop();
144         }
145     }
146 
147     public void delete(Name name) throws NamingException {
148         InvocationStack stack = InvocationStack.getInstance();
149         stack.push( new Invocation(
150                 caller, "delete",
151                 new Object[] { name } ) );
152         try
153         {
154             this.configuration.getInterceptorChain().delete( name );
155         }
156         finally
157         {
158             stack.pop();
159         }
160     }
161 
162     public void add(String upName, Name normName, Attributes entry) throws NamingException {
163         InvocationStack stack = InvocationStack.getInstance();
164         stack.push( new Invocation(
165                 caller, "add",
166                 new Object[] { upName, normName, entry } ) );
167         try
168         {
169             this.configuration.getInterceptorChain().add( upName, normName, entry );
170         }
171         finally
172         {
173             stack.pop();
174         }
175     }
176 
177     public void modify(Name name, int modOp, Attributes mods) throws NamingException {
178         InvocationStack stack = InvocationStack.getInstance();
179         // TODO Use predefined modOp Interger constants.
180         stack.push( new Invocation(
181                 caller, "modify",
182                 new Object[] { name, new Integer( modOp ), mods } ) );
183         try
184         {
185             this.configuration.getInterceptorChain().modify( name, modOp, mods );
186         }
187         finally
188         {
189             stack.pop();
190         }
191     }
192 
193     public void modify(Name name, ModificationItem[] mods) throws NamingException {
194         InvocationStack stack = InvocationStack.getInstance();
195         stack.push( new Invocation(
196                 caller, "modify",
197                 new Object[] { name, mods } ) );
198         try
199         {
200             this.configuration.getInterceptorChain().modify( name, mods );
201         }
202         finally
203         {
204             stack.pop();
205         }
206     }
207 
208     public NamingEnumeration list(Name base) throws NamingException {
209         InvocationStack stack = InvocationStack.getInstance();
210         stack.push( new Invocation(
211                 caller, "list",
212                 new Object[] { base } ) );
213         try
214         {
215             return this.configuration.getInterceptorChain().list( base );
216         }
217         finally
218         {
219             stack.pop();
220         }
221     }
222 
223     public NamingEnumeration search(Name base, Map env, ExprNode filter, SearchControls searchCtls) throws NamingException {
224         InvocationStack stack = InvocationStack.getInstance();
225         stack.push( new Invocation(
226                 caller, "search",
227                 new Object[] { base, env, filter, searchCtls } ) );
228         try
229         {
230             return this.configuration.getInterceptorChain().search( base, env, filter, searchCtls );
231         }
232         finally
233         {
234             stack.pop();
235         }
236     }
237 
238     public Attributes lookup(Name name) throws NamingException {
239         InvocationStack stack = InvocationStack.getInstance();
240         stack.push( new Invocation(
241                 caller, "lookup",
242                 new Object[] { name } ) );
243         try
244         {
245             return this.configuration.getInterceptorChain().lookup( name );
246         }
247         finally
248         {
249             stack.pop();
250         }
251     }
252 
253     public Attributes lookup(Name dn, String[] attrIds) throws NamingException {
254         InvocationStack stack = InvocationStack.getInstance();
255         stack.push( new Invocation(
256                 caller, "lookup",
257                 new Object[] { dn, attrIds } ) );
258         try
259         {
260             return this.configuration.getInterceptorChain().lookup( dn, attrIds );
261         }
262         finally
263         {
264             stack.pop();
265         }
266     }
267 
268     public boolean hasEntry(Name name) throws NamingException {
269         InvocationStack stack = InvocationStack.getInstance();
270         stack.push( new Invocation(
271                 caller, "hasEntry",
272                 new Object[] { name } ) );
273         try
274         {
275             return this.configuration.getInterceptorChain().hasEntry( name );
276         }
277         finally
278         {
279             stack.pop();
280         }
281     }
282 
283     public boolean isSuffix(Name name) throws NamingException {
284         InvocationStack stack = InvocationStack.getInstance();
285         stack.push( new Invocation(
286                 caller, "isSuffix",
287                 new Object[] { name } ) );
288         try
289         {
290             return this.configuration.getInterceptorChain().isSuffix( name );
291         }
292         finally
293         {
294             stack.pop();
295         }
296     }
297 
298     public void modifyRn(Name name, String newRn, boolean deleteOldRn) throws NamingException {
299         InvocationStack stack = InvocationStack.getInstance();
300         stack.push( new Invocation(
301                 caller, "modifyRn",
302                 new Object[] { name, newRn, deleteOldRn? Boolean.TRUE : Boolean.FALSE } ) );
303         try
304         {
305             this.configuration.getInterceptorChain().modifyRn( name, newRn, deleteOldRn );
306         }
307         finally
308         {
309             stack.pop();
310         }
311     }
312 
313     public void move(Name oriChildName, Name newParentName) throws NamingException {
314         InvocationStack stack = InvocationStack.getInstance();
315         stack.push( new Invocation(
316                 caller, "move",
317                 new Object[] { oriChildName, newParentName } ) );
318         try
319         {
320             this.configuration.getInterceptorChain().move( oriChildName, newParentName );
321         }
322         finally
323         {
324             stack.pop();
325         }
326     }
327 
328     public void move(Name oriChildName, Name newParentName, String newRn, boolean deleteOldRn) throws NamingException {
329         InvocationStack stack = InvocationStack.getInstance();
330         stack.push( new Invocation(
331                 caller, "move",
332                 new Object[] { oriChildName, newParentName, newRn, deleteOldRn? Boolean.TRUE : Boolean.FALSE } ) );
333         try
334         {
335             this.configuration.getInterceptorChain().move( oriChildName, newParentName, newRn, deleteOldRn );
336         }
337         finally
338         {
339             stack.pop();
340         }
341     }
342 
343     public Attributes getRootDSE() throws NamingException
344     {
345         InvocationStack stack = InvocationStack.getInstance();
346         stack.push( new Invocation( caller, "getRootDSE" ) );
347         try
348         {
349             return this.configuration.getInterceptorChain().getRootDSE();
350         }
351         finally
352         {
353             stack.pop();
354         }
355     }
356 }