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