]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.ads/src/main/java/org/argeo/server/ads/AdsContainer.java
ce6e137c39c8fc40e8f42fdb0c61267718a08999
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.ads / src / main / java / org / argeo / server / ads / AdsContainer.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.server.ads;
17
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.FileWriter;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.io.Writer;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
27
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import javax.naming.directory.InitialDirContext;
32
33 import org.apache.commons.io.FileUtils;
34 import org.apache.commons.io.IOUtils;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.directory.server.configuration.MutableServerStartupConfiguration;
38 import org.apache.directory.server.core.configuration.ShutdownConfiguration;
39 import org.apache.directory.server.jndi.ServerContextFactory;
40 import org.argeo.ArgeoException;
41 import org.springframework.beans.factory.DisposableBean;
42 import org.springframework.beans.factory.InitializingBean;
43 import org.springframework.core.io.Resource;
44 import org.springframework.util.Assert;
45
46 /** Wraps an Apache Directory Server instance. */
47 @SuppressWarnings("restriction")
48 public class AdsContainer implements InitializingBean, DisposableBean {
49 private final static Log log = LogFactory.getLog(AdsContainer.class);
50
51 private MutableServerStartupConfiguration configuration;
52 private Properties environment = null;
53 private File workingDirectory = new File(
54 System.getProperty("java.io.tmpdir") + File.separator
55 + "argeo-apacheDirectoryServer");
56 private Boolean deleteWorkingDirOnExit = false;
57
58 // LDIF
59 private List<Resource> ldifs = new ArrayList<Resource>();
60 private List<String> ignoredLdifAttributes = new ArrayList<String>();
61 /** default is 'demo' */
62 private String ldifPassword = "e1NIQX1pZVNWNTVRYytlUU9hWURSU2hhL0Fqek5USkU9";
63 private String ldifPasswordAttribute = "userPassword";
64 private File ldifDirectory;
65
66 @SuppressWarnings("unchecked")
67 public void afterPropertiesSet() throws Exception {
68
69 log.info("Starting directory server with id '"
70 + configuration.getInstanceId() + "' in directory "
71 + workingDirectory.getAbsolutePath());
72
73 if (deleteWorkingDirOnExit && workingDirectory.exists()) {
74 log.warn("Found existing directory " + workingDirectory
75 + " deleting it...");
76 FileUtils.deleteDirectory(workingDirectory);
77 }
78 configuration.setWorkingDirectory(workingDirectory);
79 workingDirectory.mkdirs();
80
81 if (ldifDirectory != null)
82 configuration.setLdifDirectory(ldifDirectory);
83 else
84 configuration.setLdifDirectory(new File(workingDirectory
85 .getAbsolutePath() + File.separator + "ldif"));
86
87 if (ignoredLdifAttributes.size() == 0) {
88 ignoredLdifAttributes.add("entryUUID");
89 ignoredLdifAttributes.add("structuralObjectClass");
90 ignoredLdifAttributes.add("creatorsName");
91 ignoredLdifAttributes.add("createTimestamp");
92 ignoredLdifAttributes.add("entryCSN");
93 ignoredLdifAttributes.add("modifiersName");
94 ignoredLdifAttributes.add("modifyTimestamp");
95 }
96
97 // Process provided LDIF files
98 if (ldifs.size() > 0)
99 configuration.getLdifDirectory().mkdirs();
100 for (Resource ldif : ldifs) {
101 File targetFile = new File(configuration.getLdifDirectory()
102 .getAbsolutePath()
103 + File.separator
104 + ldif.getFilename().replace(':', '_'));
105 processLdif(ldif, targetFile);
106 }
107
108 Properties env = new Properties();
109 env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
110 ServerContextFactory.class.getName());
111 Assert.notNull(environment);
112 env.putAll(environment);
113 env.putAll(configuration.toJndiEnvironment());
114
115 try {
116 new InitialDirContext(env);
117 } catch (NamingException e) {
118 throw new ArgeoException("Failed to start Apache Directory server",
119 e);
120 }
121 }
122
123 /**
124 * Processes an LDIF resource, filtering out attributes that cannot be
125 * imported in ADS and forcing a password.
126 */
127 protected void processLdif(Resource ldif, File targetFile) {
128 BufferedReader reader = null;
129 Writer writer = null;
130 try {
131 reader = new BufferedReader(new InputStreamReader(
132 ldif.getInputStream()));
133 writer = new FileWriter(targetFile);
134 String line = null;
135 lines: while ((line = reader.readLine()) != null) {
136 // comment and empty lines
137 if (line.trim().equals("") || line.startsWith("#")) {
138 writer.write(line);
139 writer.write('\n');
140 continue lines;
141 }
142
143 String[] tokens = line.split(":");
144 String attribute = null;
145 if (tokens != null && tokens.length > 1) {
146 attribute = tokens[0].trim();
147 if (ignoredLdifAttributes.contains(attribute))
148 continue lines;// ignore
149
150 if (attribute.equals("bdb_db_open")) {
151 log.warn("Ignored OpenLDAP output\n" + line);
152 continue lines;
153 }
154
155 if (ldifPassword != null
156 && attribute.equals(ldifPasswordAttribute)) {
157 line = ldifPasswordAttribute + ":: " + ldifPassword;
158 }
159
160 writer.write(line);
161 writer.write('\n');
162 } else {
163 log.warn("Ignored LDIF line\n" + line);
164 }
165 }
166 if (log.isDebugEnabled())
167 log.debug("Processed " + ldif + " to LDIF directory "
168 + configuration.getLdifDirectory());
169 } catch (IOException e) {
170 throw new ArgeoException("Cannot process LDIF " + ldif, e);
171 } finally {
172 IOUtils.closeQuietly(reader);
173 IOUtils.closeQuietly(writer);
174 }
175 }
176
177 @SuppressWarnings("unchecked")
178 public void destroy() throws Exception {
179 ShutdownConfiguration shutdown = new ShutdownConfiguration(
180 configuration.getInstanceId());
181
182 Properties env = new Properties();
183 env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
184 ServerContextFactory.class.getName());
185 Assert.notNull(environment);
186 env.putAll(environment);
187 env.putAll(shutdown.toJndiEnvironment());
188
189 log.info("Shutting down directory server with id '"
190 + configuration.getInstanceId() + "'");
191
192 try {
193 new InitialContext(env);
194 } catch (NamingException e) {
195 throw new ArgeoException("Failed to stop Apache Directory server",
196 e);
197 }
198
199 if (workingDirectory.exists() && deleteWorkingDirOnExit) {
200 if (log.isDebugEnabled())
201 log.debug("Delete Apache DS working dir " + workingDirectory);
202 FileUtils.deleteDirectory(workingDirectory);
203 }
204
205 }
206
207 public void setConfiguration(MutableServerStartupConfiguration configuration) {
208 this.configuration = configuration;
209 }
210
211 public void setWorkingDirectory(File workingDirectory) {
212 this.workingDirectory = workingDirectory;
213 }
214
215 public void setEnvironment(Properties environment) {
216 this.environment = environment;
217 }
218
219 public void setLdifs(List<Resource> ldifs) {
220 this.ldifs = ldifs;
221 }
222
223 public void setLdifDirectory(File ldifDirectory) {
224 this.ldifDirectory = ldifDirectory;
225 }
226
227 public void setDeleteWorkingDirOnExit(Boolean deleteWorkingDirOnExit) {
228 this.deleteWorkingDirOnExit = deleteWorkingDirOnExit;
229 }
230
231 public void setIgnoredLdifAttributes(List<String> ignoredLdifAttributes) {
232 this.ignoredLdifAttributes = ignoredLdifAttributes;
233 }
234
235 public void setLdifPassword(String ldifPassword) {
236 this.ldifPassword = ldifPassword;
237 }
238
239 public void setLdifPasswordAttribute(String ldifPasswordAttribute) {
240 this.ldifPasswordAttribute = ldifPasswordAttribute;
241 }
242
243 }