Skip to content

Commit

Permalink
[resolves #985] Compatibility for DS based components
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Diesler committed Nov 19, 2015
1 parent 326c585 commit e5cef1a
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 48 deletions.
33 changes: 33 additions & 0 deletions compatibility/itests/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,43 @@
<groupId>org.jboss.osgi.metadata</groupId>
<artifactId>jbosgi-metadata</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.enterprise</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<!-- Provided Dependencies -->
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.ds-annotations</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<executions>
<execution>
<id>scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<specVersion>1.2</specVersion>
<strictMode>true</strictMode>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
* #L%
*/
package com.redhat.fuse.itests;
package org.wildfly.camel.test.compatibility;

import java.io.InputStream;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* #%L
* Gravia :: Integration Tests :: Common
* %%
* Copyright (C) 2010 - 2014 JBoss by Red Hat
* %%
* Licensed 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.
* #L%
*/
package org.wildfly.camel.test.compatibility;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.osgi.StartLevelAware;
import org.jboss.gravia.Constants;
import org.jboss.gravia.resource.Resource;
import org.jboss.gravia.runtime.Module;
import org.jboss.gravia.runtime.ModuleContext;
import org.jboss.gravia.runtime.Runtime;
import org.jboss.gravia.runtime.RuntimeLocator;
import org.jboss.gravia.runtime.RuntimeType;
import org.jboss.gravia.runtime.ServiceEvent;
import org.jboss.gravia.runtime.ServiceListener;
import org.jboss.gravia.runtime.ServiceReference;
import org.jboss.osgi.metadata.OSGiManifestBuilder;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.osgi.service.component.ComponentContext;
import org.wildfly.camel.test.compatibility.subA.ServiceA;
import org.wildfly.camel.test.compatibility.subA.ServiceA1;

/**
* Test basic SCR Component
*
* @author thomas.diesler@jboss.com
* @since 04-Oct-2013
*/
@RunWith(Arquillian.class)
public class ServiceComponentTest {

@Deployment
@StartLevelAware(autostart = true)
public static Archive<?> deployment() {
final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "scr-test.jar");
archive.addClasses(ServiceA.class, ServiceA1.class);
archive.addAsResource("OSGI-INF/org.wildfly.camel.test.compatibility.subA.ServiceA.xml");
archive.addAsResource("OSGI-INF/org.wildfly.camel.test.compatibility.subA.ServiceA1.xml");
archive.setManifest(new Asset() {
@Override
public InputStream openStream() {
OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
builder.addBundleManifestVersion(2);
builder.addBundleSymbolicName(archive.getName());
builder.addBundleVersion("1.0.0");
builder.addImportPackages(RuntimeLocator.class, ComponentContext.class, Resource.class);
builder.addManifestHeader("Service-Component", "OSGI-INF/org.wildfly.camel.test.compatibility.subA.ServiceA.xml,OSGI-INF/org.wildfly.camel.test.compatibility.subA.ServiceA1.xml");
return builder.openStream();
}
});
return archive;
}

@Test
public void testServiceAvailability() throws Exception {

Runtime runtime = RuntimeLocator.getRequiredRuntime();
Module modA = runtime.getModule(getClass().getClassLoader());

if (RuntimeType.getRuntimeType(runtime) == RuntimeType.WILDFLY) {
System.out.println(Module.class.getClassLoader().loadClass(ComponentContext.class.getName()).getClassLoader());
System.out.println(ComponentContext.class.getClassLoader());
}

ModuleContext ctxA = modA.getModuleContext();
ServiceReference<ServiceA> srefA = ctxA.getServiceReference(ServiceA.class);
Assert.assertNotNull("ServiceReference not null", srefA);

ServiceA srvA = ctxA.getService(srefA);
Assert.assertEquals("ServiceA#1:ServiceA1#1:Hello", srvA.doStuff("Hello"));

assertDisableComponent(ctxA, srvA);
}

private void assertDisableComponent(final ModuleContext ctxA, ServiceA srvA) throws InterruptedException {

final CountDownLatch latch = new CountDownLatch(1);
ServiceListener listener = new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
int type = event.getType();
ServiceReference<?> sref = event.getServiceReference();
List<String> clazzes = Arrays.asList(((String[]) sref.getProperty(Constants.OBJECTCLASS)));
if (type == ServiceEvent.UNREGISTERING && clazzes.contains(ServiceA1.class.getName())) {
ctxA.removeServiceListener(this);
latch.countDown();
}
}
};
ctxA.addServiceListener(listener);

ComponentContext ccA1 = srvA.getServiceA1().getComponentContext();
ccA1.disableComponent(ServiceA1.class.getName());

Assert.assertTrue(latch.await(2, TimeUnit.SECONDS));

ServiceReference<ServiceA> srefA = ctxA.getServiceReference(ServiceA.class);
Assert.assertNull("ServiceReference null", srefA);
ServiceReference<ServiceA1> srefA1 = ctxA.getServiceReference(ServiceA1.class);
Assert.assertNull("ServiceReference null", srefA1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* #%L
* Gravia :: Integration Tests :: Common
* %%
* Copyright (C) 2010 - 2014 JBoss by Red Hat
* %%
* Licensed 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.
* #L%
*/
package org.wildfly.camel.test.compatibility.subA;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;

@Component(service = { ServiceA.class }, immediate = true)
public class ServiceA {

static AtomicInteger INSTANCE_COUNT = new AtomicInteger();
final String name = getClass().getSimpleName() + "#" + INSTANCE_COUNT.incrementAndGet();

final AtomicReference<ServiceA1> ref = new AtomicReference<ServiceA1>();

private ComponentContext context;

@Activate
void activate(ComponentContext context) {
this.context = context;
}

@Deactivate
void deactivate() {
}

public ComponentContext getComponentContext() {
return context;
}

@Reference
void bindServiceA1(ServiceA1 service) {
ref.set(service);
}

void unbindServiceA1(ServiceA1 service) {
ref.compareAndSet(service, null);
}

public ServiceA1 getServiceA1() {
return ref.get();
}

public String doStuff(String msg) {
ServiceA1 srv = ref.get();
return name + ":" + srv.doStuff(msg);
}

@Override
public String toString() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* #%L
* Gravia :: Integration Tests :: Common
* %%
* Copyright (C) 2010 - 2014 JBoss by Red Hat
* %%
* Licensed 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.
* #L%
*/
package org.wildfly.camel.test.compatibility.subA;

import java.util.concurrent.atomic.AtomicInteger;

import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;

@Component(service = { ServiceA1.class })
public class ServiceA1 {

static AtomicInteger INSTANCE_COUNT = new AtomicInteger();
final String name = getClass().getSimpleName() + "#" + INSTANCE_COUNT.incrementAndGet();

private ComponentContext context;

@Activate
void activate(ComponentContext context) {
this.context = context;
}

@Deactivate
void deactivate() {
}

public ComponentContext getComponentContext() {
return context;
}

public String doStuff(String msg) {
return name + ":" + msg;
}

@Override
public String toString() {
return name;
}
}
1 change: 1 addition & 0 deletions compatibility/itests/wildfly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<type>tar.gz</type>
<overWrite>false</overWrite>
<outputDirectory>${jboss.home}</outputDirectory>
<excludes>standalone/deployments/gravia.war</excludes>
</artifactItem>
</artifactItems>
</configuration>
Expand Down
38 changes: 0 additions & 38 deletions compatibility/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,6 @@
<artifactId>compatibility</artifactId>
<packaging>pom</packaging>

<!-- Properties -->
<properties>
<version.apache.karaf>2.3.3</version.apache.karaf>
<version.jboss.arquillian.osgi>2.1.0.Final</version.jboss.arquillian.osgi>
<version.jboss.osgi.metadata>4.0.0.CR1</version.jboss.osgi.metadata>
</properties>

<!-- DependencyManagement -->
<dependencyManagement>
<dependencies>

<!-- Arquillian OSGi -->
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-karaf-managed</artifactId>
<version>${version.jboss.arquillian.osgi}</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.osgi</groupId>
<artifactId>arquillian-osgi-bundle</artifactId>
<version>${version.jboss.arquillian.osgi}</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.testenricher</groupId>
<artifactId>arquillian-testenricher-osgi</artifactId>
<version>${version.jboss.arquillian.osgi}</version>
</dependency>

<!-- JBoss -->
<dependency>
<groupId>org.jboss.osgi.metadata</groupId>
<artifactId>jbosgi-metadata</artifactId>
<version>${version.jboss.osgi.metadata}</version>
</dependency>

</dependencies>
</dependencyManagement>

<modules>
<module>itests</module>
</modules>
Expand Down
6 changes: 3 additions & 3 deletions modules/etc/baseline/module-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@
/org/fusesource/hawtdispatch/main/hawtdispatch-1.21.jar
/org/fusesource/hawtdispatch/main/hawtdispatch-transport-1.21.jar
/org/fusesource/mqtt/main/mqtt-client-1.12.jar
/org/jboss/gravia/main/gravia-resource-1.2.0.jar
/org/jboss/gravia/main/gravia-runtime-api-1.2.0.jar
/org/jboss/gravia/main/gravia-runtime-embedded-1.2.0.jar
/org/jboss/gravia/main/gravia-resource-1.2.1.jar
/org/jboss/gravia/main/gravia-runtime-api-1.2.1.jar
/org/jboss/gravia/main/gravia-runtime-embedded-1.2.1.jar
/org/mvel/mvel2/main/mvel2-2.2.6.Final.jar
/org/osgi/core/main/org.osgi.core-4.3.1.jar
/org/osgi/enterprise/main/org.osgi.enterprise-5.0.0.jar
Expand Down
Loading

0 comments on commit e5cef1a

Please sign in to comment.