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