Search in sources :

Example 1 with ApplicationDescriptor

use of org.osgi.service.application.ApplicationDescriptor in project hale by halestudio.

the class ApplicationUtil method launchApplication.

/**
 * Launch an application inside the current framework.
 *
 * Please note that launching an application like this may fail if another
 * application is running (that is a global singleton).
 *
 * @param appId the application identifier
 * @param argList the arguments
 * @return the application return value
 * @throws ApplicationException if the application cannot be launched
 * @throws InterruptedException if the thread was interrupted while waiting
 *             for the application termination
 */
public static Object launchApplication(String appId, List<String> argList) throws ApplicationException, InterruptedException {
    BundleContext context = Activator.getContext();
    ServiceTracker<ApplicationDescriptor, ?> applicationDescriptors = new ServiceTracker<>(context, ApplicationDescriptor.class.getName(), null);
    applicationDescriptors.open();
    try {
        ServiceReference<ApplicationDescriptor> application = getApplication(applicationDescriptors.getServiceReferences(), appId, ApplicationDescriptor.APPLICATION_PID, false);
        if (application == null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new IllegalStateException("\"" + appId + "\" does not exist or is ambigous.");
        } else {
            String[] args = argList.size() == 0 ? null : (String[]) argList.toArray(new String[argList.size()]);
            try {
                Map<String, Object> launchArgs = new HashMap<>(1);
                if (args != null) {
                    launchArgs.put(IApplicationContext.APPLICATION_ARGS, args);
                }
                ApplicationDescriptor appDesc = (context.getService(application));
                ApplicationHandle handle = appDesc.launch(launchArgs);
                return handle.getExitValue(0);
            } finally {
                context.ungetService(application);
            }
        }
    } finally {
        applicationDescriptors.close();
    }
}
Also used : ApplicationHandle(org.osgi.service.application.ApplicationHandle) ServiceTracker(org.osgi.util.tracker.ServiceTracker) HashMap(java.util.HashMap) ApplicationDescriptor(org.osgi.service.application.ApplicationDescriptor) BundleContext(org.osgi.framework.BundleContext)

Example 2 with ApplicationDescriptor

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

the class EclipseScheduledApplication method handleEvent.

@Override
public synchronized void handleEvent(Event event) {
    try {
        if (removed)
            return;
        ApplicationDescriptor desc = getApplicationDescriptor();
        if (desc == null)
            // we must return and keep the scheduled app incase the application comes back
            return;
        desc.launch(getArguments(event));
    } catch (Exception e) {
        String message = NLS.bind(Messages.scheduled_app_launch_error, sr);
        Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.WARNING, 0, message, 0, e, null));
        // return here to avoid removing non-recurring apps when an error occurs
        return;
    }
    if (!isRecurring())
        remove();
}
Also used : FrameworkLogEntry(org.eclipse.osgi.framework.log.FrameworkLogEntry) ApplicationDescriptor(org.osgi.service.application.ApplicationDescriptor)

Example 3 with ApplicationDescriptor

use of org.osgi.service.application.ApplicationDescriptor 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)

Example 4 with ApplicationDescriptor

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

the class ApplicationRelaunchTest method testRelaunch.

public void testRelaunch() {
    // this is the same as ApplicationAdminTest.testSimpleApp() (but launched
    // through a different test runner app RelaunchApp which is the thing being
    // tested)
    // $NON-NLS-1$
    ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".simpleApp");
    HashMap args = getArguments();
    HashMap results = (HashMap) args.get(testResults);
    try {
        ApplicationHandle handle = app.launch(args);
        handle.destroy();
    } catch (Throwable e) {
        // $NON-NLS-1$
        fail("failed to launch simpleApp", e);
    }
    String result = (String) results.get(simpleResults);
    // $NON-NLS-1$
    assertEquals("Check application result", SUCCESS, result);
}
Also used : ApplicationHandle(org.osgi.service.application.ApplicationHandle) HashMap(java.util.HashMap) ApplicationDescriptor(org.osgi.service.application.ApplicationDescriptor)

Example 5 with ApplicationDescriptor

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

the class ApplicationRelaunchTest method getApplication.

private ApplicationDescriptor getApplication(String appName) {
    try {
        BundleContext context = getContext();
        // $NON-NLS-1$
        assertNotNull("BundleContext is null!!", context);
        Class appDescClass = ApplicationDescriptor.class;
        // $NON-NLS-1$
        assertNotNull("ApplicationDescriptor.class is null!!", appDescClass);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        ServiceReference[] refs = context.getServiceReferences(appDescClass.getName(), "(" + ApplicationDescriptor.APPLICATION_PID + "=" + appName + ")");
        if (refs == null || refs.length == 0) {
            refs = getContext().getServiceReferences(ApplicationDescriptor.class.getName(), null);
            // $NON-NLS-1$
            String availableApps = "";
            if (refs != null) {
                for (int i = 0; i < refs.length; i++) {
                    availableApps += refs[i].getProperty(ApplicationDescriptor.APPLICATION_PID);
                    if (i < refs.length - 1)
                        // $NON-NLS-1$
                        availableApps += ",";
                }
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            fail("Could not find app pid: " + appName + " available apps are: " + availableApps);
        }
        ApplicationDescriptor result = (ApplicationDescriptor) getContext().getService(refs[0]);
        if (result != null)
            getContext().ungetService(refs[0]);
        else
            // $NON-NLS-1$
            fail("Could not get application descriptor service: " + appName);
        return result;
    } catch (InvalidSyntaxException e) {
        // $NON-NLS-1$
        fail("Could not create app filter", e);
    }
    return null;
}
Also used : InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ApplicationDescriptor(org.osgi.service.application.ApplicationDescriptor) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ApplicationDescriptor (org.osgi.service.application.ApplicationDescriptor)5 ApplicationHandle (org.osgi.service.application.ApplicationHandle)3 HashMap (java.util.HashMap)2 BundleContext (org.osgi.framework.BundleContext)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 ServiceReference (org.osgi.framework.ServiceReference)2 Collection (java.util.Collection)1 Map (java.util.Map)1 FrameworkLogEntry (org.eclipse.osgi.framework.log.FrameworkLogEntry)1 EnvironmentInfo (org.eclipse.osgi.service.environment.EnvironmentInfo)1 BundleException (org.osgi.framework.BundleException)1 ApplicationException (org.osgi.service.application.ApplicationException)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1