use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.
the class EclipseStarter method updateSplash.
private static void updateSplash(Semaphore semaphore, StartupEventListener listener) throws InterruptedException {
ServiceTracker<StartupMonitor, StartupMonitor> monitorTracker = new ServiceTracker<>(context, StartupMonitor.class.getName(), null);
try {
monitorTracker.open();
} catch (IllegalStateException e) {
// do nothing; this can happen if the framework shutdown
return;
}
try {
while (true) {
StartupMonitor monitor = monitorTracker.getService();
if (monitor != null) {
try {
monitor.update();
} catch (Throwable e) {
// ignore exceptions thrown by the monitor
}
}
// can we acquire the semaphore yet?
if (semaphore.tryAcquire(50, TimeUnit.MILLISECONDS))
// done
break;
// else still working, spin another update
}
} finally {
if (listener != null) {
try {
context.removeBundleListener(listener);
monitorTracker.close();
} catch (IllegalStateException e) {
// do nothing; this can happen if the framework shutdown
}
}
}
}
use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.
the class Activator method start.
public void start(BundleContext context) throws Exception {
final boolean[] serviceChanged = { false };
ServiceListener listener = new ServiceListener() {
public void serviceChanged(ServiceEvent event) {
serviceChanged[0] = true;
}
};
context.addServiceListener(listener, "(&(objectClass=java.lang.String)(test=*))");
final boolean[] modifiedService = { false };
ServiceTracker tracker = new ServiceTracker(context, FrameworkUtil.createFilter("(&(objectClass=java.lang.String)(test=*))"), new ServiceTrackerCustomizer() {
public Object addingService(ServiceReference reference) {
return reference;
}
public void modifiedService(ServiceReference reference, Object service) {
modifiedService[0] = true;
}
public void removedService(ServiceReference reference, Object service) {
// TODO Auto-generated method stub
}
});
tracker.open();
Hashtable props = new Hashtable();
props.put("test", "value1");
ServiceRegistration registration = context.registerService(String.class.getName(), "test", props);
props.put("test", "value2");
registration.setProperties(props);
if (!serviceChanged[0])
throw new Exception("did not call service listener");
if (!modifiedService[0])
throw new Exception("did not call tracker customer");
}
use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.
the class ApplicationAdminTest method testDestroyBeforeStart01.
public void testDestroyBeforeStart01() throws InvalidSyntaxException {
// $NON-NLS-1$
ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".simpleApp");
HashMap args = getArguments();
HashMap results = (HashMap) args.get(testResults);
ServiceTrackerCustomizer trackerCustomizer = new ServiceTrackerCustomizer() {
public Object addingService(ServiceReference reference) {
ApplicationHandle handle = (ApplicationHandle) getContext().getService(reference);
handle.destroy();
return handle;
}
public void modifiedService(ServiceReference reference, Object service) {
// nothing
}
public void removedService(ServiceReference reference, Object service) {
// nothing
}
};
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
ServiceTracker tracker = new ServiceTracker(getContext(), FrameworkUtil.createFilter("(&(objectClass=" + ApplicationHandle.class.getName() + ")(" + ApplicationHandle.APPLICATION_DESCRIPTOR + "=" + app.getApplicationId() + "))"), trackerCustomizer);
tracker.open();
try {
ApplicationHandle handle = app.launch(args);
handle.destroy();
} catch (Throwable e) {
// $NON-NLS-1$
fail("failed to launch simpleApp", e);
} finally {
tracker.close();
}
String result = (String) results.get(simpleResults);
// $NON-NLS-1$
assertNull("Check application result", result);
}
use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.
the class ApplicationAdminTest method testPersistentSchedule02.
public void testPersistentSchedule02() throws InvalidSyntaxException {
// $NON-NLS-1$
ScheduledApplication scheduledApp = getScheduleApplication("schedule.1", true);
ApplicationHandleTracker handleTracker = new ApplicationHandleTracker(getContext());
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
ServiceTracker tracker = new ServiceTracker(getContext(), FrameworkUtil.createFilter("(&(objectClass=" + ApplicationHandle.class.getName() + ")(" + ApplicationHandle.APPLICATION_DESCRIPTOR + "=" + scheduledApp.getApplicationDescriptor().getApplicationId() + "))"), handleTracker);
tracker.open();
try {
ApplicationHandle handle = (ApplicationHandle) tracker.waitForService(61000);
// $NON-NLS-1$
assertNotNull("Should find an application handle", handle);
handleTracker.waitForEvent(handle.getInstanceId(), ApplicationHandle.RUNNING);
handle.destroy();
handleTracker.waitForEvent(handle.getInstanceId(), REMOVED);
HashMap results = null;
try {
results = (HashMap) handle.getExitValue(1000);
} catch (ApplicationException e) {
// $NON-NLS-1$
fail("Unexpected application exception waiting for an exit value", e);
}
// $NON-NLS-1$
assertNotNull("Null results", results);
HashMap args = new HashMap();
// $NON-NLS-1$
args.put("test.arg1", Boolean.TRUE);
// $NON-NLS-1$
args.put("test.arg2", new Integer(34));
// $NON-NLS-1$
args.put("test.arg3", new Long(34));
for (Iterator iEntries = args.entrySet().iterator(); iEntries.hasNext(); ) {
Map.Entry entry = (Map.Entry) iEntries.next();
// $NON-NLS-1$
assertEquals("key: " + entry.getKey(), entry.getValue(), results.get(entry.getKey()));
}
} catch (InterruptedException e) {
// $NON-NLS-1$
fail("got interupted", e);
} finally {
tracker.close();
scheduledApp.remove();
}
}
use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.
the class ApplicationAdminTest method testFailedApplication01.
public void testFailedApplication01() throws InvalidSyntaxException {
// $NON-NLS-1$
ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".failedApp");
ApplicationHandleTracker handleTracker = new ApplicationHandleTracker(getContext());
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
ServiceTracker tracker = new ServiceTracker(getContext(), FrameworkUtil.createFilter("(&(objectClass=" + ApplicationHandle.class.getName() + ")(" + ApplicationHandle.APPLICATION_DESCRIPTOR + "=" + app.getApplicationId() + "))"), handleTracker);
tracker.open();
try {
ApplicationHandle handle = app.launch(null);
handleTracker.waitForEvent(handle.getInstanceId(), REMOVED);
String[][] events = handleTracker.getEvents();
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String[][] expected = new String[][] { new String[] { handle.getInstanceId(), "org.eclipse.equinox.app.starting" }, new String[] { handle.getInstanceId(), ApplicationHandle.STOPPING }, new String[] { handle.getInstanceId(), "org.eclipse.equinox.app.stopped" }, new String[] { handle.getInstanceId(), "removed" } };
// $NON-NLS-1$
assertEquals("Check expected # events", expected.length, events.length);
for (int i = 0; i < events.length; i++) {
// $NON-NLS-1$
assertEquals("Check expected event id for #" + i, expected[i][0], events[i][0]);
// $NON-NLS-1$
assertEquals("Check expected event state for #" + i, expected[i][1], events[i][1]);
}
} catch (ApplicationException e) {
// $NON-NLS-1$
fail("failed to launch application", e);
} finally {
tracker.close();
}
}
Aggregations