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
.server
.jackrabbit
;
19 import java
.io
.ByteArrayInputStream
;
21 import java
.io
.InputStream
;
22 import java
.io
.InputStreamReader
;
23 import java
.util
.ArrayList
;
24 import java
.util
.HashMap
;
25 import java
.util
.List
;
28 import javax
.jcr
.Credentials
;
29 import javax
.jcr
.LoginException
;
30 import javax
.jcr
.NoSuchWorkspaceException
;
31 import javax
.jcr
.Repository
;
32 import javax
.jcr
.RepositoryException
;
33 import javax
.jcr
.Session
;
34 import javax
.jcr
.Value
;
36 import org
.apache
.commons
.io
.FileUtils
;
37 import org
.apache
.commons
.io
.IOUtils
;
38 import org
.apache
.commons
.logging
.Log
;
39 import org
.apache
.commons
.logging
.LogFactory
;
40 import org
.apache
.jackrabbit
.commons
.NamespaceHelper
;
41 import org
.apache
.jackrabbit
.commons
.cnd
.CndImporter
;
42 import org
.apache
.jackrabbit
.core
.RepositoryImpl
;
43 import org
.apache
.jackrabbit
.core
.TransientRepository
;
44 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfig
;
45 import org
.argeo
.ArgeoException
;
46 import org
.springframework
.beans
.factory
.DisposableBean
;
47 import org
.springframework
.beans
.factory
.InitializingBean
;
48 import org
.springframework
.context
.ResourceLoaderAware
;
49 import org
.springframework
.core
.io
.Resource
;
50 import org
.springframework
.core
.io
.ResourceLoader
;
53 * Wrapper around a Jackrabbit repository which allows to configure it in Spring
54 * and expose it as a {@link Repository}.
56 public class JackrabbitContainer
implements InitializingBean
, DisposableBean
,
57 Repository
, ResourceLoaderAware
{
58 private Log log
= LogFactory
.getLog(JackrabbitContainer
.class);
60 private Resource configuration
;
61 private File homeDirectory
;
63 private Boolean inMemory
= false;
65 private Repository repository
;
67 private ResourceLoader resourceLoader
;
69 /** Node type definitions in CND format */
70 private List
<byte[]> cnds
= new ArrayList
<byte[]>();
71 private List
<String
> cndFiles
= new ArrayList
<String
>();
73 /** Namespaces to register: key is prefix, value namespace */
74 private Map
<String
, String
> namespaces
= new HashMap
<String
, String
>();
76 public void afterPropertiesSet() throws Exception
{
77 // Load cnds as resources
78 for (String resUrl
: cndFiles
) {
80 Resource res
= resourceLoader
.getResource(resUrl
);
81 byte[] arr
= IOUtils
.toByteArray(res
.getInputStream());
85 if (inMemory
&& homeDirectory
.exists()) {
86 FileUtils
.deleteDirectory(homeDirectory
);
87 log
.warn("Deleted Jackrabbit home directory " + homeDirectory
);
90 RepositoryConfig config
;
91 InputStream in
= configuration
.getInputStream();
93 config
= RepositoryConfig
.create(in
,
94 homeDirectory
.getCanonicalPath());
95 } catch (Exception e
) {
96 throw new RuntimeException("Cannot read configuration", e
);
98 IOUtils
.closeQuietly(in
);
102 repository
= new TransientRepository(config
);
104 repository
= RepositoryImpl
.create(config
);
106 log
.info("Initialized Jackrabbit repository " + repository
+ " in "
107 + homeDirectory
+ " with config " + configuration
);
110 public void destroy() throws Exception
{
111 if (repository
!= null) {
112 if (repository
instanceof RepositoryImpl
)
113 ((RepositoryImpl
) repository
).shutdown();
114 else if (repository
instanceof TransientRepository
)
115 ((TransientRepository
) repository
).shutdown();
119 if (homeDirectory
.exists()) {
120 FileUtils
.deleteDirectory(homeDirectory
);
121 if (log
.isDebugEnabled())
122 log
.debug("Deleted Jackrabbit home directory "
125 log
.info("Destroyed Jackrabbit repository " + repository
+ " in "
126 + homeDirectory
+ " with config " + configuration
);
129 // JCR REPOSITORY (delegated)
130 public String
getDescriptor(String key
) {
131 return repository
.getDescriptor(key
);
134 public String
[] getDescriptorKeys() {
135 return repository
.getDescriptorKeys();
138 public Session
login() throws LoginException
, RepositoryException
{
139 Session session
= repository
.login();
140 processNewSession(session
);
144 public Session
login(Credentials credentials
, String workspaceName
)
145 throws LoginException
, NoSuchWorkspaceException
,
146 RepositoryException
{
147 Session session
= repository
.login(credentials
, workspaceName
);
148 processNewSession(session
);
152 public Session
login(Credentials credentials
) throws LoginException
,
153 RepositoryException
{
154 Session session
= repository
.login(credentials
);
155 processNewSession(session
);
159 public Session
login(String workspaceName
) throws LoginException
,
160 NoSuchWorkspaceException
, RepositoryException
{
161 Session session
= repository
.login(workspaceName
);
162 processNewSession(session
);
166 protected synchronized void processNewSession(Session session
) {
168 NamespaceHelper namespaceHelper
= new NamespaceHelper(session
);
169 namespaceHelper
.registerNamespaces(namespaces
);
171 for (byte[] arr
: cnds
)
172 CndImporter
.registerNodeTypes(new InputStreamReader(
173 new ByteArrayInputStream(arr
)), session
, true);
174 } catch (Exception e
) {
175 throw new ArgeoException("Cannot process new session", e
);
179 public void setResourceLoader(ResourceLoader resourceLoader
) {
180 this.resourceLoader
= resourceLoader
;
183 public boolean isStandardDescriptor(String key
) {
184 return repository
.isStandardDescriptor(key
);
187 public boolean isSingleValueDescriptor(String key
) {
188 return repository
.isSingleValueDescriptor(key
);
191 public Value
getDescriptorValue(String key
) {
192 return repository
.getDescriptorValue(key
);
195 public Value
[] getDescriptorValues(String key
) {
196 return repository
.getDescriptorValues(key
);
200 public void setHomeDirectory(File homeDirectory
) {
201 this.homeDirectory
= homeDirectory
;
204 public void setConfiguration(Resource configuration
) {
205 this.configuration
= configuration
;
208 public void setInMemory(Boolean inMemory
) {
209 this.inMemory
= inMemory
;
212 public void setNamespaces(Map
<String
, String
> namespaces
) {
213 this.namespaces
= namespaces
;
216 public void setCndFiles(List
<String
> cndFiles
) {
217 this.cndFiles
= cndFiles
;