use of org.osgi.framework.ServiceEvent in project felix by apache.
the class ServiceRegistry method unregisterService.
/**
* Unregister a service.
* @param reg : the service registration to unregister
*/
public void unregisterService(ServiceRegistrationImpl reg) {
m_regs.remove(reg);
fireServiceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, reg.getReference()));
}
use of org.osgi.framework.ServiceEvent in project felix by apache.
the class Activator method start.
// /////////////////////////////////////
// BundleActivator //
// /////////////////////////////////////
public void start(BundleContext context) {
m_context = context;
// Listen for factory service events.
ServiceListener sl = new ServiceListener() {
public void serviceChanged(ServiceEvent event) {
ServiceReference ref = event.getServiceReference();
Object svcObj = m_context.getService(ref);
if (event.getType() == ServiceEvent.REGISTERED) {
synchronized (Activator.this) {
// !!!!!!!!!! ORDER MATTERS (Inheritance pb)
if (!m_pluginList.contains(svcObj)) {
if (svcObj instanceof CommonPlugin) {
m_commonpluginList.add(svcObj);
firePropertyChangedEvent(CommonPlugin.COMMON_PLUGIN_ADDED, null, svcObj);
} else if (svcObj instanceof Plugin) {
m_pluginList.add(svcObj);
firePropertyChangedEvent(Plugin.PLUGIN_ADDED, null, svcObj);
}
}
}
} else if (event.getType() == ServiceEvent.UNREGISTERING) {
synchronized (Activator.this) {
removePropertyChangeListener((PropertyChangeListener) svcObj);
if (svcObj instanceof CommonPlugin) {
m_commonpluginList.remove(svcObj);
firePropertyChangedEvent(CommonPlugin.COMMON_PLUGIN_REMOVED, null, svcObj);
} else if (svcObj instanceof Plugin) {
m_pluginList.remove(svcObj);
firePropertyChangedEvent(Plugin.PLUGIN_REMOVED, null, svcObj);
}
}
} else {
m_context.ungetService(ref);
}
}
};
try {
m_context.addServiceListener(sl, "(|(objectClass=" + Plugin.class.getName() + ")(objectClass=" + CommonPlugin.class.getName() + "))");
} catch (InvalidSyntaxException ex) {
System.err.println("ShellGuiActivator: Cannot add service listener.");
System.err.println("ShellGuiActivator: " + ex);
}
// Create and display the frame.
if (m_frame == null) {
m_frame = new JFrame("OSGi GUI Remote Manager");
m_frame.setUndecorated(true);
m_frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
m_frame.setIconImage(Toolkit.getDefaultToolkit().getImage(m_context.getBundle().getResource("images/logo.gif")));
// m_frame.setResizable(false);
// m_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// TODO : add a windowListener and use a Preferences service to save screen size
m_frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
JFrame jf = (JFrame) we.getWindow();
System.out.println(" Console.gui : window closing (" + jf.getSize().height + "*" + jf.getSize().width + ")");
}
public void windowClosed(WindowEvent we) {
JFrame jf = (JFrame) we.getWindow();
System.out.println(" Console.gui : window closed (" + jf.getSize().height + "*" + jf.getSize().width + ")");
}
});
Dimension maxdim = m_frame.getToolkit().getScreenSize();
int m_width = maxdim.width - 100;
int m_height = maxdim.height - 100;
m_frame.setBounds((int) ((maxdim.width - m_width) / 2), 20, m_width, m_height);
// Right panel
JSplitPane rightSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new NodePanel(this, context), new CommonPanel(this));
rightSplitPane.setOneTouchExpandable(true);
rightSplitPane.setDividerLocation((int) (m_height * 2 / 3));
// General Panel
this.nodesTree = new NodesTree(this, context);
JSplitPane gSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this.nodesTree, rightSplitPane);
gSplitPane.setOneTouchExpandable(true);
gSplitPane.setDividerLocation((int) (m_width / 4));
m_frame.getContentPane().add(gSplitPane);
}
// Now try to manually initialize the plugin list
// since some might already be available.
// initializePlugins();
m_frame.setVisible(true);
this.nodesTree.runDiscovery();
}
use of org.osgi.framework.ServiceEvent in project karaf by apache.
the class MavenTest method awaitMavenResolver.
/**
* Invoke config admin task and await reregistration of {@link MavenResolver} service
*/
private void awaitMavenResolver(Runnable task) throws Exception {
final CountDownLatch latch = new CountDownLatch(2);
ServiceListener listener = new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
if (event.getType() == ServiceEvent.UNREGISTERING || event.getType() == ServiceEvent.REGISTERED) {
latch.countDown();
}
}
};
bundleContext.addServiceListener(listener, "(objectClass=org.ops4j.pax.url.mvn.MavenResolver)");
try {
task.run();
assertTrue(latch.await(5, TimeUnit.SECONDS));
} finally {
bundleContext.removeServiceListener(listener);
}
}
use of org.osgi.framework.ServiceEvent in project karaf by apache.
the class GuardProxyCatalogTest method testHandleServiceModified.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleServiceModified() throws Exception {
Dictionary<String, Object> config = new Hashtable<>();
config.put(Constants.SERVICE_PID, "test.1.2.3");
config.put("service.guard", "(objectClass=" + TestServiceAPI.class.getName() + ")");
config.put("doit", "role.1");
Dictionary<String, Object> config2 = new Hashtable<>();
config2.put(Constants.SERVICE_PID, "test.1.2.4");
config2.put("service.guard", "(objectClass=" + TestServiceAPI2.class.getName() + ")");
config2.put("doit", "role.2");
BundleContext bc = mockConfigAdminBundleContext(config, config2);
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
// The service being proxied has these properties
long serviceID = 1L;
final Hashtable<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(Constants.OBJECTCLASS, new String[] { TestServiceAPI.class.getName(), TestServiceAPI2.class.getName() });
serviceProps.put(Constants.SERVICE_ID, serviceID);
// will be overwritten
serviceProps.put(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY, Arrays.asList("someone"));
Object myObject = new Object();
serviceProps.put("foo.bar", myObject);
BundleContext providerBC = EasyMock.createNiceMock(BundleContext.class);
EasyMock.expect(providerBC.registerService(EasyMock.aryEq(new String[] { TestServiceAPI.class.getName(), TestServiceAPI2.class.getName() }), EasyMock.anyObject(), EasyMock.anyObject(Dictionary.class))).andAnswer((IAnswer) () -> {
final Dictionary props = (Dictionary) EasyMock.getCurrentArguments()[2];
assertEquals(Boolean.TRUE, props.get(GuardProxyCatalog.PROXY_SERVICE_KEY));
ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
ServiceReference sr = mockServiceReference(props);
EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
reg.setProperties(EasyMock.isA(Dictionary.class));
EasyMock.expectLastCall().andAnswer(() -> {
// Push the update into the service reference
ArrayList<String> oldKeys = Collections.list(props.keys());
for (String key : oldKeys) {
props.remove(key);
}
Dictionary<String, Object> newProps = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[0];
for (String key : Collections.list(newProps.keys())) {
props.put(key, newProps.get(key));
}
return null;
}).once();
EasyMock.replay(reg);
return reg;
}).anyTimes();
EasyMock.replay(providerBC);
// In some cases the proxy-creating code is looking for a classloader (e.g. when run through
// a coverage tool such as EclEmma). This will satisfy that.
BundleWiring bw = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
EasyMock.replay(bw);
// The mock bundle that provides the original service (and also the proxy is registered with this)
Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
EasyMock.replay(providerBundle);
ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
gpc.proxyIfNotAlreadyProxied(sr);
GuardProxyCatalog.CreateProxyRunnable runnable = gpc.createProxyQueue.take();
runnable.run(getProxyManager());
ServiceRegistrationHolder holder = gpc.proxyMap.get(serviceID);
ServiceRegistration<?> reg = holder.registration;
for (String key : serviceProps.keySet()) {
if (GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY.equals(key)) {
assertEquals(new HashSet(Arrays.asList("role.1", "role.2")), reg.getReference().getProperty(key));
} else {
assertEquals(serviceProps.get(key), reg.getReference().getProperty(key));
}
}
assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
// now change the original service and let the proxy react
serviceProps.put("test", "property");
assertEquals("Precondition, the mocked reference should have picked up this change", "property", sr.getProperty("test"));
gpc.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, sr));
assertEquals("Changing the service should not change the number of proxies", 1, gpc.proxyMap.size());
for (String key : serviceProps.keySet()) {
if (GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY.equals(key)) {
assertEquals(new HashSet(Arrays.asList("role.1", "role.2")), reg.getReference().getProperty(key));
} else {
assertEquals(serviceProps.get(key), reg.getReference().getProperty(key));
}
}
assertEquals("property", reg.getReference().getProperty("test"));
assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
}
use of org.osgi.framework.ServiceEvent in project karaf by apache.
the class GuardProxyCatalogTest method testHandleServiceModified2.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleServiceModified2() throws Exception {
// no configuration used in this test...
BundleContext bc = mockConfigAdminBundleContext();
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
// The service being proxied has these properties
long serviceID = 1L;
final Hashtable<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(Constants.OBJECTCLASS, new String[] { TestServiceAPI.class.getName() });
serviceProps.put(Constants.SERVICE_ID, serviceID);
BundleContext providerBC = EasyMock.createNiceMock(BundleContext.class);
EasyMock.expect(providerBC.registerService(EasyMock.aryEq(new String[] { TestServiceAPI.class.getName() }), EasyMock.anyObject(), EasyMock.anyObject(Dictionary.class))).andAnswer((IAnswer) () -> {
final Dictionary props = (Dictionary) EasyMock.getCurrentArguments()[2];
assertEquals(Boolean.TRUE, props.get(GuardProxyCatalog.PROXY_SERVICE_KEY));
ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
ServiceReference sr = mockServiceReference(props);
EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
reg.setProperties(EasyMock.isA(Dictionary.class));
EasyMock.expectLastCall().andAnswer(() -> {
// Push the update into the service reference
ArrayList<String> oldKeys = Collections.list(props.keys());
for (String key : oldKeys) {
props.remove(key);
}
Dictionary<String, Object> newProps = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[0];
for (String key : Collections.list(newProps.keys())) {
props.put(key, newProps.get(key));
}
return null;
}).once();
EasyMock.replay(reg);
return reg;
}).anyTimes();
EasyMock.replay(providerBC);
// In some cases the proxy-creating code is looking for a classloader (e.g. when run through
// a coverage tool such as EclEmma). This will satisfy that.
BundleWiring bw = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
EasyMock.replay(bw);
// The mock bundle that provides the original service (and also the proxy is registered with this)
Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
EasyMock.replay(providerBundle);
ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
gpc.proxyIfNotAlreadyProxied(sr);
GuardProxyCatalog.CreateProxyRunnable runnable = gpc.createProxyQueue.take();
runnable.run(getProxyManager());
ServiceRegistrationHolder holder = gpc.proxyMap.get(serviceID);
ServiceRegistration<?> reg = holder.registration;
assertFalse("No roles defined for this service using configuration, so roles property should not be set", Arrays.asList(reg.getReference().getPropertyKeys()).contains(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
for (String key : serviceProps.keySet()) {
assertEquals(serviceProps.get(key), reg.getReference().getProperty(key));
}
assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
// now change the original service and let the proxy react
serviceProps.put(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY, "foobar");
assertEquals("Precondition, the mocked reference should have picked up this change", "foobar", sr.getProperty(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
gpc.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, sr));
assertEquals("Changing the service should not change the number of proxies", 1, gpc.proxyMap.size());
assertFalse("The roles property set on the modified service should have been removed", Arrays.asList(reg.getReference().getPropertyKeys()).contains(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
}
Aggregations