]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jackrabbit/JackrabbitContainer.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jackrabbit / 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)) /
96 // 1000;
97 // if (log.isDebugEnabled())
98 // log.debug("Initialized JCR repository wrapper in " + duration
99 // + " s");
100 }
101
102 /** Actually creates the new repository. */
103 protected Repository createJackrabbitRepository() {
104 long begin = System.currentTimeMillis();
105 InputStream configurationIn = null;
106 Repository repository;
107 try {
108 // temporary
109 if (inMemory && getHomeDirectory().exists()) {
110 FileUtils.deleteDirectory(getHomeDirectory());
111 log.warn("Deleted Jackrabbit home directory "
112 + getHomeDirectory());
113 }
114
115 // process configuration file
116 Properties vars = getConfigurationProperties();
117 vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
118 getHomeDirectory().getCanonicalPath());
119 InputSource is;
120 if (configurationXml != null)
121 is = configurationXml;
122 else {
123 configurationIn = readConfiguration();
124 is = new InputSource(configurationIn);
125 }
126 repositoryConfig = RepositoryConfig.create(is, vars);
127
128 //
129 // Actual repository creation
130 //
131 repository = RepositoryImpl.create(repositoryConfig);
132
133 double duration = ((double) (System.currentTimeMillis() - begin)) / 1000;
134 if (log.isTraceEnabled())
135 log.trace("Created Jackrabbit repository in " + duration
136 + " s, home: " + getHomeDirectory());
137
138 return repository;
139 } catch (Exception e) {
140 throw new ArgeoException("Cannot create Jackrabbit repository "
141 + getHomeDirectory(), e);
142 } finally {
143 IOUtils.closeQuietly(configurationIn);
144 }
145 }
146
147 /** Lazy init. */
148 protected File getHomeDirectory() {
149 try {
150 if (homeDirectory == null) {
151 if (inMemory) {
152 homeDirectory = new File(
153 System.getProperty("java.io.tmpdir")
154 + File.separator
155 + System.getProperty("user.name")
156 + File.separator + "jackrabbit-"
157 + UUID.randomUUID());
158 homeDirectory.mkdirs();
159 // will it work if directory is not empty??
160 homeDirectory.deleteOnExit();
161 }
162 }
163
164 return homeDirectory.getCanonicalFile();
165 } catch (IOException e) {
166 throw new ArgeoException("Cannot get canonical file for "
167 + homeDirectory, e);
168 }
169 }
170
171 /** Executes migrations, if needed. */
172 protected void migrate() {
173 // No migration to perform
174 if (dataModelMigrations.size() == 0)
175 return;
176
177 Boolean restartAndClearCaches = false;
178
179 // migrate data
180 Session session = null;
181 try {
182 session = login();
183 for (JackrabbitDataModelMigration dataModelMigration : new TreeSet<JackrabbitDataModelMigration>(
184 dataModelMigrations)) {
185 if (dataModelMigration.migrate(session)) {
186 restartAndClearCaches = true;
187 }
188 }
189 } catch (ArgeoException e) {
190 throw e;
191 } catch (Exception e) {
192 throw new ArgeoException("Cannot migrate", e);
193 } finally {
194 JcrUtils.logoutQuietly(session);
195 }
196
197 // restart repository
198 if (restartAndClearCaches) {
199 Repository repository = getRepository();
200 if (repository instanceof RepositoryImpl) {
201 JackrabbitDataModelMigration
202 .clearRepositoryCaches(((RepositoryImpl) repository)
203 .getConfig());
204 }
205 ((JackrabbitRepository) repository).shutdown();
206 createJackrabbitRepository();
207 }
208
209 // set data model version
210 try {
211 session = login();
212 } catch (RepositoryException e) {
213 throw new ArgeoException("Cannot login to migrated repository", e);
214 }
215
216 for (JackrabbitDataModelMigration dataModelMigration : new TreeSet<JackrabbitDataModelMigration>(
217 dataModelMigrations)) {
218 try {
219 if (session.itemExists(dataModelMigration
220 .getDataModelNodePath())) {
221 Node dataModelNode = session.getNode(dataModelMigration
222 .getDataModelNodePath());
223 dataModelNode.setProperty(
224 ArgeoNames.ARGEO_DATA_MODEL_VERSION,
225 dataModelMigration.getTargetVersion());
226 session.save();
227 }
228 } catch (Exception e) {
229 log.error("Cannot set model version", e);
230 }
231 }
232 JcrUtils.logoutQuietly(session);
233
234 }
235
236 /** Shutdown the repository */
237 public void destroy() throws Exception {
238 Repository repository = getRepository();
239 if (repository != null && repository instanceof RepositoryImpl) {
240 long begin = System.currentTimeMillis();
241 ((RepositoryImpl) repository).shutdown();
242 if (inMemory)
243 if (getHomeDirectory().exists()) {
244 FileUtils.deleteDirectory(getHomeDirectory());
245 if (log.isDebugEnabled())
246 log.debug("Deleted Jackrabbit home directory "
247 + getHomeDirectory());
248 }
249 double duration = ((double) (System.currentTimeMillis() - begin)) / 1000;
250 if (log.isTraceEnabled())
251 log.trace("Destroyed Jackrabbit repository in " + duration
252 + " s, home: " + getHomeDirectory());
253 }
254 repository = null;
255 }
256
257 public void dispose() {
258 throw new IllegalArgumentException(
259 "Call destroy() method instead of dispose()");
260 }
261
262 /*
263 * UTILITIES
264 */
265 /**
266 * Reads the configuration which will initialize a {@link RepositoryConfig}.
267 */
268 protected InputStream readConfiguration() {
269 try {
270 return configuration != null ? configuration.getInputStream()
271 : null;
272 } catch (IOException e) {
273 throw new ArgeoException("Cannot read Jackrabbit configuration "
274 + configuration, e);
275 }
276 }
277
278 /**
279 * Reads the variables which will initialize a {@link Properties}. Returns
280 * null by default, to be overridden.
281 *
282 * @return a new stream or null if no variables available
283 */
284 protected InputStream readVariables() {
285 try {
286 return variables != null ? variables.getInputStream() : null;
287 } catch (IOException e) {
288 throw new ArgeoException("Cannot read Jackrabbit variables "
289 + variables, e);
290 }
291 }
292
293 /**
294 * Resolves ${} placeholders in the provided string. Based on system
295 * properties if no map is provided.
296 */
297 protected String resolvePlaceholders(String string,
298 Map<String, String> variables) {
299 return SystemPropertyUtils.resolvePlaceholders(string);
300 }
301
302 /** Generates the properties to use in the configuration. */
303 protected Properties getConfigurationProperties() {
304 if (configurationProperties != null)
305 return configurationProperties;
306
307 InputStream propsIn = null;
308 Properties vars;
309 try {
310 vars = new Properties();
311 propsIn = readVariables();
312 if (propsIn != null) {
313 vars.load(propsIn);
314 }
315 // resolve system properties
316 for (Object key : vars.keySet()) {
317 // TODO: implement a smarter mechanism to resolve nested ${}
318 String newValue = resolvePlaceholders(
319 vars.getProperty(key.toString()), null);
320 vars.put(key, newValue);
321 }
322 // override with system properties
323 vars.putAll(System.getProperties());
324
325 if (log.isTraceEnabled()) {
326 log.trace("Jackrabbit config variables:");
327 for (Object key : new TreeSet<Object>(vars.keySet()))
328 log.trace(key + "=" + vars.getProperty(key.toString()));
329 }
330
331 } catch (IOException e) {
332 throw new ArgeoException("Cannot read configuration properties", e);
333 } finally {
334 IOUtils.closeQuietly(propsIn);
335 }
336 return vars;
337 }
338
339 /*
340 * FIELDS ACCESS
341 */
342
343 public void setHomeDirectory(File homeDirectory) {
344 this.homeDirectory = homeDirectory;
345 }
346
347 public void setInMemory(Boolean inMemory) {
348 this.inMemory = inMemory;
349 }
350
351 public void setRepository(Repository repository) {
352 throw new ArgeoException("Cannot be used to wrap another repository");
353 }
354
355 public void setDataModelMigrations(
356 Set<JackrabbitDataModelMigration> dataModelMigrations) {
357 this.dataModelMigrations = dataModelMigrations;
358 }
359
360 public void setVariables(Resource variables) {
361 this.variables = variables;
362 }
363
364 public void setConfiguration(Resource configuration) {
365 this.configuration = configuration;
366 }
367
368 public void setConfigurationProperties(Properties configurationProperties) {
369 this.configurationProperties = configurationProperties;
370 }
371
372 public void setConfigurationXml(InputSource configurationXml) {
373 this.configurationXml = configurationXml;
374 }
375
376 }