1 package org
.argeo
.cms
.jcr
.internal
;
3 import java
.io
.IOException
;
5 import java
.net
.URISyntaxException
;
6 import java
.util
.Dictionary
;
7 import java
.util
.HashMap
;
8 import java
.util
.Hashtable
;
11 import javax
.jcr
.Repository
;
12 import javax
.jcr
.RepositoryException
;
13 import javax
.jcr
.RepositoryFactory
;
15 import org
.apache
.jackrabbit
.core
.RepositoryContext
;
16 import org
.argeo
.api
.cms
.CmsConstants
;
17 import org
.argeo
.api
.cms
.CmsLog
;
18 import org
.argeo
.api
.cms
.CmsState
;
19 import org
.argeo
.cms
.internal
.jcr
.RepoConf
;
20 import org
.argeo
.cms
.internal
.jcr
.RepositoryBuilder
;
21 import org
.argeo
.cms
.jcr
.internal
.osgi
.CmsJcrActivator
;
22 import org
.argeo
.util
.LangUtils
;
23 import org
.osgi
.service
.cm
.ManagedServiceFactory
;
25 /** A {@link ManagedServiceFactory} creating or referencing JCR repositories. */
26 public class RepositoryContextsFactory
{
27 private final static CmsLog log
= CmsLog
.getLog(RepositoryContextsFactory
.class);
28 // private final BundleContext bc = FrameworkUtil.getBundle(RepositoryServiceFactory.class).getBundleContext();
30 // private Map<String, RepositoryContext> repositories = new HashMap<String, RepositoryContext>();
31 // private Map<String, Object> pidToCn = new HashMap<String, Object>();
33 private RepositoryContext repositoryContext
;
35 private CmsState cmsState
;
38 Dictionary
<String
, Object
> config
= getNodeRepositoryConfig();
39 deployRepository(config
);
42 public void destroy() {
43 if (this.repositoryContext
!= null) {
44 this.repositoryContext
.getRepository().shutdown();
46 // for (String pid : repositories.keySet()) {
48 // RepositoryContext repositoryContext = repositories.get(pid);
49 // // Must start in another thread otherwise shutdown is interrupted
50 // // TODO use an executor?
52 // repositoryContext.getRepository().shutdown();
53 // if (log.isDebugEnabled())
54 // log.debug("Shut down repository " + pid
55 // + (pidToCn.containsKey(pid) ? " (" + pidToCn.get(pid) + ")" : ""));
56 // }, "Shutdown JCR repository " + pid).start();
57 // } catch (Exception e) {
58 // log.error("Error when shutting down Jackrabbit repository " + pid, e);
64 // public String getName() {
65 // return "Jackrabbit repository service factory";
68 /** Override the provided config with the framework properties */
69 private Dictionary
<String
, Object
> getNodeRepositoryConfig() {
70 Dictionary
<String
, Object
> props
= new Hashtable
<String
, Object
>();
71 for (RepoConf repoConf
: RepoConf
.values()) {
72 Object value
= getFrameworkProp(CmsConstants
.NODE_REPO_PROP_PREFIX
+ repoConf
.name());
74 props
.put(repoConf
.name(), value
);
75 if (log
.isDebugEnabled())
76 log
.debug("Set node repo configuration " + repoConf
.name() + " to " + value
);
79 props
.put(CmsConstants
.CN
, CmsConstants
.NODE_REPOSITORY
);
84 // public void updated(String pid, Dictionary<String, ?> properties) throws ConfigurationException {
85 protected void deployRepository(Dictionary
<String
, Object
> properties
) {
86 // if (repositories.containsKey(pid))
87 // throw new IllegalArgumentException("Already a repository registered for " + pid);
89 if (properties
== null)
92 Object cn
= properties
.get(CmsConstants
.CN
);
94 // for (String otherPid : pidToCn.keySet()) {
95 // Object o = pidToCn.get(otherPid);
96 // if (cn.equals(o)) {
97 // RepositoryContext repositoryContext = repositories.remove(otherPid);
98 // repositories.put(pid, repositoryContext);
99 // if (log.isDebugEnabled())
100 // log.debug("Ignoring update of Jackrabbit repository " + cn);
101 // // FIXME perform a proper update (also of the OSGi service)
107 Object labeledUri
= properties
.get(RepoConf
.labeledUri
.name());
108 if (labeledUri
== null) {
109 RepositoryBuilder repositoryBuilder
= new RepositoryBuilder();
110 RepositoryContext repositoryContext
= repositoryBuilder
.createRepositoryContext(properties
);
111 // repositories.put(pid, repositoryContext);
112 // Dictionary<String, Object> props = LangUtils.dict(Constants.SERVICE_PID, pid);
113 Dictionary
<String
, Object
> props
= new Hashtable
<>();
114 // props.put(ArgeoJcrConstants.JCR_REPOSITORY_URI,
115 // properties.get(RepoConf.labeledUri.name()));
117 props
.put(CmsConstants
.CN
, cn
);
118 // props.put(NodeConstants.JCR_REPOSITORY_ALIAS, cn);
119 // pidToCn.put(pid, cn);
121 CmsJcrActivator
.registerService(RepositoryContext
.class, repositoryContext
, props
);
122 this.repositoryContext
= repositoryContext
;
124 Object defaultWorkspace
= properties
.get(RepoConf
.defaultWorkspace
.name());
125 if (defaultWorkspace
== null)
126 defaultWorkspace
= RepoConf
.defaultWorkspace
.getDefault();
127 URI uri
= new URI(labeledUri
.toString());
128 // RepositoryFactory repositoryFactory = bc
129 // .getService(bc.getServiceReference(RepositoryFactory.class));
130 RepositoryFactory repositoryFactory
= CmsJcrActivator
.getService(RepositoryFactory
.class);
131 Map
<String
, String
> parameters
= new HashMap
<String
, String
>();
132 parameters
.put(RepoConf
.labeledUri
.name(), uri
.toString());
133 parameters
.put(RepoConf
.defaultWorkspace
.name(), defaultWorkspace
.toString());
134 Repository repository
= repositoryFactory
.getRepository(parameters
);
135 // Repository repository = NodeUtils.getRepositoryByUri(repositoryFactory,
137 // Dictionary<String, Object> props = LangUtils.dict(Constants.SERVICE_PID, pid);
138 Dictionary
<String
, Object
> props
= new Hashtable
<>();
139 props
.put(RepoConf
.labeledUri
.name(),
140 new URI(uri
.getScheme(), null, uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null)
143 props
.put(CmsConstants
.CN
, cn
);
144 // pidToCn.put(pid, cn);
146 CmsJcrActivator
.registerService(Repository
.class, repository
, props
);
149 if (cn
.equals(CmsConstants
.NODE_REPOSITORY
)) {
150 Dictionary
<String
, Object
> homeProps
= LangUtils
.dict(CmsConstants
.CN
, CmsConstants
.EGO_REPOSITORY
);
151 EgoRepository homeRepository
= new EgoRepository(repository
, true);
152 CmsJcrActivator
.registerService(Repository
.class, homeRepository
, homeProps
);
155 } catch (RepositoryException
| URISyntaxException
| IOException e
) {
156 throw new IllegalStateException("Cannot create Jackrabbit repository " + properties
, e
);
162 // public void deleted(String pid) {
163 // RepositoryContext repositoryContext = repositories.remove(pid);
164 // repositoryContext.getRepository().shutdown();
165 // if (log.isDebugEnabled())
166 // log.debug("Deleted repository " + pid);
169 private String
getFrameworkProp(String key
) {
170 return cmsState
.getDeployProperty(key
);
173 public void setCmsState(CmsState cmsState
) {
174 this.cmsState
= cmsState
;