]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/internal/RepositoryContextsFactory.java
Admin session to the proper content provider workspace
[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.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.util.LangUtils;
24 import org.osgi.service.cm.ManagedServiceFactory;
25
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();
31
32 // private Map<String, RepositoryContext> repositories = new HashMap<String, RepositoryContext>();
33 // private Map<String, Object> pidToCn = new HashMap<String, Object>();
34
35 private RepositoryContext repositoryContext;
36
37 private CmsState cmsState;
38
39 public void init() {
40 Dictionary<String, Object> config = getNodeRepositoryConfig();
41 deployRepository(config);
42 }
43
44 public void destroy() {
45 if (this.repositoryContext != null) {
46 this.repositoryContext.getRepository().shutdown();
47 }
48 // for (String pid : repositories.keySet()) {
49 // try {
50 // RepositoryContext repositoryContext = repositories.get(pid);
51 // // Must start in another thread otherwise shutdown is interrupted
52 // // TODO use an executor?
53 // new Thread(() -> {
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);
61 // }
62 // }
63 }
64
65 // @Override
66 // public String getName() {
67 // return "Jackrabbit repository service factory";
68 // }
69
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());
78 if (value != null) {
79 props.put(repoConf.name(), value);
80 if (log.isDebugEnabled())
81 log.debug("Set node repo configuration " + repoConf.name() + " to " + value);
82 }
83 }
84 props.put(CmsConstants.CN, CmsConstants.NODE_REPOSITORY);
85 return props;
86 }
87
88 private void addDeployProperty(CmsDeployProperty deployProperty, RepoConf repoConf,
89 Dictionary<String, Object> props) {
90 String value = getFrameworkProp(deployProperty.getProperty());
91 if (value != null) {
92 props.put(repoConf.name(), value);
93 }
94
95 }
96
97 // @Override
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);
102
103 if (properties == null)
104 return;
105
106 Object cn = properties.get(CmsConstants.CN);
107 // if (cn != null)
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)
116 // return;
117 // }
118 // }
119
120 try {
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()));
130 if (cn != null) {
131 props.put(CmsConstants.CN, cn);
132 // props.put(NodeConstants.JCR_REPOSITORY_ALIAS, cn);
133 // pidToCn.put(pid, cn);
134 }
135 CmsJcrActivator.registerService(RepositoryContext.class, repositoryContext, props);
136 this.repositoryContext = repositoryContext;
137 } else {
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,
150 // uri.toString());
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)
155 .toString());
156 if (cn != null) {
157 props.put(CmsConstants.CN, cn);
158 // pidToCn.put(pid, cn);
159 }
160 CmsJcrActivator.registerService(Repository.class, repository, props);
161
162 // home
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);
167 }
168 }
169 } catch (RepositoryException | URISyntaxException | IOException e) {
170 throw new IllegalStateException("Cannot create Jackrabbit repository " + properties, e);
171 }
172
173 }
174
175 // @Override
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);
181 // }
182
183 private String getFrameworkProp(String key) {
184 return cmsState.getDeployProperty(key);
185 }
186
187 public void setCmsState(CmsState cmsState) {
188 this.cmsState = cmsState;
189 }
190
191 }