1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.directory;
19
20 import java.io.File;
21 import java.net.InetAddress;
22 import java.net.InetSocketAddress;
23 import java.util.Properties;
24 import javax.naming.Context;
25 import javax.naming.directory.InitialDirContext;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.geronimo.gbean.GBeanInfo;
29 import org.apache.geronimo.gbean.GBeanInfoBuilder;
30 import org.apache.geronimo.gbean.GBeanLifecycle;
31 import org.apache.geronimo.system.serverinfo.ServerInfo;
32 import org.apache.ldap.server.configuration.ShutdownConfiguration;
33 import org.apache.ldap.server.jndi.CoreContextFactory;
34
35 /**
36 * Directory GBean
37 *
38 * @version $Rev: 406493 $ $Date: 2006-05-14 18:14:11 -0700 (Sun, 14 May 2006) $
39 */
40 public class DirectoryGBean implements GBeanLifecycle {
41
42 private static final Log log = LogFactory.getLog(DirectoryGBean.class);
43
44 private final ServerInfo serverInfo;
45 private final String workingDir;
46 private final boolean anonymousAccess;
47 private String providerURL;
48 private String securityPrincipal;
49 private String securityCredentials;
50 private String securityAuthentication;
51 private int port = 389;
52 private InetAddress host = null;
53 private boolean enableNetworking;
54 private final String configFile;
55
56 /**
57 * Geronimo class loader
58 **/
59 private ClassLoader classLoader;
60
61 public DirectoryGBean(ClassLoader classLoader, String workingDir,
62 boolean anonymousAccess, String configFile,
63 ServerInfo serverInfo) {
64
65 if (serverInfo == null)
66 throw new IllegalArgumentException("Must have a ServerInfo value in initParams.");
67
68 if (workingDir == null)
69 this.workingDir = "var/ldap";
70 else
71 this.workingDir = workingDir;
72
73 this.classLoader = classLoader;
74 this.anonymousAccess = anonymousAccess;
75 this.configFile = configFile;
76 this.serverInfo = serverInfo;
77 setHost("0.0.0.0");
78 }
79
80 public String getProviderURL() {
81 return providerURL;
82 }
83
84 public void setProviderURL(String providerURL) {
85 this.providerURL = providerURL;
86 }
87
88 public String getSecurityAuthentication() {
89 return securityAuthentication;
90 }
91
92 public void setSecurityAuthentication(String securityAuthentication) {
93 this.securityAuthentication = securityAuthentication;
94 }
95
96 public String getSecurityCredentials() {
97 return securityCredentials;
98 }
99
100 public void setSecurityCredentials(String securityCredentials) {
101 this.securityCredentials = securityCredentials;
102 }
103
104 public String getSecurityPrincipal() {
105 return securityPrincipal;
106 }
107
108 public void setSecurityPrincipal(String securityPrincipal) {
109 this.securityPrincipal = securityPrincipal;
110 }
111
112 public boolean isEnableNetworking() {
113 return enableNetworking;
114 }
115
116 public void setEnableNetworking(boolean enableNetworking) {
117 this.enableNetworking = enableNetworking;
118 }
119
120 public String getHost() {
121 if (host == null){
122 return "0.0.0.0";
123 }
124
125 return host.getHostAddress();
126 }
127
128 public void setHost(String host) {
129 try{
130 if (host == null ){
131 this.host = null;
132 return;
133 }
134
135 String strHost = host.trim();
136 if (strHost.equals("0.0.0.0")){
137 this.host = null;
138 return;
139 }
140
141 this.host = InetAddress.getByName(strHost);
142
143 } catch (Exception e){
144 throw new RuntimeException(e);
145 }
146 }
147
148 public int getPort() {
149 return port;
150 }
151
152 public void setPort(int port) {
153 this.port = port;
154 }
155
156 public void doFail() {
157 log.warn("Service failed");
158
159 }
160
161 public InetSocketAddress getListenAddress() {
162 return new InetSocketAddress(getHost(), getPort());
163 }
164
165 private void setEnvironment(Properties env){
166
167 if (providerURL != null){
168 env.put( Context.PROVIDER_URL, providerURL);
169 }
170
171 if (securityAuthentication != null){
172 env.put( Context.SECURITY_AUTHENTICATION, securityAuthentication);
173 }
174
175 if (securityPrincipal != null){
176 env.put( Context.SECURITY_PRINCIPAL, securityPrincipal);
177 }
178
179 if (securityCredentials != null){
180 env.put( Context.SECURITY_CREDENTIALS, securityCredentials);
181 }
182 }
183
184 public void doStart() throws Exception {
185 log.debug("Starting LDAP Directory service");
186
187 MutableServerStartupConfiguration startup = new MutableServerStartupConfiguration();
188
189 startup.setWorkingDirectory(serverInfo.resolveServer(workingDir));
190 startup.setAllowAnonymousAccess(anonymousAccess);
191 startup.setLdapPort(port);
192 startup.setEnableNetworking(enableNetworking);
193 startup.setHost(host);
194
195 if (configFile != null){
196 File f = serverInfo.resolve(configFile);
197 DirectoryConfigurator dc = new DirectoryConfigurator();
198 dc.configure(classLoader, startup, f);
199 }
200
201 Properties env = new Properties();
202 env.putAll(startup.toJndiEnvironment());
203 env.put( Context.INITIAL_CONTEXT_FACTORY, ServerContextFactory.class.getName());
204 setEnvironment(env);
205
206
207 ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
208 Thread.currentThread().setContextClassLoader(classLoader);
209
210
211 new InitialDirContext( env );
212
213
214 Thread.currentThread().setContextClassLoader(oldCL);
215 log.debug("LDAP Directory service started.");
216 }
217
218 public void doStop() throws Exception {
219 log.debug("Stopping LDAP Directory service");
220
221 Properties env = new Properties();
222 env.putAll(new ShutdownConfiguration().toJndiEnvironment());
223 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
224 setEnvironment(env);
225
226
227 ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
228 Thread.currentThread().setContextClassLoader(classLoader);
229
230
231 new InitialDirContext( env );
232
233
234 Thread.currentThread().setContextClassLoader(oldCL);
235
236 }
237
238 public static final GBeanInfo GBEAN_INFO;
239
240 static {
241 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Apache Directory LDAP", DirectoryGBean.class);
242
243 infoFactory.addAttribute("classLoader", ClassLoader.class, false);
244
245 infoFactory.addAttribute("providerURL", String.class, true, true);
246 infoFactory.addAttribute("securityAuthentication", String.class, true, true);
247 infoFactory.addAttribute("securityPrincipal", String.class, true, true);
248 infoFactory.addAttribute("securityCredentials", String.class, true, true);
249 infoFactory.addAttribute("port", int.class, true, true);
250 infoFactory.addAttribute("host", String.class, true, true);
251 infoFactory.addAttribute("listenAddress", InetSocketAddress.class, false, false);
252 infoFactory.addAttribute("enableNetworking", boolean.class, true, true);
253
254 infoFactory.addAttribute("workingDir", String.class, true);
255 infoFactory.addAttribute("anonymousAccess", boolean.class, true);
256 infoFactory.addAttribute("configFile", String.class, true);
257 infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
258
259 infoFactory.setConstructor(new String[] { "classLoader", "workingDir", "anonymousAccess", "configFile", "ServerInfo" });
260 GBEAN_INFO = infoFactory.getBeanInfo();
261 }
262
263 public static GBeanInfo getGBeanInfo() {
264 return GBEAN_INFO;
265 }
266
267 }