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