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
)) / 1000;
96 if (log
.isDebugEnabled())
97 log
.debug("Initialized JCR repository wrapper in " + duration
101 /** Actually creates the new repository. */
102 protected Repository
createJackrabbitRepository() {
103 long begin
= System
.currentTimeMillis();
104 InputStream configurationIn
= null;
105 Repository repository
;
108 if (inMemory
&& getHomeDirectory().exists()) {
109 FileUtils
.deleteDirectory(getHomeDirectory());
110 log
.warn("Deleted Jackrabbit home directory "
111 + getHomeDirectory());
114 // process configuration file
115 Properties vars
= getConfigurationProperties();
116 vars
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
,
117 getHomeDirectory().getCanonicalPath());
119 if (configurationXml
!= null)
120 is
= configurationXml
;
122 configurationIn
= readConfiguration();
123 is
= new InputSource(configurationIn
);
125 repositoryConfig
= RepositoryConfig
.create(is
, vars
);
128 // Actual repository creation
130 repository
= RepositoryImpl
.create(repositoryConfig
);
132 double duration
= ((double) (System
.currentTimeMillis() - begin
)) / 1000;
133 if (log
.isTraceEnabled())
134 log
.trace("Created Jackrabbit repository in " + duration
135 + " s, home: " + getHomeDirectory());
138 } catch (Exception e
) {
139 throw new ArgeoException("Cannot create Jackrabbit repository "
140 + getHomeDirectory(), e
);
142 IOUtils
.closeQuietly(configurationIn
);
147 protected File
getHomeDirectory() {
149 if (homeDirectory
== null) {
151 homeDirectory
= new File(
152 System
.getProperty("java.io.tmpdir")
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();
163 return homeDirectory
.getCanonicalFile();
164 } catch (IOException e
) {
165 throw new ArgeoException("Cannot get canonical file for "
170 /** Executes migrations, if needed. */
171 protected void migrate() {
172 // No migration to perform
173 if (dataModelMigrations
.size() == 0)
176 Boolean restartAndClearCaches
= false;
179 Session session
= null;
182 for (JackrabbitDataModelMigration dataModelMigration
: new TreeSet
<JackrabbitDataModelMigration
>(
183 dataModelMigrations
)) {
184 if (dataModelMigration
.migrate(session
)) {
185 restartAndClearCaches
= true;
188 } catch (ArgeoException e
) {
190 } catch (Exception e
) {
191 throw new ArgeoException("Cannot migrate", e
);
193 JcrUtils
.logoutQuietly(session
);
196 // restart repository
197 if (restartAndClearCaches
) {
198 Repository repository
= getRepository();
199 if (repository
instanceof RepositoryImpl
) {
200 JackrabbitDataModelMigration
201 .clearRepositoryCaches(((RepositoryImpl
) repository
)
204 ((JackrabbitRepository
) repository
).shutdown();
205 createJackrabbitRepository();
208 // set data model version
211 } catch (RepositoryException e
) {
212 throw new ArgeoException("Cannot login to migrated repository", e
);
215 for (JackrabbitDataModelMigration dataModelMigration
: new TreeSet
<JackrabbitDataModelMigration
>(
216 dataModelMigrations
)) {
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());
227 } catch (Exception e
) {
228 log
.error("Cannot set model version", e
);
231 JcrUtils
.logoutQuietly(session
);
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();
242 if (getHomeDirectory().exists()) {
243 FileUtils
.deleteDirectory(getHomeDirectory());
244 if (log
.isDebugEnabled())
245 log
.debug("Deleted Jackrabbit home directory "
246 + getHomeDirectory());
248 double duration
= ((double) (System
.currentTimeMillis() - begin
)) / 1000;
249 log
.info("Destroyed Jackrabbit repository in " + duration
250 + " s, home: " + getHomeDirectory());
255 public void dispose() {
256 throw new IllegalArgumentException(
257 "Call destroy() method instead of dispose()");
264 * Reads the configuration which will initialize a {@link RepositoryConfig}.
266 protected InputStream
readConfiguration() {
268 return configuration
!= null ? configuration
.getInputStream()
270 } catch (IOException e
) {
271 throw new ArgeoException("Cannot read Jackrabbit configuration "
277 * Reads the variables which will initialize a {@link Properties}. Returns
278 * null by default, to be overridden.
280 * @return a new stream or null if no variables available
282 protected InputStream
readVariables() {
284 return variables
!= null ? variables
.getInputStream() : null;
285 } catch (IOException e
) {
286 throw new ArgeoException("Cannot read Jackrabbit variables "
292 * Resolves ${} placeholders in the provided string. Based on system
293 * properties if no map is provided.
295 protected String
resolvePlaceholders(String string
,
296 Map
<String
, String
> variables
) {
297 return SystemPropertyUtils
.resolvePlaceholders(string
);
300 /** Generates the properties to use in the configuration. */
301 protected Properties
getConfigurationProperties() {
302 if (configurationProperties
!= null)
303 return configurationProperties
;
305 InputStream propsIn
= null;
308 vars
= new Properties();
309 propsIn
= readVariables();
310 if (propsIn
!= null) {
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
);
320 // override with system properties
321 vars
.putAll(System
.getProperties());
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()));
329 } catch (IOException e
) {
330 throw new ArgeoException("Cannot read configuration properties", e
);
332 IOUtils
.closeQuietly(propsIn
);
341 public void setHomeDirectory(File homeDirectory
) {
342 this.homeDirectory
= homeDirectory
;
345 public void setInMemory(Boolean inMemory
) {
346 this.inMemory
= inMemory
;
349 public void setRepository(Repository repository
) {
350 throw new ArgeoException("Cannot be used to wrap another repository");
353 public void setDataModelMigrations(
354 Set
<JackrabbitDataModelMigration
> dataModelMigrations
) {
355 this.dataModelMigrations
= dataModelMigrations
;
358 public void setVariables(Resource variables
) {
359 this.variables
= variables
;
362 public void setConfiguration(Resource configuration
) {
363 this.configuration
= configuration
;
366 public void setConfigurationProperties(Properties configurationProperties
) {
367 this.configurationProperties
= configurationProperties
;
370 public void setConfigurationXml(InputSource configurationXml
) {
371 this.configurationXml
= configurationXml
;