Search in sources :

Example 1 with ApplicationException

use of org.osgi.service.application.ApplicationException in project equinox.bundles by eclipse-equinox.

the class EclipseAppHandle method run.

@Override
public Object run(Object context) throws Exception {
    if (context != null) {
        // always force the use of the context if it is not null
        arguments.put(IApplicationContext.APPLICATION_ARGS, context);
    } else {
        // get the context from the arguments
        context = arguments.get(IApplicationContext.APPLICATION_ARGS);
        if (context == null) {
            // if context is null then use the args from CommandLineArgs
            context = CommandLineArgs.getApplicationArgs();
            arguments.put(IApplicationContext.APPLICATION_ARGS, context);
        }
    }
    Object tempResult = null;
    try {
        Object app;
        synchronized (this) {
            if ((status & (EclipseAppHandle.FLAG_STARTING | EclipseAppHandle.FLAG_STOPPING)) == 0)
                throw new ApplicationException(ApplicationException.APPLICATION_INTERNAL_ERROR, NLS.bind(Messages.application_instance_stopped, getInstanceId()));
            // $NON-NLS-1$
            application = getConfiguration().createExecutableExtension("run");
            app = application;
            notifyAll();
        }
        if (app instanceof IApplication)
            tempResult = ((IApplication) app).start(this);
        else
            // $NON-NLS-1$
            tempResult = EclipseAppContainer.callMethodWithException(app, "run", new Class[] { Object.class }, new Object[] { context });
        if (tempResult == null)
            tempResult = NULL_RESULT;
    } finally {
        tempResult = setInternalResult(tempResult, false, null);
    }
    if (Activator.DEBUG)
        // $NON-NLS-1$
        System.out.println(NLS.bind(Messages.application_returned, (new String[] { getApplicationDescriptor().getApplicationId(), tempResult == null ? "null" : tempResult.toString() })));
    return tempResult;
}
Also used : IApplication(org.eclipse.equinox.app.IApplication) ApplicationException(org.osgi.service.application.ApplicationException)

Example 2 with ApplicationException

use of org.osgi.service.application.ApplicationException in project equinox.bundles by eclipse-equinox.

the class EclipseAppHandle method getExitValue.

@Override
public synchronized Object getExitValue(long timeout) throws ApplicationException, InterruptedException {
    if (handleRegistration == null && application == null)
        return result;
    long startTime = System.currentTimeMillis();
    long delay = timeout;
    while (!setResult && (delay > 0 || timeout == 0)) {
        // only wait for the specified amount of time
        wait(delay);
        if (timeout > 0)
            delay -= (System.currentTimeMillis() - startTime);
    }
    if (result == null)
        throw new ApplicationException(ApplicationException.APPLICATION_EXITVALUE_NOT_AVAILABLE);
    if (result == NULL_RESULT)
        return null;
    return result;
}
Also used : ApplicationException(org.osgi.service.application.ApplicationException)

Example 3 with ApplicationException

use of org.osgi.service.application.ApplicationException in project equinox.bundles by eclipse-equinox.

the class MainApplicationLauncher method run.

@Override
public Object run(Object context) throws Exception {
    appContainer.startDefaultApp(false);
    ApplicationRunnable mainHandle = getMainHandle();
    if (mainHandle != null)
        return mainHandle.run(context);
    throw new ApplicationException(ApplicationException.APPLICATION_INTERNAL_ERROR, Messages.application_noIdFound);
}
Also used : ApplicationRunnable(org.eclipse.osgi.service.runnable.ApplicationRunnable) ApplicationException(org.osgi.service.application.ApplicationException)

Example 4 with ApplicationException

use of org.osgi.service.application.ApplicationException in project equinox.framework by eclipse-equinox.

the class RelaunchApp method start.

@Override
public Object start(IApplicationContext context) throws Exception {
    final Map arguments = context.getArguments();
    // Setting eclipse.allowAppRelaunch to true at runtime should allow us to launch
    // multiple applications in sequence
    ServiceReference<EnvironmentInfo> envref = OSGiTestsActivator.getContext().getServiceReference(EnvironmentInfo.class);
    EnvironmentInfo env = OSGiTestsActivator.getContext().getService(envref);
    if (Boolean.valueOf(env.getProperty("eclipse.allowAppRelaunch"))) {
        // $NON-NLS-1$
        throw new AssertionError("eclipse.allowAppRelaunch should not be set initially");
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    env.setProperty("eclipse.allowAppRelaunch", "true");
    OSGiTestsActivator.getContext().ungetService(envref);
    // Get a handle for the running application so we can wait for it to exit
    ServiceReference<ApplicationHandle> thisAppRef = OSGiTestsActivator.getContext().getServiceReference(ApplicationHandle.class);
    ApplicationHandle thisAppHandle = OSGiTestsActivator.getContext().getService(thisAppRef);
    new // $NON-NLS-1$
    Thread(// $NON-NLS-1$
    "launcher") {

        public void run() {
            // Wait for this application to exit
            try {
                thisAppHandle.getExitValue(0);
            } catch (ApplicationException e) {
            // does not occur for timeout 0
            } catch (InterruptedException e) {
                // I don't think this should occur
                e.printStackTrace();
            }
            // Get the descriptor for the actual test runner application.
            // Need a test runner that runs in the main thread to avoid race conditions.
            Collection<ServiceReference<ApplicationDescriptor>> testAppRefs = null;
            try {
                testAppRefs = OSGiTestsActivator.getContext().getServiceReferences(org.osgi.service.application.ApplicationDescriptor.class, // $NON-NLS-1$ //$NON-NLS-2$
                "(" + Constants.SERVICE_PID + "=org.eclipse.pde.junit.runtime.nonuithreadtestapplication)");
            } catch (InvalidSyntaxException e) {
                // shouldn't happen, the hardcoded filter expression
                // should be syntactically correct
                e.printStackTrace();
            }
            ServiceReference<ApplicationDescriptor> testAppRef = testAppRefs.iterator().next();
            ApplicationDescriptor testAppDescriptor = OSGiTestsActivator.getContext().getService(testAppRef);
            // and thereby confirm that relaunching works.
            try {
                ApplicationHandle testAppHandle;
                // after a delay when that happens.
                while (true) {
                    try {
                        testAppHandle = testAppDescriptor.launch(arguments);
                        break;
                    } catch (IllegalStateException e) {
                        Thread.sleep(100);
                    }
                }
                // Wait for the test application to exit
                testAppHandle.getExitValue(0);
            } catch (ApplicationException | InterruptedException e) {
                // ApplicationException "The main thread is not available to launch the
                // application" can happen when the test fails
                e.printStackTrace();
            } finally {
                OSGiTestsActivator.getContext().ungetService(thisAppRef);
                OSGiTestsActivator.getContext().ungetService(testAppRef);
                try {
                    // This will not return but cause the process to terminate
                    OSGiTestsActivator.getContext().getBundle(0).stop();
                } catch (BundleException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
    // mistakenly succeed.
    return null;
}
Also used : ApplicationDescriptor(org.osgi.service.application.ApplicationDescriptor) ServiceReference(org.osgi.framework.ServiceReference) ApplicationHandle(org.osgi.service.application.ApplicationHandle) ApplicationException(org.osgi.service.application.ApplicationException) EnvironmentInfo(org.eclipse.osgi.service.environment.EnvironmentInfo) Collection(java.util.Collection) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleException(org.osgi.framework.BundleException) Map(java.util.Map)

Aggregations

ApplicationException (org.osgi.service.application.ApplicationException)4 Collection (java.util.Collection)1 Map (java.util.Map)1 IApplication (org.eclipse.equinox.app.IApplication)1 EnvironmentInfo (org.eclipse.osgi.service.environment.EnvironmentInfo)1 ApplicationRunnable (org.eclipse.osgi.service.runnable.ApplicationRunnable)1 BundleException (org.osgi.framework.BundleException)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1 ApplicationDescriptor (org.osgi.service.application.ApplicationDescriptor)1 ApplicationHandle (org.osgi.service.application.ApplicationHandle)1