]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/internal/RepositoryContextsFactory.java
11e9a9e92bc0e064cf2fa9d3483251afcc7fd600
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.jcr / src / org / argeo / cms / jcr / internal / RepositoryContextsFactory.java
1 package org.argeo.cms.jcr.internal;
2
3 import java.io.IOException;
4 import java.net.URI;
5 import java.net.URISyntaxException;
6 import java.util.Dictionary;
7 import java.util.HashMap;
8 import java.util.Hashtable;
9 import java.util.Map;
10
11 import javax.jcr.Repository;
12 import javax.jcr.RepositoryException;
13 import javax.jcr.RepositoryFactory;
14
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;
24
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();
29
30 // private Map<String, RepositoryContext> repositories = new HashMap<String, RepositoryContext>();
31 // private Map<String, Object> pidToCn = new HashMap<String, Object>();
32
33 private RepositoryContext repositoryContext;
34
35 private CmsState cmsState;
36
37 public void init() {
38 Dictionary<String, Object> config = getNodeRepositoryConfig();
39 deployRepository(config);
40 }
41
42 public void destroy() {
43 if (this.repositoryContext != null) {
44 this.repositoryContext.getRepository().shutdown();
45 }
46 // for (String pid : repositories.keySet()) {
47 // try {
48 // RepositoryContext repositoryContext = repositories.get(pid);
49 // // Must start in another thread otherwise shutdown is interrupted
50 // // TODO use an executor?
51 // new Thread(() -> {
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);
59 // }
60 // }
61 }
62
63 // @Override
64 // public String getName() {
65 // return "Jackrabbit repository service factory";
66 // }
67
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());
73 if (value != null) {
74 props.put(repoConf.name(), value);
75 if (log.isDebugEnabled())
76 log.debug("Set node repo configuration " + repoConf.name() + " to " + value);
77 }
78 }
79 props.put(CmsConstants.CN, CmsConstants.NODE_REPOSITORY);
80 return props;
81 }
82
83 // @Override
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);
88
89 if (properties == null)
90 return;
91
92 Object cn = properties.get(CmsConstants.CN);
93 // if (cn != null)
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)
102 // return;
103 // }
104 // }
105
106 try {
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()));
116 if (cn != null) {
117 props.put(CmsConstants.CN, cn);
118 // props.put(NodeConstants.JCR_REPOSITORY_ALIAS, cn);
119 // pidToCn.put(pid, cn);
120 }
121 CmsJcrActivator.registerService(RepositoryContext.class, repositoryContext, props);
122 this.repositoryContext = repositoryContext;
123 } else {
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,
136 // uri.toString());
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)
141 .toString());
142 if (cn != null) {
143 props.put(CmsConstants.CN, cn);
144 // pidToCn.put(pid, cn);
145 }
146 CmsJcrActivator.registerService(Repository.class, repository, props);
147
148 // home
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);
153 }
154 }
155 } catch (RepositoryException | URISyntaxException | IOException e) {
156 throw new IllegalStateException("Cannot create Jackrabbit repository " + properties, e);
157 }
158
159 }
160
161 // @Override
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);
167 // }
168
169 private String getFrameworkProp(String key) {
170 return cmsState.getDeployProperty(key);
171 }
172
173 public void setCmsState(CmsState cmsState) {
174 this.cmsState = cmsState;
175 }
176
177 }