package org.argeo.cms.jcr.internal;
+import java.io.IOException;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
import javax.jcr.RepositoryFactory;
import org.apache.jackrabbit.core.RepositoryContext;
private Map<String, RepositoryContext> repositories = new HashMap<String, RepositoryContext>();
private Map<String, Object> pidToCn = new HashMap<String, Object>();
-
+
public void init() {
-
+
}
public void destroy() {
for (String pid : repositories.keySet()) {
try {
- repositories.get(pid).getRepository().shutdown();
+ RepositoryContext repositoryContext = repositories.get(pid);
+ repositoryContext.getRepository().shutdown();
if (log.isDebugEnabled())
log.debug("Shut down repository " + pid
+ (pidToCn.containsKey(pid) ? " (" + pidToCn.get(pid) + ")" : ""));
}
}
-
@Override
public String getName() {
return "Jackrabbit repository service factory";
if (properties == null)
return;
- if (repositories.containsKey(pid)) {
- log.warn("Ignore update of Jackrabbit repository " + pid);
- return;
- }
+ Object cn = properties.get(CmsConstants.CN);
+ if (cn != null)
+ for (String otherPid : pidToCn.keySet()) {
+ Object o = pidToCn.get(otherPid);
+ if (cn.equals(o)) {
+ RepositoryContext repositoryContext = repositories.remove(otherPid);
+ repositories.put(pid, repositoryContext);
+ if (log.isDebugEnabled())
+ log.debug("Ignore update of Jackrabbit repository " + cn);
+ // FIXME perform a proper update (also of the OSGi service)
+ return;
+ }
+ }
try {
Object labeledUri = properties.get(RepoConf.labeledUri.name());
Dictionary<String, Object> props = LangUtils.dict(Constants.SERVICE_PID, pid);
// props.put(ArgeoJcrConstants.JCR_REPOSITORY_URI,
// properties.get(RepoConf.labeledUri.name()));
- Object cn = properties.get(CmsConstants.CN);
if (cn != null) {
props.put(CmsConstants.CN, cn);
// props.put(NodeConstants.JCR_REPOSITORY_ALIAS, cn);
}
CmsJcrActivator.registerService(RepositoryContext.class, repositoryContext, props);
} else {
- try {
- Object cn = properties.get(CmsConstants.CN);
- Object defaultWorkspace = properties.get(RepoConf.defaultWorkspace.name());
- if (defaultWorkspace == null)
- defaultWorkspace = RepoConf.defaultWorkspace.getDefault();
- URI uri = new URI(labeledUri.toString());
+ Object defaultWorkspace = properties.get(RepoConf.defaultWorkspace.name());
+ if (defaultWorkspace == null)
+ defaultWorkspace = RepoConf.defaultWorkspace.getDefault();
+ URI uri = new URI(labeledUri.toString());
// RepositoryFactory repositoryFactory = bc
// .getService(bc.getServiceReference(RepositoryFactory.class));
- RepositoryFactory repositoryFactory = CmsJcrActivator.getService(RepositoryFactory.class);
- Map<String, String> parameters = new HashMap<String, String>();
- parameters.put(RepoConf.labeledUri.name(), uri.toString());
- parameters.put(RepoConf.defaultWorkspace.name(), defaultWorkspace.toString());
- Repository repository = repositoryFactory.getRepository(parameters);
- // Repository repository = NodeUtils.getRepositoryByUri(repositoryFactory,
- // uri.toString());
- Dictionary<String, Object> props = LangUtils.dict(Constants.SERVICE_PID, pid);
- props.put(RepoConf.labeledUri.name(),
- new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null)
- .toString());
- if (cn != null) {
- props.put(CmsConstants.CN, cn);
- // props.put(NodeConstants.JCR_REPOSITORY_ALIAS, cn);
- pidToCn.put(pid, cn);
- }
- CmsJcrActivator.registerService(Repository.class, repository, props);
-
- // home
- if (cn.equals(CmsConstants.NODE_REPOSITORY)) {
- Dictionary<String, Object> homeProps = LangUtils.dict(CmsConstants.CN,
- CmsConstants.EGO_REPOSITORY);
- EgoRepository homeRepository = new EgoRepository(repository, true);
- CmsJcrActivator.registerService(Repository.class, homeRepository, homeProps);
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ RepositoryFactory repositoryFactory = CmsJcrActivator.getService(RepositoryFactory.class);
+ Map<String, String> parameters = new HashMap<String, String>();
+ parameters.put(RepoConf.labeledUri.name(), uri.toString());
+ parameters.put(RepoConf.defaultWorkspace.name(), defaultWorkspace.toString());
+ Repository repository = repositoryFactory.getRepository(parameters);
+ // Repository repository = NodeUtils.getRepositoryByUri(repositoryFactory,
+ // uri.toString());
+ Dictionary<String, Object> props = LangUtils.dict(Constants.SERVICE_PID, pid);
+ props.put(RepoConf.labeledUri.name(),
+ new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null)
+ .toString());
+ if (cn != null) {
+ props.put(CmsConstants.CN, cn);
+ // props.put(NodeConstants.JCR_REPOSITORY_ALIAS, cn);
+ pidToCn.put(pid, cn);
+ }
+ CmsJcrActivator.registerService(Repository.class, repository, props);
+
+ // home
+ if (cn.equals(CmsConstants.NODE_REPOSITORY)) {
+ Dictionary<String, Object> homeProps = LangUtils.dict(CmsConstants.CN, CmsConstants.EGO_REPOSITORY);
+ EgoRepository homeRepository = new EgoRepository(repository, true);
+ CmsJcrActivator.registerService(Repository.class, homeRepository, homeProps);
}
}
- } catch (Exception e) {
+ } catch (RepositoryException | URISyntaxException | IOException e) {
throw new IllegalStateException("Cannot create Jackrabbit repository " + pid, e);
}
log.debug("Deleted repository " + pid);
}
-
}
--- /dev/null
+package org.argeo.cms.internal.osgi;
+
+import java.io.IOException;
+
+/**
+ * Workaround for killing Gogo shell by system shutdown.
+ *
+ * @see https://issues.apache.org/jira/browse/FELIX-4208
+ */
+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) // no need to bother if it is not here
+ return;
+ while (getNonDaemonCount(rootTg) > 2) {
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ // silent
+ }
+ }
+ gogoShellThread = findGogoShellThread(rootTg);
+ if (gogoShellThread == null)
+ return;
+ System.exit(0);
+ // No non-deamon threads left, forcibly halting the VM
+ //Runtime.getRuntime().halt(0);
+ }
+
+ private ThreadGroup getRootThreadGroup(ThreadGroup tg) {
+ if (tg == null)
+ tg = Thread.currentThread().getThreadGroup();
+ if (tg.getParent() == null)
+ return tg;
+ else
+ return getRootThreadGroup(tg.getParent());
+ }
+
+ private int getNonDaemonCount(ThreadGroup rootThreadGroup) {
+ Thread[] threads = new Thread[rootThreadGroup.activeCount()];
+ rootThreadGroup.enumerate(threads);
+ int nonDameonCount = 0;
+ for (Thread t : threads)
+ if (t != null && !t.isDaemon())
+ nonDameonCount++;
+ return nonDameonCount;
+ }
+
+ private Thread findGogoShellThread(ThreadGroup rootThreadGroup) {
+ Thread[] threads = new Thread[rootThreadGroup.activeCount()];
+ rootThreadGroup.enumerate(threads, true);
+ for (Thread thread : threads) {
+ if (thread.getName().equals("pipe-gosh --login --noshutdown"))
+ return thread;
+ }
+ return null;
+ }
+
+}
\ No newline at end of file
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.List;
import javax.security.auth.login.Configuration;
private Long availableSince;
// private ThreadGroup threadGroup = new ThreadGroup("CMS");
- private List<Runnable> stopHooks = new ArrayList<>();
+// private List<Runnable> stopHooks = new ArrayList<>();
private String stateUuid;
// private final boolean cleanState;
public void destroy() {
if (log.isDebugEnabled())
log.debug("CMS stopping... (" + this.stateUuid + ")");
+// new GogoShellKiller().start();
// In a different thread in order to avoid interruptions
- Thread stopHookThread = new Thread(() -> applyStopHooks(), "Apply Argeo Stop Hooks");
- stopHookThread.start();
- try {
- stopHookThread.join(10 * 60 * 1000);
- } catch (InterruptedException e) {
- // silent
- }
+// Thread stopHookThread = new Thread(() -> applyStopHooks(), "Apply Argeo Stop Hooks");
+// stopHookThread.start();
+// try {
+// stopHookThread.join(10 * 60 * 1000);
+// } catch (InterruptedException e) {
+// // silent
+// }
// internalExecutorService.shutdown();
}
/** Apply shutdown hoos in reverse order. */
- private void applyStopHooks() {
- for (int i = stopHooks.size() - 1; i >= 0; i--) {
- try {
- stopHooks.get(i).run();
- } catch (Exception e) {
- log.error("Could not run shutdown hook #" + i);
- }
- }
- // Clean hanging Gogo shell thread
- new GogoShellKiller().start();
-
-// instance = null;
- }
+// private void applyStopHooks() {
+//// for (int i = stopHooks.size() - 1; i >= 0; i--) {
+//// try {
+//// stopHooks.get(i).run();
+//// } catch (Exception e) {
+//// log.error("Could not run shutdown hook #" + i);
+//// }
+//// }
+// // Clean hanging Gogo shell thread
+// new GogoShellKiller().start();
+//
+//// instance = null;
+// }
// @Override
// public boolean isClean() {
+++ /dev/null
-package org.argeo.cms.internal.runtime;
-
-/**
- * Workaround for killing Gogo shell by system shutdown.
- *
- * @see https://issues.apache.org/jira/browse/FELIX-4208
- */
-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;
- // No non-deamon threads left, forcibly halting the VM
- Runtime.getRuntime().halt(0);
- }
-
- private ThreadGroup getRootThreadGroup(ThreadGroup tg) {
- if (tg == null)
- tg = Thread.currentThread().getThreadGroup();
- if (tg.getParent() == null)
- return tg;
- else
- return getRootThreadGroup(tg.getParent());
- }
-
- private int getNonDaemonCount(ThreadGroup rootThreadGroup) {
- Thread[] threads = new Thread[rootThreadGroup.activeCount()];
- rootThreadGroup.enumerate(threads);
- int nonDameonCount = 0;
- for (Thread t : threads)
- if (t != null && !t.isDaemon())
- nonDameonCount++;
- return nonDameonCount;
- }
-
- private Thread findGogoShellThread(ThreadGroup rootThreadGroup) {
- Thread[] threads = new Thread[rootThreadGroup.activeCount()];
- rootThreadGroup.enumerate(threads, true);
- for (Thread thread : threads) {
- if (thread.getName().equals("pipe-gosh --login --noshutdown"))
- return thread;
- }
- return null;
- }
-
-}
\ No newline at end of file