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
;
59 private Resource variables
;
60 private RepositoryConfig repositoryConfig
;
61 private File homeDirectory
;
62 private Boolean inMemory
= false;
64 /** Migrations to execute (if not already done) */
65 private Set
<JackrabbitDataModelMigration
> dataModelMigrations
= new HashSet
<JackrabbitDataModelMigration
>();
68 * Empty constructor, {@link #init()} should be called after properties have
71 public JackrabbitContainer() {
75 long begin
= System
.currentTimeMillis();
77 if (getRepository() != null)
78 throw new ArgeoException(
79 "Cannot be used to wrap another repository");
80 Repository repository
= createJackrabbitRepository();
81 super.setRepository(repository
);
86 // apply new CND files after migration
89 double duration
= ((double) (System
.currentTimeMillis() - begin
)) / 1000;
90 if (log
.isDebugEnabled())
91 log
.debug("Initialized JCR repository wrapper in " + duration
95 /** Actually creates the new repository. */
96 protected Repository
createJackrabbitRepository() {
97 long begin
= System
.currentTimeMillis();
98 InputStream configurationIn
= null;
99 Repository repository
;
102 if (inMemory
&& getHomeDirectory().exists()) {
103 FileUtils
.deleteDirectory(getHomeDirectory());
104 log
.warn("Deleted Jackrabbit home directory "
105 + getHomeDirectory());
108 // process configuration file
109 Properties vars
= getConfigurationProperties();
110 configurationIn
= readConfiguration();
111 vars
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
,
112 getHomeDirectory().getCanonicalPath());
113 repositoryConfig
= RepositoryConfig
.create(new InputSource(
114 configurationIn
), vars
);
117 // Actual repository creation
119 repository
= RepositoryImpl
.create(repositoryConfig
);
121 double duration
= ((double) (System
.currentTimeMillis() - begin
)) / 1000;
122 if (log
.isTraceEnabled())
123 log
.trace("Created Jackrabbit repository in " + duration
124 + " s, home: " + getHomeDirectory());
127 } catch (Exception e
) {
128 throw new ArgeoException("Cannot create Jackrabbit repository "
129 + getHomeDirectory(), e
);
131 IOUtils
.closeQuietly(configurationIn
);
136 protected File
getHomeDirectory() {
138 if (homeDirectory
== null) {
140 homeDirectory
= new File(
141 System
.getProperty("java.io.tmpdir")
143 + System
.getProperty("user.name")
144 + File
.separator
+ "jackrabbit-"
145 + UUID
.randomUUID());
146 homeDirectory
.mkdirs();
147 // will it work if directory is not empty??
148 homeDirectory
.deleteOnExit();
152 return homeDirectory
.getCanonicalFile();
153 } catch (IOException e
) {
154 throw new ArgeoException("Cannot get canonical file for "
159 /** Executes migrations, if needed. */
160 protected void migrate() {
161 // No migration to perform
162 if (dataModelMigrations
.size() == 0)
165 Boolean restartAndClearCaches
= false;
168 Session session
= null;
171 for (JackrabbitDataModelMigration dataModelMigration
: new TreeSet
<JackrabbitDataModelMigration
>(
172 dataModelMigrations
)) {
173 if (dataModelMigration
.migrate(session
)) {
174 restartAndClearCaches
= true;
177 } catch (ArgeoException e
) {
179 } catch (Exception e
) {
180 throw new ArgeoException("Cannot migrate", e
);
182 JcrUtils
.logoutQuietly(session
);
185 // restart repository
186 if (restartAndClearCaches
) {
187 Repository repository
= getRepository();
188 if (repository
instanceof RepositoryImpl
) {
189 JackrabbitDataModelMigration
190 .clearRepositoryCaches(((RepositoryImpl
) repository
)
193 ((JackrabbitRepository
) repository
).shutdown();
194 createJackrabbitRepository();
197 // set data model version
200 } catch (RepositoryException e
) {
201 throw new ArgeoException("Cannot login to migrated repository", e
);
204 for (JackrabbitDataModelMigration dataModelMigration
: new TreeSet
<JackrabbitDataModelMigration
>(
205 dataModelMigrations
)) {
207 if (session
.itemExists(dataModelMigration
208 .getDataModelNodePath())) {
209 Node dataModelNode
= session
.getNode(dataModelMigration
210 .getDataModelNodePath());
211 dataModelNode
.setProperty(
212 ArgeoNames
.ARGEO_DATA_MODEL_VERSION
,
213 dataModelMigration
.getTargetVersion());
216 } catch (Exception e
) {
217 log
.error("Cannot set model version", e
);
220 JcrUtils
.logoutQuietly(session
);
224 /** Shutdown the repository */
225 public void destroy() throws Exception
{
226 Repository repository
= getRepository();
227 if (repository
!= null && repository
instanceof RepositoryImpl
) {
228 long begin
= System
.currentTimeMillis();
229 ((RepositoryImpl
) repository
).shutdown();
231 if (getHomeDirectory().exists()) {
232 FileUtils
.deleteDirectory(getHomeDirectory());
233 if (log
.isDebugEnabled())
234 log
.debug("Deleted Jackrabbit home directory "
235 + getHomeDirectory());
237 double duration
= ((double) (System
.currentTimeMillis() - begin
)) / 1000;
238 log
.info("Destroyed Jackrabbit repository in " + duration
239 + " s, home: " + getHomeDirectory());
244 public void dispose() {
245 throw new IllegalArgumentException(
246 "Call destroy() method instead of dispose()");
253 * Reads the configuration which will initialize a {@link RepositoryConfig}.
255 protected InputStream
readConfiguration() {
257 return configuration
!= null ? configuration
.getInputStream()
259 } catch (IOException e
) {
260 throw new ArgeoException("Cannot read Jackrabbit configuration "
266 * Reads the variables which will initialize a {@link Properties}. Returns
267 * null by default, to be overridden.
269 * @return a new stream or null if no variables available
271 protected InputStream
readVariables() {
273 return variables
!= null ? variables
.getInputStream() : null;
274 } catch (IOException e
) {
275 throw new ArgeoException("Cannot read Jackrabbit variables "
281 * Resolves ${} placeholders in the provided string. Based on system
282 * properties if no map is provided.
284 protected String
resolvePlaceholders(String string
,
285 Map
<String
, String
> variables
) {
286 return SystemPropertyUtils
.resolvePlaceholders(string
);
289 /** Generates the properties to use in the configuration. */
290 protected Properties
getConfigurationProperties() {
291 InputStream propsIn
= null;
294 vars
= new Properties();
295 propsIn
= readVariables();
296 if (propsIn
!= null) {
299 // resolve system properties
300 for (Object key
: vars
.keySet()) {
301 // TODO: implement a smarter mechanism to resolve nested ${}
302 String newValue
= resolvePlaceholders(
303 vars
.getProperty(key
.toString()), null);
304 vars
.put(key
, newValue
);
306 // override with system properties
307 vars
.putAll(System
.getProperties());
309 if (log
.isTraceEnabled()) {
310 log
.trace("Jackrabbit config variables:");
311 for (Object key
: new TreeSet
<Object
>(vars
.keySet()))
312 log
.trace(key
+ "=" + vars
.getProperty(key
.toString()));
315 } catch (IOException e
) {
316 throw new ArgeoException("Cannot read configuration properties", e
);
318 IOUtils
.closeQuietly(propsIn
);
327 public void setHomeDirectory(File homeDirectory
) {
328 this.homeDirectory
= homeDirectory
;
331 public void setInMemory(Boolean inMemory
) {
332 this.inMemory
= inMemory
;
335 public void setRepository(Repository repository
) {
336 throw new ArgeoException("Cannot be used to wrap another repository");
339 public void setDataModelMigrations(
340 Set
<JackrabbitDataModelMigration
> dataModelMigrations
) {
341 this.dataModelMigrations
= dataModelMigrations
;
344 public void setVariables(Resource variables
) {
345 this.variables
= variables
;
348 public void setConfiguration(Resource configuration
) {
349 this.configuration
= configuration
;