tangyong / gf-cdi-osgi-integration

Glassfish CDI/OSGi Integration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

making @Publish to support filter properties

tangyong opened this issue · comments

Requirements:

[RFP146]CDI014 – The specification MUST provide a mechanism to specify additional OSGi service registration properties for CDI beans.

liking the following,

@publish({
@Property(name="lang", value="EN"),
@Property(name="country", value="US")
})
@ApplicationScoped
public class MyServiceImpl implements MyService {
@OverRide
public void doSomething() {
}
}

Define @Propery and add it into @publish annotation liking the following:

@qualifier
@target({TYPE})
@retention(RetentionPolicy.RUNTIME)
public @interface Publish {

Property[] value() default {};

/**
 * The rank of the service to find the best available service on lookups.
 *
 * @return the rank of the service. Default is 0.
 */
int rank() default 0;

}

Reference:
https://blogs.oracle.com/toddfast/entry/creating_nested_complex_java_annotations

By confirming, if defining @Property in @publish Qualifier, WELD Container will report the following error,

org.jboss.weld.exceptions.IllegalArgumentException: WELD-001301 Annotation @org.glassfish.osgicdi.Publish(rank=0, value=[@org.glassfish.osgicdi.Property(name=country, value=CN)]) is not a qualifier
at org.jboss.weld.resolution.ResolvableBuilder.checkQualifier(ResolvableBuilder.java:174)
at org.jboss.weld.resolution.ResolvableBuilder.addQualifier(ResolvableBuilder.java:145)
at org.jboss.weld.resolution.ResolvableBuilder.addQualifiers(ResolvableBuilder.java:167)
at org.jboss.weld.manager.BeanManagerImpl.resolveDecorators(BeanManagerImpl.java:731)
at org.jboss.weld.bean.AbstractClassBean.initDecorators(AbstractClassBean.java:315)
at org.jboss.weld.bean.AbstractClassBean.initializeAfterBeanDiscovery(AbstractClassBean.java:307)
at org.jboss.weld.bean.ManagedBean.initializeAfterBeanDiscovery(ManagedBean.java:361)
at org.jboss.weld.bootstrap.BeanDeployment.doAfterBeanDiscovery(BeanDeployment.java:216)
at org.jboss.weld.bootstrap.BeanDeployment.afterBeanDiscovery(BeanDeployment.java:208)
at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:352)
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:190)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:277)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:488)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:418)

So, I changed @publish from Qualifier into plain Annotation, the above problem can be resolved.

Now, the feature has been implemented.

[Using Way]

1 Service Publishing

@publish({
@Property(name="country", value="CN")
})
public class SimpleStockQuoteServiceImpl implements StockQuoteService
....

2 Service Consuming

@WebServlet(urlPatterns = "/list")
public class StockQuoteServlet extends HttpServlet {
//Inject the OSGi service by specifying the OSGiService qualifier
@Inject
@OSGiService(serviceCriteria="(country=CN)", dynamic = true,
/* wait for 1 min _/ waitTimeout=30_1000)
StockQuoteService sqs;
.....