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