]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JackrabbitRepositoryFactory.java
f7e882e7b4d8c454bb8af82046352d47733306a8
[lgpl/argeo-commons.git] / JackrabbitRepositoryFactory.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 package org.argeo.jackrabbit;
17
18 import java.io.File;
19 import java.io.InputStream;
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Properties;
25
26 import javax.jcr.Repository;
27 import javax.jcr.RepositoryException;
28 import javax.jcr.RepositoryFactory;
29 import javax.jcr.Session;
30
31 import org.apache.commons.io.IOUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.jackrabbit.commons.JcrUtils;
35 import org.apache.jackrabbit.core.RepositoryImpl;
36 import org.apache.jackrabbit.core.config.RepositoryConfig;
37 import org.apache.jackrabbit.core.config.RepositoryConfigurationParser;
38 import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
39 import org.argeo.jcr.ArgeoJcrException;
40 import org.springframework.core.io.ClassPathResource;
41 import org.springframework.core.io.Resource;
42 import org.xml.sax.InputSource;
43
44 /**
45 * Repository factory which can create new repositories and access remote
46 * Jackrabbit repositories
47 */
48 @Deprecated
49 public class JackrabbitRepositoryFactory implements RepositoryFactory {
50 // FIXME factorize with node
51 /** Key for a JCR repository alias */
52 public final static String JCR_REPOSITORY_ALIAS = "argeo.jcr.repository.alias";
53 /** Key for a JCR repository URI */
54 public final static String JCR_REPOSITORY_URI = "argeo.jcr.repository.uri";
55
56 private final static Log log = LogFactory
57 .getLog(JackrabbitRepositoryFactory.class);
58
59 private Resource fileRepositoryConfiguration = new ClassPathResource(
60 "/org/argeo/jackrabbit/repository-h2.xml");
61
62 @SuppressWarnings({ "rawtypes" })
63 public Repository getRepository(Map parameters) throws RepositoryException {
64 // // check if can be found by alias
65 // Repository repository = super.getRepository(parameters);
66 // if (repository != null)
67 // return repository;
68
69 // check if remote
70 Repository repository;
71 String uri = null;
72 if (parameters.containsKey(JCR_REPOSITORY_URI))
73 uri = parameters.get(JCR_REPOSITORY_URI).toString();
74 else if (parameters.containsKey(JcrUtils.REPOSITORY_URI))
75 uri = parameters.get(JcrUtils.REPOSITORY_URI).toString();
76
77 if (uri != null) {
78 if (uri.startsWith("http"))// http, https
79 repository = createRemoteRepository(uri);
80 else if (uri.startsWith("file"))// http, https
81 repository = createFileRepository(uri, parameters);
82 else if (uri.startsWith("vm")) {
83 log.warn("URI "
84 + uri
85 + " should have been managed by generic JCR repository factory");
86 repository = getRepositoryByAlias(getAliasFromURI(uri));
87 } else
88 throw new ArgeoJcrException("Unrecognized URI format " + uri);
89
90 }
91
92 else if (parameters.containsKey(JCR_REPOSITORY_ALIAS)) {
93 // Properties properties = new Properties();
94 // properties.putAll(parameters);
95 String alias = parameters.get(JCR_REPOSITORY_ALIAS).toString();
96 // publish(alias, repository, properties);
97 // log.info("Registered JCR repository under alias '" + alias + "'
98 // with properties " + properties);
99 repository = getRepositoryByAlias(alias);
100 } else
101 throw new ArgeoJcrException("Not enough information in "
102 + parameters);
103
104 if (repository == null)
105 throw new ArgeoJcrException("Repository not found " + parameters);
106
107 return repository;
108 }
109
110 protected Repository getRepositoryByAlias(String alias) {
111 return null;
112 }
113
114 protected Repository createRemoteRepository(String uri)
115 throws RepositoryException {
116 Map<String, String> params = new HashMap<String, String>();
117 params.put(JcrUtils.REPOSITORY_URI, uri);
118 Repository repository = new Jcr2davRepositoryFactory()
119 .getRepository(params);
120 if (repository == null)
121 throw new ArgeoJcrException("Remote Davex repository " + uri
122 + " not found");
123 log.info("Initialized remote Jackrabbit repository from uri " + uri);
124 return repository;
125 }
126
127 @SuppressWarnings({ "rawtypes", "unchecked" })
128 protected Repository createFileRepository(final String uri, Map parameters)
129 throws RepositoryException {
130 InputStream configurationIn = null;
131 try {
132 Properties vars = new Properties();
133 vars.putAll(parameters);
134 String dirPath = uri.substring("file:".length());
135 File homeDir = new File(dirPath);
136 if (homeDir.exists() && !homeDir.isDirectory())
137 throw new ArgeoJcrException("Repository home " + dirPath
138 + " is not a directory");
139 if (!homeDir.exists())
140 homeDir.mkdirs();
141 configurationIn = fileRepositoryConfiguration.getInputStream();
142 vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
143 homeDir.getCanonicalPath());
144 RepositoryConfig repositoryConfig = RepositoryConfig.create(
145 new InputSource(configurationIn), vars);
146
147 // TransientRepository repository = new
148 // TransientRepository(repositoryConfig);
149 final RepositoryImpl repository = RepositoryImpl
150 .create(repositoryConfig);
151 Session session = repository.login();
152 // FIXME make it generic
153 org.argeo.jcr.JcrUtils.addPrivilege(session, "/", "ROLE_ADMIN",
154 "jcr:all");
155 org.argeo.jcr.JcrUtils.logoutQuietly(session);
156 Runtime.getRuntime().addShutdownHook(
157 new Thread("Clean JCR repository " + uri) {
158 public void run() {
159 repository.shutdown();
160 log.info("Destroyed repository " + uri);
161 }
162 });
163 log.info("Initialized file Jackrabbit repository from uri " + uri);
164 return repository;
165 } catch (Exception e) {
166 throw new ArgeoJcrException("Cannot create repository " + uri, e);
167 } finally {
168 IOUtils.closeQuietly(configurationIn);
169 }
170 }
171
172 protected String getAliasFromURI(String uri) {
173 try {
174 URI uriObj = new URI(uri);
175 String alias = uriObj.getPath();
176 if (alias.charAt(0) == '/')
177 alias = alias.substring(1);
178 if (alias.charAt(alias.length() - 1) == '/')
179 alias = alias.substring(0, alias.length() - 1);
180 return alias;
181 } catch (URISyntaxException e) {
182 throw new ArgeoJcrException("Cannot interpret URI " + uri, e);
183 }
184 }
185
186 /**
187 * Called after the repository has been initialised. Does nothing by
188 * default.
189 */
190 @SuppressWarnings("rawtypes")
191 protected void postInitialization(Repository repository, Map parameters) {
192
193 }
194
195 public void setFileRepositoryConfiguration(
196 Resource fileRepositoryConfiguration) {
197 this.fileRepositoryConfiguration = fileRepositoryConfiguration;
198 }
199
200 }