]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitContainer.java
Improve RAP deployment in Tomcat
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / 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.jackrabbit;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.File;
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;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.concurrent.Executor;
31
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;
39
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;
61
62 /**
63 * Wrapper around a Jackrabbit repository which allows to configure it in Spring
64 * and expose it as a {@link Repository}.
65 */
66 public class JackrabbitContainer implements InitializingBean, DisposableBean,
67 Repository, ResourceLoaderAware {
68 private Log log = LogFactory.getLog(JackrabbitContainer.class);
69
70 private Resource configuration;
71 private File homeDirectory;
72 private Resource variables;
73
74 private Boolean inMemory = false;
75 private String uri = null;
76
77 private Repository repository;
78
79 private ResourceLoader resourceLoader;
80
81 /** Node type definitions in CND format */
82 private List<String> cndFiles = new ArrayList<String>();
83
84 /** Namespaces to register: key is prefix, value namespace */
85 private Map<String, String> namespaces = new HashMap<String, String>();
86
87 private Boolean autocreateWorkspaces = false;
88
89 private Executor systemExecutor;
90 private Credentials adminCredentials;
91
92 // transition from legacy spring approach
93 private Boolean alreadyInitialized = false;
94 private Boolean alreadyDisposed = false;
95
96 /** @deprecated explicitly declare {@link #init()} as init-method instead. */
97 public void afterPropertiesSet() throws Exception {
98 if (!alreadyInitialized) {
99 log.warn("## If not already done,"
100 + " declare init-method=\"init\".");
101 initImpl();
102 }
103 }
104
105 public void init() throws Exception {
106 initImpl();
107 alreadyInitialized = true;
108 }
109
110 protected void initImpl() throws Exception {
111 if (repository != null) {
112 // we are just wrapping another repository
113 importNodeTypeDefinitions(repository);
114 return;
115 }
116
117 // remote repository
118 if (uri != null && !uri.trim().equals("")) {
119 Map<String, String> params = new HashMap<String, String>();
120 params.put(org.apache.jackrabbit.commons.JcrUtils.REPOSITORY_URI,
121 uri);
122 repository = new Jcr2davRepositoryFactory().getRepository(params);
123 if (repository == null)
124 throw new ArgeoException("Remote Davex repository " + uri
125 + " not found");
126 log.info("Initialized Jackrabbit repository " + repository
127 + " from URI " + uri);
128 // do not perform further initialization since we assume that the
129 // remote repository has been properly configured
130 return;
131 }
132
133 // local repository
134 if (inMemory && homeDirectory.exists()) {
135 FileUtils.deleteDirectory(homeDirectory);
136 log.warn("Deleted Jackrabbit home directory " + homeDirectory);
137 }
138
139 RepositoryConfig config;
140 Properties vars = getConfigurationProperties();
141 InputStream in = configuration.getInputStream();
142 try {
143 vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
144 homeDirectory.getCanonicalPath());
145 config = RepositoryConfig.create(new InputSource(in), vars);
146 } catch (Exception e) {
147 throw new RuntimeException("Cannot read configuration", e);
148 } finally {
149 IOUtils.closeQuietly(in);
150 }
151
152 if (inMemory)
153 repository = new TransientRepository(config);
154 else
155 repository = RepositoryImpl.create(config);
156
157 if (cndFiles != null && cndFiles.size() > 0)
158 importNodeTypeDefinitions(repository);
159
160 log.info("Initialized Jackrabbit repository " + repository + " in "
161 + homeDirectory + " with config " + configuration);
162 }
163
164 /**
165 * @deprecated explicitly declare {@link #dispose()} as destroy-method
166 * instead.
167 */
168 public void destroy() throws Exception {
169 if (!alreadyDisposed) {
170 log.warn("## If not already done,"
171 + " declare destroy-method=\"dispose\".");
172 disposeImpl();
173 }
174 }
175
176 public void dispose() throws Exception {
177 disposeImpl();
178 alreadyDisposed = true;
179 }
180
181 protected void disposeImpl() throws Exception {
182 if (repository != null) {
183 if (repository instanceof JackrabbitRepository)
184 ((JackrabbitRepository) repository).shutdown();
185 else if (repository instanceof RepositoryImpl)
186 ((RepositoryImpl) repository).shutdown();
187 else if (repository instanceof TransientRepository)
188 ((TransientRepository) repository).shutdown();
189 }
190
191 if (inMemory)
192 if (homeDirectory.exists()) {
193 FileUtils.deleteDirectory(homeDirectory);
194 if (log.isDebugEnabled())
195 log.debug("Deleted Jackrabbit home directory "
196 + homeDirectory);
197 }
198
199 if (uri != null && !uri.trim().equals(""))
200 log.info("Destroyed Jackrabbit repository with uri " + uri);
201 else
202 log.info("Destroyed Jackrabbit repository " + repository + " in "
203 + homeDirectory + " with config " + configuration);
204 }
205
206 protected Properties getConfigurationProperties() {
207 InputStream propsIn = null;
208 Properties vars;
209 try {
210 vars = new Properties();
211 if (variables != null) {
212 propsIn = variables.getInputStream();
213 vars.load(propsIn);
214 }
215 // resolve system properties
216 for (Object key : vars.keySet()) {
217 // TODO: implement a smarter mechanism to resolve nested ${}
218 String newValue = SystemPropertyUtils.resolvePlaceholders(vars
219 .getProperty(key.toString()));
220 vars.put(key, newValue);
221 }
222 // override with system properties
223 vars.putAll(System.getProperties());
224 } catch (IOException e) {
225 throw new ArgeoException("Cannot read configuration properties", e);
226 } finally {
227 IOUtils.closeQuietly(propsIn);
228 }
229 return vars;
230 }
231
232 /**
233 * Import declared node type definitions, trying to update them if they have
234 * changed. In case of failures an error will be logged but no exception
235 * will be thrown.
236 */
237 protected void importNodeTypeDefinitions(final Repository repository) {
238 final Credentials credentialsToUse = null;
239 // if (systemExecutor == null) {
240 // if (adminCredentials == null) {
241 // log.error("No system executor or admin credentials found,"
242 // + " cannot import node types");
243 // return;
244 // }
245 // credentialsToUse = adminCredentials;
246 // } else {
247 // credentialsToUse = null;
248 // }
249
250 Runnable action = new Runnable() {
251 public void run() {
252 Reader reader = null;
253 Session session = null;
254 try {
255 session = repository.login(credentialsToUse);
256 processNewSession(session);
257 // Load cnds as resources
258 for (String resUrl : cndFiles) {
259 Resource res = resourceLoader.getResource(resUrl);
260 byte[] arr = IOUtils.toByteArray(res.getInputStream());
261 reader = new InputStreamReader(
262 new ByteArrayInputStream(arr));
263 CndImporter.registerNodeTypes(reader, session, true);
264 }
265 session.save();
266 } catch (Exception e) {
267 log.error(
268 "Cannot import node type definitions " + cndFiles,
269 e);
270 JcrUtils.discardQuietly(session);
271 } finally {
272 IOUtils.closeQuietly(reader);
273 JcrUtils.logoutQuietly(session);
274 }
275 }
276 };
277
278 if (systemExecutor != null)
279 systemExecutor.execute(action);
280 else
281 action.run();
282 }
283
284 // JCR REPOSITORY (delegated)
285 public String getDescriptor(String key) {
286 return repository.getDescriptor(key);
287 }
288
289 public String[] getDescriptorKeys() {
290 return repository.getDescriptorKeys();
291 }
292
293 public Session login() throws LoginException, RepositoryException {
294 Session session = repository.login();
295 processNewSession(session);
296 return session;
297 }
298
299 public Session login(Credentials credentials, String workspaceName)
300 throws LoginException, NoSuchWorkspaceException,
301 RepositoryException {
302 Session session;
303 try {
304 session = repository.login(credentials, workspaceName);
305 } catch (NoSuchWorkspaceException e) {
306 if (autocreateWorkspaces)
307 session = createWorkspaceAndLogsIn(credentials, workspaceName);
308 else
309 throw e;
310 }
311 processNewSession(session);
312 return session;
313 }
314
315 public Session login(Credentials credentials) throws LoginException,
316 RepositoryException {
317 Session session = repository.login(credentials);
318 processNewSession(session);
319 return session;
320 }
321
322 public Session login(String workspaceName) throws LoginException,
323 NoSuchWorkspaceException, RepositoryException {
324 Session session;
325 try {
326 session = repository.login(workspaceName);
327 } catch (NoSuchWorkspaceException e) {
328 if (autocreateWorkspaces)
329 session = createWorkspaceAndLogsIn(null, workspaceName);
330 else
331 throw e;
332 }
333 processNewSession(session);
334 return session;
335 }
336
337 protected synchronized void processNewSession(Session session) {
338 try {
339 NamespaceHelper namespaceHelper = new NamespaceHelper(session);
340 namespaceHelper.registerNamespaces(namespaces);
341 } catch (Exception e) {
342 throw new ArgeoException("Cannot process new session", e);
343 }
344 }
345
346 /**
347 * Logs in to the default workspace, creates the required workspace, logs
348 * out, logs in to the required workspace.
349 */
350 protected Session createWorkspaceAndLogsIn(Credentials credentials,
351 String workspaceName) throws RepositoryException {
352 if (workspaceName == null)
353 throw new ArgeoException("No workspace specified.");
354 Session session = repository.login(credentials);
355 session.getWorkspace().createWorkspace(workspaceName);
356 session.logout();
357 return repository.login(credentials, workspaceName);
358 }
359
360 public void setResourceLoader(ResourceLoader resourceLoader) {
361 this.resourceLoader = resourceLoader;
362 }
363
364 public boolean isStandardDescriptor(String key) {
365 return repository.isStandardDescriptor(key);
366 }
367
368 public boolean isSingleValueDescriptor(String key) {
369 return repository.isSingleValueDescriptor(key);
370 }
371
372 public Value getDescriptorValue(String key) {
373 return repository.getDescriptorValue(key);
374 }
375
376 public Value[] getDescriptorValues(String key) {
377 return repository.getDescriptorValues(key);
378 }
379
380 // BEANS METHODS
381 public void setHomeDirectory(File homeDirectory) {
382 this.homeDirectory = homeDirectory;
383 }
384
385 public void setConfiguration(Resource configuration) {
386 this.configuration = configuration;
387 }
388
389 public void setInMemory(Boolean inMemory) {
390 this.inMemory = inMemory;
391 }
392
393 public void setNamespaces(Map<String, String> namespaces) {
394 this.namespaces = namespaces;
395 }
396
397 public void setCndFiles(List<String> cndFiles) {
398 this.cndFiles = cndFiles;
399 }
400
401 public void setVariables(Resource variables) {
402 this.variables = variables;
403 }
404
405 public void setUri(String uri) {
406 this.uri = uri;
407 }
408
409 public void setSystemExecutor(Executor systemExecutor) {
410 this.systemExecutor = systemExecutor;
411 }
412
413 public void setAdminCredentials(Credentials adminCredentials) {
414 this.adminCredentials = adminCredentials;
415 }
416
417 public void setRepository(Repository repository) {
418 this.repository = repository;
419 }
420
421 }