1 package org
.argeo
.cms
.internal
.osgi
;
4 * Workaround for killing Gogo shell by system shutdown.
6 * @see "https://issues.apache.org/jira/browse/FELIX-4208"
8 class GogoShellKiller
extends Thread
{
10 public GogoShellKiller() {
11 super("Gogo Shell Killer");
17 ThreadGroup rootTg
= getRootThreadGroup(null);
18 Thread gogoShellThread
= findGogoShellThread(rootTg
);
19 if (gogoShellThread
== null) // no need to bother if it is not here
21 while (getNonDaemonCount(rootTg
) > 2) {
24 } catch (InterruptedException e
) {
28 gogoShellThread
= findGogoShellThread(rootTg
);
29 if (gogoShellThread
== null)
32 // No non-deamon threads left, forcibly halting the VM
33 //Runtime.getRuntime().halt(0);
36 private ThreadGroup
getRootThreadGroup(ThreadGroup tg
) {
38 tg
= Thread
.currentThread().getThreadGroup();
39 if (tg
.getParent() == null)
42 return getRootThreadGroup(tg
.getParent());
45 private int getNonDaemonCount(ThreadGroup rootThreadGroup
) {
46 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
47 rootThreadGroup
.enumerate(threads
);
48 int nonDameonCount
= 0;
49 for (Thread t
: threads
)
50 if (t
!= null && !t
.isDaemon())
52 return nonDameonCount
;
55 private Thread
findGogoShellThread(ThreadGroup rootThreadGroup
) {
56 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
57 rootThreadGroup
.enumerate(threads
, true);
58 for (Thread thread
: threads
) {
59 if (thread
.getName().equals("pipe-gosh --login --noshutdown"))