Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot register beans created by producer methods as OSGi services #34

Open
wants to merge 3 commits into
base: 0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions itest/src/it/itest-cdi10/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.ops4j.pax.cdi.samples</groupId>
<artifactId>pax-cdi-sample-osgi-provider-producer-method</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.ops4j.pax.cdi</groupId>
<artifactId>pax-cdi-extension</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2013 Harald Wellmann.
*
* 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.
*/
package org.ops4j.pax.cdi.test;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerClass;
import org.osgi.framework.InvalidSyntaxException;
import sample.osgiProviderProducerMethod.Greeter;

import javax.inject.Inject;

import static org.junit.Assert.assertEquals;
import static org.ops4j.pax.cdi.test.support.TestConfiguration.cdiProviderBundles;
import static org.ops4j.pax.cdi.test.support.TestConfiguration.paxCdiProviderAdapter;
import static org.ops4j.pax.cdi.test.support.TestConfiguration.regressionDefaults;
import static org.ops4j.pax.cdi.test.support.TestConfiguration.workspaceBundle;
import static org.ops4j.pax.exam.CoreOptions.options;


@RunWith(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class GreeterProducerTest {

@Inject
private Greeter greeter;

@Configuration
public Option[] config() {
return options(
regressionDefaults(),

workspaceBundle("org.ops4j.pax.cdi.samples", "pax-cdi-sample-osgi-provider-producer-method"),

paxCdiProviderAdapter(),
cdiProviderBundles());
}

@Test
public void checkInjection() throws InvalidSyntaxException {
assertEquals(greeter.sayHello(), "Hello!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.ops4j.pax.cdi.extension.impl.component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -26,7 +27,6 @@
import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionPoint;
Expand Down Expand Up @@ -175,22 +175,33 @@ private <S> void registerService(Bean<S> bean, S service, ComponentDescriptor de
return;
}

Class<?> klass = bean.getBeanClass();
AnnotatedType<?> annotatedType = beanManager.createAnnotatedType(klass);
OsgiServiceProvider provider = annotatedType.getAnnotation(OsgiServiceProvider.class);
OsgiServiceProvider provider = find(OsgiServiceProvider.class, bean.getQualifiers());

String[] typeNames;
if (provider.classes().length == 0) {
typeNames = getTypeNamesForBeanTypes(bean);
}
else {
typeNames = getTypeNamesForClasses(provider.classes());
if (provider != null) {
String[] typeNames;
if (provider.classes().length == 0) {
typeNames = getTypeNamesForBeanTypes(bean);
}
else {
typeNames = getTypeNamesForClasses(provider.classes());
}

// todo: This will not fetch the properties correctly for a producer method
Properties properties = bean.getBeanClass().getAnnotation(Properties.class);
Dictionary<String, Object> props = createProperties(properties);
log.debug("publishing service {}, props = {}", typeNames[0], props);
ServiceRegistration<?> reg = bundleContext.registerService(typeNames, service, props);
descriptor.setServiceRegistration(reg);
}
}

Dictionary<String, Object> props = createProperties(klass, service);
log.debug("publishing service {}, props = {}", typeNames[0], props);
ServiceRegistration<?> reg = bundleContext.registerService(typeNames, service, props);
descriptor.setServiceRegistration(reg);
private <T extends Annotation> T find(Class<T> annotationType, Iterable<Annotation> qualifiers) {
for (Annotation qualifier : qualifiers ) {
if (annotationType.isInstance(qualifier)) {
return (T) qualifier;
}
}
return null;
}

private <S> void unregisterService(Bean<S> bean, Object service,
Expand Down Expand Up @@ -231,8 +242,7 @@ private String[] getTypeNamesForClasses(Class<?>[] classes) {
return typeNames;
}

private Dictionary<String, Object> createProperties(Class<?> klass, Object service) {
Properties props = klass.getAnnotation(Properties.class);
private Dictionary<String, Object> createProperties(Properties props) {
if (props == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>pax-cdi-samples</artifactId>
<groupId>org.ops4j.pax.cdi</groupId>
<version>0.13.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>org.ops4j.pax.cdi.samples</groupId>
<artifactId>pax-cdi-sample-osgi-provider-producer-method</artifactId>
<packaging>bundle</packaging>

<name>OPS4J Pax CDI Sample - OSGi Provider Producer Method</name>

<dependencies>
<dependency>
<groupId>org.ops4j.pax.cdi</groupId>
<artifactId>pax-cdi-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Bundle-SymbolicName>org.ops4j.pax.cdi.sample.osgi-provider-producer-method</Bundle-SymbolicName>
<Require-Capability>
org.ops4j.pax.cdi.extension; filter:="(&amp;(extension=pax-cdi-extension)(version&gt;=${version;==;${pax.cdi.osgi.version.clean}})(!(version&gt;=${version;=+;${pax.cdi.osgi.version.clean}})))",
osgi.extender; filter:="(osgi.extender=pax.cdi)"
</Require-Capability>
</instructions>
</configuration>
<executions>
<execution>
<id>versions</id>
<phase>validate</phase>
<goals>
<goal>cleanVersions</goal>
</goals>
<configuration>
<versions>
<pax.cdi.osgi.version.clean>${project.version}</pax.cdi.osgi.version.clean>
</versions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sample.osgiProviderProducerMethod;


import org.ops4j.pax.cdi.api.OsgiServiceProvider;
import org.ops4j.pax.cdi.api.SingletonScoped;

import javax.enterprise.inject.Produces;


/**
* @author mwinkels
* @since Jul 25, 2016
*/
public class Beans {

@Produces
@OsgiServiceProvider
@SingletonScoped
public Greeter greeter() {
return new EnglishGreeter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2013 Harald Wellmann
*
* 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.
*/

package sample.osgiProviderProducerMethod;

/**
* @author mwinkels
* @since Jul 25, 2016
*/
public class EnglishGreeter implements Greeter {

@Override
public String sayHello() {
return "Hello!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2013 Harald Wellmann
*
* 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.
*/

package sample.osgiProviderProducerMethod;


/**
* @author mwinkels
* @since Jul 25, 2016
*/
public interface Greeter {
String sayHello();
}
1 change: 1 addition & 0 deletions pax-cdi-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<module>pax-cdi-sample5-client1</module>
<module>pax-cdi-sample5-client2</module>
<module>pax-cdi-sample6</module>
<module>pax-cdi-sample-osgi-provider-producer-method</module>
</modules>

</project>