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