1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
61
62
63
64
65
66 if (obj instanceof Context) {
67 Context ctx = (Context) obj;
68 try {
69 ctx.close();
70 } catch (Exception e) {
71
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 }