View Javadoc

1   /*
2    * Copyright 2002-2005 the original author or authors.
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 net.sf.ldaptemplate.support;
18  
19  import java.util.Hashtable;
20  
21  import javax.naming.Context;
22  import javax.naming.Name;
23  import javax.naming.directory.Attributes;
24  import javax.naming.spi.DirObjectFactory;
25  
26  /***
27   * Default implementation of the DirObjectFactory interface. Creates a
28   * DirContextAdapter from the supplied arguments.
29   * 
30   * @author Mattias Arthursson
31   */
32  public class DefaultDirObjectFactory implements DirObjectFactory {
33      /***
34       * Key to use in the ContextSource implementation to store the value of the
35       * base path suffix, if any, in the Ldap Environment.
36       */
37      public static final String JNDI_ENV_BASE_PATH_KEY = "net.sf.ldaptemplate.base.path";
38  
39      /***
40       * Creates a DirContextAdapter from the supplied arguments.
41       * 
42       * @param obj
43       * @param name
44       * @param nameCtx
45       * @param environment
46       * @param attrs
47       * @return a new DirContextAdapter from the attributes and name.
48       * @throws Exception
49       */
50      public Object getObjectInstance(Object obj, Name name, Context nameCtx,
51              Hashtable environment, Attributes attrs) throws Exception {
52  
53          try {
54              DirContextAdapter dirContextAdapter = new DirContextAdapter(attrs,
55                      stripBasePath(name, environment));
56              dirContextAdapter.setUpdateMode(true);
57  
58              return dirContextAdapter;
59          } finally {
60              // It seems that the object supplied to the obj parameter is a
61              // DirContext instance with reference to the same Ldap connection as
62              // the original context. Since it is not the same instance (that's
63              // the nameCtx parameter) this one really needs to be closed in
64              // order to correctly clean up and return the connection to the pool
65              // when we're finished with the surrounding operation.
66              if (obj instanceof Context) {
67                  Context ctx = (Context) obj;
68                  try {
69                      ctx.close();
70                  } catch (Exception e) {
71                      // Never mind this
72                  }
73  
74              }
75          }
76      }
77  
78      /***
79       * Rather crude method to remove the base suffix if specified in the LDAP
80       * environment. This could probably be improved to increase performance.
81       * 
82       * @param name
83       *            the full distinguished name of the found object.
84       * @param environment
85       *            the context environment.
86       * @return the DN, without the base suffix.
87       */
88      private Name stripBasePath(Name name, Hashtable environment) {
89          if (environment.containsKey(JNDI_ENV_BASE_PATH_KEY)) {
90              DistinguishedName distinguishedName = new DistinguishedName(name
91                      .toString());
92              distinguishedName.removeFirst((Name) environment
93                      .get(JNDI_ENV_BASE_PATH_KEY));
94              return distinguishedName;
95          } else {
96              return name;
97          }
98      }
99  
100     /***
101      * Returns null.
102      * 
103      * @param obj
104      * @param name
105      * @param nameCtx
106      * @param environment
107      * @return null.
108      * @throws Exception
109      */
110     public Object getObjectInstance(Object obj, Name name, Context nameCtx,
111             Hashtable environment) throws Exception {
112         return null;
113     }
114 
115 }