]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java
e994a5cff1fedefec0af58161b189225d9daa627
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / 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.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
28 import org.apache.commons.io.IOUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.jackrabbit.commons.JcrUtils;
32 import org.apache.jackrabbit.core.RepositoryImpl;
33 import org.apache.jackrabbit.core.config.RepositoryConfig;
34 import org.apache.jackrabbit.core.config.RepositoryConfigurationParser;
35 import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
36 import org.argeo.ArgeoException;
37 import org.argeo.jcr.ArgeoJcrConstants;
38 import org.argeo.jcr.DefaultRepositoryFactory;
39 import org.springframework.core.io.ClassPathResource;
40 import org.springframework.core.io.Resource;
41 import org.xml.sax.InputSource;
42
43 /**
44 * Repository factory which can create new repositories and access remote
45 * Jackrabbit repositories
46 */
47 public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
48 implements RepositoryFactory, ArgeoJcrConstants {
49
50 private final static Log log = LogFactory
51 .getLog(JackrabbitRepositoryFactory.class);
52
53 private Resource fileRepositoryConfiguration = new ClassPathResource(
54 "/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 String uri = null;
65 if (parameters.containsKey(JCR_REPOSITORY_URI))
66 uri = parameters.get(JCR_REPOSITORY_URI).toString();
67 else if (parameters.containsKey(JcrUtils.REPOSITORY_URI))
68 uri = parameters.get(JcrUtils.REPOSITORY_URI).toString();
69
70 if (uri != null) {
71 if (uri.startsWith("http"))// http, https
72 repository = createRemoteRepository(uri);
73 else if (uri.startsWith("file"))// http, https
74 repository = createFileRepository(uri, parameters);
75 else if (uri.startsWith("vm")) {
76 log.warn("URI "
77 + uri
78 + " should have been managed by generic JCR repository factory");
79 repository = getRepositoryByAlias(getAliasFromURI(uri));
80 }
81 }
82
83 // publish under alias
84 if (parameters.containsKey(JCR_REPOSITORY_ALIAS)) {
85 Properties properties = new Properties();
86 properties.putAll(parameters);
87 String alias = parameters.get(JCR_REPOSITORY_ALIAS).toString();
88 publish(alias, repository, properties);
89 log.info("Registered JCR repository under alias '" + alias
90 + "' with properties " + properties);
91 }
92
93 return repository;
94 }
95
96 protected Repository createRemoteRepository(String uri)
97 throws RepositoryException {
98 Map<String, String> params = new HashMap<String, String>();
99 params.put(JcrUtils.REPOSITORY_URI, uri);
100 Repository repository = new Jcr2davRepositoryFactory()
101 .getRepository(params);
102 if (repository == null)
103 throw new ArgeoException("Remote Davex repository " + uri
104 + " not found");
105 log.info("Initialized remote Jackrabbit repository from uri " + uri);
106 return repository;
107 }
108
109 @SuppressWarnings({ "rawtypes", "unchecked" })
110 protected Repository createFileRepository(final String uri, Map parameters)
111 throws RepositoryException {
112 InputStream configurationIn = null;
113 try {
114 Properties vars = new Properties();
115 vars.putAll(parameters);
116 String dirPath = uri.substring("file:".length());
117 File homeDir = new File(dirPath);
118 if (homeDir.exists() && !homeDir.isDirectory())
119 throw new ArgeoException("Repository home " + dirPath
120 + " is not a directory");
121 if (!homeDir.exists())
122 homeDir.mkdirs();
123 configurationIn = fileRepositoryConfiguration.getInputStream();
124 vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
125 homeDir.getCanonicalPath());
126 RepositoryConfig repositoryConfig = RepositoryConfig.create(
127 new InputSource(configurationIn), vars);
128
129 // TransientRepository repository = new
130 // TransientRepository(repositoryConfig);
131 final RepositoryImpl repository = RepositoryImpl
132 .create(repositoryConfig);
133 Runtime.getRuntime().addShutdownHook(
134 new Thread("Clean JCR repository " + uri) {
135 public void run() {
136 repository.shutdown();
137 log.info("Destroyed repository " + uri);
138 }
139 });
140 log.info("Initialized file Jackrabbit repository from uri " + uri);
141 return repository;
142 } catch (Exception e) {
143 throw new ArgeoException("Cannot create repository " + uri, e);
144 } finally {
145 IOUtils.closeQuietly(configurationIn);
146 }
147 }
148
149 /**
150 * Called after the repository has been initialised. Does nothing by
151 * default.
152 */
153 @SuppressWarnings("rawtypes")
154 protected void postInitialization(Repository repository, Map parameters) {
155
156 }
157
158 public void setFileRepositoryConfiguration(
159 Resource fileRepositoryConfiguration) {
160 this.fileRepositoryConfiguration = fileRepositoryConfiguration;
161 }
162
163 }