1 package org
.argeo
.cms
.jcr
.internal
;
4 import java
.net
.URISyntaxException
;
5 import java
.util
.Collection
;
6 import java
.util
.HashMap
;
9 import javax
.jcr
.Repository
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.RepositoryFactory
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.apache
.jackrabbit
.jcr2dav
.Jcr2davRepositoryFactory
;
16 import org
.argeo
.api
.NodeConstants
;
17 import org
.argeo
.cms
.internal
.jcr
.RepoConf
;
18 import org
.argeo
.cms
.jcr
.internal
.osgi
.CmsJcrActivator
;
19 import org
.osgi
.framework
.BundleContext
;
20 import org
.osgi
.framework
.InvalidSyntaxException
;
21 import org
.osgi
.framework
.ServiceReference
;
24 * OSGi-aware Jackrabbit repository factory which can retrieve/publish
25 * {@link Repository} as OSGi services.
27 public class NodeRepositoryFactory
implements RepositoryFactory
{
28 private final Log log
= LogFactory
.getLog(getClass());
29 // private final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
31 // private Resource fileRepositoryConfiguration = new ClassPathResource(
32 // "/org/argeo/cms/internal/kernel/repository-localfs.xml");
34 protected Repository
getRepositoryByAlias(String alias
) {
35 BundleContext bundleContext
= CmsJcrActivator
.getBundleContext();
36 if (bundleContext
!= null) {
38 Collection
<ServiceReference
<Repository
>> srs
= bundleContext
.getServiceReferences(Repository
.class,
39 "(" + NodeConstants
.CN
+ "=" + alias
+ ")");
41 throw new IllegalArgumentException("No repository with alias " + alias
+ " found in OSGi registry");
42 else if (srs
.size() > 1)
43 throw new IllegalArgumentException(
44 srs
.size() + " repositories with alias " + alias
+ " found in OSGi registry");
45 return bundleContext
.getService(srs
.iterator().next());
46 } catch (InvalidSyntaxException e
) {
47 throw new IllegalArgumentException("Cannot find repository with alias " + alias
, e
);
50 // TODO ability to filter static services
55 // private void publish(String alias, Repository repository, Properties
57 // if (bundleContext != null) {
58 // // do not modify reference
59 // Hashtable<String, String> props = new Hashtable<String, String>();
60 // props.putAll(props);
61 // props.put(JCR_REPOSITORY_ALIAS, alias);
62 // bundleContext.registerService(Repository.class.getName(), repository,
67 @SuppressWarnings({ "rawtypes" })
68 public Repository
getRepository(Map parameters
) throws RepositoryException
{
69 // // check if can be found by alias
70 // Repository repository = super.getRepository(parameters);
71 // if (repository != null)
75 Repository repository
;
77 if (parameters
.containsKey(RepoConf
.labeledUri
.name()))
78 uri
= parameters
.get(NodeConstants
.LABELED_URI
).toString();
79 else if (parameters
.containsKey(KernelConstants
.JACKRABBIT_REPOSITORY_URI
))
80 uri
= parameters
.get(KernelConstants
.JACKRABBIT_REPOSITORY_URI
).toString();
83 if (uri
.startsWith("http")) {// http, https
84 Object defaultWorkspace
= parameters
.get(RepoConf
.defaultWorkspace
.name());
85 repository
= createRemoteRepository(uri
, defaultWorkspace
!= null ? defaultWorkspace
.toString() : null);
86 } else if (uri
.startsWith("file"))// http, https
87 repository
= createFileRepository(uri
, parameters
);
88 else if (uri
.startsWith("vm")) {
89 // log.warn("URI " + uri + " should have been managed by generic
90 // JCR repository factory");
91 repository
= getRepositoryByAlias(getAliasFromURI(uri
));
93 throw new IllegalArgumentException("Unrecognized URI format " + uri
);
97 else if (parameters
.containsKey(NodeConstants
.CN
)) {
98 // Properties properties = new Properties();
99 // properties.putAll(parameters);
100 String alias
= parameters
.get(NodeConstants
.CN
).toString();
101 // publish(alias, repository, properties);
102 // log.info("Registered JCR repository under alias '" + alias + "'
103 // with properties " + properties);
104 repository
= getRepositoryByAlias(alias
);
106 throw new IllegalArgumentException("Not enough information in " + parameters
);
108 if (repository
== null)
109 throw new IllegalArgumentException("Repository not found " + parameters
);
114 protected Repository
createRemoteRepository(String uri
, String defaultWorkspace
) throws RepositoryException
{
115 Map
<String
, String
> params
= new HashMap
<String
, String
>();
116 params
.put(KernelConstants
.JACKRABBIT_REPOSITORY_URI
, uri
);
117 if (defaultWorkspace
!= null)
118 params
.put(KernelConstants
.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
, defaultWorkspace
);
119 Repository repository
= new Jcr2davRepositoryFactory().getRepository(params
);
120 if (repository
== null)
121 throw new IllegalArgumentException("Remote Davex repository " + uri
+ " not found");
122 log
.info("Initialized remote Jackrabbit repository from uri " + uri
);
126 @SuppressWarnings({ "rawtypes" })
127 protected Repository
createFileRepository(final String uri
, Map parameters
) throws RepositoryException
{
128 throw new UnsupportedOperationException();
129 // InputStream configurationIn = null;
131 // Properties vars = new Properties();
132 // vars.putAll(parameters);
133 // String dirPath = uri.substring("file:".length());
134 // File homeDir = new File(dirPath);
135 // if (homeDir.exists() && !homeDir.isDirectory())
136 // throw new ArgeoJcrException("Repository home " + dirPath + " is not a
138 // if (!homeDir.exists())
140 // configurationIn = fileRepositoryConfiguration.getInputStream();
141 // vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
142 // homeDir.getCanonicalPath());
143 // RepositoryConfig repositoryConfig = RepositoryConfig.create(new
144 // InputSource(configurationIn), vars);
146 // // TransientRepository repository = new
147 // // TransientRepository(repositoryConfig);
148 // final RepositoryImpl repository =
149 // RepositoryImpl.create(repositoryConfig);
150 // Session session = repository.login();
151 // // FIXME make it generic
152 // org.argeo.jcr.JcrUtils.addPrivilege(session, "/", "ROLE_ADMIN",
154 // org.argeo.jcr.JcrUtils.logoutQuietly(session);
155 // Runtime.getRuntime().addShutdownHook(new Thread("Clean JCR repository
157 // public void run() {
158 // repository.shutdown();
159 // log.info("Destroyed repository " + uri);
162 // log.info("Initialized file Jackrabbit repository from uri " + uri);
163 // return repository;
164 // } catch (Exception e) {
165 // throw new ArgeoJcrException("Cannot create repository " + uri, e);
167 // IOUtils.closeQuietly(configurationIn);
171 protected String
getAliasFromURI(String uri
) {
173 URI uriObj
= new URI(uri
);
174 String alias
= uriObj
.getPath();
175 if (alias
.charAt(0) == '/')
176 alias
= alias
.substring(1);
177 if (alias
.charAt(alias
.length() - 1) == '/')
178 alias
= alias
.substring(0, alias
.length() - 1);
180 } catch (URISyntaxException e
) {
181 throw new IllegalArgumentException("Cannot interpret URI " + uri
, e
);
186 * Called after the repository has been initialised. Does nothing by default.
188 @SuppressWarnings("rawtypes")
189 protected void postInitialization(Repository repository
, Map parameters
) {