1 package net.sf.provisioner.config;
2
3 import java.io.FileNotFoundException;
4 import java.io.InputStream;
5 import java.util.Hashtable;
6
7 import javax.naming.Context;
8 import javax.naming.NamingException;
9 import javax.naming.directory.DirContext;
10 import javax.naming.directory.InitialDirContext;
11
12 import org.apache.log4j.Logger;
13 import org.jdom.Element;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class LDAPNetworkElement extends NetworkElement {
32
33
34 Logger logger = Logger.getLogger(getClass());
35
36 private String host;
37 private String baseDN;
38 private String userDN;
39 private String authType;
40
41 public LDAPNetworkElement() {
42 super();
43 }
44
45
46
47
48
49
50
51
52
53 public LDAPNetworkElement(InputStream xmlConfig) throws FileNotFoundException {
54 super(xmlConfig);
55 }
56
57
58
59
60
61
62 void getParameters(Element current) {
63 super.getParameters(current);
64
65 for (Element child : filterChildParameters(current)) {
66 String paramName = child.getAttributeValue("name");
67 String value = child.getAttributeValue("value");
68 if (paramName.equals("host")) {
69 name = value;
70 host = value;
71 } else if (paramName.equals("userDN")) {
72 userDN = value;
73 } else if (paramName.equals("baseDN")) {
74 baseDN = value;
75 } else if (paramName.equals("authType")) {
76 authType = value;
77 }
78 }
79 }
80
81 public String getHost() {
82 return host;
83 }
84
85 public String getBaseDN() {
86 return baseDN;
87 }
88
89 public String getUserDN() {
90 return userDN;
91 }
92
93 public String getAuthType() {
94 return authType;
95 }
96
97 public DirContext findRootContext() {
98 Hashtable env = new Hashtable();
99 env.put(Context.SECURITY_PRINCIPAL , this.userDN);
100 env.put(Context.SECURITY_CREDENTIALS , this.password);
101 env.put(Context.SECURITY_AUTHENTICATION, this.authType);
102 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
103
104 String url = "";
105 if (!this.host.startsWith("ldap://")) {
106 url+= "ldap://";
107 }
108 url+= this.host;
109 if (!url.endsWith(("/"))) {
110 url+= "/";
111 }
112
113 if (port != null && port.length() > 0) {
114 try {
115 int portNum = Integer.parseInt(port);
116 url+= ":" + portNum;
117 } catch (NumberFormatException nfe) {
118 throw nfe;
119 }
120 }
121 url+= this.baseDN;
122
123 logger.trace("finding root context with uri: " + url);
124 env.put(Context.PROVIDER_URL , url);
125
126 DirContext ctx;
127 try {
128 ctx = new InitialDirContext(env);
129 return ctx;
130 } catch (NamingException e) {
131
132 throw new RuntimeException(e);
133 }
134 }
135 }