1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.lang
.management
.ManagementFactory
;
4 import java
.security
.PrivilegedAction
;
5 import java
.util
.HashMap
;
8 import javax
.jcr
.Repository
;
9 import javax
.jcr
.RepositoryFactory
;
10 import javax
.security
.auth
.Subject
;
11 import javax
.transaction
.TransactionManager
;
12 import javax
.transaction
.TransactionSynchronizationRegistry
;
13 import javax
.transaction
.UserTransaction
;
15 import org
.apache
.commons
.logging
.Log
;
16 import org
.apache
.commons
.logging
.LogFactory
;
17 import org
.apache
.jackrabbit
.util
.TransientFileFactory
;
18 import org
.argeo
.ArgeoException
;
19 import org
.argeo
.cms
.CmsException
;
20 import org
.argeo
.cms
.internal
.transaction
.SimpleTransactionManager
;
21 import org
.argeo
.jackrabbit
.OsgiJackrabbitRepositoryFactory
;
22 import org
.argeo
.jcr
.ArgeoJcrConstants
;
23 import org
.argeo
.security
.core
.InternalAuthentication
;
24 import org
.eclipse
.equinox
.http
.servlet
.ExtendedHttpService
;
25 import org
.osgi
.framework
.BundleContext
;
26 import org
.osgi
.framework
.ServiceEvent
;
27 import org
.osgi
.framework
.ServiceListener
;
28 import org
.osgi
.framework
.ServiceReference
;
29 import org
.osgi
.util
.tracker
.ServiceTracker
;
30 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
33 * Argeo CMS Kernel. Responsible for :
36 * <li>provisioning</li>
37 * <li>transaction</li>
39 * <li>local and remote file systems access</li>
43 final class Kernel
implements ServiceListener
{
44 private final static Log log
= LogFactory
.getLog(Kernel
.class);
46 private final BundleContext bundleContext
= Activator
.getBundleContext();
47 private final NodeSecurity nodeSecurity
;
49 ThreadGroup threadGroup
= new ThreadGroup(Kernel
.class.getSimpleName());
52 private SimpleTransactionManager transactionManager
;
53 private OsgiJackrabbitRepositoryFactory repositoryFactory
;
54 private NodeHttp nodeHttp
;
55 private KernelThread kernelThread
;
58 nodeSecurity
= new NodeSecurity(bundleContext
);
62 Subject
.doAs(nodeSecurity
.getKernelSubject(),
63 new PrivilegedAction
<Void
>() {
74 private void doInit() {
75 ClassLoader currentContextCl
= Thread
.currentThread()
76 .getContextClassLoader();
77 Thread
.currentThread().setContextClassLoader(
78 Kernel
.class.getClassLoader());
79 long begin
= System
.currentTimeMillis();
80 InternalAuthentication initAuth
= new InternalAuthentication(
81 KernelConstants
.DEFAULT_SECURITY_KEY
);
82 SecurityContextHolder
.getContext().setAuthentication(initAuth
);
86 transactionManager
= new SimpleTransactionManager();
89 node
= new JackrabbitNode(bundleContext
);
91 // JCR repository factory
92 repositoryFactory
= new OsgiJackrabbitRepositoryFactory();
95 nodeSecurity
.getUserAdmin().setTransactionManager(
99 ExtendedHttpService httpService
= waitForHttpService();
100 nodeHttp
= new NodeHttp(httpService
, node
);
103 kernelThread
= new KernelThread(this);
104 kernelThread
.setContextClassLoader(Kernel
.class.getClassLoader());
105 kernelThread
.start();
107 // Publish services to OSGi
108 bundleContext
.registerService(TransactionManager
.class,
109 transactionManager
, null);
110 bundleContext
.registerService(UserTransaction
.class,
111 transactionManager
, null);
112 bundleContext
.registerService(
113 TransactionSynchronizationRegistry
.class,
114 transactionManager
.getTransactionSynchronizationRegistry(),
116 nodeSecurity
.publish();
117 node
.publish(repositoryFactory
);
118 bundleContext
.registerService(RepositoryFactory
.class,
119 repositoryFactory
, null);
121 bundleContext
.addServiceListener(Kernel
.this);
122 } catch (Exception e
) {
123 log
.error("Cannot initialize Argeo CMS", e
);
124 throw new ArgeoException("Cannot initialize", e
);
126 Thread
.currentThread().setContextClassLoader(currentContextCl
);
129 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
130 log
.info("## ARGEO CMS UP in " + (jvmUptime
/ 1000) + "."
131 + (jvmUptime
% 1000) + "s ##");
132 long initDuration
= System
.currentTimeMillis() - begin
;
133 if (log
.isTraceEnabled())
134 log
.trace("Kernel initialization took " + initDuration
+ "ms");
135 directorsCut(initDuration
);
139 long begin
= System
.currentTimeMillis();
141 kernelThread
.destroyAndJoin();
143 if (nodeHttp
!= null)
145 // if (nodeSecurity != null)
146 // nodeSecurity.destroy();
150 bundleContext
.removeServiceListener(this);
152 // Clean hanging threads from Jackrabbit
153 TransientFileFactory
.shutdown();
155 // Clean hanging Gogo shell thread
156 new GogoShellKiller().start();
158 nodeSecurity
.destroy();
159 long duration
= System
.currentTimeMillis() - begin
;
160 log
.info("## ARGEO CMS DOWN in " + (duration
/ 1000) + "."
161 + (duration
% 1000) + "s ##");
165 public void serviceChanged(ServiceEvent event
) {
166 ServiceReference
<?
> sr
= event
.getServiceReference();
167 Object jcrRepoAlias
= sr
168 .getProperty(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
);
169 if (jcrRepoAlias
!= null) {// JCR repository
170 String alias
= jcrRepoAlias
.toString();
171 Repository repository
= (Repository
) bundleContext
.getService(sr
);
172 Map
<String
, Object
> props
= new HashMap
<String
, Object
>();
173 for (String key
: sr
.getPropertyKeys())
174 props
.put(key
, sr
.getProperty(key
));
175 if (ServiceEvent
.REGISTERED
== event
.getType()) {
177 repositoryFactory
.register(repository
, props
);
178 nodeHttp
.registerRepositoryServlets(alias
, repository
);
179 } catch (Exception e
) {
180 throw new CmsException("Could not publish JCR repository "
183 } else if (ServiceEvent
.UNREGISTERING
== event
.getType()) {
184 repositoryFactory
.unregister(repository
, props
);
185 nodeHttp
.unregisterRepositoryServlets(alias
);
191 private ExtendedHttpService
waitForHttpService() {
192 final ServiceTracker
<ExtendedHttpService
, ExtendedHttpService
> st
= new ServiceTracker
<ExtendedHttpService
, ExtendedHttpService
>(
193 bundleContext
, ExtendedHttpService
.class, null);
195 ExtendedHttpService httpService
;
197 httpService
= st
.waitForService(1000);
198 } catch (InterruptedException e
) {
202 if (httpService
== null)
203 throw new CmsException("Could not find "
204 + ExtendedHttpService
.class + " service.");
208 final private static void directorsCut(long initDuration
) {
209 // final long ms = 128l + (long) (Math.random() * 128d);
210 long ms
= initDuration
/ 100;
211 log
.info("Spend " + ms
+ "ms"
212 + " reflecting on the progress brought to mankind"
213 + " by Free Software...");
214 long beginNano
= System
.nanoTime();
217 } catch (InterruptedException e
) {
220 long durationNano
= System
.nanoTime() - beginNano
;
221 final double M
= 1000d
* 1000d
;
222 double sleepAccuracy
= ((double) durationNano
) / (ms
* M
);
223 if (log
.isDebugEnabled())
224 log
.debug("Sleep accuracy: "
225 + String
.format("%.2f", 100 - (sleepAccuracy
* 100 - 100))
229 /** Workaround for blocking Gogo shell by system shutdown. */
230 private class GogoShellKiller
extends Thread
{
232 public GogoShellKiller() {
233 super("Gogo shell killer");
239 ThreadGroup rootTg
= getRootThreadGroup(null);
240 Thread gogoShellThread
= findGogoShellThread(rootTg
);
241 if (gogoShellThread
== null)
243 while (getNonDaemonCount(rootTg
) > 2) {
246 } catch (InterruptedException e
) {
250 gogoShellThread
= findGogoShellThread(rootTg
);
251 if (gogoShellThread
== null)
257 private static ThreadGroup
getRootThreadGroup(ThreadGroup tg
) {
259 tg
= Thread
.currentThread().getThreadGroup();
260 if (tg
.getParent() == null)
263 return getRootThreadGroup(tg
.getParent());
266 private static int getNonDaemonCount(ThreadGroup rootThreadGroup
) {
267 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
268 rootThreadGroup
.enumerate(threads
);
269 int nonDameonCount
= 0;
270 for (Thread t
: threads
)
273 return nonDameonCount
;
276 private static Thread
findGogoShellThread(ThreadGroup rootThreadGroup
) {
277 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
278 rootThreadGroup
.enumerate(threads
, true);
279 for (Thread thread
: threads
) {
280 if (thread
.getName().equals("Gogo shell"))