1 package org
.argeo
.cms
.internal
.kernel
;
3 import static bitronix
.tm
.TransactionManagerServices
.getTransactionManager
;
4 import static bitronix
.tm
.TransactionManagerServices
.getTransactionSynchronizationRegistry
;
5 import static java
.util
.Locale
.ENGLISH
;
6 import static org
.argeo
.cms
.internal
.kernel
.KernelUtils
.getFrameworkProp
;
7 import static org
.argeo
.cms
.internal
.kernel
.KernelUtils
.getOsgiInstanceDir
;
8 import static org
.argeo
.jcr
.ArgeoJcrConstants
.ALIAS_NODE
;
9 import static org
.argeo
.jcr
.ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
;
10 import static org
.argeo
.util
.LocaleChoice
.asLocaleList
;
11 import static org
.osgi
.framework
.Constants
.FRAMEWORK_UUID
;
14 import java
.io
.FileFilter
;
15 import java
.io
.IOException
;
16 import java
.lang
.management
.ManagementFactory
;
17 import java
.security
.PrivilegedAction
;
18 import java
.util
.HashMap
;
19 import java
.util
.Hashtable
;
20 import java
.util
.List
;
21 import java
.util
.Locale
;
24 import javax
.jcr
.Repository
;
25 import javax
.jcr
.RepositoryFactory
;
26 import javax
.security
.auth
.Subject
;
27 import javax
.transaction
.TransactionManager
;
28 import javax
.transaction
.TransactionSynchronizationRegistry
;
29 import javax
.transaction
.UserTransaction
;
31 import org
.apache
.commons
.io
.FileUtils
;
32 import org
.apache
.commons
.logging
.Log
;
33 import org
.apache
.commons
.logging
.LogFactory
;
34 import org
.apache
.jackrabbit
.util
.TransientFileFactory
;
35 import org
.argeo
.ArgeoException
;
36 import org
.argeo
.ArgeoLogger
;
37 import org
.argeo
.cms
.CmsException
;
38 import org
.argeo
.jackrabbit
.OsgiJackrabbitRepositoryFactory
;
39 import org
.argeo
.jcr
.ArgeoJcrConstants
;
40 import org
.eclipse
.equinox
.http
.jetty
.JettyConfigurator
;
41 import org
.eclipse
.equinox
.http
.jetty
.JettyConstants
;
42 import org
.eclipse
.equinox
.http
.servlet
.ExtendedHttpService
;
43 import org
.osgi
.framework
.BundleContext
;
44 import org
.osgi
.framework
.ServiceEvent
;
45 import org
.osgi
.framework
.ServiceListener
;
46 import org
.osgi
.framework
.ServiceReference
;
47 import org
.osgi
.framework
.ServiceRegistration
;
48 import org
.osgi
.service
.cm
.Configuration
;
49 import org
.osgi
.service
.cm
.ConfigurationAdmin
;
50 import org
.osgi
.service
.useradmin
.UserAdmin
;
52 import bitronix
.tm
.BitronixTransactionManager
;
53 import bitronix
.tm
.BitronixTransactionSynchronizationRegistry
;
54 import bitronix
.tm
.TransactionManagerServices
;
57 * Argeo CMS Kernel. Responsible for :
60 * <li>provisioning</li>
61 * <li>transaction</li>
63 * <li>local and remote file systems access</li>
67 final class Kernel
implements KernelHeader
, KernelConstants
, ServiceListener
{
71 private ServiceReference
<ConfigurationAdmin
> configurationAdmin
;
75 private ServiceRegistration
<ArgeoLogger
> loggerReg
;
76 private ServiceRegistration
<TransactionManager
> tmReg
;
77 private ServiceRegistration
<UserTransaction
> utReg
;
78 private ServiceRegistration
<TransactionSynchronizationRegistry
> tsrReg
;
79 private ServiceRegistration
<Repository
> repositoryReg
;
80 private ServiceRegistration
<RepositoryFactory
> repositoryFactoryReg
;
81 private ServiceRegistration
<UserAdmin
> userAdminReg
;
84 * SERVICES IMPLEMENTATIONS
86 private NodeLogger logger
;
87 private BitronixTransactionManager transactionManager
;
88 private BitronixTransactionSynchronizationRegistry transactionSynchronizationRegistry
;
89 private OsgiJackrabbitRepositoryFactory repositoryFactory
;
90 NodeRepository repository
;
91 private NodeUserAdmin userAdmin
;
94 private final static Log log
= LogFactory
.getLog(Kernel
.class);
95 ThreadGroup threadGroup
= new ThreadGroup(Kernel
.class.getSimpleName());
96 private final BundleContext bc
= Activator
.getBundleContext();
97 private final NodeSecurity nodeSecurity
;
98 private DataHttp dataHttp
;
99 private KernelThread kernelThread
;
101 private Locale defaultLocale
= null;
102 private List
<Locale
> locales
= null;
105 nodeSecurity
= new NodeSecurity();
106 // log.debug(bc.getDataFile(""));
107 // log.debug(bc.getDataFile("test"));
111 Subject
.doAs(nodeSecurity
.getKernelSubject(),
112 new PrivilegedAction
<Void
>() {
121 private void doInit() {
122 long begin
= System
.currentTimeMillis();
123 ConfigurationAdmin conf
= findConfigurationAdmin();
124 // Use CMS bundle classloader
125 ClassLoader currentContextCl
= Thread
.currentThread()
126 .getContextClassLoader();
127 Thread
.currentThread().setContextClassLoader(
128 Kernel
.class.getClassLoader());
130 if (nodeSecurity
.isFirstInit())
133 defaultLocale
= new Locale(getFrameworkProp(I18N_DEFAULT_LOCALE
,
134 ENGLISH
.getLanguage()));
135 locales
= asLocaleList(getFrameworkProp(I18N_LOCALES
));
136 logger
= new NodeLogger();
138 // Initialise services
139 initTransactionManager();
140 repository
= new NodeRepository();
141 repositoryFactory
= new OsgiJackrabbitRepositoryFactory();
142 userAdmin
= new NodeUserAdmin(transactionManager
, repository
);
146 ServiceReference
<ExtendedHttpService
> sr
= bc
147 .getServiceReference(ExtendedHttpService
.class);
152 kernelThread
= new KernelThread(this);
153 kernelThread
.setContextClassLoader(Kernel
.class.getClassLoader());
154 kernelThread
.start();
156 // Publish services to OSGi
158 } catch (Exception e
) {
159 log
.error("Cannot initialize Argeo CMS", e
);
160 throw new ArgeoException("Cannot initialize", e
);
162 Thread
.currentThread().setContextClassLoader(currentContextCl
);
165 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
166 log
.info("## ARGEO CMS UP in " + (jvmUptime
/ 1000) + "."
167 + (jvmUptime
% 1000) + "s ##");
168 long initDuration
= System
.currentTimeMillis() - begin
;
169 if (log
.isTraceEnabled())
170 log
.trace("Kernel initialization took " + initDuration
+ "ms");
171 directorsCut(initDuration
);
174 private void firstInit() {
175 log
.info("## FIRST INIT ##");
176 String nodeInit
= getFrameworkProp(NODE_INIT
);
177 if (nodeInit
== null)
178 nodeInit
= "../../init";
180 if (nodeInit
.startsWith("."))
181 initDir
= KernelUtils
.getExecutionDir(nodeInit
);
183 initDir
= new File(nodeInit
);
184 // TODO also uncompress archives
185 if (initDir
.exists())
187 FileUtils
.copyDirectory(initDir
, getOsgiInstanceDir(),
191 public boolean accept(File pathname
) {
192 if (pathname
.getName().equals(".svn")
193 || pathname
.getName().equals(".git"))
198 log
.info("CMS initialized from " + initDir
.getCanonicalPath());
199 } catch (IOException e
) {
200 throw new CmsException("Cannot initialize from " + initDir
, e
);
205 private ConfigurationAdmin
findConfigurationAdmin() {
206 configurationAdmin
= bc
.getServiceReference(ConfigurationAdmin
.class);
207 if (configurationAdmin
== null) {
210 return bc
.getService(configurationAdmin
);
213 private void initTransactionManager() {
214 bitronix
.tm
.Configuration tmConf
= TransactionManagerServices
216 tmConf
.setServerId(getFrameworkProp(FRAMEWORK_UUID
));
218 // File tmBaseDir = new File(getFrameworkProp(TRANSACTIONS_HOME,
219 // getOsgiInstancePath(DIR_TRANSACTIONS)));
220 File tmBaseDir
= bc
.getDataFile(DIR_TRANSACTIONS
);
221 File tmDir1
= new File(tmBaseDir
, "btm1");
223 tmConf
.setLogPart1Filename(new File(tmDir1
, tmDir1
.getName() + ".tlog")
225 File tmDir2
= new File(tmBaseDir
, "btm2");
227 tmConf
.setLogPart2Filename(new File(tmDir2
, tmDir2
.getName() + ".tlog")
229 transactionManager
= getTransactionManager();
230 transactionSynchronizationRegistry
= getTransactionSynchronizationRegistry();
233 private void initWebServer(ConfigurationAdmin conf
) {
234 String httpPort
= getFrameworkProp("org.osgi.service.http.port");
235 String httpsPort
= getFrameworkProp("org.osgi.service.http.port.secure");
237 if (httpPort
!= null || httpsPort
!= null) {
238 Hashtable
<String
, Object
> jettyProps
= new Hashtable
<String
, Object
>();
239 if (httpPort
!= null) {
240 jettyProps
.put(JettyConstants
.HTTP_PORT
, httpPort
);
241 jettyProps
.put(JettyConstants
.HTTP_ENABLED
, true);
243 if (httpsPort
!= null) {
244 jettyProps
.put(JettyConstants
.HTTPS_PORT
, httpsPort
);
245 jettyProps
.put(JettyConstants
.HTTPS_ENABLED
, true);
246 jettyProps
.put(JettyConstants
.SSL_KEYSTORETYPE
, "PKCS12");
247 jettyProps
.put(JettyConstants
.SSL_KEYSTORE
, nodeSecurity
248 .getHttpServerKeyStore().getCanonicalPath());
249 jettyProps
.put(JettyConstants
.SSL_PASSWORD
, "changeit");
250 jettyProps
.put(JettyConstants
.SSL_WANTCLIENTAUTH
, true);
253 // TODO make filter more generic
254 String filter
= "(" + JettyConstants
.HTTP_PORT
+ "="
256 if (conf
.listConfigurations(filter
) != null)
258 Configuration jettyConf
= conf
.createFactoryConfiguration(
259 JETTY_FACTORY_PID
, null);
260 jettyConf
.update(jettyProps
);
262 JettyConfigurator
.startServer("default", jettyProps
);
265 } catch (Exception e
) {
266 throw new CmsException("Cannot initialize web server on "
267 + httpPortsMsg(httpPort
, httpsPort
), e
);
271 private void publish() {
272 // Listen to service publication (also ours)
273 bc
.addServiceListener(Kernel
.this);
276 loggerReg
= bc
.registerService(ArgeoLogger
.class, logger
, null);
278 tmReg
= bc
.registerService(TransactionManager
.class,
279 transactionManager
, null);
280 utReg
= bc
.registerService(UserTransaction
.class, transactionManager
,
282 tsrReg
= bc
.registerService(TransactionSynchronizationRegistry
.class,
283 transactionSynchronizationRegistry
, null);
285 userAdminReg
= bc
.registerService(UserAdmin
.class, userAdmin
,
286 userAdmin
.currentState());
288 Hashtable
<String
, String
> regProps
= new Hashtable
<String
, String
>();
289 regProps
.put(JCR_REPOSITORY_ALIAS
, ALIAS_NODE
);
290 repositoryReg
= bc
.registerService(Repository
.class, repository
,
292 repositoryFactoryReg
= bc
.registerService(RepositoryFactory
.class,
293 repositoryFactory
, null);
297 long begin
= System
.currentTimeMillis();
300 kernelThread
.destroyAndJoin();
302 if (dataHttp
!= null)
304 if (userAdmin
!= null)
306 if (repository
!= null)
307 repository
.destroy();
308 if (transactionManager
!= null)
309 transactionManager
.shutdown();
311 bc
.removeServiceListener(this);
313 // Clean hanging threads from Jackrabbit
314 TransientFileFactory
.shutdown();
316 // Clean hanging Gogo shell thread
317 new GogoShellKiller().start();
319 nodeSecurity
.destroy();
320 long duration
= System
.currentTimeMillis() - begin
;
321 log
.info("## ARGEO CMS DOWN in " + (duration
/ 1000) + "."
322 + (duration
% 1000) + "s ##");
325 private void unpublish() {
326 userAdminReg
.unregister();
327 repositoryFactoryReg
.unregister();
328 repositoryReg
.unregister();
332 loggerReg
.unregister();
336 public void serviceChanged(ServiceEvent event
) {
337 ServiceReference
<?
> sr
= event
.getServiceReference();
338 Object service
= bc
.getService(sr
);
339 if (service
instanceof Repository
) {
340 Object jcrRepoAlias
= sr
341 .getProperty(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
);
342 if (jcrRepoAlias
!= null) {// JCR repository
343 String alias
= jcrRepoAlias
.toString();
344 Repository repository
= (Repository
) bc
.getService(sr
);
345 Map
<String
, Object
> props
= new HashMap
<String
, Object
>();
346 for (String key
: sr
.getPropertyKeys())
347 props
.put(key
, sr
.getProperty(key
));
348 if (ServiceEvent
.REGISTERED
== event
.getType()) {
350 repositoryFactory
.register(repository
, props
);
351 dataHttp
.registerRepositoryServlets(alias
, repository
);
352 } catch (Exception e
) {
353 throw new CmsException(
354 "Could not publish JCR repository " + alias
, e
);
356 } else if (ServiceEvent
.UNREGISTERING
== event
.getType()) {
357 repositoryFactory
.unregister(repository
, props
);
358 dataHttp
.unregisterRepositoryServlets(alias
);
361 } else if (service
instanceof ExtendedHttpService
) {
362 if (ServiceEvent
.REGISTERED
== event
.getType()) {
364 } else if (ServiceEvent
.UNREGISTERING
== event
.getType()) {
371 private void addHttpService(ServiceReference
<?
> sr
) {
372 // for (String key : sr.getPropertyKeys())
373 // log.debug(key + "=" + sr.getProperty(key));
374 ExtendedHttpService httpService
= (ExtendedHttpService
) bc
376 // TODO find constants
377 Object httpPort
= sr
.getProperty("http.port");
378 Object httpsPort
= sr
.getProperty("https.port");
379 dataHttp
= new DataHttp(httpService
, repository
);
380 if (log
.isDebugEnabled())
381 log
.debug(httpPortsMsg(httpPort
, httpsPort
));
384 private String
httpPortsMsg(Object httpPort
, Object httpsPort
) {
385 return "HTTP " + httpPort
386 + (httpsPort
!= null ?
" - HTTPS " + httpsPort
: "");
390 public Locale
getDefaultLocale() {
391 return defaultLocale
;
396 public List
<Locale
> getLocales() {
400 final private static void directorsCut(long initDuration
) {
401 // final long ms = 128l + (long) (Math.random() * 128d);
402 long ms
= initDuration
/ 100;
403 log
.info("Spend " + ms
+ "ms"
404 + " reflecting on the progress brought to mankind"
405 + " by Free Software...");
406 long beginNano
= System
.nanoTime();
409 } catch (InterruptedException e
) {
412 long durationNano
= System
.nanoTime() - beginNano
;
413 final double M
= 1000d
* 1000d
;
414 double sleepAccuracy
= ((double) durationNano
) / (ms
* M
);
415 if (log
.isDebugEnabled())
416 log
.debug("Sleep accuracy: "
417 + String
.format("%.2f", 100 - (sleepAccuracy
* 100 - 100))
421 /** Workaround for blocking Gogo shell by system shutdown. */
422 private class GogoShellKiller
extends Thread
{
424 public GogoShellKiller() {
425 super("Gogo shell killer");
431 ThreadGroup rootTg
= getRootThreadGroup(null);
432 Thread gogoShellThread
= findGogoShellThread(rootTg
);
433 if (gogoShellThread
== null)
435 while (getNonDaemonCount(rootTg
) > 2) {
438 } catch (InterruptedException e
) {
442 gogoShellThread
= findGogoShellThread(rootTg
);
443 if (gogoShellThread
== null)
449 private static ThreadGroup
getRootThreadGroup(ThreadGroup tg
) {
451 tg
= Thread
.currentThread().getThreadGroup();
452 if (tg
.getParent() == null)
455 return getRootThreadGroup(tg
.getParent());
458 private static int getNonDaemonCount(ThreadGroup rootThreadGroup
) {
459 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
460 rootThreadGroup
.enumerate(threads
);
461 int nonDameonCount
= 0;
462 for (Thread t
: threads
)
465 return nonDameonCount
;
468 private static Thread
findGogoShellThread(ThreadGroup rootThreadGroup
) {
469 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
470 rootThreadGroup
.enumerate(threads
, true);
471 for (Thread thread
: threads
) {
472 if (thread
.getName().equals("Gogo shell"))