1 package org
.argeo
.cms
.internal
.kernel
;
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
.osgi
.framework
.BundleContext
;
19 import org
.osgi
.framework
.FrameworkUtil
;
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 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
) {
36 Collection
<ServiceReference
<Repository
>> srs
= bundleContext
.getServiceReferences(Repository
.class,
37 "(" + NodeConstants
.CN
+ "=" + alias
+ ")");
39 throw new IllegalArgumentException("No repository with alias " + alias
+ " found in OSGi registry");
40 else if (srs
.size() > 1)
41 throw new IllegalArgumentException(
42 srs
.size() + " repositories with alias " + alias
+ " found in OSGi registry");
43 return bundleContext
.getService(srs
.iterator().next());
44 } catch (InvalidSyntaxException e
) {
45 throw new IllegalArgumentException("Cannot find repository with alias " + alias
, e
);
49 // private void publish(String alias, Repository repository, Properties
51 // if (bundleContext != null) {
52 // // do not modify reference
53 // Hashtable<String, String> props = new Hashtable<String, String>();
54 // props.putAll(props);
55 // props.put(JCR_REPOSITORY_ALIAS, alias);
56 // bundleContext.registerService(Repository.class.getName(), repository,
61 @SuppressWarnings({ "rawtypes" })
62 public Repository
getRepository(Map parameters
) throws RepositoryException
{
63 // // check if can be found by alias
64 // Repository repository = super.getRepository(parameters);
65 // if (repository != null)
69 Repository repository
;
71 if (parameters
.containsKey(RepoConf
.labeledUri
.name()))
72 uri
= parameters
.get(NodeConstants
.LABELED_URI
).toString();
73 else if (parameters
.containsKey(KernelConstants
.JACKRABBIT_REPOSITORY_URI
))
74 uri
= parameters
.get(KernelConstants
.JACKRABBIT_REPOSITORY_URI
).toString();
77 if (uri
.startsWith("http")) {// http, https
78 Object defaultWorkspace
= parameters
.get(RepoConf
.defaultWorkspace
.name());
79 repository
= createRemoteRepository(uri
, defaultWorkspace
!= null ? defaultWorkspace
.toString() : null);
80 } else if (uri
.startsWith("file"))// http, https
81 repository
= createFileRepository(uri
, parameters
);
82 else if (uri
.startsWith("vm")) {
83 // log.warn("URI " + uri + " should have been managed by generic
84 // JCR repository factory");
85 repository
= getRepositoryByAlias(getAliasFromURI(uri
));
87 throw new IllegalArgumentException("Unrecognized URI format " + uri
);
91 else if (parameters
.containsKey(NodeConstants
.CN
)) {
92 // Properties properties = new Properties();
93 // properties.putAll(parameters);
94 String alias
= parameters
.get(NodeConstants
.CN
).toString();
95 // publish(alias, repository, properties);
96 // log.info("Registered JCR repository under alias '" + alias + "'
97 // with properties " + properties);
98 repository
= getRepositoryByAlias(alias
);
100 throw new IllegalArgumentException("Not enough information in " + parameters
);
102 if (repository
== null)
103 throw new IllegalArgumentException("Repository not found " + parameters
);
108 protected Repository
createRemoteRepository(String uri
, String defaultWorkspace
) throws RepositoryException
{
109 Map
<String
, String
> params
= new HashMap
<String
, String
>();
110 params
.put(KernelConstants
.JACKRABBIT_REPOSITORY_URI
, uri
);
111 if (defaultWorkspace
!= null)
112 params
.put(KernelConstants
.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
, defaultWorkspace
);
113 Repository repository
= new Jcr2davRepositoryFactory().getRepository(params
);
114 if (repository
== null)
115 throw new IllegalArgumentException("Remote Davex repository " + uri
+ " not found");
116 log
.info("Initialized remote Jackrabbit repository from uri " + uri
);
120 @SuppressWarnings({ "rawtypes" })
121 protected Repository
createFileRepository(final String uri
, Map parameters
) throws RepositoryException
{
122 throw new UnsupportedOperationException();
123 // InputStream configurationIn = null;
125 // Properties vars = new Properties();
126 // vars.putAll(parameters);
127 // String dirPath = uri.substring("file:".length());
128 // File homeDir = new File(dirPath);
129 // if (homeDir.exists() && !homeDir.isDirectory())
130 // throw new ArgeoJcrException("Repository home " + dirPath + " is not a
132 // if (!homeDir.exists())
134 // configurationIn = fileRepositoryConfiguration.getInputStream();
135 // vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
136 // homeDir.getCanonicalPath());
137 // RepositoryConfig repositoryConfig = RepositoryConfig.create(new
138 // InputSource(configurationIn), vars);
140 // // TransientRepository repository = new
141 // // TransientRepository(repositoryConfig);
142 // final RepositoryImpl repository =
143 // RepositoryImpl.create(repositoryConfig);
144 // Session session = repository.login();
145 // // FIXME make it generic
146 // org.argeo.jcr.JcrUtils.addPrivilege(session, "/", "ROLE_ADMIN",
148 // org.argeo.jcr.JcrUtils.logoutQuietly(session);
149 // Runtime.getRuntime().addShutdownHook(new Thread("Clean JCR repository
151 // public void run() {
152 // repository.shutdown();
153 // log.info("Destroyed repository " + uri);
156 // log.info("Initialized file Jackrabbit repository from uri " + uri);
157 // return repository;
158 // } catch (Exception e) {
159 // throw new ArgeoJcrException("Cannot create repository " + uri, e);
161 // IOUtils.closeQuietly(configurationIn);
165 protected String
getAliasFromURI(String uri
) {
167 URI uriObj
= new URI(uri
);
168 String alias
= uriObj
.getPath();
169 if (alias
.charAt(0) == '/')
170 alias
= alias
.substring(1);
171 if (alias
.charAt(alias
.length() - 1) == '/')
172 alias
= alias
.substring(0, alias
.length() - 1);
174 } catch (URISyntaxException e
) {
175 throw new IllegalArgumentException("Cannot interpret URI " + uri
, e
);
180 * Called after the repository has been initialised. Does nothing by default.
182 @SuppressWarnings("rawtypes")
183 protected void postInitialization(Repository repository
, Map parameters
) {