]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jackrabbit/JackrabbitContainer.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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 import java.util.HashMap;
22 import java.util.Map;
23
24 import javax.jcr.Credentials;
25 import javax.jcr.LoginException;
26 import javax.jcr.NoSuchWorkspaceException;
27 import javax.jcr.Repository;
28 import javax.jcr.RepositoryException;
29 import javax.jcr.Session;
30 import javax.jcr.Value;
31
32 import org.apache.commons.io.FileUtils;
33 import org.apache.commons.io.IOUtils;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.jackrabbit.commons.NamespaceHelper;
37 import org.apache.jackrabbit.core.RepositoryImpl;
38 import org.apache.jackrabbit.core.TransientRepository;
39 import org.apache.jackrabbit.core.config.RepositoryConfig;
40 import org.argeo.ArgeoException;
41 import org.springframework.beans.factory.DisposableBean;
42 import org.springframework.beans.factory.InitializingBean;
43 import org.springframework.core.io.Resource;
44
45 /**
46 * Wrapper around a Jackrabbit repository which allows to configure it in Spring
47 * and expose it as a {@link Repository}.
48 */
49 public class JackrabbitContainer implements InitializingBean, DisposableBean,
50 Repository {
51 private Log log = LogFactory.getLog(JackrabbitContainer.class);
52
53 private Resource configuration;
54 private File homeDirectory;
55
56 private Boolean inMemory = false;
57
58 private Repository repository;
59
60 /** Namespaces to register: key is prefix, value namespace */
61 private Map<String, String> namespaces = new HashMap<String, String>();
62
63 public void afterPropertiesSet() throws Exception {
64 if (inMemory && homeDirectory.exists()) {
65 FileUtils.deleteDirectory(homeDirectory);
66 log.warn("Deleted Jackrabbit home directory " + homeDirectory);
67 }
68
69 RepositoryConfig config;
70 InputStream in = configuration.getInputStream();
71 try {
72 config = RepositoryConfig.create(in,
73 homeDirectory.getCanonicalPath());
74 } catch (Exception e) {
75 throw new RuntimeException("Cannot read configuration", e);
76 } finally {
77 IOUtils.closeQuietly(in);
78 }
79
80 if (inMemory)
81 repository = new TransientRepository(config);
82 else
83 repository = RepositoryImpl.create(config);
84
85 log.info("Initialized Jackrabbit repository " + repository + " in "
86 + homeDirectory + " with config " + configuration);
87 }
88
89 public void destroy() throws Exception {
90 if (repository != null) {
91 if (repository instanceof RepositoryImpl)
92 ((RepositoryImpl) repository).shutdown();
93 else if (repository instanceof TransientRepository)
94 ((TransientRepository) repository).shutdown();
95 }
96
97 if (inMemory)
98 if (homeDirectory.exists()) {
99 FileUtils.deleteDirectory(homeDirectory);
100 if (log.isDebugEnabled())
101 log.debug("Deleted Jackrabbit home directory "
102 + homeDirectory);
103 }
104 log.info("Destroyed Jackrabbit repository " + repository + " in "
105 + homeDirectory + " with config " + configuration);
106 }
107
108 // JCR REPOSITORY (delegated)
109 public String getDescriptor(String key) {
110 return repository.getDescriptor(key);
111 }
112
113 public String[] getDescriptorKeys() {
114 return repository.getDescriptorKeys();
115 }
116
117 public Session login() throws LoginException, RepositoryException {
118 Session session = repository.login();
119 processNewSession(session);
120 return session;
121 }
122
123 public Session login(Credentials credentials, String workspaceName)
124 throws LoginException, NoSuchWorkspaceException,
125 RepositoryException {
126 Session session = repository.login(credentials, workspaceName);
127 processNewSession(session);
128 return session;
129 }
130
131 public Session login(Credentials credentials) throws LoginException,
132 RepositoryException {
133 Session session = repository.login(credentials);
134 processNewSession(session);
135 return session;
136 }
137
138 public Session login(String workspaceName) throws LoginException,
139 NoSuchWorkspaceException, RepositoryException {
140 Session session = repository.login(workspaceName);
141 processNewSession(session);
142 return session;
143 }
144
145 protected void processNewSession(Session session) {
146 try {
147 NamespaceHelper namespaceHelper = new NamespaceHelper(session);
148 namespaceHelper.registerNamespaces(namespaces);
149 } catch (RepositoryException e) {
150 throw new ArgeoException("Cannot process new session", e);
151 }
152 }
153
154 public boolean isStandardDescriptor(String key) {
155 return repository.isStandardDescriptor(key);
156 }
157
158 public boolean isSingleValueDescriptor(String key) {
159 return repository.isSingleValueDescriptor(key);
160 }
161
162 public Value getDescriptorValue(String key) {
163 return repository.getDescriptorValue(key);
164 }
165
166 public Value[] getDescriptorValues(String key) {
167 return repository.getDescriptorValues(key);
168 }
169
170 // BEANS METHODS
171 public void setHomeDirectory(File homeDirectory) {
172 this.homeDirectory = homeDirectory;
173 }
174
175 public void setConfiguration(Resource configuration) {
176 this.configuration = configuration;
177 }
178
179 public void setInMemory(Boolean inMemory) {
180 this.inMemory = inMemory;
181 }
182
183 public void setNamespaces(Map<String, String> namespaces) {
184 this.namespaces = namespaces;
185 }
186
187 }