]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jackrabbit/JackrabbitContainer.java
In JCR tests create one temporary directory per user, in order to avoid authorization...
[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.File;
20 import java.io.InputStream;
21
22 import javax.jcr.Credentials;
23 import javax.jcr.LoginException;
24 import javax.jcr.NoSuchWorkspaceException;
25 import javax.jcr.Repository;
26 import javax.jcr.RepositoryException;
27 import javax.jcr.Session;
28
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.io.IOUtils;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.jackrabbit.core.RepositoryImpl;
34 import org.apache.jackrabbit.core.TransientRepository;
35 import org.apache.jackrabbit.core.config.RepositoryConfig;
36 import org.springframework.beans.factory.DisposableBean;
37 import org.springframework.beans.factory.InitializingBean;
38 import org.springframework.core.io.Resource;
39
40 public class JackrabbitContainer implements InitializingBean, DisposableBean,
41 Repository {
42 private Log log = LogFactory.getLog(JackrabbitContainer.class);
43
44 private Resource configuration;
45 private File homeDirectory;
46
47 private Boolean inMemory = false;
48
49 private Repository repository;
50
51 public void afterPropertiesSet() throws Exception {
52 if (inMemory && homeDirectory.exists()) {
53 FileUtils.deleteDirectory(homeDirectory);
54 log.warn("Deleted Jackrabbit home directory " + homeDirectory);
55 }
56
57 RepositoryConfig config;
58 InputStream in = configuration.getInputStream();
59 try {
60 config = RepositoryConfig.create(in, homeDirectory
61 .getCanonicalPath());
62 } catch (Exception e) {
63 throw new RuntimeException("Cannot read configuration", e);
64 } finally {
65 IOUtils.closeQuietly(in);
66 }
67
68 if (inMemory)
69 repository = new TransientRepository(config);
70 else
71 repository = RepositoryImpl.create(config);
72
73 log.info("Initialized Jackrabbit repository " + repository + " in "
74 + homeDirectory + " with config " + configuration);
75 }
76
77 public void destroy() throws Exception {
78 if (repository != null) {
79 if (repository instanceof RepositoryImpl)
80 ((RepositoryImpl) repository).shutdown();
81 else if (repository instanceof TransientRepository)
82 ((TransientRepository) repository).shutdown();
83 }
84
85 if (inMemory)
86 if (homeDirectory.exists()) {
87 FileUtils.deleteDirectory(homeDirectory);
88 if (log.isDebugEnabled())
89 log.debug("Deleted Jackrabbit home directory "
90 + homeDirectory);
91 }
92 log.info("Destroyed Jackrabbit repository " + repository + " in "
93 + homeDirectory + " with config " + configuration);
94 }
95
96 // JCR REPOSITORY (delegated)
97 public String getDescriptor(String key) {
98 return repository.getDescriptor(key);
99 }
100
101 public String[] getDescriptorKeys() {
102 return repository.getDescriptorKeys();
103 }
104
105 public Session login() throws LoginException, RepositoryException {
106 return repository.login();
107 }
108
109 public Session login(Credentials credentials, String workspaceName)
110 throws LoginException, NoSuchWorkspaceException,
111 RepositoryException {
112 return repository.login(credentials, workspaceName);
113 }
114
115 public Session login(Credentials credentials) throws LoginException,
116 RepositoryException {
117 return repository.login(credentials);
118 }
119
120 public Session login(String workspaceName) throws LoginException,
121 NoSuchWorkspaceException, RepositoryException {
122 return repository.login(workspaceName);
123 }
124
125 // BEANS METHODS
126 public void setHomeDirectory(File homeDirectory) {
127 this.homeDirectory = homeDirectory;
128 }
129
130 public void setConfiguration(Resource configuration) {
131 this.configuration = configuration;
132 }
133
134 public void setInMemory(Boolean inMemory) {
135 this.inMemory = inMemory;
136 }
137
138 }