]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.ads/src/main/java/org/argeo/server/ads/AdsContainer.java
0753e14ebd5cf46bd4c582ee424d20f1236b7f23
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.ads / src / main / java / org / argeo / server / ads / AdsContainer.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.server.ads;
18
19 import java.io.File;
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;
25
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import javax.naming.directory.InitialDirContext;
30
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;
43
44 @SuppressWarnings("restriction")
45 public class AdsContainer implements InitializingBean, DisposableBean {
46 private final static Log log = LogFactory.getLog(AdsContainer.class);
47
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;
56
57 @SuppressWarnings("unchecked")
58 public void afterPropertiesSet() throws Exception {
59
60 log.info("Starting directory server with id '"
61 + configuration.getInstanceId() + "' in directory "
62 + workingDirectory.getAbsolutePath());
63
64 if (deleteWorkingDirOnExit && workingDirectory.exists()) {
65 log.warn("Found existing directory " + workingDirectory
66 + " deleting it...");
67 FileUtils.deleteDirectory(workingDirectory);
68 }
69 configuration.setWorkingDirectory(workingDirectory);
70 workingDirectory.mkdirs();
71
72 if (ldifDirectory != null)
73 configuration.setLdifDirectory(ldifDirectory);
74 else
75 configuration.setLdifDirectory(new File(workingDirectory
76 .getAbsolutePath()
77 + File.separator + "ldif"));
78
79 // Deals with provided LDIF files
80 if (ldifs.size() > 0)
81 configuration.getLdifDirectory().mkdirs();
82 for (Resource ldif : ldifs) {
83 File targetFile = new File(configuration.getLdifDirectory()
84 .getAbsolutePath()
85 + File.separator + ldif.getFilename().replace(':', '_'));
86 OutputStream output = null;
87 try {
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());
93 } finally {
94 IOUtils.closeQuietly(output);
95 }
96 }
97
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());
104
105 try {
106 new InitialDirContext(env);
107 } catch (NamingException e) {
108 throw new ArgeoException("Failed to start Apache Directory server",
109 e);
110 }
111 }
112
113 @SuppressWarnings("unchecked")
114 public void destroy() throws Exception {
115 ShutdownConfiguration shutdown = new ShutdownConfiguration(
116 configuration.getInstanceId());
117
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());
124
125 log.info("Shutting down directory server with id '"
126 + configuration.getInstanceId() + "'");
127
128 try {
129 new InitialContext(env);
130 } catch (NamingException e) {
131 throw new ArgeoException("Failed to stop Apache Directory server",
132 e);
133 }
134
135 if (workingDirectory.exists() && deleteWorkingDirOnExit) {
136 if (log.isDebugEnabled())
137 log.debug("Delete Apache DS working dir " + workingDirectory);
138 FileUtils.deleteDirectory(workingDirectory);
139 }
140
141 }
142
143 public void setConfiguration(MutableServerStartupConfiguration configuration) {
144 this.configuration = configuration;
145 }
146
147 public void setWorkingDirectory(File workingDirectory) {
148 this.workingDirectory = workingDirectory;
149 }
150
151 public void setEnvironment(Properties environment) {
152 this.environment = environment;
153 }
154
155 public void setLdifs(List<Resource> ldifs) {
156 this.ldifs = ldifs;
157 }
158
159 public void setLdifDirectory(File ldifDirectory) {
160 this.ldifDirectory = ldifDirectory;
161 }
162
163 public void setDeleteWorkingDirOnExit(Boolean deleteWorkingDirOnExit) {
164 this.deleteWorkingDirOnExit = deleteWorkingDirOnExit;
165 }
166
167 }