2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.server
.ads
;
20 import java
.io
.FileOutputStream
;
21 import java
.io
.OutputStream
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
24 import java
.util
.Properties
;
26 import javax
.naming
.Context
;
27 import javax
.naming
.InitialContext
;
28 import javax
.naming
.NamingException
;
29 import javax
.naming
.directory
.InitialDirContext
;
31 import org
.apache
.commons
.io
.FileUtils
;
32 import org
.apache
.commons
.io
.IOUtils
;
33 import org
.apache
.commons
.logging
.Log
;
34 import org
.apache
.commons
.logging
.LogFactory
;
35 import org
.apache
.directory
.server
.configuration
.MutableServerStartupConfiguration
;
36 import org
.apache
.directory
.server
.core
.configuration
.ShutdownConfiguration
;
37 import org
.apache
.directory
.server
.jndi
.ServerContextFactory
;
38 import org
.argeo
.ArgeoException
;
39 import org
.springframework
.beans
.factory
.DisposableBean
;
40 import org
.springframework
.beans
.factory
.InitializingBean
;
41 import org
.springframework
.core
.io
.Resource
;
42 import org
.springframework
.util
.Assert
;
44 @SuppressWarnings("restriction")
45 public class AdsContainer
implements InitializingBean
, DisposableBean
{
46 private final static Log log
= LogFactory
.getLog(AdsContainer
.class);
48 private MutableServerStartupConfiguration configuration
;
49 private Properties environment
= null;
50 private File workingDirectory
= new File(System
51 .getProperty("java.io.tmpdir")
52 + File
.separator
+ "argeo-apacheDirectoryServer");
53 private List
<Resource
> ldifs
= new ArrayList
<Resource
>();
54 private File ldifDirectory
;
55 private Boolean deleteWorkingDirOnExit
= false;
57 @SuppressWarnings("unchecked")
58 public void afterPropertiesSet() throws Exception
{
60 log
.info("Starting directory server with id '"
61 + configuration
.getInstanceId() + "' in directory "
62 + workingDirectory
.getAbsolutePath());
64 if (deleteWorkingDirOnExit
&& workingDirectory
.exists()) {
65 log
.warn("Found existing directory " + workingDirectory
67 FileUtils
.deleteDirectory(workingDirectory
);
69 configuration
.setWorkingDirectory(workingDirectory
);
70 workingDirectory
.mkdirs();
72 if (ldifDirectory
!= null)
73 configuration
.setLdifDirectory(ldifDirectory
);
75 configuration
.setLdifDirectory(new File(workingDirectory
77 + File
.separator
+ "ldif"));
79 // Deals with provided LDIF files
81 configuration
.getLdifDirectory().mkdirs();
82 for (Resource ldif
: ldifs
) {
83 File targetFile
= new File(configuration
.getLdifDirectory()
85 + File
.separator
+ ldif
.getFilename().replace(':', '_'));
86 OutputStream output
= null;
88 output
= new FileOutputStream(targetFile
);
89 IOUtils
.copy(ldif
.getInputStream(), output
);
90 if (log
.isDebugEnabled())
91 log
.debug("Copied " + ldif
+ " to LDIF directory "
92 + configuration
.getLdifDirectory());
94 IOUtils
.closeQuietly(output
);
98 Properties env
= new Properties();
99 env
.setProperty(Context
.INITIAL_CONTEXT_FACTORY
,
100 ServerContextFactory
.class.getName());
101 Assert
.notNull(environment
);
102 env
.putAll(environment
);
103 env
.putAll(configuration
.toJndiEnvironment());
106 new InitialDirContext(env
);
107 } catch (NamingException e
) {
108 throw new ArgeoException("Failed to start Apache Directory server",
113 @SuppressWarnings("unchecked")
114 public void destroy() throws Exception
{
115 ShutdownConfiguration shutdown
= new ShutdownConfiguration(
116 configuration
.getInstanceId());
118 Properties env
= new Properties();
119 env
.setProperty(Context
.INITIAL_CONTEXT_FACTORY
,
120 ServerContextFactory
.class.getName());
121 Assert
.notNull(environment
);
122 env
.putAll(environment
);
123 env
.putAll(shutdown
.toJndiEnvironment());
125 log
.info("Shutting down directory server with id '"
126 + configuration
.getInstanceId() + "'");
129 new InitialContext(env
);
130 } catch (NamingException e
) {
131 throw new ArgeoException("Failed to stop Apache Directory server",
135 if (workingDirectory
.exists() && deleteWorkingDirOnExit
) {
136 if (log
.isDebugEnabled())
137 log
.debug("Delete Apache DS working dir " + workingDirectory
);
138 FileUtils
.deleteDirectory(workingDirectory
);
143 public void setConfiguration(MutableServerStartupConfiguration configuration
) {
144 this.configuration
= configuration
;
147 public void setWorkingDirectory(File workingDirectory
) {
148 this.workingDirectory
= workingDirectory
;
151 public void setEnvironment(Properties environment
) {
152 this.environment
= environment
;
155 public void setLdifs(List
<Resource
> ldifs
) {
159 public void setLdifDirectory(File ldifDirectory
) {
160 this.ldifDirectory
= ldifDirectory
;
163 public void setDeleteWorkingDirOnExit(Boolean deleteWorkingDirOnExit
) {
164 this.deleteWorkingDirOnExit
= deleteWorkingDirOnExit
;