Repository: aries-rsa Updated Branches: refs/heads/master e4bd3f93a -> bce388e25
[ARIES-1764] Also send events via eventadmin Project: http://git-wip-us.apache.org/repos/asf/aries-rsa/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-rsa/commit/bce388e2 Tree: http://git-wip-us.apache.org/repos/asf/aries-rsa/tree/bce388e2 Diff: http://git-wip-us.apache.org/repos/asf/aries-rsa/diff/bce388e2 Branch: refs/heads/master Commit: bce388e25e5291f37aa396dba275538801b84afa Parents: e4bd3f9 Author: Christian Schneider <[email protected]> Authored: Tue Nov 28 22:41:39 2017 +0100 Committer: Christian Schneider <[email protected]> Committed: Tue Nov 28 22:41:39 2017 +0100 ---------------------------------------------------------------------- itests/felix/pom.xml | 5 + .../aries/rsa/itests/felix/RsaTestBase.java | 1 + .../aries/rsa/provider/tcp/TcpEndpoint.java | 2 + rsa/bnd.bnd | 4 +- .../apache/aries/rsa/core/EventProducer.java | 120 -------------- .../aries/rsa/core/RemoteServiceAdminCore.java | 1 + .../aries/rsa/core/event/EventAdminSender.java | 72 ++++++++ .../aries/rsa/core/event/EventProducer.java | 123 ++++++++++++++ .../aries/rsa/core/EventProducerTest.java | 163 ------------------ .../rsa/core/RemoteServiceAdminCoreTest.java | 3 + .../aries/rsa/core/event/EventProducerTest.java | 166 +++++++++++++++++++ 11 files changed, 376 insertions(+), 284 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/itests/felix/pom.xml ---------------------------------------------------------------------- diff --git a/itests/felix/pom.xml b/itests/felix/pom.xml index 594d46f..482a9d6 100644 --- a/itests/felix/pom.xml +++ b/itests/felix/pom.xml @@ -29,6 +29,11 @@ <dependencies> <dependency> <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.eventadmin</artifactId> + <version>1.4.10</version> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.configadmin</artifactId> <version>1.8.14</version> </dependency> http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/itests/felix/src/test/java/org/apache/aries/rsa/itests/felix/RsaTestBase.java ---------------------------------------------------------------------- diff --git a/itests/felix/src/test/java/org/apache/aries/rsa/itests/felix/RsaTestBase.java b/itests/felix/src/test/java/org/apache/aries/rsa/itests/felix/RsaTestBase.java index b6bbbd9..051884b 100644 --- a/itests/felix/src/test/java/org/apache/aries/rsa/itests/felix/RsaTestBase.java +++ b/itests/felix/src/test/java/org/apache/aries/rsa/itests/felix/RsaTestBase.java @@ -118,6 +118,7 @@ public class RsaTestBase { systemProperty("pax.exam.osgi.unresolved.fail").value("true"), systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("INFO"), systemProperty("aries.rsa.hostname").value("localhost"), + mvn("org.apache.felix", "org.apache.felix.eventadmin"), mvn("org.apache.felix", "org.apache.felix.configadmin"), mvn("org.apache.felix", "org.apache.felix.scr"), mvn("org.apache.aries.rsa", "org.apache.aries.rsa.core"), http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/provider/tcp/src/main/java/org/apache/aries/rsa/provider/tcp/TcpEndpoint.java ---------------------------------------------------------------------- diff --git a/provider/tcp/src/main/java/org/apache/aries/rsa/provider/tcp/TcpEndpoint.java b/provider/tcp/src/main/java/org/apache/aries/rsa/provider/tcp/TcpEndpoint.java index 25f6d62..33b7e0c 100644 --- a/provider/tcp/src/main/java/org/apache/aries/rsa/provider/tcp/TcpEndpoint.java +++ b/provider/tcp/src/main/java/org/apache/aries/rsa/provider/tcp/TcpEndpoint.java @@ -19,6 +19,7 @@ package org.apache.aries.rsa.provider.tcp; import java.io.IOException; +import java.util.Arrays; import java.util.Map; import org.apache.aries.rsa.spi.Endpoint; @@ -41,6 +42,7 @@ public class TcpEndpoint implements Endpoint { String endpointId = String.format("tcp://%s:%s",hostName, tcpServer.getPort()); effectiveProperties.put(RemoteConstants.ENDPOINT_ID, endpointId); effectiveProperties.put(RemoteConstants.SERVICE_EXPORTED_CONFIGS, ""); + effectiveProperties.put(RemoteConstants.SERVICE_INTENTS, Arrays.asList("osgi.basic, osgi.async")); this.epd = new EndpointDescription(effectiveProperties); } http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/rsa/bnd.bnd ---------------------------------------------------------------------- diff --git a/rsa/bnd.bnd b/rsa/bnd.bnd index d4e599d..d22dddd 100644 --- a/rsa/bnd.bnd +++ b/rsa/bnd.bnd @@ -1,5 +1,7 @@ Bundle-Activator: org.apache.aries.rsa.core.Activator -Private-Package: org.apache.aries.rsa.core +Private-Package: \ + org.apache.aries.rsa.core,\ + org.apache.aries.rsa.core.event Export-Package: org.osgi.service.remoteserviceadmin Provide-Capability: osgi.service;\ objectClass:List<String>="org.osgi.service.remoteserviceadmin.RemoteServiceAdmin";\ http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/rsa/src/main/java/org/apache/aries/rsa/core/EventProducer.java ---------------------------------------------------------------------- diff --git a/rsa/src/main/java/org/apache/aries/rsa/core/EventProducer.java b/rsa/src/main/java/org/apache/aries/rsa/core/EventProducer.java deleted file mode 100644 index fb1addb..0000000 --- a/rsa/src/main/java/org/apache/aries/rsa/core/EventProducer.java +++ /dev/null @@ -1,120 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.aries.rsa.core; - -import java.util.List; - -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; -import org.osgi.service.remoteserviceadmin.ExportReference; -import org.osgi.service.remoteserviceadmin.ExportRegistration; -import org.osgi.service.remoteserviceadmin.ImportReference; -import org.osgi.service.remoteserviceadmin.ImportRegistration; -import org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent; -import org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class EventProducer { - - private static final Logger LOG = LoggerFactory.getLogger(EventProducer.class); - private final BundleContext bctx; - - public EventProducer(BundleContext bc) { - bctx = bc; - } - - protected void publishNotification(List<ExportRegistration> erl) { - for (ExportRegistration exportRegistration : erl) { - publishNotification(exportRegistration); - } - } - - protected void publishNotification(ExportRegistration er) { - if (er.getException() == null) { - notify(RemoteServiceAdminEvent.EXPORT_REGISTRATION, er.getExportReference(), null); - } else { - notify(RemoteServiceAdminEvent.EXPORT_ERROR, (ExportReference) null, er.getException()); - } - } - - protected void publishNotification(ImportRegistration ir) { - if (ir.getException() == null) { - notify(RemoteServiceAdminEvent.IMPORT_REGISTRATION, ir.getImportReference(), null); - } else { - notify(RemoteServiceAdminEvent.IMPORT_ERROR, (ImportReference) null, ir.getException()); - } - } - - public void notifyRemoval(ExportReference er) { - notify(RemoteServiceAdminEvent.EXPORT_UNREGISTRATION, er, null); - } - - public void notifyRemoval(ImportRegistration ir) { - notify(RemoteServiceAdminEvent.IMPORT_UNREGISTRATION, ir.getImportReference(), null); - } - - private void notify(int type, ExportReference er, Throwable ex) { - try { - RemoteServiceAdminEvent event = new RemoteServiceAdminEvent(type, bctx.getBundle(), er, ex); - notifyListeners(event); - } catch (IllegalStateException ise) { - LOG.debug("can't send notifications since bundle context is no longer valid"); - } - } - private void notify(int type, ImportReference ir, Throwable ex) { - try { - RemoteServiceAdminEvent event = new RemoteServiceAdminEvent(type, bctx.getBundle(), ir, ex); - notifyListeners(event); - } catch (IllegalStateException ise) { - LOG.debug("can't send notifications since bundle context is no longer valid"); - } - } - - @SuppressWarnings({ - "rawtypes", "unchecked" - }) - private void notifyListeners(RemoteServiceAdminEvent rsae) { - try { - ServiceReference[] listenerRefs = bctx.getServiceReferences( - RemoteServiceAdminListener.class.getName(), null); - if (listenerRefs != null) { - for (ServiceReference sref : listenerRefs) { - RemoteServiceAdminListener rsal = (RemoteServiceAdminListener)bctx.getService(sref); - if (rsal != null) { - try { - Bundle bundle = sref.getBundle(); - if (bundle != null) { - LOG.debug("notify RemoteServiceAdminListener {} of bundle {}", - rsal, bundle.getSymbolicName()); - rsal.remoteAdminEvent(rsae); - } - } finally { - bctx.ungetService(sref); - } - } - } - } - } catch (InvalidSyntaxException e) { - LOG.error(e.getMessage(), e); - } - } -} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/rsa/src/main/java/org/apache/aries/rsa/core/RemoteServiceAdminCore.java ---------------------------------------------------------------------- diff --git a/rsa/src/main/java/org/apache/aries/rsa/core/RemoteServiceAdminCore.java b/rsa/src/main/java/org/apache/aries/rsa/core/RemoteServiceAdminCore.java index fa04b10..3f2359f 100644 --- a/rsa/src/main/java/org/apache/aries/rsa/core/RemoteServiceAdminCore.java +++ b/rsa/src/main/java/org/apache/aries/rsa/core/RemoteServiceAdminCore.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.aries.rsa.core.event.EventProducer; import org.apache.aries.rsa.spi.DistributionProvider; import org.apache.aries.rsa.spi.Endpoint; import org.apache.aries.rsa.util.EndpointHelper; http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/rsa/src/main/java/org/apache/aries/rsa/core/event/EventAdminSender.java ---------------------------------------------------------------------- diff --git a/rsa/src/main/java/org/apache/aries/rsa/core/event/EventAdminSender.java b/rsa/src/main/java/org/apache/aries/rsa/core/event/EventAdminSender.java new file mode 100644 index 0000000..f42afc9 --- /dev/null +++ b/rsa/src/main/java/org/apache/aries/rsa/core/event/EventAdminSender.java @@ -0,0 +1,72 @@ +package org.apache.aries.rsa.core.event; + +import java.util.HashMap; +import java.util.Map; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.service.event.Event; +import org.osgi.service.event.EventAdmin; +import org.osgi.service.remoteserviceadmin.EndpointDescription; +import org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent; + +public class EventAdminSender { + private HashMap<Integer, String> typeToTopic; + private BundleContext context; + + public EventAdminSender(BundleContext context) { + this.context = context; + typeToTopic = new HashMap<>(); + typeToTopic.put(RemoteServiceAdminEvent.EXPORT_ERROR, "EXPORT_ERROR"); + typeToTopic.put(RemoteServiceAdminEvent.EXPORT_REGISTRATION, "EXPORT_REGISTRATION"); + typeToTopic.put(RemoteServiceAdminEvent.EXPORT_UNREGISTRATION, "EXPORT_UNREGISTRATION"); + typeToTopic.put(RemoteServiceAdminEvent.EXPORT_UPDATE, "EXPORT_UPDATE"); + typeToTopic.put(RemoteServiceAdminEvent.EXPORT_WARNING, "EXPORT_WARNING"); + typeToTopic.put(RemoteServiceAdminEvent.IMPORT_ERROR, "IMPORT_ERROR"); + typeToTopic.put(RemoteServiceAdminEvent.IMPORT_REGISTRATION, "IMPORT_REGISTRATION"); + typeToTopic.put(RemoteServiceAdminEvent.IMPORT_UNREGISTRATION, "IMPORT_UNREGISTRATION"); + typeToTopic.put(RemoteServiceAdminEvent.IMPORT_UPDATE, "IMPORT_UPDATE"); + typeToTopic.put(RemoteServiceAdminEvent.IMPORT_WARNING, "IMPORT_WARNING"); + } + + public void send(RemoteServiceAdminEvent rsaEvent) { + Event event = toEvent(rsaEvent); + ServiceReference<EventAdmin> sref = this.context.getServiceReference(EventAdmin.class); + if (sref != null) { + EventAdmin eventAdmin = this.context.getService(sref); + eventAdmin.postEvent(event); + this.context.ungetService(sref); + } + } + + private Event toEvent(RemoteServiceAdminEvent rsaEvent) { + String topic = getTopic(rsaEvent); + Map<String, Object> props = new HashMap<>(); + props.put("bundle", rsaEvent.getSource()); + props.put("bundle.id", rsaEvent.getSource().getBundleId()); + props.put("bundle.symbolicname", rsaEvent.getSource().getSymbolicName()); + props.put("bundle.version", rsaEvent.getSource().getVersion()); + props.put("bundle.signer", ""); // TODO What to put here + if (rsaEvent.getException() != null) { + props.put("exception", rsaEvent.getException()); + props.put("exception.class", rsaEvent.getException().getClass()); + props.put("exception.class", rsaEvent.getException().getMessage()); + } + if (rsaEvent.getExportReference() != null) { + EndpointDescription endpoint = rsaEvent.getExportReference().getExportedEndpoint(); + props.put("endpoint.framework.uuid", endpoint.getFrameworkUUID()); + props.put("endpoint.id", endpoint.getId()); + props.put("objectClass", endpoint.getInterfaces()); + } + if (rsaEvent.getImportReference() != null && rsaEvent.getImportReference().getImportedEndpoint() != null) { + props.put("service.imported.configs", rsaEvent.getImportReference().getImportedEndpoint().getConfigurationTypes()); + } + props.put("timestamp", System.currentTimeMillis()); + props.put("event", rsaEvent); + return new Event(topic, props); + } + + private String getTopic(RemoteServiceAdminEvent rsaEvent) { + return "org/osgi/service/remoteserviceadmin/" + typeToTopic.get(rsaEvent.getType()); + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/rsa/src/main/java/org/apache/aries/rsa/core/event/EventProducer.java ---------------------------------------------------------------------- diff --git a/rsa/src/main/java/org/apache/aries/rsa/core/event/EventProducer.java b/rsa/src/main/java/org/apache/aries/rsa/core/event/EventProducer.java new file mode 100644 index 0000000..1ad79d4 --- /dev/null +++ b/rsa/src/main/java/org/apache/aries/rsa/core/event/EventProducer.java @@ -0,0 +1,123 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.rsa.core.event; + +import java.util.List; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; +import org.osgi.service.remoteserviceadmin.ExportReference; +import org.osgi.service.remoteserviceadmin.ExportRegistration; +import org.osgi.service.remoteserviceadmin.ImportReference; +import org.osgi.service.remoteserviceadmin.ImportRegistration; +import org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent; +import org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EventProducer { + + private static final Logger LOG = LoggerFactory.getLogger(EventProducer.class); + private final BundleContext bctx; + private EventAdminSender eventAdminSender; + + public EventProducer(BundleContext bc) { + bctx = bc; + eventAdminSender = new EventAdminSender(bc); + } + + public void publishNotification(List<ExportRegistration> erl) { + for (ExportRegistration exportRegistration : erl) { + publishNotification(exportRegistration); + } + } + + protected void publishNotification(ExportRegistration er) { + if (er.getException() == null) { + notify(RemoteServiceAdminEvent.EXPORT_REGISTRATION, er.getExportReference(), null); + } else { + notify(RemoteServiceAdminEvent.EXPORT_ERROR, (ExportReference) null, er.getException()); + } + } + + public void publishNotification(ImportRegistration ir) { + if (ir.getException() == null) { + notify(RemoteServiceAdminEvent.IMPORT_REGISTRATION, ir.getImportReference(), null); + } else { + notify(RemoteServiceAdminEvent.IMPORT_ERROR, (ImportReference) null, ir.getException()); + } + } + + public void notifyRemoval(ExportReference er) { + notify(RemoteServiceAdminEvent.EXPORT_UNREGISTRATION, er, null); + } + + public void notifyRemoval(ImportRegistration ir) { + notify(RemoteServiceAdminEvent.IMPORT_UNREGISTRATION, ir.getImportReference(), null); + } + + private void notify(int type, ExportReference er, Throwable ex) { + try { + RemoteServiceAdminEvent event = new RemoteServiceAdminEvent(type, bctx.getBundle(), er, ex); + notifyListeners(event); + } catch (IllegalStateException ise) { + LOG.debug("can't send notifications since bundle context is no longer valid"); + } + } + private void notify(int type, ImportReference ir, Throwable ex) { + try { + RemoteServiceAdminEvent event = new RemoteServiceAdminEvent(type, bctx.getBundle(), ir, ex); + notifyListeners(event); + } catch (IllegalStateException ise) { + LOG.debug("can't send notifications since bundle context is no longer valid"); + } + } + + @SuppressWarnings({ + "rawtypes", "unchecked" + }) + private void notifyListeners(RemoteServiceAdminEvent rsae) { + try { + ServiceReference[] listenerRefs = bctx.getServiceReferences( + RemoteServiceAdminListener.class.getName(), null); + if (listenerRefs != null) { + for (ServiceReference sref : listenerRefs) { + RemoteServiceAdminListener rsal = (RemoteServiceAdminListener)bctx.getService(sref); + if (rsal != null) { + try { + Bundle bundle = sref.getBundle(); + if (bundle != null) { + LOG.debug("notify RemoteServiceAdminListener {} of bundle {}", + rsal, bundle.getSymbolicName()); + rsal.remoteAdminEvent(rsae); + } + } finally { + bctx.ungetService(sref); + } + } + } + } + } catch (InvalidSyntaxException e) { + LOG.error(e.getMessage(), e); + } + eventAdminSender.send(rsae); + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/rsa/src/test/java/org/apache/aries/rsa/core/EventProducerTest.java ---------------------------------------------------------------------- diff --git a/rsa/src/test/java/org/apache/aries/rsa/core/EventProducerTest.java b/rsa/src/test/java/org/apache/aries/rsa/core/EventProducerTest.java deleted file mode 100644 index 8b00534..0000000 --- a/rsa/src/test/java/org/apache/aries/rsa/core/EventProducerTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.aries.rsa.core; - -import java.util.Arrays; -import java.util.Dictionary; -import java.util.Hashtable; -import java.util.List; -import java.util.UUID; - -import org.apache.aries.rsa.spi.Endpoint; -import org.easymock.EasyMock; -import org.easymock.IAnswer; -import org.junit.Assert; -import org.junit.Test; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; -import org.osgi.service.remoteserviceadmin.EndpointDescription; -import org.osgi.service.remoteserviceadmin.ExportReference; -import org.osgi.service.remoteserviceadmin.ExportRegistration; -import org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent; -import org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public class EventProducerTest { - - - @Test - public void testPublishNotification() throws Exception { - RemoteServiceAdminCore rsaCore = EasyMock.createNiceMock(RemoteServiceAdminCore.class); - EasyMock.replay(rsaCore); - - final EndpointDescription epd = EasyMock.createNiceMock(EndpointDescription.class); - EasyMock.expect(epd.getServiceId()).andReturn(Long.MAX_VALUE).anyTimes(); - final String uuid = UUID.randomUUID().toString(); - EasyMock.expect(epd.getFrameworkUUID()).andReturn(uuid).anyTimes(); - EasyMock.expect(epd.getId()).andReturn("foo://bar").anyTimes(); - final List<String> interfaces = Arrays.asList("org.foo.Bar", "org.boo.Far"); - EasyMock.expect(epd.getInterfaces()).andReturn(interfaces).anyTimes(); - EasyMock.expect(epd.getConfigurationTypes()).andReturn(Arrays.asList("org.apache.cxf.ws")).anyTimes(); - EasyMock.replay(epd); - final ServiceReference sref = EasyMock.createNiceMock(ServiceReference.class); - EasyMock.replay(sref); - - final Bundle bundle = EasyMock.createNiceMock(Bundle.class); - EasyMock.expect(bundle.getBundleId()).andReturn(42L).anyTimes(); - EasyMock.expect(bundle.getSymbolicName()).andReturn("test.bundle").anyTimes(); - Dictionary<String, String> headers = new Hashtable<String, String>(); - headers.put("Bundle-Version", "1.2.3.test"); - EasyMock.expect(bundle.getHeaders()).andReturn(headers).anyTimes(); - EasyMock.replay(bundle); - - RemoteServiceAdminListener rsal = EasyMock.createNiceMock(RemoteServiceAdminListener.class); - rsal.remoteAdminEvent((RemoteServiceAdminEvent) EasyMock.anyObject()); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - RemoteServiceAdminEvent rsae = (RemoteServiceAdminEvent) EasyMock.getCurrentArguments()[0]; - Assert.assertNull(rsae.getException()); - Assert.assertEquals(RemoteServiceAdminEvent.EXPORT_REGISTRATION, rsae.getType()); - Assert.assertSame(bundle, rsae.getSource()); - ExportReference er = rsae.getExportReference(); - Assert.assertSame(epd, er.getExportedEndpoint()); - Assert.assertSame(sref, er.getExportedService()); - - return null; - } - }); - EasyMock.replay(rsal); - - ServiceReference rsalSref = EasyMock.createNiceMock(ServiceReference.class); - EasyMock.expect(rsalSref.getBundle()).andReturn(bundle).anyTimes(); - EasyMock.replay(rsalSref); - - BundleContext bc = EasyMock.createNiceMock(BundleContext.class); - EasyMock.expect(bc.getBundle()).andReturn(bundle).anyTimes(); - EasyMock.expect(bc.getServiceReferences(RemoteServiceAdminListener.class.getName(), null)) - .andReturn(new ServiceReference[] {rsalSref}).anyTimes(); - EasyMock.expect(bc.getService(rsalSref)).andReturn(rsal).anyTimes(); - Endpoint endpoint = EasyMock.mock(Endpoint.class); - EasyMock.expect(endpoint.description()).andReturn(epd); - EasyMock.replay(endpoint); - EasyMock.replay(bc); - EventProducer eventProducer = new EventProducer(bc); - - ExportRegistrationImpl ereg = new ExportRegistrationImpl(sref, endpoint, rsaCore); - eventProducer.publishNotification(ereg); - - EasyMock.verify(rsaCore, sref, bundle, rsal, rsalSref, bc); - } - - @Test - public void testPublishErrorNotification() throws Exception { - RemoteServiceAdminCore rsaCore = EasyMock.createNiceMock(RemoteServiceAdminCore.class); - EasyMock.replay(rsaCore); - - final EndpointDescription endpoint = EasyMock.createNiceMock(EndpointDescription.class); - EasyMock.expect(endpoint.getInterfaces()).andReturn(Arrays.asList("org.foo.Bar")).anyTimes(); - EasyMock.replay(endpoint); - final ServiceReference sref = EasyMock.createNiceMock(ServiceReference.class); - EasyMock.replay(sref); - - final Bundle bundle = EasyMock.createNiceMock(Bundle.class); - EasyMock.expect(bundle.getBundleId()).andReturn(42L).anyTimes(); - EasyMock.expect(bundle.getSymbolicName()).andReturn("test.bundle").anyTimes(); - EasyMock.expect(bundle.getHeaders()).andReturn(new Hashtable<String, String>()).anyTimes(); - EasyMock.replay(bundle); - - final Exception exportException = new Exception(); - - RemoteServiceAdminListener rsal = EasyMock.createNiceMock(RemoteServiceAdminListener.class); - rsal.remoteAdminEvent((RemoteServiceAdminEvent) EasyMock.anyObject()); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - RemoteServiceAdminEvent rsae = (RemoteServiceAdminEvent) EasyMock.getCurrentArguments()[0]; - Assert.assertSame(exportException, rsae.getException()); - Assert.assertEquals(RemoteServiceAdminEvent.EXPORT_ERROR, rsae.getType()); - Assert.assertSame(bundle, rsae.getSource()); - Assert.assertNull(rsae.getImportReference()); - Assert.assertNull(rsae.getExportReference()); - - return null; - } - }); - EasyMock.replay(rsal); - - ServiceReference rsalSref = EasyMock.createNiceMock(ServiceReference.class); - EasyMock.expect(rsalSref.getBundle()).andReturn(bundle).anyTimes(); - EasyMock.replay(rsalSref); - - BundleContext bc = EasyMock.createNiceMock(BundleContext.class); - - EasyMock.expect(bc.getBundle()).andReturn(bundle).anyTimes(); - EasyMock.expect(bc.getServiceReferences(RemoteServiceAdminListener.class.getName(), null)) - .andReturn(new ServiceReference[] {rsalSref}).anyTimes(); - EasyMock.expect(bc.getService(rsalSref)).andReturn(rsal).anyTimes(); - EasyMock.replay(bc); - EventProducer eventProducer = new EventProducer(bc); - - ExportRegistrationImpl ereg = new ExportRegistrationImpl(rsaCore, exportException); - eventProducer.publishNotification(Arrays.<ExportRegistration>asList(ereg)); - - EasyMock.verify(rsaCore, sref, bundle, rsal, rsalSref, bc); - } -} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/rsa/src/test/java/org/apache/aries/rsa/core/RemoteServiceAdminCoreTest.java ---------------------------------------------------------------------- diff --git a/rsa/src/test/java/org/apache/aries/rsa/core/RemoteServiceAdminCoreTest.java b/rsa/src/test/java/org/apache/aries/rsa/core/RemoteServiceAdminCoreTest.java index 79c6c76..4ef5102 100644 --- a/rsa/src/test/java/org/apache/aries/rsa/core/RemoteServiceAdminCoreTest.java +++ b/rsa/src/test/java/org/apache/aries/rsa/core/RemoteServiceAdminCoreTest.java @@ -45,6 +45,7 @@ import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; +import org.osgi.service.event.EventAdmin; import org.osgi.service.packageadmin.PackageAdmin; import org.osgi.service.remoteserviceadmin.EndpointDescription; import org.osgi.service.remoteserviceadmin.ExportRegistration; @@ -251,6 +252,7 @@ public class RemoteServiceAdminCoreTest { .andReturn(FrameworkUtil.createFilter("(service.id=51)")).anyTimes(); EasyMock.expect(bc.getProperty(org.osgi.framework.Constants.FRAMEWORK_UUID)).andReturn("1111"); EasyMock.expect(bc.getServiceReference(PackageAdmin.class)).andReturn(null); + EasyMock.expect(bc.getServiceReference(EventAdmin.class)).andReturn(null).atLeastOnce(); EasyMock.replay(bc); Map<String, Object> eProps = new HashMap<String, Object>(sProps); @@ -410,6 +412,7 @@ public class RemoteServiceAdminCoreTest { .andReturn(FrameworkUtil.createFilter("(service.id=51)")).anyTimes(); EasyMock.expect(bc.getProperty(org.osgi.framework.Constants.FRAMEWORK_UUID)).andReturn("1111"); EasyMock.expect(bc.getServiceReference(PackageAdmin.class)).andReturn(null); + EasyMock.expect(bc.getServiceReference(EventAdmin.class)).andReturn(null).atLeastOnce(); EasyMock.replay(bc); Map<String, Object> eProps = new HashMap<String, Object>(sProps); http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bce388e2/rsa/src/test/java/org/apache/aries/rsa/core/event/EventProducerTest.java ---------------------------------------------------------------------- diff --git a/rsa/src/test/java/org/apache/aries/rsa/core/event/EventProducerTest.java b/rsa/src/test/java/org/apache/aries/rsa/core/event/EventProducerTest.java new file mode 100644 index 0000000..602bcbe --- /dev/null +++ b/rsa/src/test/java/org/apache/aries/rsa/core/event/EventProducerTest.java @@ -0,0 +1,166 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.rsa.core.event; + +import java.util.Arrays; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.List; +import java.util.UUID; + +import org.apache.aries.rsa.core.ExportRegistrationImpl; +import org.apache.aries.rsa.core.RemoteServiceAdminCore; +import org.apache.aries.rsa.core.event.EventProducer; +import org.apache.aries.rsa.spi.Endpoint; +import org.easymock.EasyMock; +import org.easymock.IAnswer; +import org.junit.Assert; +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.service.remoteserviceadmin.EndpointDescription; +import org.osgi.service.remoteserviceadmin.ExportReference; +import org.osgi.service.remoteserviceadmin.ExportRegistration; +import org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent; +import org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener; + +@SuppressWarnings({"rawtypes", "unchecked"}) +public class EventProducerTest { + + + @Test + public void testPublishNotification() throws Exception { + RemoteServiceAdminCore rsaCore = EasyMock.createNiceMock(RemoteServiceAdminCore.class); + EasyMock.replay(rsaCore); + + final EndpointDescription epd = EasyMock.createNiceMock(EndpointDescription.class); + EasyMock.expect(epd.getServiceId()).andReturn(Long.MAX_VALUE).anyTimes(); + final String uuid = UUID.randomUUID().toString(); + EasyMock.expect(epd.getFrameworkUUID()).andReturn(uuid).anyTimes(); + EasyMock.expect(epd.getId()).andReturn("foo://bar").anyTimes(); + final List<String> interfaces = Arrays.asList("org.foo.Bar", "org.boo.Far"); + EasyMock.expect(epd.getInterfaces()).andReturn(interfaces).anyTimes(); + EasyMock.expect(epd.getConfigurationTypes()).andReturn(Arrays.asList("org.apache.cxf.ws")).anyTimes(); + EasyMock.replay(epd); + final ServiceReference sref = EasyMock.createNiceMock(ServiceReference.class); + EasyMock.replay(sref); + + final Bundle bundle = EasyMock.createNiceMock(Bundle.class); + EasyMock.expect(bundle.getBundleId()).andReturn(42L).anyTimes(); + EasyMock.expect(bundle.getSymbolicName()).andReturn("test.bundle").anyTimes(); + Dictionary<String, String> headers = new Hashtable<String, String>(); + headers.put("Bundle-Version", "1.2.3.test"); + EasyMock.expect(bundle.getHeaders()).andReturn(headers).anyTimes(); + EasyMock.replay(bundle); + + RemoteServiceAdminListener rsal = EasyMock.createNiceMock(RemoteServiceAdminListener.class); + rsal.remoteAdminEvent((RemoteServiceAdminEvent) EasyMock.anyObject()); + EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { + @Override + public Object answer() throws Throwable { + RemoteServiceAdminEvent rsae = (RemoteServiceAdminEvent) EasyMock.getCurrentArguments()[0]; + Assert.assertNull(rsae.getException()); + Assert.assertEquals(RemoteServiceAdminEvent.EXPORT_REGISTRATION, rsae.getType()); + Assert.assertSame(bundle, rsae.getSource()); + ExportReference er = rsae.getExportReference(); + Assert.assertSame(epd, er.getExportedEndpoint()); + Assert.assertSame(sref, er.getExportedService()); + + return null; + } + }); + EasyMock.replay(rsal); + + ServiceReference rsalSref = EasyMock.createNiceMock(ServiceReference.class); + EasyMock.expect(rsalSref.getBundle()).andReturn(bundle).anyTimes(); + EasyMock.replay(rsalSref); + + BundleContext bc = EasyMock.createNiceMock(BundleContext.class); + EasyMock.expect(bc.getBundle()).andReturn(bundle).anyTimes(); + EasyMock.expect(bc.getServiceReferences(RemoteServiceAdminListener.class.getName(), null)) + .andReturn(new ServiceReference[] {rsalSref}).anyTimes(); + EasyMock.expect(bc.getService(rsalSref)).andReturn(rsal).anyTimes(); + Endpoint endpoint = EasyMock.mock(Endpoint.class); + EasyMock.expect(endpoint.description()).andReturn(epd); + EasyMock.replay(endpoint); + EasyMock.replay(bc); + EventProducer eventProducer = new EventProducer(bc); + + ExportRegistrationImpl ereg = new ExportRegistrationImpl(sref, endpoint, rsaCore); + eventProducer.publishNotification(ereg); + + EasyMock.verify(rsaCore, sref, bundle, rsal, rsalSref, bc); + } + + @Test + public void testPublishErrorNotification() throws Exception { + RemoteServiceAdminCore rsaCore = EasyMock.createNiceMock(RemoteServiceAdminCore.class); + EasyMock.replay(rsaCore); + + final EndpointDescription endpoint = EasyMock.createNiceMock(EndpointDescription.class); + EasyMock.expect(endpoint.getInterfaces()).andReturn(Arrays.asList("org.foo.Bar")).anyTimes(); + EasyMock.replay(endpoint); + final ServiceReference sref = EasyMock.createNiceMock(ServiceReference.class); + EasyMock.replay(sref); + + final Bundle bundle = EasyMock.createNiceMock(Bundle.class); + EasyMock.expect(bundle.getBundleId()).andReturn(42L).anyTimes(); + EasyMock.expect(bundle.getSymbolicName()).andReturn("test.bundle").anyTimes(); + EasyMock.expect(bundle.getHeaders()).andReturn(new Hashtable<String, String>()).anyTimes(); + EasyMock.replay(bundle); + + final Exception exportException = new Exception(); + + RemoteServiceAdminListener rsal = EasyMock.createNiceMock(RemoteServiceAdminListener.class); + rsal.remoteAdminEvent((RemoteServiceAdminEvent) EasyMock.anyObject()); + EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { + @Override + public Object answer() throws Throwable { + RemoteServiceAdminEvent rsae = (RemoteServiceAdminEvent) EasyMock.getCurrentArguments()[0]; + Assert.assertSame(exportException, rsae.getException()); + Assert.assertEquals(RemoteServiceAdminEvent.EXPORT_ERROR, rsae.getType()); + Assert.assertSame(bundle, rsae.getSource()); + Assert.assertNull(rsae.getImportReference()); + Assert.assertNull(rsae.getExportReference()); + + return null; + } + }); + EasyMock.replay(rsal); + + ServiceReference rsalSref = EasyMock.createNiceMock(ServiceReference.class); + EasyMock.expect(rsalSref.getBundle()).andReturn(bundle).anyTimes(); + EasyMock.replay(rsalSref); + + BundleContext bc = EasyMock.createNiceMock(BundleContext.class); + + EasyMock.expect(bc.getBundle()).andReturn(bundle).anyTimes(); + EasyMock.expect(bc.getServiceReferences(RemoteServiceAdminListener.class.getName(), null)) + .andReturn(new ServiceReference[] {rsalSref}).anyTimes(); + EasyMock.expect(bc.getService(rsalSref)).andReturn(rsal).anyTimes(); + EasyMock.replay(bc); + EventProducer eventProducer = new EventProducer(bc); + + ExportRegistrationImpl ereg = new ExportRegistrationImpl(rsaCore, exportException); + eventProducer.publishNotification(Arrays.<ExportRegistration>asList(ereg)); + + EasyMock.verify(rsaCore, sref, bundle, rsal, rsalSref, bc); + } +}
