]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsDeployment.java
7dedfe811db420e682a09730db55407870b5b880
[lgpl/argeo-commons.git] / CmsDeployment.java
1 package org.argeo.cms.internal.kernel;
2
3 import static org.argeo.node.DataModelNamespace.CMS_DATA_MODEL_NAMESPACE;
4
5 import java.io.InputStreamReader;
6 import java.io.Reader;
7 import java.lang.management.ManagementFactory;
8 import java.net.URL;
9 import java.util.HashSet;
10 import java.util.Hashtable;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14
15 import javax.jcr.Repository;
16 import javax.jcr.Session;
17 import javax.security.auth.callback.CallbackHandler;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.jackrabbit.commons.cnd.CndImporter;
22 import org.apache.jackrabbit.core.RepositoryContext;
23 import org.argeo.cms.CmsException;
24 import org.argeo.cms.internal.http.NodeHttp;
25 import org.argeo.jcr.JcrUtils;
26 import org.argeo.node.DataModelNamespace;
27 import org.argeo.node.NodeConstants;
28 import org.argeo.node.NodeDeployment;
29 import org.argeo.node.NodeState;
30 import org.argeo.node.security.CryptoKeyring;
31 import org.argeo.osgi.useradmin.UserAdminConf;
32 import org.argeo.util.LangUtils;
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.Constants;
36 import org.osgi.framework.FrameworkUtil;
37 import org.osgi.framework.ServiceReference;
38 import org.osgi.framework.wiring.BundleCapability;
39 import org.osgi.framework.wiring.BundleWire;
40 import org.osgi.framework.wiring.BundleWiring;
41 import org.osgi.service.cm.Configuration;
42 import org.osgi.service.cm.ConfigurationAdmin;
43 import org.osgi.service.cm.ManagedService;
44 import org.osgi.service.useradmin.UserAdmin;
45 import org.osgi.util.tracker.ServiceTracker;
46
47 public class CmsDeployment implements NodeDeployment {
48 private final static String LEGACY_JCR_REPOSITORY_ALIAS = "argeo.jcr.repository.alias";
49
50 private final Log log = LogFactory.getLog(getClass());
51 private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
52
53 private DeployConfig deployConfig;
54 private HomeRepository homeRepository;
55
56 private Long availableSince;
57
58 private final boolean cleanState;
59
60 private NodeHttp nodeHttp;
61
62 // Readiness
63 private boolean nodeAvailable = false;
64 private boolean userAdminAvailable = false;
65 private boolean httpExpected = false;
66 private boolean httpAvailable = false;
67
68 public CmsDeployment() {
69 ServiceReference<NodeState> nodeStateSr = bc.getServiceReference(NodeState.class);
70 if (nodeStateSr == null)
71 throw new CmsException("No node state available");
72
73 NodeState nodeState = bc.getService(nodeStateSr);
74 cleanState = nodeState.isClean();
75
76 nodeHttp = new NodeHttp();
77 initTrackers();
78 }
79
80 private void initTrackers() {
81 new ServiceTracker<NodeHttp, NodeHttp>(bc, NodeHttp.class, null) {
82
83 @Override
84 public NodeHttp addingService(ServiceReference<NodeHttp> reference) {
85 httpAvailable = true;
86 checkReadiness();
87 return super.addingService(reference);
88 }
89 }.open();
90 new RepositoryContextStc().open();
91 new ServiceTracker<UserAdmin, UserAdmin>(bc, UserAdmin.class, null) {
92 @Override
93 public UserAdmin addingService(ServiceReference<UserAdmin> reference) {
94 userAdminAvailable = true;
95 checkReadiness();
96 return super.addingService(reference);
97 }
98 }.open();
99 new ServiceTracker<ConfigurationAdmin, ConfigurationAdmin>(bc, ConfigurationAdmin.class, null) {
100 @Override
101 public ConfigurationAdmin addingService(ServiceReference<ConfigurationAdmin> reference) {
102 ConfigurationAdmin configurationAdmin = bc.getService(reference);
103 deployConfig = new DeployConfig(configurationAdmin, cleanState);
104 httpExpected = deployConfig.getProps(KernelConstants.JETTY_FACTORY_PID, "default") != null;
105 try {
106 // Configuration[] configs = configurationAdmin
107 // .listConfigurations("(service.factoryPid=" +
108 // NodeConstants.NODE_REPOS_FACTORY_PID + ")");
109 // for (Configuration config : configs) {
110 // Object cn = config.getProperties().get(NodeConstants.CN);
111 // if (log.isDebugEnabled())
112 // log.debug("Standalone repo cn: " + cn);
113 // }
114 Configuration[] configs = configurationAdmin
115 .listConfigurations("(service.factoryPid=" + NodeConstants.NODE_USER_ADMIN_PID + ")");
116
117 boolean hasDomain = false;
118 for (Configuration config : configs) {
119 Object realm = config.getProperties().get(UserAdminConf.realm.name());
120 if (realm != null) {
121 log.debug("Found realm: " + realm);
122 hasDomain = true;
123 }
124 }
125 if (hasDomain) {
126 loadIpaJaasConfiguration();
127 }
128 } catch (Exception e) {
129 throw new CmsException("Cannot initialize config", e);
130 }
131 return super.addingService(reference);
132 }
133 }.open();
134 }
135
136 private void loadIpaJaasConfiguration() {
137 if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) {
138 String jaasConfig = KernelConstants.JAAS_CONFIG_IPA;
139 URL url = getClass().getClassLoader().getResource(jaasConfig);
140 KernelUtils.setJaasConfiguration(url);
141 log.debug("Set IPA JAAS configuration.");
142 }
143 }
144
145 public void shutdown() {
146 if (nodeHttp != null)
147 nodeHttp.destroy();
148 if (deployConfig != null)
149 deployConfig.save();
150 }
151
152 private void checkReadiness() {
153 if (nodeAvailable && userAdminAvailable && (httpExpected ? httpAvailable : true)) {
154 String data = KernelUtils.getFrameworkProp(KernelUtils.OSGI_INSTANCE_AREA);
155 String state = KernelUtils.getFrameworkProp(KernelUtils.OSGI_CONFIGURATION_AREA);
156 availableSince = System.currentTimeMillis();
157 long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
158 String jvmUptimeStr = " in " + (jvmUptime / 1000) + "." + (jvmUptime % 1000) + "s";
159 log.info("## ARGEO NODE AVAILABLE" + (log.isDebugEnabled() ? jvmUptimeStr : "") + " ##");
160 if (log.isDebugEnabled()) {
161 log.debug("## state: " + state);
162 if (data != null)
163 log.debug("## data: " + data);
164 }
165 long begin = bc.getService(bc.getServiceReference(NodeState.class)).getAvailableSince();
166 long initDuration = System.currentTimeMillis() - begin;
167 if (log.isTraceEnabled())
168 log.trace("Kernel initialization took " + initDuration + "ms");
169 tributeToFreeSoftware(initDuration);
170 }
171 }
172
173 final private void tributeToFreeSoftware(long initDuration) {
174 if (log.isTraceEnabled()) {
175 long ms = initDuration / 100;
176 log.trace("Spend " + ms + "ms" + " reflecting on the progress brought to mankind" + " by Free Software...");
177 long beginNano = System.nanoTime();
178 try {
179 Thread.sleep(ms, 0);
180 } catch (InterruptedException e) {
181 // silent
182 }
183 long durationNano = System.nanoTime() - beginNano;
184 final double M = 1000d * 1000d;
185 double sleepAccuracy = ((double) durationNano) / (ms * M);
186 log.trace("Sleep accuracy: " + String.format("%.2f", 100 - (sleepAccuracy * 100 - 100)) + " %");
187 }
188 }
189
190 private void prepareNodeRepository(Repository deployedNodeRepository) {
191 if (availableSince != null) {
192 throw new CmsException("Deployment is already available");
193 }
194
195 // home
196 prepareDataModel(KernelUtils.openAdminSession(deployedNodeRepository));
197 }
198
199 private void prepareHomeRepository(Repository deployedRepository) {
200 Hashtable<String, String> regProps = new Hashtable<String, String>();
201 regProps.put(NodeConstants.CN, NodeConstants.HOME);
202 regProps.put(LEGACY_JCR_REPOSITORY_ALIAS, NodeConstants.HOME);
203 homeRepository = new HomeRepository(deployedRepository);
204 // register
205 bc.registerService(Repository.class, homeRepository, regProps);
206
207 new ServiceTracker<CallbackHandler, CallbackHandler>(bc, CallbackHandler.class, null) {
208
209 @Override
210 public CallbackHandler addingService(ServiceReference<CallbackHandler> reference) {
211 NodeKeyRing nodeKeyring = new NodeKeyRing(homeRepository);
212 CallbackHandler callbackHandler = bc.getService(reference);
213 nodeKeyring.setDefaultCallbackHandler(callbackHandler);
214 bc.registerService(LangUtils.names(CryptoKeyring.class, ManagedService.class), nodeKeyring,
215 LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_KEYRING_PID));
216 return callbackHandler;
217 }
218
219 }.open();
220 }
221
222 /** Session is logged out. */
223 private void prepareDataModel(Session adminSession) {
224 try {
225 Set<String> processed = new HashSet<String>();
226 bundles: for (Bundle bundle : bc.getBundles()) {
227 BundleWiring wiring = bundle.adapt(BundleWiring.class);
228 if (wiring == null)
229 continue bundles;
230 processWiring(adminSession, wiring, processed);
231 }
232 } finally {
233 JcrUtils.logoutQuietly(adminSession);
234 }
235 }
236
237 private void processWiring(Session adminSession, BundleWiring wiring, Set<String> processed) {
238 // recursively process requirements first
239 List<BundleWire> requiredWires = wiring.getRequiredWires(CMS_DATA_MODEL_NAMESPACE);
240 for (BundleWire wire : requiredWires) {
241 processWiring(adminSession, wire.getProviderWiring(), processed);
242 // registerCnd(adminSession, wire.getCapability(), processed);
243 }
244 List<BundleCapability> capabilities = wiring.getCapabilities(CMS_DATA_MODEL_NAMESPACE);
245 for (BundleCapability capability : capabilities) {
246 registerDataModelCapability(adminSession, capability, processed);
247 }
248 }
249
250 private void registerDataModelCapability(Session adminSession, BundleCapability capability, Set<String> processed) {
251 Map<String, Object> attrs = capability.getAttributes();
252 String name = (String) attrs.get(DataModelNamespace.CAPABILITY_NAME_ATTRIBUTE);
253 if (processed.contains(name)) {
254 if (log.isTraceEnabled())
255 log.trace("Data model " + name + " has already been processed");
256 return;
257 }
258
259 // CND
260 String path = (String) attrs.get(DataModelNamespace.CAPABILITY_CND_ATTRIBUTE);
261 if (path != null) {
262 URL url = capability.getRevision().getBundle().getResource(path);
263 if (url == null)
264 throw new CmsException("No data model '" + name + "' found under path " + path);
265 try (Reader reader = new InputStreamReader(url.openStream())) {
266 CndImporter.registerNodeTypes(reader, adminSession, true);
267 processed.add(name);
268 if (log.isDebugEnabled())
269 log.debug("Registered CND " + url);
270 } catch (Exception e) {
271 throw new CmsException("Cannot import CND " + url, e);
272 }
273 }
274
275 if (!asBoolean((String) attrs.get(DataModelNamespace.CAPABILITY_ABSTRACT_ATTRIBUTE))) {
276 Hashtable<String, Object> properties = new Hashtable<>();
277 properties.put(LEGACY_JCR_REPOSITORY_ALIAS, name);
278 properties.put(NodeConstants.CN, name);
279 if (name.equals(NodeConstants.NODE))
280 properties.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
281 LocalRepository localRepository = new LocalRepository(adminSession.getRepository(), capability);
282 bc.registerService(Repository.class, localRepository, properties);
283 if (log.isDebugEnabled())
284 log.debug("Published data model " + name);
285 }
286 }
287
288 private boolean asBoolean(String value) {
289 if (value == null)
290 return false;
291 switch (value) {
292 case "true":
293 return true;
294 case "false":
295 return false;
296 default:
297 throw new CmsException("Unsupported value for attribute " + DataModelNamespace.CAPABILITY_ABSTRACT_ATTRIBUTE
298 + ": " + value);
299 }
300 }
301
302 @Override
303 public Long getAvailableSince() {
304 return availableSince;
305 }
306
307 private class RepositoryContextStc extends ServiceTracker<RepositoryContext, RepositoryContext> {
308
309 public RepositoryContextStc() {
310 super(bc, RepositoryContext.class, null);
311 }
312
313 @Override
314 public RepositoryContext addingService(ServiceReference<RepositoryContext> reference) {
315 RepositoryContext nodeRepo = bc.getService(reference);
316 Object cn = reference.getProperty(NodeConstants.CN);
317 if (cn != null) {
318 if (cn.equals(NodeConstants.NODE)) {
319 prepareNodeRepository(nodeRepo.getRepository());
320 prepareHomeRepository(nodeRepo.getRepository());
321 nodeAvailable = true;
322 checkReadiness();
323 } else {
324 // TODO standalone
325 }
326 }
327 return nodeRepo;
328 }
329
330 @Override
331 public void modifiedService(ServiceReference<RepositoryContext> reference, RepositoryContext service) {
332 }
333
334 @Override
335 public void removedService(ServiceReference<RepositoryContext> reference, RepositoryContext service) {
336 }
337
338 }
339
340 }