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
.InputStream
;
22 import java
.io
.InputStreamReader
;
23 import java
.io
.Reader
;
24 import java
.util
.ArrayList
;
25 import java
.util
.HashMap
;
26 import java
.util
.List
;
28 import java
.util
.Properties
;
29 import java
.util
.concurrent
.Executor
;
31 import javax
.jcr
.Credentials
;
32 import javax
.jcr
.LoginException
;
33 import javax
.jcr
.NoSuchWorkspaceException
;
34 import javax
.jcr
.Repository
;
35 import javax
.jcr
.RepositoryException
;
36 import javax
.jcr
.Session
;
37 import javax
.jcr
.Value
;
39 import org
.apache
.commons
.io
.FileUtils
;
40 import org
.apache
.commons
.io
.IOUtils
;
41 import org
.apache
.commons
.logging
.Log
;
42 import org
.apache
.commons
.logging
.LogFactory
;
43 import org
.apache
.jackrabbit
.api
.JackrabbitRepository
;
44 import org
.apache
.jackrabbit
.commons
.NamespaceHelper
;
45 import org
.apache
.jackrabbit
.commons
.cnd
.CndImporter
;
46 import org
.apache
.jackrabbit
.core
.RepositoryImpl
;
47 import org
.apache
.jackrabbit
.core
.TransientRepository
;
48 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfig
;
49 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfigurationParser
;
50 import org
.apache
.jackrabbit
.jcr2dav
.Jcr2davRepositoryFactory
;
51 import org
.argeo
.ArgeoException
;
52 import org
.argeo
.jcr
.JcrUtils
;
53 import org
.springframework
.beans
.factory
.DisposableBean
;
54 import org
.springframework
.beans
.factory
.InitializingBean
;
55 import org
.springframework
.context
.ResourceLoaderAware
;
56 import org
.springframework
.core
.io
.Resource
;
57 import org
.springframework
.core
.io
.ResourceLoader
;
58 import org
.xml
.sax
.InputSource
;
61 * Wrapper around a Jackrabbit repository which allows to configure it in Spring
62 * and expose it as a {@link Repository}.
64 public class JackrabbitContainer
implements InitializingBean
, DisposableBean
,
65 Repository
, ResourceLoaderAware
{
66 private Log log
= LogFactory
.getLog(JackrabbitContainer
.class);
68 private Resource configuration
;
69 private File homeDirectory
;
70 private Resource variables
;
72 private Boolean inMemory
= false;
73 private String uri
= null;
75 private Repository repository
;
77 private ResourceLoader resourceLoader
;
79 /** Node type definitions in CND format */
80 private List
<String
> cndFiles
= new ArrayList
<String
>();
82 /** Namespaces to register: key is prefix, value namespace */
83 private Map
<String
, String
> namespaces
= new HashMap
<String
, String
>();
85 private Boolean autocreateWorkspaces
= false;
87 private Executor systemExecutor
;
89 public void afterPropertiesSet() throws Exception
{
91 if (uri
!= null && !uri
.trim().equals("")) {
92 Map
<String
, String
> params
= new HashMap
<String
, String
>();
93 params
.put(org
.apache
.jackrabbit
.commons
.JcrUtils
.REPOSITORY_URI
,
95 repository
= new Jcr2davRepositoryFactory().getRepository(params
);
96 if (repository
== null)
97 throw new ArgeoException("Remote Davex repository " + uri
99 log
.info("Initialized Jackrabbit repository " + repository
100 + " from uri " + uri
);
101 // do not perform further initialization since we assume that the
102 // remote repository has been properly configured
107 if (inMemory
&& homeDirectory
.exists()) {
108 FileUtils
.deleteDirectory(homeDirectory
);
109 log
.warn("Deleted Jackrabbit home directory " + homeDirectory
);
112 RepositoryConfig config
;
113 InputStream in
= configuration
.getInputStream();
114 InputStream propsIn
= null;
116 Properties vars
= new Properties();
117 if (variables
!= null) {
118 propsIn
= variables
.getInputStream();
121 // override with system properties
122 vars
.putAll(System
.getProperties());
123 vars
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
,
124 homeDirectory
.getCanonicalPath());
125 config
= RepositoryConfig
.create(new InputSource(in
), vars
);
126 } catch (Exception e
) {
127 throw new RuntimeException("Cannot read configuration", e
);
129 IOUtils
.closeQuietly(in
);
130 IOUtils
.closeQuietly(propsIn
);
134 repository
= new TransientRepository(config
);
136 repository
= RepositoryImpl
.create(config
);
138 importNodeTypeDefinitions(repository
);
140 log
.info("Initialized Jackrabbit repository " + repository
+ " in "
141 + homeDirectory
+ " with config " + configuration
);
145 * Import declared node type definitions, trying to update them if they have
146 * changed. In case of failures an error will be logged but no exception
149 protected void importNodeTypeDefinitions(final Repository repository
) {
150 if (systemExecutor
== null) {
151 log
.warn("No system executor found");
155 systemExecutor
.execute(new Runnable() {
157 Reader reader
= null;
158 Session session
= null;
160 session
= repository
.login();
161 // Load cnds as resources
162 for (String resUrl
: cndFiles
) {
163 Resource res
= resourceLoader
.getResource(resUrl
);
164 byte[] arr
= IOUtils
.toByteArray(res
.getInputStream());
165 reader
= new InputStreamReader(
166 new ByteArrayInputStream(arr
));
167 CndImporter
.registerNodeTypes(reader
, session
, true);
170 } catch (Exception e
) {
172 "Cannot import node type definitions " + cndFiles
,
174 JcrUtils
.discardQuietly(session
);
176 IOUtils
.closeQuietly(reader
);
177 JcrUtils
.logoutQuietly(session
);
184 public void destroy() throws Exception
{
185 if (repository
!= null) {
186 if (repository
instanceof JackrabbitRepository
)
187 ((JackrabbitRepository
) repository
).shutdown();
188 else if (repository
instanceof RepositoryImpl
)
189 ((RepositoryImpl
) repository
).shutdown();
190 else if (repository
instanceof TransientRepository
)
191 ((TransientRepository
) repository
).shutdown();
195 if (homeDirectory
.exists()) {
196 FileUtils
.deleteDirectory(homeDirectory
);
197 if (log
.isDebugEnabled())
198 log
.debug("Deleted Jackrabbit home directory "
202 if (uri
!= null && !uri
.trim().equals(""))
203 log
.info("Destroyed Jackrabbit repository with uri " + uri
);
205 log
.info("Destroyed Jackrabbit repository " + repository
+ " in "
206 + homeDirectory
+ " with config " + configuration
);
209 // JCR REPOSITORY (delegated)
210 public String
getDescriptor(String key
) {
211 return repository
.getDescriptor(key
);
214 public String
[] getDescriptorKeys() {
215 return repository
.getDescriptorKeys();
218 public Session
login() throws LoginException
, RepositoryException
{
219 Session session
= repository
.login();
220 processNewSession(session
);
224 public Session
login(Credentials credentials
, String workspaceName
)
225 throws LoginException
, NoSuchWorkspaceException
,
226 RepositoryException
{
229 session
= repository
.login(credentials
, workspaceName
);
230 } catch (NoSuchWorkspaceException e
) {
231 if (autocreateWorkspaces
)
232 session
= createWorkspaceAndLogsIn(credentials
, workspaceName
);
236 processNewSession(session
);
240 public Session
login(Credentials credentials
) throws LoginException
,
241 RepositoryException
{
242 Session session
= repository
.login(credentials
);
243 processNewSession(session
);
247 public Session
login(String workspaceName
) throws LoginException
,
248 NoSuchWorkspaceException
, RepositoryException
{
251 session
= repository
.login(workspaceName
);
252 } catch (NoSuchWorkspaceException e
) {
253 if (autocreateWorkspaces
)
254 session
= createWorkspaceAndLogsIn(null, workspaceName
);
258 processNewSession(session
);
262 protected synchronized void processNewSession(Session session
) {
264 NamespaceHelper namespaceHelper
= new NamespaceHelper(session
);
265 namespaceHelper
.registerNamespaces(namespaces
);
266 } catch (Exception e
) {
267 throw new ArgeoException("Cannot process new session", e
);
272 * Logs in to the default workspace, creates the required workspace, logs
273 * out, logs in to the required workspace.
275 protected Session
createWorkspaceAndLogsIn(Credentials credentials
,
276 String workspaceName
) throws RepositoryException
{
277 if (workspaceName
== null)
278 throw new ArgeoException("No workspace specified.");
279 Session session
= repository
.login(credentials
);
280 session
.getWorkspace().createWorkspace(workspaceName
);
282 return repository
.login(credentials
, workspaceName
);
285 public void setResourceLoader(ResourceLoader resourceLoader
) {
286 this.resourceLoader
= resourceLoader
;
289 public boolean isStandardDescriptor(String key
) {
290 return repository
.isStandardDescriptor(key
);
293 public boolean isSingleValueDescriptor(String key
) {
294 return repository
.isSingleValueDescriptor(key
);
297 public Value
getDescriptorValue(String key
) {
298 return repository
.getDescriptorValue(key
);
301 public Value
[] getDescriptorValues(String key
) {
302 return repository
.getDescriptorValues(key
);
306 public void setHomeDirectory(File homeDirectory
) {
307 this.homeDirectory
= homeDirectory
;
310 public void setConfiguration(Resource configuration
) {
311 this.configuration
= configuration
;
314 public void setInMemory(Boolean inMemory
) {
315 this.inMemory
= inMemory
;
318 public void setNamespaces(Map
<String
, String
> namespaces
) {
319 this.namespaces
= namespaces
;
322 public void setCndFiles(List
<String
> cndFiles
) {
323 this.cndFiles
= cndFiles
;
326 public void setVariables(Resource variables
) {
327 this.variables
= variables
;
330 public void setUri(String uri
) {
334 public void setSystemExecutor(Executor systemExecutor
) {
335 this.systemExecutor
= systemExecutor
;