]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/src/org/argeo/osgi/boot/AdminThread.java
Merge remote-tracking branch 'origin/master' into v2.x
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / src / org / argeo / osgi / boot / AdminThread.java
1 package org.argeo.osgi.boot;
2
3 import java.io.File;
4
5 import org.osgi.framework.BundleContext;
6 import org.osgi.framework.launch.Framework;
7
8 /** Monitors the runtime and can shut it down. */
9 public class AdminThread extends Thread {
10 public final static String PROP_ARGEO_OSGI_SHUTDOWN_FILE = "argeo.osgi.shutdownFile";
11 private File shutdownFile;
12 private final BundleContext bundleContext;
13
14 public AdminThread(BundleContext bundleContext) {
15 super("OSGi Boot Admin");
16 this.bundleContext = bundleContext;
17 if (System.getProperty(PROP_ARGEO_OSGI_SHUTDOWN_FILE) != null) {
18 shutdownFile = new File(
19 System.getProperty(PROP_ARGEO_OSGI_SHUTDOWN_FILE));
20 if (!shutdownFile.exists()) {
21 shutdownFile = null;
22 OsgiBootUtils.warn("Shutdown file " + shutdownFile
23 + " not found, feature deactivated");
24 }
25 }
26 }
27
28 public void run() {
29 if (shutdownFile != null) {
30 // wait for file to be removed
31 while (shutdownFile.exists()) {
32 try {
33 Thread.sleep(1000);
34 } catch (InterruptedException e) {
35 e.printStackTrace();
36 }
37 }
38
39 Framework framework = (Framework) bundleContext.getBundle(0);
40 try {
41 // shutdown framework
42 framework.stop();
43 // wait 10 mins for shutdown
44 framework.waitForStop(10 * 60 * 1000);
45 // close VM
46 System.exit(0);
47 } catch (Exception e) {
48 e.printStackTrace();
49 System.exit(1);
50 }
51 }
52 }
53 }