1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.lang
.management
.ManagementFactory
;
4 import java
.util
.HashMap
;
7 import javax
.jcr
.Repository
;
8 import javax
.jcr
.RepositoryFactory
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.apache
.jackrabbit
.util
.TransientFileFactory
;
13 import org
.argeo
.ArgeoException
;
14 import org
.argeo
.cms
.CmsException
;
15 import org
.argeo
.jackrabbit
.OsgiJackrabbitRepositoryFactory
;
16 import org
.argeo
.jcr
.ArgeoJcrConstants
;
17 import org
.argeo
.security
.core
.InternalAuthentication
;
18 import org
.eclipse
.equinox
.http
.servlet
.ExtendedHttpService
;
19 import org
.osgi
.framework
.BundleContext
;
20 import org
.osgi
.framework
.ServiceEvent
;
21 import org
.osgi
.framework
.ServiceListener
;
22 import org
.osgi
.framework
.ServiceReference
;
23 import org
.osgi
.util
.tracker
.ServiceTracker
;
24 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
27 * Argeo CMS Kernel. Responsible for :
30 * <li>provisioning</li>
31 * <li>transaction</li>
33 * <li>local and remote file systems access</li>
37 final class Kernel
implements ServiceListener
{
38 private final static Log log
= LogFactory
.getLog(Kernel
.class);
40 private final BundleContext bundleContext
= Activator
.getBundleContext();
42 ThreadGroup threadGroup
= new ThreadGroup(Kernel
.class.getSimpleName());
44 OsgiJackrabbitRepositoryFactory repositoryFactory
;
45 NodeSecurity nodeSecurity
;
47 private KernelThread kernelThread
;
50 ClassLoader currentContextCl
= Thread
.currentThread()
51 .getContextClassLoader();
52 Thread
.currentThread().setContextClassLoader(
53 Kernel
.class.getClassLoader());
54 long begin
= System
.currentTimeMillis();
55 InternalAuthentication initAuth
= new InternalAuthentication(
56 KernelConstants
.DEFAULT_SECURITY_KEY
);
57 SecurityContextHolder
.getContext().setAuthentication(initAuth
);
61 node
= new JackrabbitNode(bundleContext
);
63 // JCR repository factory
64 repositoryFactory
= new OsgiJackrabbitRepositoryFactory();
67 nodeSecurity
= new NodeSecurity(bundleContext
, node
);
70 ExtendedHttpService httpService
= waitForHttpService();
71 nodeHttp
= new NodeHttp(httpService
, node
, nodeSecurity
);
74 kernelThread
= new KernelThread(this);
75 kernelThread
.setContextClassLoader(Kernel
.class.getClassLoader());
78 // Publish services to OSGi
79 nodeSecurity
.publish();
80 node
.publish(repositoryFactory
);
81 bundleContext
.registerService(RepositoryFactory
.class,
82 repositoryFactory
, null);
84 bundleContext
.addServiceListener(Kernel
.this);
85 } catch (Exception e
) {
86 log
.error("Cannot initialize Argeo CMS", e
);
87 throw new ArgeoException("Cannot initialize", e
);
89 Thread
.currentThread().setContextClassLoader(currentContextCl
);
92 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
93 log
.info("## ARGEO CMS UP in " + (jvmUptime
/ 1000) + "."
94 + (jvmUptime
% 1000) + "s ##");
95 long initDuration
= System
.currentTimeMillis() - begin
;
96 if (log
.isTraceEnabled())
97 log
.trace("Kernel initialization took " + initDuration
+ "ms");
98 directorsCut(initDuration
);
102 long begin
= System
.currentTimeMillis();
104 kernelThread
.destroyAndJoin();
106 if (nodeHttp
!= null)
108 if (nodeSecurity
!= null)
109 nodeSecurity
.destroy();
113 bundleContext
.removeServiceListener(this);
115 // Clean hanging threads from Jackrabbit
116 TransientFileFactory
.shutdown();
118 long duration
= System
.currentTimeMillis() - begin
;
119 log
.info("## ARGEO CMS DOWN in " + (duration
/ 1000) + "."
120 + (duration
% 1000) + "s ##");
124 public void serviceChanged(ServiceEvent event
) {
125 ServiceReference
<?
> sr
= event
.getServiceReference();
126 Object jcrRepoAlias
= sr
127 .getProperty(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
);
128 if (jcrRepoAlias
!= null) {// JCR repository
129 String alias
= jcrRepoAlias
.toString();
130 Repository repository
= (Repository
) bundleContext
.getService(sr
);
131 Map
<String
, Object
> props
= new HashMap
<String
, Object
>();
132 for (String key
: sr
.getPropertyKeys())
133 props
.put(key
, sr
.getProperty(key
));
134 if (ServiceEvent
.REGISTERED
== event
.getType()) {
136 repositoryFactory
.register(repository
, props
);
137 nodeHttp
.registerRepositoryServlets(alias
, repository
);
138 } catch (Exception e
) {
139 throw new CmsException("Could not publish JCR repository "
142 } else if (ServiceEvent
.UNREGISTERING
== event
.getType()) {
143 repositoryFactory
.unregister(repository
, props
);
144 nodeHttp
.unregisterRepositoryServlets(alias
);
150 private ExtendedHttpService
waitForHttpService() {
151 final ServiceTracker
<ExtendedHttpService
, ExtendedHttpService
> st
= new ServiceTracker
<ExtendedHttpService
, ExtendedHttpService
>(
152 bundleContext
, ExtendedHttpService
.class, null);
154 ExtendedHttpService httpService
;
156 httpService
= st
.waitForService(1000);
157 } catch (InterruptedException e
) {
161 if (httpService
== null)
162 throw new CmsException("Could not find "
163 + ExtendedHttpService
.class + " service.");
167 final private static void directorsCut(long initDuration
) {
168 // final long ms = 128l + (long) (Math.random() * 128d);
169 long ms
= initDuration
/ 100;
170 log
.info("Spend " + ms
+ "ms"
171 + " reflecting on the progress brought to mankind"
172 + " by Free Software...");
173 long beginNano
= System
.nanoTime();
176 } catch (InterruptedException e
) {
179 long durationNano
= System
.nanoTime() - beginNano
;
180 final double M
= 1000d
* 1000d
;
181 double sleepAccuracy
= ((double) durationNano
) / (ms
* M
);
182 if (log
.isDebugEnabled())
183 log
.debug("Sleep accuracy: "
184 + String
.format("%.2f", 100 - (sleepAccuracy
* 100 - 100))