1

So I initiated a clean OSGI shutdown Best way to shutdown an OSGi Container (specifically equinox) I use the bundle.stop() way to achieve the same. Now the question arises if I call a bundle.stop() in case some critical failure happens, doing a clean shutdown means that I have a process exit code of 0, Is there any way that I can send out a exit code of 1 from the process after invoking a bundle.stop(), so that the process consumer knows that this was not a normal shutdown?

Thanks!

Community
  • 1
  • 1
javaresearcher
  • 101
  • 1
  • 4

1 Answers1

1

You should use the org.eclipse.equinox.app.IApplication interface, which gives you ability to return a result from the start() method, which is then returned as exit code from the Java process. In case you don't want to use this API, the following code, shows how Equinox itself controls the exit code of the Java process:

import org.eclipse.osgi.service.environment.EnvironmentInfo;

private static EnvironmentInfo getEnvironmentInfo() {
    BundleContext bc = Activator.getContext();
    if (bc == null)
        return null;
    ServiceReference infoRef = bc.getServiceReference(EnvironmentInfo.class.getName());
    if (infoRef == null)
        return null;
    EnvironmentInfo envInfo = (EnvironmentInfo) bc.getService(infoRef);
    if (envInfo == null)
        return null;
    bc.ungetService(infoRef);
    return envInfo;
}


public static void setExitCode(int exitCode) {
    String key = "eclipse.exitcode";
    String value = Integer.toString(exitCode); // the exit code
    EnvironmentInfo envInfo = getEnvironmentInfo();
    if (envInfo != null)
        envInfo.setProperty(key, value);
    else
        System.getProperties().setProperty(key, value);
}

The code above is not taken one for one, but it gives the idea.

rancidfishbreath
  • 3,944
  • 2
  • 30
  • 44
Danail Nachev
  • 19,231
  • 3
  • 21
  • 17
  • Thanks Danail, I made a somewhat similar implementation, setting a system property , listening for it and further propagating the exit code – javaresearcher Jul 13 '11 at 21:22