1 package org
.argeo
.cms
.internal
.kernel
;
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)
21 while (getNonDaemonCount(rootTg
) > 2) {
24 } catch (InterruptedException e
) {
28 gogoShellThread
= findGogoShellThread(rootTg
);
29 if (gogoShellThread
== null)
31 // No non-deamon threads left, forcibly halting the VM
32 Runtime
.getRuntime().halt(0);
35 private ThreadGroup
getRootThreadGroup(ThreadGroup tg
) {
37 tg
= Thread
.currentThread().getThreadGroup();
38 if (tg
.getParent() == null)
41 return getRootThreadGroup(tg
.getParent());
44 private int getNonDaemonCount(ThreadGroup rootThreadGroup
) {
45 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
46 rootThreadGroup
.enumerate(threads
);
47 int nonDameonCount
= 0;
48 for (Thread t
: threads
)
49 if (t
!= null && !t
.isDaemon())
51 return nonDameonCount
;
54 private Thread
findGogoShellThread(ThreadGroup rootThreadGroup
) {
55 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
56 rootThreadGroup
.enumerate(threads
, true);
57 for (Thread thread
: threads
) {
58 if (thread
.getName().equals("Gogo shell"))