1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.talika.jsm;
24
25 import java.util.Hashtable;
26 import java.util.Properties;
27 import javax.naming.*;
28 import javax.naming.directory.*;
29 import javax.naming.spi.*;
30 import javax.management.*;
31
32 import org.jboss.util.ServiceMBeanSupport;
33
34 /***
35 *
36 * @author Jose M. Palomar <josem@talika.org>
37 * @version $Revision: 20 $
38 */
39 public class LDAPSecurityModuleService extends ServiceMBeanSupport implements LDAPSecurityModuleServiceMBean, ObjectFactory
40 {
41
42 public LDAPSecurityModuleService() {
43 }
44
45 public LDAPSecurityModuleService(String name, String url, String bindDN, String passwd, String searchBase) {
46 _name = name;
47 _url = url;
48 _bindDN = bindDN;
49 _passwd = passwd;
50 _searchBase = searchBase;
51 }
52
53 public String getName()
54 {
55 return "LDAP Security Module";
56 }
57
58 protected ObjectName getObjectName(MBeanServer server, ObjectName name)
59 throws javax.management.MalformedObjectNameException
60 {
61 this.server = server;
62 return new ObjectName(OBJECT_NAME);
63 }
64
65 protected void initService()
66 throws Exception
67 {
68 }
69
70 protected void startService()
71 throws Exception
72 {
73
74
75 if(_name == null || _url == null || _searchBase == null) {
76
77 if(_name == null) log.log("InstanceName attribute not set");
78 if(_url == null) log.log("Url attribute not set");
79 if(_searchBase == null) log.log("SearchBase attribute not set");
80
81 log.log("LDAP Security Module not started");
82
83 return;
84
85 }
86
87
88 Properties p = new Properties();
89 p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
90 p.setProperty(Context.PROVIDER_URL, _url);
91 if(_bindDN != null) p.setProperty(Context.SECURITY_PRINCIPAL, _bindDN);
92 if(_passwd != null) p.setProperty(Context.SECURITY_CREDENTIALS, _passwd);
93 DirContext dirCtx = new InitialDirContext(p);
94
95
96 LDAPSecurityModule ldapsm = new LDAPSecurityModule(dirCtx, _searchBase);
97
98
99 Reference ref = new Reference(ldapsm.getClass().toString(), getClass().getName(), null);
100 Context ctx = (Context) new InitialContext();
101 jndiName = JNDI_NAME_PREFIX + "/" + _name;
102 try {
103 ctx.bind(jndiName, ref);
104 }
105 catch(NameNotFoundException nabe) {
106 ctx.createSubcontext(JNDI_NAME_PREFIX);
107 ctx.bind(jndiName, ref);
108 }
109
110
111 ldapsmTable.put(_name, ldapsm);
112
113 log.log("LDAP Security Module " + _name + " bound to " + jndiName);
114 log.log("Url: " + _url);
115 log.log("Searh Base: " + _searchBase);
116 if(_bindDN != null) log.log("BindDN: " + _bindDN);
117 if(_passwd != null) log.log("Password: " + _passwd);
118
119 }
120
121 protected void stopService()
122 {
123 try
124 {
125 new InitialContext().unbind(jndiName);
126 ldapsmTable.remove(_name);
127 }
128 catch (CommunicationException e) {
129 }
130 catch (Exception e)
131 {
132 log.exception(e);
133 }
134 }
135
136 protected void destroyService()
137 {
138 }
139
140 public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment)
141 throws Exception
142 {
143 return ldapsmTable.get(name.get(name.size()-1));
144 }
145
146 public void setInstanceName(String name) {
147 _name = name;
148 }
149
150 public String getInstanceName() {
151 return _name;
152 }
153
154 public void setUrl(String url) {
155 _url = url;
156 }
157
158 public String getUrl() {
159 return _url;
160 }
161
162 public void setBindDN(String bindDN) {
163 _bindDN = bindDN;
164 }
165
166 public String getBindDN() {
167 return _bindDN;
168 }
169
170 public void setPassword(String password) {
171 _passwd = password;
172 }
173
174 public String getPassword() {
175 return _passwd;
176 }
177
178 public void setSearchBase(String searchBase) {
179 _searchBase = searchBase;
180 }
181
182 public String getSearchBase() {
183 return _searchBase;
184 }
185
186
187 private MBeanServer server = null;
188 private String _name = null;
189 private String _url = null;
190 private String _bindDN = null;
191 private String _passwd = null;
192 private String _searchBase = null;
193 private String jndiName = null;
194
195 private static Hashtable ldapsmTable = new Hashtable();
196
197
198 public static String JNDI_NAME_PREFIX = "java:/security";
199
200 }