]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jackrabbit/JackrabbitContainer.java
6bddeefd2d30c9d1cfb964aba9716e4fc3306981
[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
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;
35
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;
51
52 /**
53 * Wrapper around a Jackrabbit repository which allows to configure it in Spring
54 * and expose it as a {@link Repository}.
55 */
56 public class JackrabbitContainer implements InitializingBean, DisposableBean,
57 Repository, ResourceLoaderAware {
58 private Log log = LogFactory.getLog(JackrabbitContainer.class);
59
60 private Resource configuration;
61 private File homeDirectory;
62
63 private Boolean inMemory = false;
64
65 private Repository repository;
66
67 private ResourceLoader resourceLoader;
68
69 /** Node type definitions in CND format */
70 private List<byte[]> cnds = new ArrayList<byte[]>();
71 private List<String> cndFiles = new ArrayList<String>();
72
73 /** Namespaces to register: key is prefix, value namespace */
74 private Map<String, String> namespaces = new HashMap<String, String>();
75
76 public void afterPropertiesSet() throws Exception {
77 // Load cnds as resources
78 for (String resUrl : cndFiles) {
79
80 Resource res = resourceLoader.getResource(resUrl);
81 byte[] arr = IOUtils.toByteArray(res.getInputStream());
82 cnds.add(arr);
83 }
84
85 if (inMemory && homeDirectory.exists()) {
86 FileUtils.deleteDirectory(homeDirectory);
87 log.warn("Deleted Jackrabbit home directory " + homeDirectory);
88 }
89
90 RepositoryConfig config;
91 InputStream in = configuration.getInputStream();
92 try {
93 config = RepositoryConfig.create(in,
94 homeDirectory.getCanonicalPath());
95 } catch (Exception e) {
96 throw new RuntimeException("Cannot read configuration", e);
97 } finally {
98 IOUtils.closeQuietly(in);
99 }
100
101 if (inMemory)
102 repository = new TransientRepository(config);
103 else
104 repository = RepositoryImpl.create(config);
105
106 log.info("Initialized Jackrabbit repository " + repository + " in "
107 + homeDirectory + " with config " + configuration);
108 }
109
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();
116 }
117
118 if (inMemory)
119 if (homeDirectory.exists()) {
120 FileUtils.deleteDirectory(homeDirectory);
121 if (log.isDebugEnabled())
122 log.debug("Deleted Jackrabbit home directory "
123 + homeDirectory);
124 }
125 log.info("Destroyed Jackrabbit repository " + repository + " in "
126 + homeDirectory + " with config " + configuration);
127 }
128
129 // JCR REPOSITORY (delegated)
130 public String getDescriptor(String key) {
131 return repository.getDescriptor(key);
132 }
133
134 public String[] getDescriptorKeys() {
135 return repository.getDescriptorKeys();
136 }
137
138 public Session login() throws LoginException, RepositoryException {
139 Session session = repository.login();
140 processNewSession(session);
141 return session;
142 }
143
144 public Session login(Credentials credentials, String workspaceName)
145 throws LoginException, NoSuchWorkspaceException,
146 RepositoryException {
147 Session session = repository.login(credentials, workspaceName);
148 processNewSession(session);
149 return session;
150 }
151
152 public Session login(Credentials credentials) throws LoginException,
153 RepositoryException {
154 Session session = repository.login(credentials);
155 processNewSession(session);
156 return session;
157 }
158
159 public Session login(String workspaceName) throws LoginException,
160 NoSuchWorkspaceException, RepositoryException {
161 Session session = repository.login(workspaceName);
162 processNewSession(session);
163 return session;
164 }
165
166 protected synchronized void processNewSession(Session session) {
167 try {
168 NamespaceHelper namespaceHelper = new NamespaceHelper(session);
169 namespaceHelper.registerNamespaces(namespaces);
170
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);
176 }
177 }
178
179 public void setResourceLoader(ResourceLoader resourceLoader) {
180 this.resourceLoader = resourceLoader;
181 }
182
183 public boolean isStandardDescriptor(String key) {
184 return repository.isStandardDescriptor(key);
185 }
186
187 public boolean isSingleValueDescriptor(String key) {
188 return repository.isSingleValueDescriptor(key);
189 }
190
191 public Value getDescriptorValue(String key) {
192 return repository.getDescriptorValue(key);
193 }
194
195 public Value[] getDescriptorValues(String key) {
196 return repository.getDescriptorValues(key);
197 }
198
199 // BEANS METHODS
200 public void setHomeDirectory(File homeDirectory) {
201 this.homeDirectory = homeDirectory;
202 }
203
204 public void setConfiguration(Resource configuration) {
205 this.configuration = configuration;
206 }
207
208 public void setInMemory(Boolean inMemory) {
209 this.inMemory = inMemory;
210 }
211
212 public void setNamespaces(Map<String, String> namespaces) {
213 this.namespaces = namespaces;
214 }
215
216 public void setCndFiles(List<String> cndFiles) {
217 this.cndFiles = cndFiles;
218 }
219
220 }