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