Skip to content

Commit

Permalink
Refactored client support
Browse files Browse the repository at this point in the history
The producer method for proxied JAX-RS interfaces is now registered correctly (only once). Furthermore, the DelegatingBean was removed and we use AbstractImmutableBean from WELDX
  • Loading branch information
jharting committed Nov 12, 2010
1 parent 183d371 commit 6dfd152
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 133 deletions.
27 changes: 27 additions & 0 deletions api/src/main/java/org/jboss/seam/rest/client/RestClient.java
Expand Up @@ -25,6 +25,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.enterprise.util.AnnotationLiteral;
import javax.enterprise.util.Nonbinding;
import javax.inject.Qualifier;

Expand All @@ -50,4 +51,30 @@
*/
@Nonbinding
String value();

/**
* Annotation literal for @{link RestClient} qualifier.
*/
@SuppressWarnings("all")
public static class RestClientLiteral extends AnnotationLiteral<RestClient> implements RestClient
{
private static final long serialVersionUID = -8456396489504116441L;

private final String value;

public RestClientLiteral()
{
this("");
}

public RestClientLiteral(String value)
{
this.value = value;
}

public String value()
{
return value;
}
}
}
Expand Up @@ -22,7 +22,6 @@
package org.jboss.seam.rest.client;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -34,28 +33,15 @@
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessProducerMethod;

import org.jboss.logging.Logger;
import org.jboss.seam.rest.util.Annotations;
import org.jboss.seam.rest.util.DelegatingBean;
import org.jboss.seam.rest.util.Utils;
/**
* The Seam REST Client extension provides injection of
* - org.jboss.resteasy.client.ClientRequest instances
* - REST clients - proxied JAX-RS interfaces capable of invoking client requests
*
*
* How @RestClient injection of proxied JAX-RS interfaces works:
* 1.) We register the RestClientProducer
* - it is useless since by default it produces Object instances only
* - we let the container create the Bean instance for the RestClientProducer
* and use it as a delegate later on
* 2.) @RestClient injection points are scanned for JAX-RS interfaces
* 3.) We create and register the correct RestClientProducer using the delegate from step 1.
* - all found JAX-RS interfaces are added to the type closure of the second RestClientProducer
*
*
* @author <a href="mailto:jharting@redhat.com">Jozef Hartinger</a>
**/
public class RestClientExtension implements Extension
Expand All @@ -65,7 +51,7 @@ public class RestClientExtension implements Extension
private boolean enabled;

private Set<Type> jaxrsInterfaces = new HashSet<Type>();
private Bean<Object> restClientProducerDelegate;
private Bean<RestClientProducer> restClientProducerBean;
public void registerExtension(@Observes BeforeBeanDiscovery event, BeanManager manager)
{
enabled = Utils.isClassAvailable(RESTEASY_PROVIDER_FACTORY_NAME);
Expand All @@ -78,12 +64,9 @@ public void registerExtension(@Observes BeforeBeanDiscovery event, BeanManager m
}
}

public void getRestClientProducerDelegate(@Observes ProcessProducerMethod<RestClientProducer, Object> event)
public void getRestClientProducerDelegate(@Observes ProcessBean<RestClientProducer> event)
{
if (event.getBean().getTypes().size() == 1) // make sure it is the produceRestClient() method
{
this.restClientProducerDelegate = event.getBean();
}
this.restClientProducerBean = event.getBean();
}

public <T> void scanInjectionPointsForJaxrsInterfaces(@Observes ProcessBean<T> event)
Expand Down Expand Up @@ -115,29 +98,17 @@ public <T> void scanInjectionPointsForJaxrsInterfaces(@Observes ProcessBean<T> e
/**
* Registers the RestClientProducer if there is an injection point that requires it
*/
public void afterBeanDiscovery(@Observes AfterBeanDiscovery event)
public void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager manager)
{
if (enabled && !jaxrsInterfaces.isEmpty())
{
if (restClientProducerDelegate == null)
if (restClientProducerBean == null)
{
log.warn("ProcessProducerMethod<RestClientProducer, Object> not fired. Client extension may not work properly.");
return;
}
// register an additional RestClientProducer that supports all the interfaces
event.addBean(wrapBean(restClientProducerDelegate, jaxrsInterfaces));
event.addBean(new RestClientProducerBean(restClientProducerBean, jaxrsInterfaces, manager));
}
}

private <T> Bean<T> wrapBean(final Bean<T> delegate, final Set<Type> types)
{
return new DelegatingBean<T>(delegate)
{
@Override
public Set<Type> getTypes()
{
return Collections.unmodifiableSet(types);
}
};
}
}
Expand Up @@ -52,7 +52,10 @@ public class RestClientProducer
@Inject
private Interpolator interpolator;

@Produces @RestClient("")
/**
* Producer for proxied JAX-RS interfaces - REST Clients
* This method is registered as a producer method through RestClientExtension
*/
public Object produceRestClient(InjectionPoint ip, ClientExecutor executor)
{
RestClient qualifier = Annotations.getAnnotation(ip.getQualifiers(), RestClient.class);
Expand All @@ -67,6 +70,9 @@ public Object produceRestClient(InjectionPoint ip, ClientExecutor executor)
return ProxyFactory.create(clazz, url, executor);
}

/**
* Producer for ClientRequests
*/
@Produces @RestClient("")
public ClientRequest produceClientRequest(InjectionPoint ip, ClientExecutor executor)
{
Expand Down
@@ -0,0 +1,101 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.seam.rest.client;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.resteasy.client.ClientExecutor;
import org.jboss.weld.extensions.bean.AbstractImmutableBean;
import org.jboss.weld.extensions.bean.Beans;
import org.jboss.weld.extensions.literal.DefaultLiteral;

/**
* We need to create a producer method with the type closure discovered at boot time.
* Therefore, the producer method has to be registered by extension.
* @author <a href="mailto:jharting@redhat.com">Jozef Hartinger</a>
*
*/
public class RestClientProducerBean extends AbstractImmutableBean<Object>
{
private Bean<RestClientProducer> beanDefiningProducerMethod;
private BeanManager manager;
private List<InjectionPoint> injectionPoints;

public RestClientProducerBean(Bean<RestClientProducer> producerBean, Set<Type> types, BeanManager manager)
{
super(RestClientProducer.class, null, Collections.<Annotation>singleton(new RestClient.RestClientLiteral()), Dependent.class, null, types, false, false, null, null);

this.beanDefiningProducerMethod = producerBean;
this.manager = manager;

AnnotatedMethod<? super RestClientProducer> annotatedMethod = null;

for (AnnotatedMethod<? super RestClientProducer> method : manager.createAnnotatedType(RestClientProducer.class).getMethods())
{
if (method.getAnnotations().isEmpty())
{
annotatedMethod = method;
}
}

if (annotatedMethod == null)
{
throw new RuntimeException("Unable to find @RestClient producer method.");
}

injectionPoints = Beans.createInjectionPoints(annotatedMethod, this, manager);
}

public Object create(CreationalContext<Object> creationalContext)
{
// get an instance of the bean declaring the producer method
RestClientProducer producer = (RestClientProducer) manager.getReference(beanDefiningProducerMethod, RestClientProducer.class, manager.createCreationalContext(beanDefiningProducerMethod));

InjectionPoint ip = (InjectionPoint) manager.getInjectableReference(injectionPoints.get(0), creationalContext);
ClientExecutor executor = (ClientExecutor) manager.getInjectableReference(injectionPoints.get(1), creationalContext);

return producer.produceRestClient(ip, executor);
}

public void destroy(Object instance, CreationalContext<Object> creationalContext)
{
creationalContext.release();
}

@Override
public Set<InjectionPoint> getInjectionPoints()
{
return new HashSet<InjectionPoint>(injectionPoints);
}
}
95 changes: 0 additions & 95 deletions impl/src/main/java/org/jboss/seam/rest/util/DelegatingBean.java

This file was deleted.

Expand Up @@ -30,12 +30,13 @@
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.seam.rest.client.RestClientExtension;
import org.jboss.seam.rest.util.Annotations;
import org.jboss.seam.rest.util.DelegatingBean;
import org.jboss.seam.rest.util.Interpolator;
import org.jboss.seam.rest.util.Utils;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.weld.extensions.bean.Beans;
import org.jboss.weld.extensions.literal.DefaultLiteral;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
Expand All @@ -52,6 +53,8 @@ public static WebArchive getDeployment()
{
WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war");
war.addPackage(RestClientTest.class.getPackage()); // test classes
war.addPackage(Beans.class.getPackage());
war.addClass(DefaultLiteral.class);
war.addWebResource("beans.xml", "classes/META-INF/beans.xml");
war.addWebResource("org/jboss/seam/rest/test/client/web.xml", "web.xml");
war.addLibrary(getSeamRest());
Expand All @@ -62,7 +65,7 @@ public static JavaArchive getSeamRest()
{
JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "test.jar");
jar.addPackage(RestClientExtension.class.getPackage());
jar.addClasses(Utils.class, Annotations.class, DelegatingBean.class, Interpolator.class);
jar.addClasses(Utils.class, Annotations.class, Interpolator.class);
jar.addManifestResource("org/jboss/seam/rest/test/client/javax.enterprise.inject.spi.Extension", "services/javax.enterprise.inject.spi.Extension");
return jar;
}
Expand Down

0 comments on commit 6dfd152

Please sign in to comment.