]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/Kernel.java
ad6ca084a345b20fa4f621b0bca45e719edf4cce
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / Kernel.java
1 package org.argeo.cms.internal.kernel;
2
3 import java.lang.management.ManagementFactory;
4
5 import javax.jcr.RepositoryFactory;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.apache.jackrabbit.util.TransientFileFactory;
10 import org.argeo.ArgeoException;
11 import org.argeo.jackrabbit.OsgiJackrabbitRepositoryFactory;
12 import org.argeo.security.core.InternalAuthentication;
13 import org.osgi.framework.BundleContext;
14 import org.springframework.security.core.context.SecurityContextHolder;
15
16 /**
17 * Argeo CMS Kernel. Responsible for :
18 * <ul>
19 * <li>security</li>
20 * <li>provisioning</li>
21 * <li>transaction</li>
22 * <li>logging</li>
23 * <li>local and remote file systems access</li>
24 * <li>OS access</li>
25 * </ul>
26 */
27 final class Kernel {
28 private final static Log log = LogFactory.getLog(Kernel.class);
29 // private static final String PROP_WORKBENCH_AUTOSTART =
30 // "org.eclipse.rap.workbenchAutostart";
31
32 private final BundleContext bundleContext;
33
34 private JackrabbitNode node;
35 private RepositoryFactory repositoryFactory;
36 private NodeSecurity nodeSecurity;
37 private NodeHttp nodeHttp;
38
39 Kernel(BundleContext bundleContext) {
40 this.bundleContext = bundleContext;
41 }
42
43 void init() {
44 ClassLoader currentContextCl = Thread.currentThread()
45 .getContextClassLoader();
46 // We use the CMS bundle classloader during initialization
47 Thread.currentThread().setContextClassLoader(
48 Kernel.class.getClassLoader());
49
50 long begin = System.currentTimeMillis();
51 InternalAuthentication initAuth = new InternalAuthentication(
52 KernelConstants.DEFAULT_SECURITY_KEY);
53 SecurityContextHolder.getContext().setAuthentication(initAuth);
54
55 try {
56 node = new JackrabbitNode(bundleContext);
57 repositoryFactory = new OsgiJackrabbitRepositoryFactory();
58 nodeSecurity = new NodeSecurity(bundleContext, node);
59 nodeHttp = new NodeHttp(bundleContext, node, nodeSecurity);
60
61 // Publish services to OSGi
62 nodeSecurity.publish();
63 node.publish();
64 bundleContext.registerService(RepositoryFactory.class,
65 repositoryFactory, null);
66 nodeHttp.publish();
67 } catch (Exception e) {
68 log.error("Cannot initialize Argeo CMS", e);
69 throw new ArgeoException("Cannot initialize", e);
70 } finally {
71 Thread.currentThread().setContextClassLoader(currentContextCl);
72 }
73
74 long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
75 log.info("## ARGEO CMS UP in " + (jvmUptime / 1000) + "."
76 + (jvmUptime % 1000) + "s ##");
77 long initDuration = System.currentTimeMillis() - begin;
78 if (log.isTraceEnabled())
79 log.trace("Kernel initialization took " + initDuration + "ms");
80 directorsCut(initDuration);
81 }
82
83 void destroy() {
84 long begin = System.currentTimeMillis();
85
86 nodeHttp = null;
87 nodeSecurity.destroy();
88 node.destroy();
89
90 // Clean hanging threads from Jackrabbit
91 TransientFileFactory.shutdown();
92
93 long duration = System.currentTimeMillis() - begin;
94 log.info("## ARGEO CMS DOWN in " + (duration / 1000) + "."
95 + (duration % 1000) + "s ##");
96 }
97
98 private void directorsCut(long initDuration) {
99 // final long ms = 128l + (long) (Math.random() * 128d);
100 long ms = initDuration / 10;
101 log.info("Spend " + ms + "ms"
102 + " reflecting on the progress brought to mankind"
103 + " by Free Software...");
104 long beginNano = System.nanoTime();
105 try {
106 Thread.sleep(ms, 0);
107 } catch (InterruptedException e) {
108 // silent
109 }
110 long durationNano = System.nanoTime() - beginNano;
111 final double M = 1000d * 1000d;
112 double sleepAccuracy = ((double) durationNano) / (ms * M);
113 if (log.isDebugEnabled())
114 log.debug("Sleep accuracy: "
115 + String.format("%.2f", sleepAccuracy * 100) + " %");
116 }
117
118 }