]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JackrabbitContainer.java
82d58fb31fdc025cd8d78428cd3c36f21f106319
[lgpl/argeo-commons.git] / JackrabbitContainer.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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.jackrabbit;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Properties;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import java.util.UUID;
27
28 import javax.jcr.Node;
29 import javax.jcr.Repository;
30 import javax.jcr.RepositoryException;
31 import javax.jcr.Session;
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.jackrabbit.api.JackrabbitRepository;
38 import org.apache.jackrabbit.core.RepositoryImpl;
39 import org.apache.jackrabbit.core.config.RepositoryConfig;
40 import org.apache.jackrabbit.core.config.RepositoryConfigurationParser;
41 import org.argeo.ArgeoException;
42 import org.argeo.jcr.ArgeoNames;
43 import org.argeo.jcr.JcrUtils;
44 import org.argeo.jcr.MaintainedRepository;
45 import org.springframework.core.io.Resource;
46 import org.springframework.util.SystemPropertyUtils;
47 import org.xml.sax.InputSource;
48
49 /**
50 * Wrapper around a Jackrabbit repository which allows to configure it in Spring
51 * and expose it as a {@link Repository}.
52 */
53 public class JackrabbitContainer extends JackrabbitWrapper implements
54 MaintainedRepository {
55 private final static Log log = LogFactory.getLog(JackrabbitContainer.class);
56
57 // local
58 private Resource configuration;
59
60 private Resource variables;
61
62 private RepositoryConfig repositoryConfig;
63 private File homeDirectory;
64 private Boolean inMemory = false;
65
66 /** Migrations to execute (if not already done) */
67 private Set<JackrabbitDataModelMigration> dataModelMigrations = new HashSet<JackrabbitDataModelMigration>();
68
69 /** Straight (non spring) values */
70 private Properties configurationProperties;
71 private InputSource configurationXml;
72
73 /**
74 * Empty constructor, {@link #init()} should be called after properties have
75 * been set
76 */
77 public JackrabbitContainer() {
78 }
79
80 public void init() {
81 long begin = System.currentTimeMillis();
82
83 if (getRepository() != null)
84 throw new ArgeoException(
85 "Cannot be used to wrap another repository");
86 Repository repository = createJackrabbitRepository();
87 super.setRepository(repository);
88
89 // migrate if needed
90 migrate();
91
92 // apply new CND files after migration
93 prepareDataModel();
94
95 double duration = ((double) (System.currentTimeMillis() - begin)) / 1000;
96 if (log.isDebugEnabled())
97 log.debug("Initialized JCR repository wrapper in " + duration
98 + " s");
99 }
100
101 /** Actually creates the new repository. */
102 protected Repository createJackrabbitRepository() {
103 long begin = System.currentTimeMillis();
104 InputStream configurationIn = null;
105 Repository repository;
106 try {
107 // temporary
108 if (inMemory && getHomeDirectory().exists()) {
109 FileUtils.deleteDirectory(getHomeDirectory());
110 log.warn("Deleted Jackrabbit home directory "
111 + getHomeDirectory());
112 }
113
114 // process configuration file
115 Properties vars = getConfigurationProperties();
116 vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
117 getHomeDirectory().getCanonicalPath());
118 InputSource is;
119 if (configurationXml != null)
120 is = configurationXml;
121 else {
122 configurationIn = readConfiguration();
123 is = new InputSource(configurationIn);
124 }
125 repositoryConfig = RepositoryConfig.create(is, vars);
126
127 //
128 // Actual repository creation
129 //
130 repository = RepositoryImpl.create(repositoryConfig);
131
132 double duration = ((double) (System.currentTimeMillis() - begin)) / 1000;
133 if (log.isTraceEnabled())
134 log.trace("Created Jackrabbit repository in " + duration
135 + " s, home: " + getHomeDirectory());
136
137 return repository;
138 } catch (Exception e) {
139 throw new ArgeoException("Cannot create Jackrabbit repository "
140 + getHomeDirectory(), e);
141 } finally {
142 IOUtils.closeQuietly(configurationIn);
143 }
144 }
145
146 /** Lazy init. */
147 protected File getHomeDirectory() {
148 try {
149 if (homeDirectory == null) {
150 if (inMemory) {
151 homeDirectory = new File(
152 System.getProperty("java.io.tmpdir")
153 + File.separator
154 + System.getProperty("user.name")
155 + File.separator + "jackrabbit-"
156 + UUID.randomUUID());
157 homeDirectory.mkdirs();
158 // will it work if directory is not empty??
159 homeDirectory.deleteOnExit();
160 }
161 }
162
163 return homeDirectory.getCanonicalFile();
164 } catch (IOException e) {
165 throw new ArgeoException("Cannot get canonical file for "
166 + homeDirectory, e);
167 }
168 }
169
170 /** Executes migrations, if needed. */
171 protected void migrate() {
172 // No migration to perform
173 if (dataModelMigrations.size() == 0)
174 return;
175
176 Boolean restartAndClearCaches = false;
177
178 // migrate data
179 Session session = null;
180 try {
181 session = login();
182 for (JackrabbitDataModelMigration dataModelMigration : new TreeSet<JackrabbitDataModelMigration>(
183 dataModelMigrations)) {
184 if (dataModelMigration.migrate(session)) {
185 restartAndClearCaches = true;
186 }
187 }
188 } catch (ArgeoException e) {
189 throw e;
190 } catch (Exception e) {
191 throw new ArgeoException("Cannot migrate", e);
192 } finally {
193 JcrUtils.logoutQuietly(session);
194 }
195
196 // restart repository
197 if (restartAndClearCaches) {
198 Repository repository = getRepository();
199 if (repository instanceof RepositoryImpl) {
200 JackrabbitDataModelMigration
201 .clearRepositoryCaches(((RepositoryImpl) repository)
202 .getConfig());
203 }
204 ((JackrabbitRepository) repository).shutdown();
205 createJackrabbitRepository();
206 }
207
208 // set data model version
209 try {
210 session = login();
211 } catch (RepositoryException e) {
212 throw new ArgeoException("Cannot login to migrated repository", e);
213 }
214
215 for (JackrabbitDataModelMigration dataModelMigration : new TreeSet<JackrabbitDataModelMigration>(
216 dataModelMigrations)) {
217 try {
218 if (session.itemExists(dataModelMigration
219 .getDataModelNodePath())) {
220 Node dataModelNode = session.getNode(dataModelMigration
221 .getDataModelNodePath());
222 dataModelNode.setProperty(
223 ArgeoNames.ARGEO_DATA_MODEL_VERSION,
224 dataModelMigration.getTargetVersion());
225 session.save();
226 }
227 } catch (Exception e) {
228 log.error("Cannot set model version", e);
229 }
230 }
231 JcrUtils.logoutQuietly(session);
232
233 }
234
235 /** Shutdown the repository */
236 public void destroy() throws Exception {
237 Repository repository = getRepository();
238 if (repository != null && repository instanceof RepositoryImpl) {
239 long begin = System.currentTimeMillis();
240 ((RepositoryImpl) repository).shutdown();
241 if (inMemory)
242 if (getHomeDirectory().exists()) {
243 FileUtils.deleteDirectory(getHomeDirectory());
244 if (log.isDebugEnabled())
245 log.debug("Deleted Jackrabbit home directory "
246 + getHomeDirectory());
247 }
248 double duration = ((double) (System.currentTimeMillis() - begin)) / 1000;
249 log.info("Destroyed Jackrabbit repository in " + duration
250 + " s, home: " + getHomeDirectory());
251 }
252 repository = null;
253 }
254
255 public void dispose() {
256 throw new IllegalArgumentException(
257 "Call destroy() method instead of dispose()");
258 }
259
260 /*
261 * UTILITIES
262 */
263 /**
264 * Reads the configuration which will initialize a {@link RepositoryConfig}.
265 */
266 protected InputStream readConfiguration() {
267 try {
268 return configuration != null ? configuration.getInputStream()
269 : null;
270 } catch (IOException e) {
271 throw new ArgeoException("Cannot read Jackrabbit configuration "
272 + configuration, e);
273 }
274 }
275
276 /**
277 * Reads the variables which will initialize a {@link Properties}. Returns
278 * null by default, to be overridden.
279 *
280 * @return a new stream or null if no variables available
281 */
282 protected InputStream readVariables() {
283 try {
284 return variables != null ? variables.getInputStream() : null;
285 } catch (IOException e) {
286 throw new ArgeoException("Cannot read Jackrabbit variables "
287 + variables, e);
288 }
289 }
290
291 /**
292 * Resolves ${} placeholders in the provided string. Based on system
293 * properties if no map is provided.
294 */
295 protected String resolvePlaceholders(String string,
296 Map<String, String> variables) {
297 return SystemPropertyUtils.resolvePlaceholders(string);
298 }
299
300 /** Generates the properties to use in the configuration. */
301 protected Properties getConfigurationProperties() {
302 if (configurationProperties != null)
303 return configurationProperties;
304
305 InputStream propsIn = null;
306 Properties vars;
307 try {
308 vars = new Properties();
309 propsIn = readVariables();
310 if (propsIn != null) {
311 vars.load(propsIn);
312 }
313 // resolve system properties
314 for (Object key : vars.keySet()) {
315 // TODO: implement a smarter mechanism to resolve nested ${}
316 String newValue = resolvePlaceholders(
317 vars.getProperty(key.toString()), null);
318 vars.put(key, newValue);
319 }
320 // override with system properties
321 vars.putAll(System.getProperties());
322
323 if (log.isTraceEnabled()) {
324 log.trace("Jackrabbit config variables:");
325 for (Object key : new TreeSet<Object>(vars.keySet()))
326 log.trace(key + "=" + vars.getProperty(key.toString()));
327 }
328
329 } catch (IOException e) {
330 throw new ArgeoException("Cannot read configuration properties", e);
331 } finally {
332 IOUtils.closeQuietly(propsIn);
333 }
334 return vars;
335 }
336
337 /*
338 * FIELDS ACCESS
339 */
340
341 public void setHomeDirectory(File homeDirectory) {
342 this.homeDirectory = homeDirectory;
343 }
344
345 public void setInMemory(Boolean inMemory) {
346 this.inMemory = inMemory;
347 }
348
349 public void setRepository(Repository repository) {
350 throw new ArgeoException("Cannot be used to wrap another repository");
351 }
352
353 public void setDataModelMigrations(
354 Set<JackrabbitDataModelMigration> dataModelMigrations) {
355 this.dataModelMigrations = dataModelMigrations;
356 }
357
358 public void setVariables(Resource variables) {
359 this.variables = variables;
360 }
361
362 public void setConfiguration(Resource configuration) {
363 this.configuration = configuration;
364 }
365
366 public void setConfigurationProperties(Properties configurationProperties) {
367 this.configurationProperties = configurationProperties;
368 }
369
370 public void setConfigurationXml(InputSource configurationXml) {
371 this.configurationXml = configurationXml;
372 }
373
374 }