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
.CmsDeployProperty
;
20 import org
.argeo
.cms
.internal
.jcr
.RepoConf
;
21 import org
.argeo
.cms
.internal
.jcr
.RepositoryBuilder
;
22 import org
.argeo
.cms
.jcr
.internal
.osgi
.CmsJcrActivator
;
23 import org
.argeo
.cms
.util
.LangUtils
;
24 import org
.osgi
.service
.cm
.ManagedServiceFactory
;
26 /** A {@link ManagedServiceFactory} creating or referencing JCR repositories. */
27 public class RepositoryContextsFactory
{
28 private final static CmsLog log
= CmsLog
.getLog(RepositoryContextsFactory
.class);
29 private final static String NODE_REPO_PROP_PREFIX
= "argeo.node.repo.";
30 // private final BundleContext bc = FrameworkUtil.getBundle(RepositoryServiceFactory.class).getBundleContext();
32 // private Map<String, RepositoryContext> repositories = new HashMap<String, RepositoryContext>();
33 // private Map<String, Object> pidToCn = new HashMap<String, Object>();
35 private RepositoryContext repositoryContext
;
37 private CmsState cmsState
;
40 Dictionary
<String
, Object
> config
= getNodeRepositoryConfig();
41 deployRepository(config
);
44 public void destroy() {
45 if (this.repositoryContext
!= null) {
46 this.repositoryContext
.getRepository().shutdown();
48 // for (String pid : repositories.keySet()) {
50 // RepositoryContext repositoryContext = repositories.get(pid);
51 // // Must start in another thread otherwise shutdown is interrupted
52 // // TODO use an executor?
54 // repositoryContext.getRepository().shutdown();
55 // if (log.isDebugEnabled())
56 // log.debug("Shut down repository " + pid
57 // + (pidToCn.containsKey(pid) ? " (" + pidToCn.get(pid) + ")" : ""));
58 // }, "Shutdown JCR repository " + pid).start();
59 // } catch (Exception e) {
60 // log.error("Error when shutting down Jackrabbit repository " + pid, e);
66 // public String getName() {
67 // return "Jackrabbit repository service factory";
70 /** Override the provided config with the framework properties */
71 private Dictionary
<String
, Object
> getNodeRepositoryConfig() {
72 Dictionary
<String
, Object
> props
= new Hashtable
<String
, Object
>();
73 addDeployProperty(CmsDeployProperty
.DB_URL
, RepoConf
.dburl
, props
);
74 addDeployProperty(CmsDeployProperty
.DB_USER
, RepoConf
.dbuser
, props
);
75 addDeployProperty(CmsDeployProperty
.DB_PASSWORD
, RepoConf
.dbpassword
, props
);
76 for (RepoConf repoConf
: RepoConf
.values()) {
77 Object value
= getFrameworkProp(NODE_REPO_PROP_PREFIX
+ repoConf
.name());
79 props
.put(repoConf
.name(), value
);
80 if (log
.isDebugEnabled())
81 log
.debug("Set node repo configuration " + repoConf
.name() + " to " + value
);
84 props
.put(CmsConstants
.CN
, CmsConstants
.NODE_REPOSITORY
);
88 private void addDeployProperty(CmsDeployProperty deployProperty
, RepoConf repoConf
,
89 Dictionary
<String
, Object
> props
) {
90 String value
= getFrameworkProp(deployProperty
.getProperty());
92 props
.put(repoConf
.name(), value
);
98 // public void updated(String pid, Dictionary<String, ?> properties) throws ConfigurationException {
99 protected void deployRepository(Dictionary
<String
, Object
> properties
) {
100 // if (repositories.containsKey(pid))
101 // throw new IllegalArgumentException("Already a repository registered for " + pid);
103 if (properties
== null)
106 Object cn
= properties
.get(CmsConstants
.CN
);
108 // for (String otherPid : pidToCn.keySet()) {
109 // Object o = pidToCn.get(otherPid);
110 // if (cn.equals(o)) {
111 // RepositoryContext repositoryContext = repositories.remove(otherPid);
112 // repositories.put(pid, repositoryContext);
113 // if (log.isDebugEnabled())
114 // log.debug("Ignoring update of Jackrabbit repository " + cn);
115 // // FIXME perform a proper update (also of the OSGi service)
121 Object labeledUri
= properties
.get(RepoConf
.labeledUri
.name());
122 if (labeledUri
== null) {
123 RepositoryBuilder repositoryBuilder
= new RepositoryBuilder();
124 RepositoryContext repositoryContext
= repositoryBuilder
.createRepositoryContext(properties
);
125 // repositories.put(pid, repositoryContext);
126 // Dictionary<String, Object> props = LangUtils.dict(Constants.SERVICE_PID, pid);
127 Dictionary
<String
, Object
> props
= new Hashtable
<>();
128 // props.put(ArgeoJcrConstants.JCR_REPOSITORY_URI,
129 // properties.get(RepoConf.labeledUri.name()));
131 props
.put(CmsConstants
.CN
, cn
);
132 // props.put(NodeConstants.JCR_REPOSITORY_ALIAS, cn);
133 // pidToCn.put(pid, cn);
135 CmsJcrActivator
.registerService(RepositoryContext
.class, repositoryContext
, props
);
136 this.repositoryContext
= repositoryContext
;
138 Object defaultWorkspace
= properties
.get(RepoConf
.defaultWorkspace
.name());
139 if (defaultWorkspace
== null)
140 defaultWorkspace
= RepoConf
.defaultWorkspace
.getDefault();
141 URI uri
= new URI(labeledUri
.toString());
142 // RepositoryFactory repositoryFactory = bc
143 // .getService(bc.getServiceReference(RepositoryFactory.class));
144 RepositoryFactory repositoryFactory
= CmsJcrActivator
.getService(RepositoryFactory
.class);
145 Map
<String
, String
> parameters
= new HashMap
<String
, String
>();
146 parameters
.put(RepoConf
.labeledUri
.name(), uri
.toString());
147 parameters
.put(RepoConf
.defaultWorkspace
.name(), defaultWorkspace
.toString());
148 Repository repository
= repositoryFactory
.getRepository(parameters
);
149 // Repository repository = NodeUtils.getRepositoryByUri(repositoryFactory,
151 // Dictionary<String, Object> props = LangUtils.dict(Constants.SERVICE_PID, pid);
152 Dictionary
<String
, Object
> props
= new Hashtable
<>();
153 props
.put(RepoConf
.labeledUri
.name(),
154 new URI(uri
.getScheme(), null, uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null)
157 props
.put(CmsConstants
.CN
, cn
);
158 // pidToCn.put(pid, cn);
160 CmsJcrActivator
.registerService(Repository
.class, repository
, props
);
163 if (cn
.equals(CmsConstants
.NODE_REPOSITORY
)) {
164 Dictionary
<String
, Object
> homeProps
= LangUtils
.dict(CmsConstants
.CN
, CmsConstants
.EGO_REPOSITORY
);
165 EgoRepository homeRepository
= new EgoRepository(repository
, true);
166 CmsJcrActivator
.registerService(Repository
.class, homeRepository
, homeProps
);
169 } catch (RepositoryException
| URISyntaxException
| IOException e
) {
170 throw new IllegalStateException("Cannot create Jackrabbit repository " + properties
, e
);
176 // public void deleted(String pid) {
177 // RepositoryContext repositoryContext = repositories.remove(pid);
178 // repositoryContext.getRepository().shutdown();
179 // if (log.isDebugEnabled())
180 // log.debug("Deleted repository " + pid);
183 private String
getFrameworkProp(String key
) {
184 return cmsState
.getDeployProperty(key
);
187 public void setCmsState(CmsState cmsState
) {
188 this.cmsState
= cmsState
;