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
.jackrabbit
.jcr2dav
.Jcr2davRepositoryFactory
;
14 import org
.argeo
.api
.cms
.CmsConstants
;
15 import org
.argeo
.api
.cms
.CmsLog
;
16 import org
.argeo
.cms
.internal
.jcr
.RepoConf
;
17 import org
.argeo
.cms
.jcr
.internal
.osgi
.CmsJcrActivator
;
18 import org
.osgi
.framework
.BundleContext
;
19 import org
.osgi
.framework
.InvalidSyntaxException
;
20 import org
.osgi
.framework
.ServiceReference
;
23 * OSGi-aware Jackrabbit repository factory which can retrieve/publish
24 * {@link Repository} as OSGi services.
26 public class JcrRepositoryFactory
implements RepositoryFactory
{
27 private final CmsLog log
= CmsLog
.getLog(getClass());
28 // private final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
30 // private Resource fileRepositoryConfiguration = new ClassPathResource(
31 // "/org/argeo/cms/internal/kernel/repository-localfs.xml");
33 protected Repository
getRepositoryByAlias(String alias
) {
34 BundleContext bundleContext
= CmsJcrActivator
.getBundleContext();
35 if (bundleContext
!= null) {
37 Collection
<ServiceReference
<Repository
>> srs
= bundleContext
.getServiceReferences(Repository
.class,
38 "(" + CmsConstants
.CN
+ "=" + alias
+ ")");
40 throw new IllegalArgumentException("No repository with alias " + alias
+ " found in OSGi registry");
41 else if (srs
.size() > 1)
42 throw new IllegalArgumentException(
43 srs
.size() + " repositories with alias " + alias
+ " found in OSGi registry");
44 return bundleContext
.getService(srs
.iterator().next());
45 } catch (InvalidSyntaxException e
) {
46 throw new IllegalArgumentException("Cannot find repository with alias " + alias
, e
);
49 // TODO ability to filter static services
54 // private void publish(String alias, Repository repository, Properties
56 // if (bundleContext != null) {
57 // // do not modify reference
58 // Hashtable<String, String> props = new Hashtable<String, String>();
59 // props.putAll(props);
60 // props.put(JCR_REPOSITORY_ALIAS, alias);
61 // bundleContext.registerService(Repository.class.getName(), repository,
66 @SuppressWarnings({ "rawtypes" })
67 public Repository
getRepository(Map parameters
) throws RepositoryException
{
68 // // check if can be found by alias
69 // Repository repository = super.getRepository(parameters);
70 // if (repository != null)
74 Repository repository
;
76 if (parameters
.containsKey(RepoConf
.labeledUri
.name()))
77 uri
= parameters
.get(CmsConstants
.LABELED_URI
).toString();
78 else if (parameters
.containsKey(KernelConstants
.JACKRABBIT_REPOSITORY_URI
))
79 uri
= parameters
.get(KernelConstants
.JACKRABBIT_REPOSITORY_URI
).toString();
82 if (uri
.startsWith("http")) {// http, https
83 Object defaultWorkspace
= parameters
.get(RepoConf
.defaultWorkspace
.name());
84 repository
= createRemoteRepository(uri
, defaultWorkspace
!= null ? defaultWorkspace
.toString() : null);
85 } else if (uri
.startsWith("file"))// http, https
86 repository
= createFileRepository(uri
, parameters
);
87 else if (uri
.startsWith("vm")) {
88 // log.warn("URI " + uri + " should have been managed by generic
89 // JCR repository factory");
90 repository
= getRepositoryByAlias(getAliasFromURI(uri
));
92 throw new IllegalArgumentException("Unrecognized URI format " + uri
);
96 else if (parameters
.containsKey(CmsConstants
.CN
)) {
97 // Properties properties = new Properties();
98 // properties.putAll(parameters);
99 String alias
= parameters
.get(CmsConstants
.CN
).toString();
100 // publish(alias, repository, properties);
101 // log.info("Registered JCR repository under alias '" + alias + "'
102 // with properties " + properties);
103 repository
= getRepositoryByAlias(alias
);
105 throw new IllegalArgumentException("Not enough information in " + parameters
);
107 if (repository
== null)
108 throw new IllegalArgumentException("Repository not found " + parameters
);
113 protected Repository
createRemoteRepository(String uri
, String defaultWorkspace
) throws RepositoryException
{
114 Map
<String
, String
> params
= new HashMap
<String
, String
>();
115 params
.put(KernelConstants
.JACKRABBIT_REPOSITORY_URI
, uri
);
116 if (defaultWorkspace
!= null)
117 params
.put(KernelConstants
.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
, defaultWorkspace
);
118 Repository repository
= new Jcr2davRepositoryFactory().getRepository(params
);
119 if (repository
== null)
120 throw new IllegalArgumentException("Remote Davex repository " + uri
+ " not found");
121 log
.info("Initialized remote Jackrabbit repository from uri " + uri
);
125 @SuppressWarnings({ "rawtypes" })
126 protected Repository
createFileRepository(final String uri
, Map parameters
) throws RepositoryException
{
127 throw new UnsupportedOperationException();
128 // InputStream configurationIn = null;
130 // Properties vars = new Properties();
131 // vars.putAll(parameters);
132 // String dirPath = uri.substring("file:".length());
133 // File homeDir = new File(dirPath);
134 // if (homeDir.exists() && !homeDir.isDirectory())
135 // throw new ArgeoJcrException("Repository home " + dirPath + " is not a
137 // if (!homeDir.exists())
139 // configurationIn = fileRepositoryConfiguration.getInputStream();
140 // vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
141 // homeDir.getCanonicalPath());
142 // RepositoryConfig repositoryConfig = RepositoryConfig.create(new
143 // InputSource(configurationIn), vars);
145 // // TransientRepository repository = new
146 // // TransientRepository(repositoryConfig);
147 // final RepositoryImpl repository =
148 // RepositoryImpl.create(repositoryConfig);
149 // Session session = repository.login();
150 // // FIXME make it generic
151 // org.argeo.jcr.JcrUtils.addPrivilege(session, "/", "ROLE_ADMIN",
153 // org.argeo.jcr.JcrUtils.logoutQuietly(session);
154 // Runtime.getRuntime().addShutdownHook(new Thread("Clean JCR repository
156 // public void run() {
157 // repository.shutdown();
158 // log.info("Destroyed repository " + uri);
161 // log.info("Initialized file Jackrabbit repository from uri " + uri);
162 // return repository;
163 // } catch (Exception e) {
164 // throw new ArgeoJcrException("Cannot create repository " + uri, e);
166 // IOUtils.closeQuietly(configurationIn);
170 protected String
getAliasFromURI(String uri
) {
172 URI uriObj
= new URI(uri
);
173 String alias
= uriObj
.getPath();
174 if (alias
.charAt(0) == '/')
175 alias
= alias
.substring(1);
176 if (alias
.charAt(alias
.length() - 1) == '/')
177 alias
= alias
.substring(0, alias
.length() - 1);
179 } catch (URISyntaxException e
) {
180 throw new IllegalArgumentException("Cannot interpret URI " + uri
, e
);
185 * Called after the repository has been initialised. Does nothing by default.
187 @SuppressWarnings("rawtypes")
188 protected void postInitialization(Repository repository
, Map parameters
) {