package org.argeo.cms.internal.kernel;
import java.lang.management.ManagementFactory;
+import java.util.HashMap;
+import java.util.Map;
import javax.jcr.Repository;
import javax.jcr.RepositoryFactory;
final class Kernel implements ServiceListener {
private final static Log log = LogFactory.getLog(Kernel.class);
- private final BundleContext bundleContext;
- private final ThreadGroup threadGroup = new ThreadGroup("Argeo CMS Kernel");
+ private final BundleContext bundleContext = Activator.getBundleContext();
- private JackrabbitNode node;
- private RepositoryFactory repositoryFactory;
- private NodeSecurity nodeSecurity;
- private NodeHttp nodeHttp;
-
- Kernel(BundleContext bundleContext) {
- this.bundleContext = bundleContext;
- }
+ ThreadGroup threadGroup = new ThreadGroup(Kernel.class.getSimpleName());
+ JackrabbitNode node;
+ OsgiJackrabbitRepositoryFactory repositoryFactory;
+ NodeSecurity nodeSecurity;
+ NodeHttp nodeHttp;
+ private KernelThread kernelThread;
void init() {
- new Thread(threadGroup, "init") {
- @Override
- public void run() {
- // CMS bundle classloader used during initialisation
- Thread.currentThread().setContextClassLoader(
- Kernel.class.getClassLoader());
- doInit();
- }
- }.start();
- }
-
- /** Run asynchronously */
- protected void doInit() {
+ ClassLoader currentContextCl = Thread.currentThread()
+ .getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(
+ Kernel.class.getClassLoader());
long begin = System.currentTimeMillis();
InternalAuthentication initAuth = new InternalAuthentication(
KernelConstants.DEFAULT_SECURITY_KEY);
SecurityContextHolder.getContext().setAuthentication(initAuth);
try {
+ // Jackrabbit node
node = new JackrabbitNode(bundleContext);
+
+ // JCR repository factory
repositoryFactory = new OsgiJackrabbitRepositoryFactory();
+
+ // Authentication
nodeSecurity = new NodeSecurity(bundleContext, node);
// Equinox dependency
ExtendedHttpService httpService = waitForHttpService();
nodeHttp = new NodeHttp(httpService, node, nodeSecurity);
+ // Kernel thread
+ kernelThread = new KernelThread(this);
+ kernelThread.setContextClassLoader(Kernel.class.getClassLoader());
+ kernelThread.start();
+
// Publish services to OSGi
nodeSecurity.publish();
- node.publish();
+ node.publish(repositoryFactory);
bundleContext.registerService(RepositoryFactory.class,
repositoryFactory, null);
} catch (Exception e) {
log.error("Cannot initialize Argeo CMS", e);
throw new ArgeoException("Cannot initialize", e);
+ } finally {
+ Thread.currentThread().setContextClassLoader(currentContextCl);
}
long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
void destroy() {
long begin = System.currentTimeMillis();
+ kernelThread.destroyAndJoin();
+
if (nodeHttp != null)
nodeHttp.destroy();
if (nodeSecurity != null)
if (jcrRepoAlias != null) {// JCR repository
String alias = jcrRepoAlias.toString();
Repository repository = (Repository) bundleContext.getService(sr);
+ Map<String, Object> props = new HashMap<String, Object>();
+ for (String key : sr.getPropertyKeys())
+ props.put(key, sr.getProperty(key));
if (ServiceEvent.REGISTERED == event.getType()) {
try {
- nodeHttp.registerWebdavServlet(alias, repository, true);
- nodeHttp.registerWebdavServlet(alias, repository, false);
- nodeHttp.registerRemotingServlet(alias, repository, true);
- nodeHttp.registerRemotingServlet(alias, repository, false);
+ repositoryFactory.register(repository, props);
+ nodeHttp.registerRepositoryServlets(alias, repository);
} catch (Exception e) {
throw new CmsException("Could not publish JCR repository "
+ alias, e);
}
} else if (ServiceEvent.UNREGISTERING == event.getType()) {
+ repositoryFactory.unregister(repository, props);
+ nodeHttp.unregisterRepositoryServlets(alias);
}
}
}
- final private static void directorsCut(long initDuration) {
- // final long ms = 128l + (long) (Math.random() * 128d);
- long ms = initDuration / 10;
- log.info("Spend " + ms + "ms"
- + " reflecting on the progress brought to mankind"
- + " by Free Software...");
- long beginNano = System.nanoTime();
- try {
- Thread.sleep(ms, 0);
- } catch (InterruptedException e) {
- // silent
- }
- long durationNano = System.nanoTime() - beginNano;
- final double M = 1000d * 1000d;
- double sleepAccuracy = ((double) durationNano) / (ms * M);
- if (log.isTraceEnabled())
- log.trace("Sleep accuracy: "
- + String.format("%.2f", sleepAccuracy * 100) + " %");
- }
-
private ExtendedHttpService waitForHttpService() {
final ServiceTracker<ExtendedHttpService, ExtendedHttpService> st = new ServiceTracker<ExtendedHttpService, ExtendedHttpService>(
bundleContext, ExtendedHttpService.class, null);
+ ExtendedHttpService.class + " service.");
return httpService;
}
-}
+
+ final private static void directorsCut(long initDuration) {
+ // final long ms = 128l + (long) (Math.random() * 128d);
+ long ms = initDuration / 100;
+ log.info("Spend " + ms + "ms"
+ + " reflecting on the progress brought to mankind"
+ + " by Free Software...");
+ long beginNano = System.nanoTime();
+ try {
+ Thread.sleep(ms, 0);
+ } catch (InterruptedException e) {
+ // silent
+ }
+ long durationNano = System.nanoTime() - beginNano;
+ final double M = 1000d * 1000d;
+ double sleepAccuracy = ((double) durationNano) / (ms * M);
+ if (log.isDebugEnabled())
+ log.debug("Sleep accuracy: "
+ + String.format("%.2f", 100 - (sleepAccuracy * 100 - 100))
+ + " %");
+ }
+}
\ No newline at end of file