KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > directory > DirectoryGBean


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.InetSocketAddress JavaDoc;
23 import java.util.Properties JavaDoc;
24 import javax.naming.Context JavaDoc;
25 import javax.naming.directory.InitialDirContext JavaDoc;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 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 JavaDoc workingDir;
46     private final boolean anonymousAccess;
47     private String JavaDoc providerURL;
48     private String JavaDoc securityPrincipal;
49     private String JavaDoc securityCredentials;
50     private String JavaDoc securityAuthentication;
51     private int port = 389;
52     private InetAddress JavaDoc host = null;
53     private boolean enableNetworking;
54     private final String JavaDoc configFile;
55     
56     /**
57      * Geronimo class loader
58      **/

59     private ClassLoader JavaDoc classLoader;
60
61     public DirectoryGBean(ClassLoader JavaDoc classLoader, String JavaDoc workingDir,
62             boolean anonymousAccess, String JavaDoc configFile,
63             ServerInfo serverInfo) {
64         
65         if (serverInfo == null)
66             throw new IllegalArgumentException JavaDoc("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 JavaDoc getProviderURL() {
81         return providerURL;
82     }
83
84     public void setProviderURL(String JavaDoc providerURL) {
85         this.providerURL = providerURL;
86     }
87
88     public String JavaDoc getSecurityAuthentication() {
89         return securityAuthentication;
90     }
91
92     public void setSecurityAuthentication(String JavaDoc securityAuthentication) {
93         this.securityAuthentication = securityAuthentication;
94     }
95
96     public String JavaDoc getSecurityCredentials() {
97         return securityCredentials;
98     }
99
100     public void setSecurityCredentials(String JavaDoc securityCredentials) {
101         this.securityCredentials = securityCredentials;
102     }
103
104     public String JavaDoc getSecurityPrincipal() {
105         return securityPrincipal;
106     }
107
108     public void setSecurityPrincipal(String JavaDoc 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 JavaDoc getHost() {
121         if (host == null){
122             return "0.0.0.0";
123         }
124         
125         return host.getHostAddress();
126     }
127
128     public void setHost(String JavaDoc host) {
129         try{
130             if (host == null ){
131                 this.host = null;
132                 return;
133             }
134             
135             String JavaDoc 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 JavaDoc e){
144             throw new RuntimeException JavaDoc(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         //Insert failure code here
159
}
160
161     public InetSocketAddress JavaDoc getListenAddress() {
162         return new InetSocketAddress JavaDoc(getHost(), getPort());
163     }
164
165     private void setEnvironment(Properties JavaDoc 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 JavaDoc {
185         log.debug("Starting LDAP Directory service");
186         
187         MutableServerStartupConfiguration startup = new MutableServerStartupConfiguration();
188         // put some mandatory JNDI properties here
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 JavaDoc f = serverInfo.resolve(configFile);
197             DirectoryConfigurator dc = new DirectoryConfigurator();
198             dc.configure(classLoader, startup, f);
199         }
200         
201         Properties JavaDoc env = new Properties JavaDoc();
202         env.putAll(startup.toJndiEnvironment());
203         env.put( Context.INITIAL_CONTEXT_FACTORY, ServerContextFactory.class.getName());
204         setEnvironment(env);
205         
206         //Load the proper class for the Context Loader
207
ClassLoader JavaDoc oldCL = Thread.currentThread().getContextClassLoader();
208         Thread.currentThread().setContextClassLoader(classLoader);
209
210         //Fire it up
211
new InitialDirContext JavaDoc( env );
212         
213         //Set it back
214
Thread.currentThread().setContextClassLoader(oldCL);
215         log.debug("LDAP Directory service started.");
216     }
217
218     public void doStop() throws Exception JavaDoc {
219         log.debug("Stopping LDAP Directory service");
220         //Insert stopping code here
221
Properties JavaDoc env = new Properties JavaDoc();
222         env.putAll(new ShutdownConfiguration().toJndiEnvironment());
223         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
224         setEnvironment(env);
225         
226         //Load the proper class for th Context Loader
227
ClassLoader JavaDoc oldCL = Thread.currentThread().getContextClassLoader();
228         Thread.currentThread().setContextClassLoader(classLoader);
229
230         //Shut it down
231
new InitialDirContext JavaDoc( env );
232
233         //Set it back
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 JavaDoc.class, false);
244         
245         infoFactory.addAttribute("providerURL", String JavaDoc.class, true, true);
246         infoFactory.addAttribute("securityAuthentication", String JavaDoc.class, true, true);
247         infoFactory.addAttribute("securityPrincipal", String JavaDoc.class, true, true);
248         infoFactory.addAttribute("securityCredentials", String JavaDoc.class, true, true);
249         infoFactory.addAttribute("port", int.class, true, true);
250         infoFactory.addAttribute("host", String JavaDoc.class, true, true);
251         infoFactory.addAttribute("listenAddress", InetSocketAddress JavaDoc.class, false, false);
252         infoFactory.addAttribute("enableNetworking", boolean.class, true, true);
253         
254         infoFactory.addAttribute("workingDir", String JavaDoc.class, true);
255         infoFactory.addAttribute("anonymousAccess", boolean.class, true);
256         infoFactory.addAttribute("configFile", String JavaDoc.class, true);
257         infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
258
259         infoFactory.setConstructor(new String JavaDoc[] { "classLoader", "workingDir", "anonymousAccess", "configFile", "ServerInfo" });
260         GBEAN_INFO = infoFactory.getBeanInfo();
261     }
262
263     public static GBeanInfo getGBeanInfo() {
264         return GBEAN_INFO;
265     }
266
267 }
268
Popular Tags