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();
109 // KernelUtils.logFrameworkProperties(log);
112 // Initialise services
113 logger
= new NodeLogger();
114 // transactionManager = new SimpleTransactionManager();
115 initBitronixTransactionManager();
116 repository
= new NodeRepository(bc
);
117 repositoryFactory
= new OsgiJackrabbitRepositoryFactory();
118 userAdmin
= new NodeUserAdmin(transactionManager
, repository
);
121 ServiceReference
<ExtendedHttpService
> sr
= bc
122 .getServiceReference(ExtendedHttpService
.class);
127 kernelThread
= new KernelThread(this);
128 kernelThread
.setContextClassLoader(Kernel
.class.getClassLoader());
129 kernelThread
.start();
131 // Publish services to OSGi
133 } catch (Exception e
) {
134 log
.error("Cannot initialize Argeo CMS", e
);
135 throw new ArgeoException("Cannot initialize", e
);
137 Thread
.currentThread().setContextClassLoader(currentContextCl
);
140 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
141 log
.info("## ARGEO CMS UP in " + (jvmUptime
/ 1000) + "."
142 + (jvmUptime
% 1000) + "s ##");
143 long initDuration
= System
.currentTimeMillis() - begin
;
144 if (log
.isTraceEnabled())
145 log
.trace("Kernel initialization took " + initDuration
+ "ms");
146 directorsCut(initDuration
);
149 private void initBitronixTransactionManager() {
150 Configuration tmConf
= TransactionManagerServices
.getConfiguration();
151 tmConf
.setServerId(getFrameworkProp(FRAMEWORK_UUID
));
153 File tmBaseDir
= new File(getFrameworkProp(TRANSACTIONS_HOME
,
154 getOsgiInstancePath("transactions")));
155 File tmDir1
= new File(tmBaseDir
, "btm1");
157 tmConf
.setLogPart1Filename(new File(tmDir1
, tmDir1
.getName() + ".tlog")
159 File tmDir2
= new File(tmBaseDir
, "btm2");
161 tmConf
.setLogPart2Filename(new File(tmDir2
, tmDir2
.getName() + ".tlog")
163 transactionManager
= getTransactionManager();
164 transactionSynchronizationRegistry
= getTransactionSynchronizationRegistry();
167 private void publish() {
168 // Listen to service publication (also ours)
169 bc
.addServiceListener(Kernel
.this);
172 loggerReg
= bc
.registerService(ArgeoLogger
.class, logger
, null);
174 tmReg
= bc
.registerService(TransactionManager
.class,
175 transactionManager
, null);
176 utReg
= bc
.registerService(UserTransaction
.class, transactionManager
,
178 tsrReg
= bc
.registerService(TransactionSynchronizationRegistry
.class,
179 transactionSynchronizationRegistry
, null);
181 userAdminReg
= bc
.registerService(UserAdmin
.class, userAdmin
,
182 userAdmin
.currentState());
184 Hashtable
<String
, String
> regProps
= new Hashtable
<String
, String
>();
185 regProps
.put(JCR_REPOSITORY_ALIAS
, ALIAS_NODE
);
186 repositoryReg
= bc
.registerService(Repository
.class, repository
,
188 repositoryFactoryReg
= bc
.registerService(RepositoryFactory
.class,
189 repositoryFactory
, null);
193 long begin
= System
.currentTimeMillis();
196 kernelThread
.destroyAndJoin();
198 if (dataHttp
!= null)
200 if (userAdmin
!= null)
202 if (repository
!= null)
203 repository
.destroy();
204 if (transactionManager
!= null)
205 transactionManager
.shutdown();
207 bc
.removeServiceListener(this);
209 // Clean hanging threads from Jackrabbit
210 TransientFileFactory
.shutdown();
212 // Clean hanging Gogo shell thread
213 new GogoShellKiller().start();
215 nodeSecurity
.destroy();
216 long duration
= System
.currentTimeMillis() - begin
;
217 log
.info("## ARGEO CMS DOWN in " + (duration
/ 1000) + "."
218 + (duration
% 1000) + "s ##");
221 private void unpublish() {
222 userAdminReg
.unregister();
223 repositoryFactoryReg
.unregister();
224 repositoryReg
.unregister();
228 loggerReg
.unregister();
232 public void serviceChanged(ServiceEvent event
) {
233 ServiceReference
<?
> sr
= event
.getServiceReference();
234 Object service
= bc
.getService(sr
);
235 if (service
instanceof Repository
) {
236 Object jcrRepoAlias
= sr
237 .getProperty(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
);
238 if (jcrRepoAlias
!= null) {// JCR repository
239 String alias
= jcrRepoAlias
.toString();
240 Repository repository
= (Repository
) bc
.getService(sr
);
241 Map
<String
, Object
> props
= new HashMap
<String
, Object
>();
242 for (String key
: sr
.getPropertyKeys())
243 props
.put(key
, sr
.getProperty(key
));
244 if (ServiceEvent
.REGISTERED
== event
.getType()) {
246 repositoryFactory
.register(repository
, props
);
247 dataHttp
.registerRepositoryServlets(alias
, repository
);
248 } catch (Exception e
) {
249 throw new CmsException(
250 "Could not publish JCR repository " + alias
, e
);
252 } else if (ServiceEvent
.UNREGISTERING
== event
.getType()) {
253 repositoryFactory
.unregister(repository
, props
);
254 dataHttp
.unregisterRepositoryServlets(alias
);
257 } else if (service
instanceof ExtendedHttpService
) {
258 if (ServiceEvent
.REGISTERED
== event
.getType()) {
260 } else if (ServiceEvent
.UNREGISTERING
== event
.getType()) {
267 private void addHttpService(ServiceReference
<?
> sr
) {
268 // for (String key : sr.getPropertyKeys())
269 // log.debug(key + "=" + sr.getProperty(key));
270 ExtendedHttpService httpService
= (ExtendedHttpService
) bc
272 // TODO find constants
273 Object httpPort
= sr
.getProperty("http.port");
274 Object httpsPort
= sr
.getProperty("https.port");
275 dataHttp
= new DataHttp(httpService
, repository
);
276 if (log
.isDebugEnabled())
277 log
.debug("HTTP " + httpPort
278 + (httpsPort
!= null ?
" - HTTPS " + httpsPort
: ""));
281 // private ExtendedHttpService waitForHttpService() {
282 // final ServiceTracker<ExtendedHttpService, ExtendedHttpService> st = new
283 // ServiceTracker<ExtendedHttpService, ExtendedHttpService>(
284 // bc, ExtendedHttpService.class, null);
286 // ExtendedHttpService httpService;
288 // httpService = st.waitForService(1000);
289 // } catch (InterruptedException e) {
290 // httpService = null;
293 // if (httpService == null)
294 // throw new CmsException("Could not find "
295 // + ExtendedHttpService.class + " service.");
296 // return httpService;
299 final private static void directorsCut(long initDuration
) {
300 // final long ms = 128l + (long) (Math.random() * 128d);
301 long ms
= initDuration
/ 100;
302 log
.info("Spend " + ms
+ "ms"
303 + " reflecting on the progress brought to mankind"
304 + " by Free Software...");
305 long beginNano
= System
.nanoTime();
308 } catch (InterruptedException e
) {
311 long durationNano
= System
.nanoTime() - beginNano
;
312 final double M
= 1000d
* 1000d
;
313 double sleepAccuracy
= ((double) durationNano
) / (ms
* M
);
314 if (log
.isDebugEnabled())
315 log
.debug("Sleep accuracy: "
316 + String
.format("%.2f", 100 - (sleepAccuracy
* 100 - 100))
320 /** Workaround for blocking Gogo shell by system shutdown. */
321 private class GogoShellKiller
extends Thread
{
323 public GogoShellKiller() {
324 super("Gogo shell killer");
330 ThreadGroup rootTg
= getRootThreadGroup(null);
331 Thread gogoShellThread
= findGogoShellThread(rootTg
);
332 if (gogoShellThread
== null)
334 while (getNonDaemonCount(rootTg
) > 2) {
337 } catch (InterruptedException e
) {
341 gogoShellThread
= findGogoShellThread(rootTg
);
342 if (gogoShellThread
== null)
348 private static ThreadGroup
getRootThreadGroup(ThreadGroup tg
) {
350 tg
= Thread
.currentThread().getThreadGroup();
351 if (tg
.getParent() == null)
354 return getRootThreadGroup(tg
.getParent());
357 private static int getNonDaemonCount(ThreadGroup rootThreadGroup
) {
358 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
359 rootThreadGroup
.enumerate(threads
);
360 int nonDameonCount
= 0;
361 for (Thread t
: threads
)
364 return nonDameonCount
;
367 private static Thread
findGogoShellThread(ThreadGroup rootThreadGroup
) {
368 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
369 rootThreadGroup
.enumerate(threads
, true);
370 for (Thread thread
: threads
) {
371 if (thread
.getName().equals("Gogo shell"))