2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.jackrabbit
;
19 import java
.io
.ByteArrayInputStream
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.io
.InputStreamReader
;
24 import java
.io
.Reader
;
25 import java
.util
.ArrayList
;
26 import java
.util
.HashMap
;
27 import java
.util
.List
;
29 import java
.util
.Properties
;
30 import java
.util
.concurrent
.Executor
;
32 import javax
.jcr
.Credentials
;
33 import javax
.jcr
.LoginException
;
34 import javax
.jcr
.NoSuchWorkspaceException
;
35 import javax
.jcr
.Repository
;
36 import javax
.jcr
.RepositoryException
;
37 import javax
.jcr
.Session
;
38 import javax
.jcr
.Value
;
40 import org
.apache
.commons
.io
.FileUtils
;
41 import org
.apache
.commons
.io
.IOUtils
;
42 import org
.apache
.commons
.logging
.Log
;
43 import org
.apache
.commons
.logging
.LogFactory
;
44 import org
.apache
.jackrabbit
.api
.JackrabbitRepository
;
45 import org
.apache
.jackrabbit
.commons
.NamespaceHelper
;
46 import org
.apache
.jackrabbit
.commons
.cnd
.CndImporter
;
47 import org
.apache
.jackrabbit
.core
.RepositoryImpl
;
48 import org
.apache
.jackrabbit
.core
.TransientRepository
;
49 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfig
;
50 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfigurationParser
;
51 import org
.apache
.jackrabbit
.jcr2dav
.Jcr2davRepositoryFactory
;
52 import org
.argeo
.ArgeoException
;
53 import org
.argeo
.jcr
.JcrUtils
;
54 import org
.springframework
.beans
.factory
.DisposableBean
;
55 import org
.springframework
.beans
.factory
.InitializingBean
;
56 import org
.springframework
.context
.ResourceLoaderAware
;
57 import org
.springframework
.core
.io
.Resource
;
58 import org
.springframework
.core
.io
.ResourceLoader
;
59 import org
.springframework
.util
.SystemPropertyUtils
;
60 import org
.xml
.sax
.InputSource
;
63 * Wrapper around a Jackrabbit repository which allows to configure it in Spring
64 * and expose it as a {@link Repository}.
66 public class JackrabbitContainer
implements InitializingBean
, DisposableBean
,
67 Repository
, ResourceLoaderAware
{
68 private Log log
= LogFactory
.getLog(JackrabbitContainer
.class);
70 private Resource configuration
;
71 private File homeDirectory
;
72 private Resource variables
;
74 private Boolean inMemory
= false;
75 private String uri
= null;
77 private Repository repository
;
79 private ResourceLoader resourceLoader
;
81 /** Node type definitions in CND format */
82 private List
<String
> cndFiles
= new ArrayList
<String
>();
84 /** Namespaces to register: key is prefix, value namespace */
85 private Map
<String
, String
> namespaces
= new HashMap
<String
, String
>();
87 private Boolean autocreateWorkspaces
= false;
89 private Executor systemExecutor
;
90 private Credentials adminCredentials
;
92 public void afterPropertiesSet() throws Exception
{
94 if (uri
!= null && !uri
.trim().equals("")) {
95 Map
<String
, String
> params
= new HashMap
<String
, String
>();
96 params
.put(org
.apache
.jackrabbit
.commons
.JcrUtils
.REPOSITORY_URI
,
98 repository
= new Jcr2davRepositoryFactory().getRepository(params
);
99 if (repository
== null)
100 throw new ArgeoException("Remote Davex repository " + uri
102 log
.info("Initialized Jackrabbit repository " + repository
103 + " from URI " + uri
);
104 // do not perform further initialization since we assume that the
105 // remote repository has been properly configured
110 if (inMemory
&& homeDirectory
.exists()) {
111 FileUtils
.deleteDirectory(homeDirectory
);
112 log
.warn("Deleted Jackrabbit home directory " + homeDirectory
);
115 RepositoryConfig config
;
116 Properties vars
= getConfigurationProperties();
117 InputStream in
= configuration
.getInputStream();
119 vars
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
,
120 homeDirectory
.getCanonicalPath());
121 config
= RepositoryConfig
.create(new InputSource(in
), vars
);
122 } catch (Exception e
) {
123 throw new RuntimeException("Cannot read configuration", e
);
125 IOUtils
.closeQuietly(in
);
129 repository
= new TransientRepository(config
);
131 repository
= RepositoryImpl
.create(config
);
133 if (cndFiles
!= null && cndFiles
.size() > 0)
134 importNodeTypeDefinitions(repository
);
136 log
.info("Initialized Jackrabbit repository " + repository
+ " in "
137 + homeDirectory
+ " with config " + configuration
);
140 protected Properties
getConfigurationProperties() {
141 InputStream propsIn
= null;
144 vars
= new Properties();
145 if (variables
!= null) {
146 propsIn
= variables
.getInputStream();
149 // resolve system properties
150 for (Object key
: vars
.keySet()) {
151 // TODO: implement a smarter mechanism to resolve nested ${}
152 String newValue
= SystemPropertyUtils
.resolvePlaceholders(vars
153 .getProperty(key
.toString()));
154 vars
.put(key
, newValue
);
156 // override with system properties
157 vars
.putAll(System
.getProperties());
158 } catch (IOException e
) {
159 throw new ArgeoException("Cannot read configuration properties", e
);
161 IOUtils
.closeQuietly(propsIn
);
167 * Import declared node type definitions, trying to update them if they have
168 * changed. In case of failures an error will be logged but no exception
171 protected void importNodeTypeDefinitions(final Repository repository
) {
172 final Credentials credentialsToUse
;
173 if (systemExecutor
== null) {
174 if (adminCredentials
== null)
175 throw new ArgeoException(
176 "No system executor or admin credentials found");
177 credentialsToUse
= adminCredentials
;
179 credentialsToUse
= null;
182 Runnable action
= new Runnable() {
184 Reader reader
= null;
185 Session session
= null;
187 session
= repository
.login(credentialsToUse
);
188 processNewSession(session
);
189 // Load cnds as resources
190 for (String resUrl
: cndFiles
) {
191 Resource res
= resourceLoader
.getResource(resUrl
);
192 byte[] arr
= IOUtils
.toByteArray(res
.getInputStream());
193 reader
= new InputStreamReader(
194 new ByteArrayInputStream(arr
));
195 CndImporter
.registerNodeTypes(reader
, session
, true);
198 } catch (Exception e
) {
200 "Cannot import node type definitions " + cndFiles
,
202 JcrUtils
.discardQuietly(session
);
204 IOUtils
.closeQuietly(reader
);
205 JcrUtils
.logoutQuietly(session
);
210 if (systemExecutor
!= null)
211 systemExecutor
.execute(action
);
216 public void destroy() throws Exception
{
217 if (repository
!= null) {
218 if (repository
instanceof JackrabbitRepository
)
219 ((JackrabbitRepository
) repository
).shutdown();
220 else if (repository
instanceof RepositoryImpl
)
221 ((RepositoryImpl
) repository
).shutdown();
222 else if (repository
instanceof TransientRepository
)
223 ((TransientRepository
) repository
).shutdown();
227 if (homeDirectory
.exists()) {
228 FileUtils
.deleteDirectory(homeDirectory
);
229 if (log
.isDebugEnabled())
230 log
.debug("Deleted Jackrabbit home directory "
234 if (uri
!= null && !uri
.trim().equals(""))
235 log
.info("Destroyed Jackrabbit repository with uri " + uri
);
237 log
.info("Destroyed Jackrabbit repository " + repository
+ " in "
238 + homeDirectory
+ " with config " + configuration
);
241 // JCR REPOSITORY (delegated)
242 public String
getDescriptor(String key
) {
243 return repository
.getDescriptor(key
);
246 public String
[] getDescriptorKeys() {
247 return repository
.getDescriptorKeys();
250 public Session
login() throws LoginException
, RepositoryException
{
251 Session session
= repository
.login();
252 processNewSession(session
);
256 public Session
login(Credentials credentials
, String workspaceName
)
257 throws LoginException
, NoSuchWorkspaceException
,
258 RepositoryException
{
261 session
= repository
.login(credentials
, workspaceName
);
262 } catch (NoSuchWorkspaceException e
) {
263 if (autocreateWorkspaces
)
264 session
= createWorkspaceAndLogsIn(credentials
, workspaceName
);
268 processNewSession(session
);
272 public Session
login(Credentials credentials
) throws LoginException
,
273 RepositoryException
{
274 Session session
= repository
.login(credentials
);
275 processNewSession(session
);
279 public Session
login(String workspaceName
) throws LoginException
,
280 NoSuchWorkspaceException
, RepositoryException
{
283 session
= repository
.login(workspaceName
);
284 } catch (NoSuchWorkspaceException e
) {
285 if (autocreateWorkspaces
)
286 session
= createWorkspaceAndLogsIn(null, workspaceName
);
290 processNewSession(session
);
294 protected synchronized void processNewSession(Session session
) {
296 NamespaceHelper namespaceHelper
= new NamespaceHelper(session
);
297 namespaceHelper
.registerNamespaces(namespaces
);
298 } catch (Exception e
) {
299 throw new ArgeoException("Cannot process new session", e
);
304 * Logs in to the default workspace, creates the required workspace, logs
305 * out, logs in to the required workspace.
307 protected Session
createWorkspaceAndLogsIn(Credentials credentials
,
308 String workspaceName
) throws RepositoryException
{
309 if (workspaceName
== null)
310 throw new ArgeoException("No workspace specified.");
311 Session session
= repository
.login(credentials
);
312 session
.getWorkspace().createWorkspace(workspaceName
);
314 return repository
.login(credentials
, workspaceName
);
317 public void setResourceLoader(ResourceLoader resourceLoader
) {
318 this.resourceLoader
= resourceLoader
;
321 public boolean isStandardDescriptor(String key
) {
322 return repository
.isStandardDescriptor(key
);
325 public boolean isSingleValueDescriptor(String key
) {
326 return repository
.isSingleValueDescriptor(key
);
329 public Value
getDescriptorValue(String key
) {
330 return repository
.getDescriptorValue(key
);
333 public Value
[] getDescriptorValues(String key
) {
334 return repository
.getDescriptorValues(key
);
338 public void setHomeDirectory(File homeDirectory
) {
339 this.homeDirectory
= homeDirectory
;
342 public void setConfiguration(Resource configuration
) {
343 this.configuration
= configuration
;
346 public void setInMemory(Boolean inMemory
) {
347 this.inMemory
= inMemory
;
350 public void setNamespaces(Map
<String
, String
> namespaces
) {
351 this.namespaces
= namespaces
;
354 public void setCndFiles(List
<String
> cndFiles
) {
355 this.cndFiles
= cndFiles
;
358 public void setVariables(Resource variables
) {
359 this.variables
= variables
;
362 public void setUri(String uri
) {
366 public void setSystemExecutor(Executor systemExecutor
) {
367 this.systemExecutor
= systemExecutor
;
370 public void setAdminCredentials(Credentials adminCredentials
) {
371 this.adminCredentials
= adminCredentials
;