+ @Override
+ public void serviceChanged(ServiceEvent event) {
+ ServiceReference<?> sr = event.getServiceReference();
+ Object service = bundleContext.getService(sr);
+ if (service instanceof Repository) {
+ Object jcrRepoAlias = sr
+ .getProperty(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS);
+ 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 {
+ 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);
+ }
+ }
+ } else if (service instanceof ExtendedHttpService) {
+ if (ServiceEvent.REGISTERED == event.getType()) {
+ addHttpService(sr);
+ } else if (ServiceEvent.UNREGISTERING == event.getType()) {
+ nodeHttp.destroy();
+ nodeHttp = null;
+ }
+ }
+ }
+
+ private void addHttpService(ServiceReference<?> sr) {
+// for (String key : sr.getPropertyKeys())
+// log.debug(key + "=" + sr.getProperty(key));
+ ExtendedHttpService httpService = (ExtendedHttpService) bundleContext
+ .getService(sr);
+ // TODO find constants
+ Object httpPort = sr.getProperty("http.port");
+ Object httpsPort = sr.getProperty("https.port");
+ nodeHttp = new NodeHttp(httpService, node);
+ if (log.isDebugEnabled())
+ log.debug("HTTP " + httpPort
+ + (httpsPort != null ? " - HTTPS " + httpsPort : ""));
+ }
+
+ private ExtendedHttpService waitForHttpService() {
+ final ServiceTracker<ExtendedHttpService, ExtendedHttpService> st = new ServiceTracker<ExtendedHttpService, ExtendedHttpService>(
+ bundleContext, ExtendedHttpService.class, null);
+ st.open();
+ ExtendedHttpService httpService;
+ try {
+ httpService = st.waitForService(1000);
+ } catch (InterruptedException e) {
+ httpService = null;
+ }
+
+ if (httpService == null)
+ throw new CmsException("Could not find "
+ + 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))
+ + " %");
+ }
+
+ /** Workaround for blocking Gogo shell by system shutdown. */
+ private class GogoShellKiller extends Thread {
+
+ public GogoShellKiller() {
+ super("Gogo shell killer");
+ setDaemon(true);
+ }
+
+ @Override
+ public void run() {
+ ThreadGroup rootTg = getRootThreadGroup(null);
+ Thread gogoShellThread = findGogoShellThread(rootTg);
+ if (gogoShellThread == null)
+ return;
+ while (getNonDaemonCount(rootTg) > 2) {
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ // silent
+ }
+ }
+ gogoShellThread = findGogoShellThread(rootTg);
+ if (gogoShellThread == null)
+ return;
+ System.exit(0);
+ }
+ }
+
+ private static ThreadGroup getRootThreadGroup(ThreadGroup tg) {
+ if (tg == null)
+ tg = Thread.currentThread().getThreadGroup();
+ if (tg.getParent() == null)
+ return tg;
+ else
+ return getRootThreadGroup(tg.getParent());
+ }
+
+ private static int getNonDaemonCount(ThreadGroup rootThreadGroup) {
+ Thread[] threads = new Thread[rootThreadGroup.activeCount()];
+ rootThreadGroup.enumerate(threads);
+ int nonDameonCount = 0;
+ for (Thread t : threads)
+ if (!t.isDaemon())
+ nonDameonCount++;
+ return nonDameonCount;
+ }
+
+ private static Thread findGogoShellThread(ThreadGroup rootThreadGroup) {
+ Thread[] threads = new Thread[rootThreadGroup.activeCount()];
+ rootThreadGroup.enumerate(threads, true);
+ for (Thread thread : threads) {
+ if (thread.getName().equals("Gogo shell"))
+ return thread;
+ }
+ return null;
+ }
+
+}
\ No newline at end of file