1 package org
.argeo
.cms
.internal
.kernel
;
3 import static bitronix
.tm
.TransactionManagerServices
.getTransactionManager
;
4 import static bitronix
.tm
.TransactionManagerServices
.getTransactionSynchronizationRegistry
;
5 import static org
.argeo
.cms
.internal
.kernel
.KernelUtils
.getFrameworkProp
;
6 import static org
.argeo
.cms
.internal
.kernel
.KernelUtils
.getOsgiInstancePath
;
7 import static org
.argeo
.jcr
.ArgeoJcrConstants
.ALIAS_NODE
;
8 import static org
.argeo
.jcr
.ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
;
9 import static org
.osgi
.framework
.Constants
.FRAMEWORK_UUID
;
12 import java
.lang
.management
.ManagementFactory
;
13 import java
.security
.PrivilegedAction
;
14 import java
.util
.HashMap
;
15 import java
.util
.Hashtable
;
18 import javax
.jcr
.Repository
;
19 import javax
.jcr
.RepositoryFactory
;
20 import javax
.security
.auth
.Subject
;
21 import javax
.transaction
.TransactionManager
;
22 import javax
.transaction
.TransactionSynchronizationRegistry
;
23 import javax
.transaction
.UserTransaction
;
25 import org
.apache
.commons
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.apache
.jackrabbit
.util
.TransientFileFactory
;
28 import org
.argeo
.ArgeoException
;
29 import org
.argeo
.ArgeoLogger
;
30 import org
.argeo
.cms
.CmsException
;
31 import org
.argeo
.jackrabbit
.OsgiJackrabbitRepositoryFactory
;
32 import org
.argeo
.jcr
.ArgeoJcrConstants
;
33 import org
.eclipse
.equinox
.http
.servlet
.ExtendedHttpService
;
34 import org
.osgi
.framework
.BundleContext
;
35 import org
.osgi
.framework
.ServiceEvent
;
36 import org
.osgi
.framework
.ServiceListener
;
37 import org
.osgi
.framework
.ServiceReference
;
38 import org
.osgi
.framework
.ServiceRegistration
;
39 import org
.osgi
.service
.useradmin
.UserAdmin
;
41 import bitronix
.tm
.BitronixTransactionManager
;
42 import bitronix
.tm
.BitronixTransactionSynchronizationRegistry
;
43 import bitronix
.tm
.Configuration
;
44 import bitronix
.tm
.TransactionManagerServices
;
47 * Argeo CMS Kernel. Responsible for :
50 * <li>provisioning</li>
51 * <li>transaction</li>
53 * <li>local and remote file systems access</li>
57 final class Kernel
implements KernelConstants
, ServiceListener
{
61 private ServiceRegistration
<ArgeoLogger
> loggerReg
;
62 private ServiceRegistration
<TransactionManager
> tmReg
;
63 private ServiceRegistration
<UserTransaction
> utReg
;
64 private ServiceRegistration
<TransactionSynchronizationRegistry
> tsrReg
;
65 private ServiceRegistration
<Repository
> repositoryReg
;
66 private ServiceRegistration
<RepositoryFactory
> repositoryFactoryReg
;
67 private ServiceRegistration
<UserAdmin
> userAdminReg
;
70 * SERVICES IMPLEMENTATIONS
72 private NodeLogger logger
;
73 private BitronixTransactionManager transactionManager
;
74 private BitronixTransactionSynchronizationRegistry transactionSynchronizationRegistry
;
75 private OsgiJackrabbitRepositoryFactory repositoryFactory
;
76 NodeRepository repository
;
77 private NodeUserAdmin userAdmin
;
80 private final static Log log
= LogFactory
.getLog(Kernel
.class);
81 ThreadGroup threadGroup
= new ThreadGroup(Kernel
.class.getSimpleName());
82 private final BundleContext bc
= Activator
.getBundleContext();
83 private final NodeSecurity nodeSecurity
;
84 private DataHttp dataHttp
;
85 private KernelThread kernelThread
;
88 nodeSecurity
= new NodeSecurity();
92 Subject
.doAs(nodeSecurity
.getKernelSubject(),
93 new PrivilegedAction
<Void
>() {
102 private void doInit() {
103 ClassLoader currentContextCl
= Thread
.currentThread()
104 .getContextClassLoader();
105 Thread
.currentThread().setContextClassLoader(
106 Kernel
.class.getClassLoader());
107 long begin
= System
.currentTimeMillis();
110 // Initialise services
111 logger
= new NodeLogger();
112 // transactionManager = new SimpleTransactionManager();
113 initBitronixTransactionManager();
114 repository
= new NodeRepository(bc
);
115 repositoryFactory
= new OsgiJackrabbitRepositoryFactory();
116 userAdmin
= new NodeUserAdmin(transactionManager
, repository
);
119 ServiceReference
<ExtendedHttpService
> sr
= bc
120 .getServiceReference(ExtendedHttpService
.class);
125 kernelThread
= new KernelThread(this);
126 kernelThread
.setContextClassLoader(Kernel
.class.getClassLoader());
127 kernelThread
.start();
129 // Publish services to OSGi
131 } catch (Exception e
) {
132 log
.error("Cannot initialize Argeo CMS", e
);
133 throw new ArgeoException("Cannot initialize", e
);
135 Thread
.currentThread().setContextClassLoader(currentContextCl
);
138 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
139 log
.info("## ARGEO CMS UP in " + (jvmUptime
/ 1000) + "."
140 + (jvmUptime
% 1000) + "s ##");
141 long initDuration
= System
.currentTimeMillis() - begin
;
142 if (log
.isTraceEnabled())
143 log
.trace("Kernel initialization took " + initDuration
+ "ms");
144 directorsCut(initDuration
);
147 private void initBitronixTransactionManager() {
148 Configuration tmConf
= TransactionManagerServices
.getConfiguration();
149 tmConf
.setServerId(getFrameworkProp(FRAMEWORK_UUID
));
151 File tmBaseDir
= new File(getFrameworkProp(TRANSACTIONS_HOME
,
152 getOsgiInstancePath("transactions")));
153 File tmDir1
= new File(tmBaseDir
, "btm1");
155 tmConf
.setLogPart1Filename(new File(tmDir1
, tmDir1
.getName() + ".tlog")
157 File tmDir2
= new File(tmBaseDir
, "btm2");
159 tmConf
.setLogPart2Filename(new File(tmDir2
, tmDir2
.getName() + ".tlog")
161 transactionManager
= getTransactionManager();
162 transactionSynchronizationRegistry
= getTransactionSynchronizationRegistry();
165 private void publish() {
166 // Listen to service publication (also ours)
167 bc
.addServiceListener(Kernel
.this);
170 loggerReg
= bc
.registerService(ArgeoLogger
.class, logger
, null);
172 tmReg
= bc
.registerService(TransactionManager
.class,
173 transactionManager
, null);
174 utReg
= bc
.registerService(UserTransaction
.class, transactionManager
,
176 tsrReg
= bc
.registerService(TransactionSynchronizationRegistry
.class,
177 transactionSynchronizationRegistry
, null);
179 userAdminReg
= bc
.registerService(UserAdmin
.class, userAdmin
,
180 userAdmin
.currentState());
182 Hashtable
<String
, String
> regProps
= new Hashtable
<String
, String
>();
183 regProps
.put(JCR_REPOSITORY_ALIAS
, ALIAS_NODE
);
184 repositoryReg
= bc
.registerService(Repository
.class, repository
,
186 repositoryFactoryReg
= bc
.registerService(RepositoryFactory
.class,
187 repositoryFactory
, null);
191 long begin
= System
.currentTimeMillis();
194 kernelThread
.destroyAndJoin();
196 if (dataHttp
!= null)
198 if (userAdmin
!= null)
200 if (repository
!= null)
201 repository
.destroy();
202 if (transactionManager
!= null)
203 transactionManager
.shutdown();
205 bc
.removeServiceListener(this);
207 // Clean hanging threads from Jackrabbit
208 TransientFileFactory
.shutdown();
210 // Clean hanging Gogo shell thread
211 new GogoShellKiller().start();
213 nodeSecurity
.destroy();
214 long duration
= System
.currentTimeMillis() - begin
;
215 log
.info("## ARGEO CMS DOWN in " + (duration
/ 1000) + "."
216 + (duration
% 1000) + "s ##");
219 private void unpublish() {
220 userAdminReg
.unregister();
221 repositoryFactoryReg
.unregister();
222 repositoryReg
.unregister();
226 loggerReg
.unregister();
230 public void serviceChanged(ServiceEvent event
) {
231 ServiceReference
<?
> sr
= event
.getServiceReference();
232 Object service
= bc
.getService(sr
);
233 if (service
instanceof Repository
) {
234 Object jcrRepoAlias
= sr
235 .getProperty(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
);
236 if (jcrRepoAlias
!= null) {// JCR repository
237 String alias
= jcrRepoAlias
.toString();
238 Repository repository
= (Repository
) bc
.getService(sr
);
239 Map
<String
, Object
> props
= new HashMap
<String
, Object
>();
240 for (String key
: sr
.getPropertyKeys())
241 props
.put(key
, sr
.getProperty(key
));
242 if (ServiceEvent
.REGISTERED
== event
.getType()) {
244 repositoryFactory
.register(repository
, props
);
245 dataHttp
.registerRepositoryServlets(alias
, repository
);
246 } catch (Exception e
) {
247 throw new CmsException(
248 "Could not publish JCR repository " + alias
, e
);
250 } else if (ServiceEvent
.UNREGISTERING
== event
.getType()) {
251 repositoryFactory
.unregister(repository
, props
);
252 dataHttp
.unregisterRepositoryServlets(alias
);
255 } else if (service
instanceof ExtendedHttpService
) {
256 if (ServiceEvent
.REGISTERED
== event
.getType()) {
258 } else if (ServiceEvent
.UNREGISTERING
== event
.getType()) {
265 private void addHttpService(ServiceReference
<?
> sr
) {
266 // for (String key : sr.getPropertyKeys())
267 // log.debug(key + "=" + sr.getProperty(key));
268 ExtendedHttpService httpService
= (ExtendedHttpService
) bc
270 // TODO find constants
271 Object httpPort
= sr
.getProperty("http.port");
272 Object httpsPort
= sr
.getProperty("https.port");
273 dataHttp
= new DataHttp(httpService
, repository
);
274 if (log
.isDebugEnabled())
275 log
.debug("HTTP " + httpPort
276 + (httpsPort
!= null ?
" - HTTPS " + httpsPort
: ""));
279 // private ExtendedHttpService waitForHttpService() {
280 // final ServiceTracker<ExtendedHttpService, ExtendedHttpService> st = new
281 // ServiceTracker<ExtendedHttpService, ExtendedHttpService>(
282 // bc, ExtendedHttpService.class, null);
284 // ExtendedHttpService httpService;
286 // httpService = st.waitForService(1000);
287 // } catch (InterruptedException e) {
288 // httpService = null;
291 // if (httpService == null)
292 // throw new CmsException("Could not find "
293 // + ExtendedHttpService.class + " service.");
294 // return httpService;
297 final private static void directorsCut(long initDuration
) {
298 // final long ms = 128l + (long) (Math.random() * 128d);
299 long ms
= initDuration
/ 100;
300 log
.info("Spend " + ms
+ "ms"
301 + " reflecting on the progress brought to mankind"
302 + " by Free Software...");
303 long beginNano
= System
.nanoTime();
306 } catch (InterruptedException e
) {
309 long durationNano
= System
.nanoTime() - beginNano
;
310 final double M
= 1000d
* 1000d
;
311 double sleepAccuracy
= ((double) durationNano
) / (ms
* M
);
312 if (log
.isDebugEnabled())
313 log
.debug("Sleep accuracy: "
314 + String
.format("%.2f", 100 - (sleepAccuracy
* 100 - 100))
318 /** Workaround for blocking Gogo shell by system shutdown. */
319 private class GogoShellKiller
extends Thread
{
321 public GogoShellKiller() {
322 super("Gogo shell killer");
328 ThreadGroup rootTg
= getRootThreadGroup(null);
329 Thread gogoShellThread
= findGogoShellThread(rootTg
);
330 if (gogoShellThread
== null)
332 while (getNonDaemonCount(rootTg
) > 2) {
335 } catch (InterruptedException e
) {
339 gogoShellThread
= findGogoShellThread(rootTg
);
340 if (gogoShellThread
== null)
346 private static ThreadGroup
getRootThreadGroup(ThreadGroup tg
) {
348 tg
= Thread
.currentThread().getThreadGroup();
349 if (tg
.getParent() == null)
352 return getRootThreadGroup(tg
.getParent());
355 private static int getNonDaemonCount(ThreadGroup rootThreadGroup
) {
356 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
357 rootThreadGroup
.enumerate(threads
);
358 int nonDameonCount
= 0;
359 for (Thread t
: threads
)
362 return nonDameonCount
;
365 private static Thread
findGogoShellThread(ThreadGroup rootThreadGroup
) {
366 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
367 rootThreadGroup
.enumerate(threads
, true);
368 for (Thread thread
: threads
) {
369 if (thread
.getName().equals("Gogo shell"))