|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig.handler; |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| import java.io.InputStream; |
|
12 |
| import java.util.Hashtable; |
|
13 |
| import java.util.Properties; |
|
14 |
| |
|
15 |
| import javax.naming.Context; |
|
16 |
| import javax.naming.NamingEnumeration; |
|
17 |
| import javax.naming.directory.InitialDirContext; |
|
18 |
| import javax.naming.directory.SearchControls; |
|
19 |
| import javax.naming.directory.SearchResult; |
|
20 |
| |
|
21 |
| import org.jconfig.Configuration; |
|
22 |
| import org.jconfig.ConfigurationManagerException; |
|
23 |
| import org.jconfig.DefaultConfiguration; |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| public class LDAPHandler implements ConfigurationHandler { |
|
29 |
| |
|
30 |
| private String baseDN; |
|
31 |
| |
|
32 |
0
| public LDAPHandler() {
|
|
33 |
| } |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
0
| public Configuration load(String configurationName) throws ConfigurationManagerException {
|
|
40 |
0
| InitialDirContext ctx = getContext();
|
|
41 |
0
| if ( ctx == null ) {
|
|
42 |
0
| throw new ConfigurationManagerException("There is no connection to the LDAP server");
|
|
43 |
| } |
|
44 |
0
| Configuration config = new DefaultConfiguration(null);
|
|
45 |
0
| getCategories(ctx,config);
|
|
46 |
0
| getAllProperties(ctx,config);
|
|
47 |
0
| return config;
|
|
48 |
| } |
|
49 |
| |
|
50 |
0
| private void getCategories(InitialDirContext ctx,Configuration config) {
|
|
51 |
0
| try {
|
|
52 |
0
| SearchControls sctrl = new SearchControls();
|
|
53 |
0
| String filter = "(&(objectclass=organizationalUnit)(ou=*))";
|
|
54 |
0
| NamingEnumeration enm = ctx.search(baseDN, filter, sctrl);
|
|
55 |
0
| while (enm.hasMore()) {
|
|
56 |
0
| SearchResult sr = (SearchResult) enm.next();
|
|
57 |
0
| config.setCategory(sr.getName());
|
|
58 |
| } |
|
59 |
| |
|
60 |
| } catch (Exception e) { |
|
61 |
| |
|
62 |
0
| e.printStackTrace();
|
|
63 |
| } |
|
64 |
| } |
|
65 |
| |
|
66 |
0
| private void getAllProperties(InitialDirContext ctx,Configuration config) {
|
|
67 |
| } |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
0
| public void store() throws ConfigurationManagerException {
|
|
72 |
| } |
|
73 |
| |
|
74 |
0
| private InitialDirContext getContext() {
|
|
75 |
0
| Properties props = new Properties();
|
|
76 |
0
| try {
|
|
77 |
0
| InputStream is = getClass().getClassLoader().getResourceAsStream("ldap.properties");
|
|
78 |
0
| if ( is == null ) {
|
|
79 |
0
| return null;
|
|
80 |
| } |
|
81 |
0
| props.load(is);
|
|
82 |
0
| Hashtable env = new Hashtable();
|
|
83 |
| |
|
84 |
0
| env.put(Context.INITIAL_CONTEXT_FACTORY, props.getProperty("Context", ""));
|
|
85 |
0
| env.put(Context.PROVIDER_URL, props.getProperty("Host", ""));
|
|
86 |
0
| env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
|
87 |
0
| env.put(Context.SECURITY_PRINCIPAL, props.getProperty("RootDN", ""));
|
|
88 |
0
| env.put(Context.SECURITY_CREDENTIALS, props.getProperty("Password", ""));
|
|
89 |
0
| baseDN = props.getProperty("BaseDN","");
|
|
90 |
0
| return new InitialDirContext(env);
|
|
91 |
| } |
|
92 |
| catch (Exception e) { |
|
93 |
| |
|
94 |
0
| e.printStackTrace();
|
|
95 |
0
| return null;
|
|
96 |
| } |
|
97 |
| } |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
0
| public void store(Configuration configuration) throws ConfigurationManagerException {
|
|
103 |
| } |
|
104 |
| |
|
105 |
| } |