2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.cms
.internal
.kernel
;
19 import java
.net
.URISyntaxException
;
20 import java
.util
.Collection
;
21 import java
.util
.HashMap
;
24 import javax
.jcr
.Repository
;
25 import javax
.jcr
.RepositoryException
;
26 import javax
.jcr
.RepositoryFactory
;
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
.jcr
.ArgeoJcrException
;
32 import org
.argeo
.node
.NodeConstants
;
33 import org
.osgi
.framework
.BundleContext
;
34 import org
.osgi
.framework
.FrameworkUtil
;
35 import org
.osgi
.framework
.InvalidSyntaxException
;
36 import org
.osgi
.framework
.ServiceReference
;
37 import org
.springframework
.core
.io
.Resource
;
40 * OSGi-aware Jackrabbit repository factory which can retrieve/publish
41 * {@link Repository} as OSGi services.
43 class NodeRepositoryFactory
implements RepositoryFactory
{
44 private final Log log
= LogFactory
.getLog(getClass());
45 private final BundleContext bundleContext
= FrameworkUtil
.getBundle(getClass()).getBundleContext();
47 // private Resource fileRepositoryConfiguration = new ClassPathResource(
48 // "/org/argeo/cms/internal/kernel/repository-localfs.xml");
50 protected Repository
getRepositoryByAlias(String alias
) {
52 Collection
<ServiceReference
<Repository
>> srs
= bundleContext
.getServiceReferences(Repository
.class,
53 "(" + NodeConstants
.CN
+ "=" + alias
+ ")");
55 throw new ArgeoJcrException("No repository with alias " + alias
+ " found in OSGi registry");
56 else if (srs
.size() > 1)
57 throw new ArgeoJcrException(
58 srs
.size() + " repositories with alias " + alias
+ " found in OSGi registry");
59 return bundleContext
.getService(srs
.iterator().next());
60 } catch (InvalidSyntaxException e
) {
61 throw new ArgeoJcrException("Cannot find repository with alias " + alias
, e
);
65 // private void publish(String alias, Repository repository, Properties
67 // if (bundleContext != null) {
68 // // do not modify reference
69 // Hashtable<String, String> props = new Hashtable<String, String>();
70 // props.putAll(props);
71 // props.put(JCR_REPOSITORY_ALIAS, alias);
72 // bundleContext.registerService(Repository.class.getName(), repository,
77 @SuppressWarnings({ "rawtypes" })
78 public Repository
getRepository(Map parameters
) throws RepositoryException
{
79 // // check if can be found by alias
80 // Repository repository = super.getRepository(parameters);
81 // if (repository != null)
85 Repository repository
;
87 if (parameters
.containsKey(NodeConstants
.LABELED_URI
))
88 uri
= parameters
.get(NodeConstants
.LABELED_URI
).toString();
89 else if (parameters
.containsKey(KernelConstants
.JACKRABBIT_REPOSITORY_URI
))
90 uri
= parameters
.get(KernelConstants
.JACKRABBIT_REPOSITORY_URI
).toString();
93 if (uri
.startsWith("http"))// http, https
94 repository
= createRemoteRepository(uri
);
95 else if (uri
.startsWith("file"))// http, https
96 repository
= createFileRepository(uri
, parameters
);
97 else if (uri
.startsWith("vm")) {
98 // log.warn("URI " + uri + " should have been managed by generic
99 // JCR repository factory");
100 repository
= getRepositoryByAlias(getAliasFromURI(uri
));
102 throw new ArgeoJcrException("Unrecognized URI format " + uri
);
106 else if (parameters
.containsKey(NodeConstants
.CN
)) {
107 // Properties properties = new Properties();
108 // properties.putAll(parameters);
109 String alias
= parameters
.get(NodeConstants
.CN
).toString();
110 // publish(alias, repository, properties);
111 // log.info("Registered JCR repository under alias '" + alias + "'
112 // with properties " + properties);
113 repository
= getRepositoryByAlias(alias
);
115 throw new ArgeoJcrException("Not enough information in " + parameters
);
117 if (repository
== null)
118 throw new ArgeoJcrException("Repository not found " + parameters
);
123 protected Repository
createRemoteRepository(String uri
) throws RepositoryException
{
124 Map
<String
, String
> params
= new HashMap
<String
, String
>();
125 params
.put(KernelConstants
.JACKRABBIT_REPOSITORY_URI
, uri
);
126 Repository repository
= new Jcr2davRepositoryFactory().getRepository(params
);
127 if (repository
== null)
128 throw new ArgeoJcrException("Remote Davex repository " + uri
+ " not found");
129 log
.info("Initialized remote Jackrabbit repository from uri " + uri
);
133 @SuppressWarnings({ "rawtypes" })
134 protected Repository
createFileRepository(final String uri
, Map parameters
) throws RepositoryException
{
135 throw new UnsupportedOperationException();
136 // InputStream configurationIn = null;
138 // Properties vars = new Properties();
139 // vars.putAll(parameters);
140 // String dirPath = uri.substring("file:".length());
141 // File homeDir = new File(dirPath);
142 // if (homeDir.exists() && !homeDir.isDirectory())
143 // throw new ArgeoJcrException("Repository home " + dirPath + " is not a
145 // if (!homeDir.exists())
147 // configurationIn = fileRepositoryConfiguration.getInputStream();
148 // vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
149 // homeDir.getCanonicalPath());
150 // RepositoryConfig repositoryConfig = RepositoryConfig.create(new
151 // InputSource(configurationIn), vars);
153 // // TransientRepository repository = new
154 // // TransientRepository(repositoryConfig);
155 // final RepositoryImpl repository =
156 // RepositoryImpl.create(repositoryConfig);
157 // Session session = repository.login();
158 // // FIXME make it generic
159 // org.argeo.jcr.JcrUtils.addPrivilege(session, "/", "ROLE_ADMIN",
161 // org.argeo.jcr.JcrUtils.logoutQuietly(session);
162 // Runtime.getRuntime().addShutdownHook(new Thread("Clean JCR repository
164 // public void run() {
165 // repository.shutdown();
166 // log.info("Destroyed repository " + uri);
169 // log.info("Initialized file Jackrabbit repository from uri " + uri);
170 // return repository;
171 // } catch (Exception e) {
172 // throw new ArgeoJcrException("Cannot create repository " + uri, e);
174 // IOUtils.closeQuietly(configurationIn);
178 protected String
getAliasFromURI(String uri
) {
180 URI uriObj
= new URI(uri
);
181 String alias
= uriObj
.getPath();
182 if (alias
.charAt(0) == '/')
183 alias
= alias
.substring(1);
184 if (alias
.charAt(alias
.length() - 1) == '/')
185 alias
= alias
.substring(0, alias
.length() - 1);
187 } catch (URISyntaxException e
) {
188 throw new ArgeoJcrException("Cannot interpret URI " + uri
, e
);
193 * Called after the repository has been initialised. Does nothing by
196 @SuppressWarnings("rawtypes")
197 protected void postInitialization(Repository repository
, Map parameters
) {
201 public void setFileRepositoryConfiguration(Resource fileRepositoryConfiguration
) {
202 // this.fileRepositoryConfiguration = fileRepositoryConfiguration;