2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.jackrabbit
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
21 import java
.util
.HashSet
;
23 import java
.util
.Properties
;
25 import java
.util
.TreeSet
;
26 import java
.util
.UUID
;
28 import javax
.jcr
.Node
;
29 import javax
.jcr
.Repository
;
30 import javax
.jcr
.RepositoryException
;
31 import javax
.jcr
.Session
;
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
;
50 * Wrapper around a Jackrabbit repository which allows to configure it in Spring
51 * and expose it as a {@link Repository}.
53 public class JackrabbitContainer
extends JackrabbitWrapper
implements
54 MaintainedRepository
{
55 private final static Log log
= LogFactory
.getLog(JackrabbitContainer
.class);
58 private Resource configuration
;
60 private Resource variables
;
62 private RepositoryConfig repositoryConfig
;
63 private File homeDirectory
;
64 private Boolean inMemory
= false;
66 /** Migrations to execute (if not already done) */
67 private Set
<JackrabbitDataModelMigration
> dataModelMigrations
= new HashSet
<JackrabbitDataModelMigration
>();
69 /** Straight (non spring) values */
70 private Properties configurationProperties
;
71 private InputSource configurationXml
;
74 * Empty constructor, {@link #init()} should be called after properties have
77 public JackrabbitContainer() {
81 // long begin = System.currentTimeMillis();
83 if (getRepository() != null)
84 throw new ArgeoException(
85 "Cannot be used to wrap another repository");
86 Repository repository
= createJackrabbitRepository();
87 super.setRepository(repository
);
92 // apply new CND files after migration
95 // double duration = ((double) (System.currentTimeMillis() - begin)) /
97 // if (log.isDebugEnabled())
98 // log.debug("Initialized JCR repository wrapper in " + duration
102 /** Actually creates the new repository. */
103 protected Repository
createJackrabbitRepository() {
104 long begin
= System
.currentTimeMillis();
105 InputStream configurationIn
= null;
106 Repository repository
;
109 if (inMemory
&& getHomeDirectory().exists()) {
110 FileUtils
.deleteDirectory(getHomeDirectory());
111 log
.warn("Deleted Jackrabbit home directory "
112 + getHomeDirectory());
115 // process configuration file
116 Properties vars
= getConfigurationProperties();
117 vars
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
,
118 getHomeDirectory().getCanonicalPath());
120 if (configurationXml
!= null)
121 is
= configurationXml
;
123 configurationIn
= readConfiguration();
124 is
= new InputSource(configurationIn
);
126 repositoryConfig
= RepositoryConfig
.create(is
, vars
);
129 // Actual repository creation
131 repository
= RepositoryImpl
.create(repositoryConfig
);
133 double duration
= ((double) (System
.currentTimeMillis() - begin
)) / 1000;
134 if (log
.isTraceEnabled())
135 log
.trace("Created Jackrabbit repository in " + duration
136 + " s, home: " + getHomeDirectory());
139 } catch (Exception e
) {
140 throw new ArgeoException("Cannot create Jackrabbit repository "
141 + getHomeDirectory(), e
);
143 IOUtils
.closeQuietly(configurationIn
);
148 protected File
getHomeDirectory() {
150 if (homeDirectory
== null) {
152 homeDirectory
= new File(
153 System
.getProperty("java.io.tmpdir")
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();
164 return homeDirectory
.getCanonicalFile();
165 } catch (IOException e
) {
166 throw new ArgeoException("Cannot get canonical file for "
171 /** Executes migrations, if needed. */
172 protected void migrate() {
173 // No migration to perform
174 if (dataModelMigrations
.size() == 0)
177 Boolean restartAndClearCaches
= false;
180 Session session
= null;
183 for (JackrabbitDataModelMigration dataModelMigration
: new TreeSet
<JackrabbitDataModelMigration
>(
184 dataModelMigrations
)) {
185 if (dataModelMigration
.migrate(session
)) {
186 restartAndClearCaches
= true;
189 } catch (ArgeoException e
) {
191 } catch (Exception e
) {
192 throw new ArgeoException("Cannot migrate", e
);
194 JcrUtils
.logoutQuietly(session
);
197 // restart repository
198 if (restartAndClearCaches
) {
199 Repository repository
= getRepository();
200 if (repository
instanceof RepositoryImpl
) {
201 JackrabbitDataModelMigration
202 .clearRepositoryCaches(((RepositoryImpl
) repository
)
205 ((JackrabbitRepository
) repository
).shutdown();
206 createJackrabbitRepository();
209 // set data model version
212 } catch (RepositoryException e
) {
213 throw new ArgeoException("Cannot login to migrated repository", e
);
216 for (JackrabbitDataModelMigration dataModelMigration
: new TreeSet
<JackrabbitDataModelMigration
>(
217 dataModelMigrations
)) {
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());
228 } catch (Exception e
) {
229 log
.error("Cannot set model version", e
);
232 JcrUtils
.logoutQuietly(session
);
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();
243 if (getHomeDirectory().exists()) {
244 FileUtils
.deleteDirectory(getHomeDirectory());
245 if (log
.isDebugEnabled())
246 log
.debug("Deleted Jackrabbit home directory "
247 + getHomeDirectory());
249 double duration
= ((double) (System
.currentTimeMillis() - begin
)) / 1000;
250 if (log
.isTraceEnabled())
251 log
.trace("Destroyed Jackrabbit repository in " + duration
252 + " s, home: " + getHomeDirectory());
257 public void dispose() {
258 throw new IllegalArgumentException(
259 "Call destroy() method instead of dispose()");
266 * Reads the configuration which will initialize a {@link RepositoryConfig}.
268 protected InputStream
readConfiguration() {
270 return configuration
!= null ? configuration
.getInputStream()
272 } catch (IOException e
) {
273 throw new ArgeoException("Cannot read Jackrabbit configuration "
279 * Reads the variables which will initialize a {@link Properties}. Returns
280 * null by default, to be overridden.
282 * @return a new stream or null if no variables available
284 protected InputStream
readVariables() {
286 return variables
!= null ? variables
.getInputStream() : null;
287 } catch (IOException e
) {
288 throw new ArgeoException("Cannot read Jackrabbit variables "
294 * Resolves ${} placeholders in the provided string. Based on system
295 * properties if no map is provided.
297 protected String
resolvePlaceholders(String string
,
298 Map
<String
, String
> variables
) {
299 return SystemPropertyUtils
.resolvePlaceholders(string
);
302 /** Generates the properties to use in the configuration. */
303 protected Properties
getConfigurationProperties() {
304 if (configurationProperties
!= null)
305 return configurationProperties
;
307 InputStream propsIn
= null;
310 vars
= new Properties();
311 propsIn
= readVariables();
312 if (propsIn
!= null) {
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
);
322 // override with system properties
323 vars
.putAll(System
.getProperties());
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()));
331 } catch (IOException e
) {
332 throw new ArgeoException("Cannot read configuration properties", e
);
334 IOUtils
.closeQuietly(propsIn
);
343 public void setHomeDirectory(File homeDirectory
) {
344 this.homeDirectory
= homeDirectory
;
347 public void setInMemory(Boolean inMemory
) {
348 this.inMemory
= inMemory
;
351 public void setRepository(Repository repository
) {
352 throw new ArgeoException("Cannot be used to wrap another repository");
355 public void setDataModelMigrations(
356 Set
<JackrabbitDataModelMigration
> dataModelMigrations
) {
357 this.dataModelMigrations
= dataModelMigrations
;
360 public void setVariables(Resource variables
) {
361 this.variables
= variables
;
364 public void setConfiguration(Resource configuration
) {
365 this.configuration
= configuration
;
368 public void setConfigurationProperties(Properties configurationProperties
) {
369 this.configurationProperties
= configurationProperties
;
372 public void setConfigurationXml(InputSource configurationXml
) {
373 this.configurationXml
= configurationXml
;