Search in sources :

Example 1 with ApplicationHandle

use of org.osgi.service.application.ApplicationHandle in project knime-core by knime.

the class ProfileManagerTest method setup.

/**
 * Sets up the test environment.
 */
@Before
public void setup() {
    String app = "Unknown";
    Bundle myself = FrameworkUtil.getBundle(getClass());
    if (myself != null) {
        BundleContext ctx = myself.getBundleContext();
        ServiceReference<ApplicationHandle> ser = ctx.getServiceReference(ApplicationHandle.class);
        if (ser != null) {
            ApplicationHandle appHandle = ctx.getService(ser);
            app = appHandle.getInstanceId();
            ctx.ungetService(ser);
        }
    }
    // Only KNIME applications set new default preferences
    assumeThat("Not started with a KNIME application", app, containsString(".knime."));
}
Also used : ApplicationHandle(org.osgi.service.application.ApplicationHandle) Bundle(org.osgi.framework.Bundle) StringContains.containsString(org.hamcrest.core.StringContains.containsString) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Example 2 with ApplicationHandle

use of org.osgi.service.application.ApplicationHandle in project knime-core by knime.

the class EclipseUtil method determineServerUsage.

/**
 * Checks whether this KNIME instance runs as an RMI application on the server.
 *
 * @return <code>true</code> if we are running on the server, <code>false</code> otherwise
 * @since 2.12
 */
public static boolean determineServerUsage() {
    Bundle myself = FrameworkUtil.getBundle(EclipseUtil.class);
    if (myself != null) {
        BundleContext ctx = myself.getBundleContext();
        ServiceReference<ApplicationHandle> ser = ctx.getServiceReference(ApplicationHandle.class);
        if (ser != null) {
            ApplicationHandle appHandle = ctx.getService(ser);
            String instanceId = appHandle.getInstanceId();
            boolean b = (instanceId != null) && instanceId.contains("KNIME_REMOTE_APPLICATION");
            ctx.ungetService(ser);
            return b;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
Also used : ApplicationHandle(org.osgi.service.application.ApplicationHandle) Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext)

Example 3 with ApplicationHandle

use of org.osgi.service.application.ApplicationHandle 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 4 with ApplicationHandle

use of org.osgi.service.application.ApplicationHandle 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 5 with ApplicationHandle

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

Aggregations

ApplicationHandle (org.osgi.service.application.ApplicationHandle)5 BundleContext (org.osgi.framework.BundleContext)3 ApplicationDescriptor (org.osgi.service.application.ApplicationDescriptor)3 HashMap (java.util.HashMap)2 Bundle (org.osgi.framework.Bundle)2 Collection (java.util.Collection)1 Map (java.util.Map)1 EnvironmentInfo (org.eclipse.osgi.service.environment.EnvironmentInfo)1 StringContains.containsString (org.hamcrest.core.StringContains.containsString)1 Before (org.junit.Before)1 BundleException (org.osgi.framework.BundleException)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1 ApplicationException (org.osgi.service.application.ApplicationException)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1