]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jackrabbit/JackrabbitContainer.java
Remote JCR client working
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / server / jackrabbit / JackrabbitContainer.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.server.jackrabbit;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.File;
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;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import javax.jcr.Credentials;
30 import javax.jcr.LoginException;
31 import javax.jcr.NoSuchWorkspaceException;
32 import javax.jcr.Repository;
33 import javax.jcr.RepositoryException;
34 import javax.jcr.Session;
35 import javax.jcr.Value;
36
37 import org.apache.commons.io.FileUtils;
38 import org.apache.commons.io.IOUtils;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.jackrabbit.api.JackrabbitRepository;
42 import org.apache.jackrabbit.commons.JcrUtils;
43 import org.apache.jackrabbit.commons.NamespaceHelper;
44 import org.apache.jackrabbit.commons.cnd.CndImporter;
45 import org.apache.jackrabbit.core.RepositoryFactoryImpl;
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.springframework.beans.factory.DisposableBean;
53 import org.springframework.beans.factory.InitializingBean;
54 import org.springframework.context.ResourceLoaderAware;
55 import org.springframework.core.io.Resource;
56 import org.springframework.core.io.ResourceLoader;
57 import org.xml.sax.InputSource;
58
59 /**
60 * Wrapper around a Jackrabbit repository which allows to configure it in Spring
61 * and expose it as a {@link Repository}.
62 */
63 public class JackrabbitContainer implements InitializingBean, DisposableBean,
64 Repository, ResourceLoaderAware {
65 private Log log = LogFactory.getLog(JackrabbitContainer.class);
66
67 private Resource configuration;
68 private File homeDirectory;
69 private Resource variables;
70
71 private Boolean inMemory = false;
72 private String uri = null;
73
74 private Repository repository;
75
76 private ResourceLoader resourceLoader;
77
78 /** Node type definitions in CND format */
79 private List<byte[]> cnds = new ArrayList<byte[]>();
80 private List<String> cndFiles = new ArrayList<String>();
81
82 /** Namespaces to register: key is prefix, value namespace */
83 private Map<String, String> namespaces = new HashMap<String, String>();
84
85 private Boolean autocreateWorkspaces = false;
86
87 public void afterPropertiesSet() throws Exception {
88 // Load cnds as resources
89 for (String resUrl : cndFiles) {
90 Resource res = resourceLoader.getResource(resUrl);
91 byte[] arr = IOUtils.toByteArray(res.getInputStream());
92 cnds.add(arr);
93 }
94
95 if (uri != null) {
96 // Thread.currentThread().setContextClassLoader(
97 // getClass().getClassLoader());
98 // repository = JcrUtils.getRepository(uri);
99 Map<String, String> params = new HashMap<String, String>();
100 params.put(JcrUtils.REPOSITORY_URI, uri);
101 repository = new Jcr2davRepositoryFactory().getRepository(params);
102 if (repository == null)
103 throw new ArgeoException("Remote Davex repository " + uri
104 + " not found");
105 log.info("Initialized Jackrabbit repository " + repository
106 + " from uri " + uri);
107 } else {
108 if (inMemory && homeDirectory.exists()) {
109 FileUtils.deleteDirectory(homeDirectory);
110 log.warn("Deleted Jackrabbit home directory " + homeDirectory);
111 }
112
113 RepositoryConfig config;
114 InputStream in = configuration.getInputStream();
115 InputStream propsIn = null;
116 try {
117 Properties vars = new Properties();
118 if (variables != null) {
119 propsIn = variables.getInputStream();
120 vars.load(propsIn);
121 }
122 // override with system properties
123 vars.putAll(System.getProperties());
124 vars.put(
125 RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
126 homeDirectory.getCanonicalPath());
127 config = RepositoryConfig.create(new InputSource(in), vars);
128 } catch (Exception e) {
129 throw new RuntimeException("Cannot read configuration", e);
130 } finally {
131 IOUtils.closeQuietly(in);
132 IOUtils.closeQuietly(propsIn);
133 }
134
135 if (inMemory)
136 repository = new TransientRepository(config);
137 else
138 repository = RepositoryImpl.create(config);
139
140 log.info("Initialized Jackrabbit repository " + repository + " in "
141 + homeDirectory + " with config " + configuration);
142 }
143 }
144
145 public void destroy() throws Exception {
146 if (repository != null) {
147 if (repository instanceof JackrabbitRepository)
148 ((JackrabbitRepository) repository).shutdown();
149 else if (repository instanceof RepositoryImpl)
150 ((RepositoryImpl) repository).shutdown();
151 else if (repository instanceof TransientRepository)
152 ((TransientRepository) repository).shutdown();
153 }
154
155 if (inMemory)
156 if (homeDirectory.exists()) {
157 FileUtils.deleteDirectory(homeDirectory);
158 if (log.isDebugEnabled())
159 log.debug("Deleted Jackrabbit home directory "
160 + homeDirectory);
161 }
162
163 if (uri != null)
164 log.info("Destroyed Jackrabbit repository with uri " + uri);
165 else
166 log.info("Destroyed Jackrabbit repository " + repository + " in "
167 + homeDirectory + " with config " + configuration);
168 }
169
170 // JCR REPOSITORY (delegated)
171 public String getDescriptor(String key) {
172 return repository.getDescriptor(key);
173 }
174
175 public String[] getDescriptorKeys() {
176 return repository.getDescriptorKeys();
177 }
178
179 public Session login() throws LoginException, RepositoryException {
180 Session session = repository.login();
181 processNewSession(session);
182 return session;
183 }
184
185 public Session login(Credentials credentials, String workspaceName)
186 throws LoginException, NoSuchWorkspaceException,
187 RepositoryException {
188 Session session;
189 try {
190 session = repository.login(credentials, workspaceName);
191 } catch (NoSuchWorkspaceException e) {
192 if (autocreateWorkspaces)
193 session = createWorkspaceAndLogsIn(credentials, workspaceName);
194 else
195 throw e;
196 }
197 processNewSession(session);
198 return session;
199 }
200
201 public Session login(Credentials credentials) throws LoginException,
202 RepositoryException {
203 Session session = repository.login(credentials);
204 processNewSession(session);
205 return session;
206 }
207
208 public Session login(String workspaceName) throws LoginException,
209 NoSuchWorkspaceException, RepositoryException {
210 Session session;
211 try {
212 session = repository.login(workspaceName);
213 } catch (NoSuchWorkspaceException e) {
214 if (autocreateWorkspaces)
215 session = createWorkspaceAndLogsIn(null, workspaceName);
216 else
217 throw e;
218 }
219 processNewSession(session);
220 return session;
221 }
222
223 protected synchronized void processNewSession(Session session) {
224 try {
225 NamespaceHelper namespaceHelper = new NamespaceHelper(session);
226 namespaceHelper.registerNamespaces(namespaces);
227
228 for (byte[] arr : cnds)
229 CndImporter.registerNodeTypes(new InputStreamReader(
230 new ByteArrayInputStream(arr)), session, true);
231 } catch (Exception e) {
232 throw new ArgeoException("Cannot process new session", e);
233 }
234 }
235
236 /**
237 * Logs in to the default workspace, creates the required workspace, logs
238 * out, logs in to the required workspace.
239 */
240 protected Session createWorkspaceAndLogsIn(Credentials credentials,
241 String workspaceName) throws RepositoryException {
242 if (workspaceName == null)
243 throw new ArgeoException("No workspace specified.");
244 Session session = repository.login(credentials);
245 session.getWorkspace().createWorkspace(workspaceName);
246 session.logout();
247 return repository.login(credentials, workspaceName);
248 }
249
250 public void setResourceLoader(ResourceLoader resourceLoader) {
251 this.resourceLoader = resourceLoader;
252 }
253
254 public boolean isStandardDescriptor(String key) {
255 return repository.isStandardDescriptor(key);
256 }
257
258 public boolean isSingleValueDescriptor(String key) {
259 return repository.isSingleValueDescriptor(key);
260 }
261
262 public Value getDescriptorValue(String key) {
263 return repository.getDescriptorValue(key);
264 }
265
266 public Value[] getDescriptorValues(String key) {
267 return repository.getDescriptorValues(key);
268 }
269
270 // BEANS METHODS
271 public void setHomeDirectory(File homeDirectory) {
272 this.homeDirectory = homeDirectory;
273 }
274
275 public void setConfiguration(Resource configuration) {
276 this.configuration = configuration;
277 }
278
279 public void setInMemory(Boolean inMemory) {
280 this.inMemory = inMemory;
281 }
282
283 public void setNamespaces(Map<String, String> namespaces) {
284 this.namespaces = namespaces;
285 }
286
287 public void setCndFiles(List<String> cndFiles) {
288 this.cndFiles = cndFiles;
289 }
290
291 public void setVariables(Resource variables) {
292 this.variables = variables;
293 }
294
295 public void setUri(String uri) {
296 this.uri = uri;
297 }
298
299 }