]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JackrabbitRepositoryFactory.java
d64bb5e688d137d66edef8ef509bf3641c4cf037
[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.util.HashMap;
21 import java.util.Map;
22 import java.util.Properties;
23
24 import javax.jcr.Repository;
25 import javax.jcr.RepositoryException;
26 import javax.jcr.RepositoryFactory;
27 import javax.jcr.Session;
28
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.jackrabbit.commons.JcrUtils;
33 import org.apache.jackrabbit.core.RepositoryImpl;
34 import org.apache.jackrabbit.core.config.RepositoryConfig;
35 import org.apache.jackrabbit.core.config.RepositoryConfigurationParser;
36 import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
37 import org.argeo.ArgeoException;
38 import org.argeo.jcr.ArgeoJcrConstants;
39 import org.argeo.jcr.DefaultRepositoryFactory;
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 public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
49 implements RepositoryFactory, ArgeoJcrConstants {
50
51 private final static Log log = LogFactory
52 .getLog(JackrabbitRepositoryFactory.class);
53
54 private Resource fileRepositoryConfiguration = new ClassPathResource(
55 "/org/argeo/jackrabbit/repository-h2.xml");
56
57 @SuppressWarnings({ "rawtypes", "unchecked" })
58 public Repository getRepository(Map parameters) throws RepositoryException {
59 // check if can be found by alias
60 Repository repository = super.getRepository(parameters);
61 if (repository != null)
62 return repository;
63
64 // check if remote
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 }
83
84 // publish under alias
85 if (parameters.containsKey(JCR_REPOSITORY_ALIAS)) {
86 Properties properties = new Properties();
87 properties.putAll(parameters);
88 String alias = parameters.get(JCR_REPOSITORY_ALIAS).toString();
89 publish(alias, repository, properties);
90 log.info("Registered JCR repository under alias '" + alias
91 + "' with properties " + properties);
92 }
93
94 return repository;
95 }
96
97 protected Repository createRemoteRepository(String uri)
98 throws RepositoryException {
99 Map<String, String> params = new HashMap<String, String>();
100 params.put(JcrUtils.REPOSITORY_URI, uri);
101 Repository repository = new Jcr2davRepositoryFactory()
102 .getRepository(params);
103 if (repository == null)
104 throw new ArgeoException("Remote Davex repository " + uri
105 + " not found");
106 log.info("Initialized remote Jackrabbit repository from uri " + uri);
107 return repository;
108 }
109
110 @SuppressWarnings({ "rawtypes", "unchecked" })
111 protected Repository createFileRepository(final String uri, Map parameters)
112 throws RepositoryException {
113 InputStream configurationIn = null;
114 try {
115 Properties vars = new Properties();
116 vars.putAll(parameters);
117 String dirPath = uri.substring("file:".length());
118 File homeDir = new File(dirPath);
119 if (homeDir.exists() && !homeDir.isDirectory())
120 throw new ArgeoException("Repository home " + dirPath
121 + " is not a directory");
122 if (!homeDir.exists())
123 homeDir.mkdirs();
124 configurationIn = fileRepositoryConfiguration.getInputStream();
125 vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
126 homeDir.getCanonicalPath());
127 RepositoryConfig repositoryConfig = RepositoryConfig.create(
128 new InputSource(configurationIn), vars);
129
130 // TransientRepository repository = new
131 // TransientRepository(repositoryConfig);
132 final RepositoryImpl repository = RepositoryImpl
133 .create(repositoryConfig);
134 Session session = repository.login();
135 // FIXME make it generic
136 org.argeo.jcr.JcrUtils.addPrivilege(session, "/", "ROLE_ADMIN",
137 "jcr:all");
138 org.argeo.jcr.JcrUtils.logoutQuietly(session);
139 Runtime.getRuntime().addShutdownHook(
140 new Thread("Clean JCR repository " + uri) {
141 public void run() {
142 repository.shutdown();
143 log.info("Destroyed repository " + uri);
144 }
145 });
146 log.info("Initialized file Jackrabbit repository from uri " + uri);
147 return repository;
148 } catch (Exception e) {
149 throw new ArgeoException("Cannot create repository " + uri, e);
150 } finally {
151 IOUtils.closeQuietly(configurationIn);
152 }
153 }
154
155 /**
156 * Called after the repository has been initialised. Does nothing by
157 * default.
158 */
159 @SuppressWarnings("rawtypes")
160 protected void postInitialization(Repository repository, Map parameters) {
161
162 }
163
164 public void setFileRepositoryConfiguration(
165 Resource fileRepositoryConfiguration) {
166 this.fileRepositoryConfiguration = fileRepositoryConfiguration;
167 }
168
169 }