]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/internal/kernel/Kernel.java
Introduce aggregating node user admin
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / Kernel.java
index f0c2d90d1f99758a1adc4e9238afa93e3324c3f3..83f21202e45612855b87d23d29a226479469df3a 100644 (file)
@@ -1,6 +1,9 @@
 package org.argeo.cms.internal.kernel;
 
 import java.lang.management.ManagementFactory;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.jcr.Repository;
 import javax.jcr.RepositoryFactory;
@@ -35,49 +38,52 @@ import org.springframework.security.core.context.SecurityContextHolder;
 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() {
+               URL url = getClass().getClassLoader().getResource(
+                               KernelConstants.JAAS_CONFIG);
+               System.setProperty("java.security.auth.login.config",
+                               url.toExternalForm());
+
+               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);
 
@@ -85,6 +91,8 @@ final class Kernel implements ServiceListener {
                } 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();
@@ -99,6 +107,8 @@ final class Kernel implements ServiceListener {
        void destroy() {
                long begin = System.currentTimeMillis();
 
+               kernelThread.destroyAndJoin();
+
                if (nodeHttp != null)
                        nodeHttp.destroy();
                if (nodeSecurity != null)
@@ -124,42 +134,25 @@ final class Kernel implements ServiceListener {
                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);
@@ -176,4 +169,25 @@ final class Kernel implements ServiceListener {
                                        + 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