Skip to content

Commit

Permalink
Re-implementation of rest-client-microservice-provider using Resteasy…
Browse files Browse the repository at this point in the history
… clien API proxy framework. Removed old implementation and cdi rest gateway implementation.
  • Loading branch information
RadekKoubsky authored and marvec committed Oct 29, 2016
1 parent e5cbdaf commit 18946af
Show file tree
Hide file tree
Showing 21 changed files with 511 additions and 910 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import io.silverware.microservices.providers.cdi.internal.MicroserviceProxyBean;
import io.silverware.microservices.providers.cdi.internal.MicroservicesCDIExtension;
import io.silverware.microservices.providers.cdi.internal.MicroservicesInitEvent;
import io.silverware.microservices.providers.cdi.internal.RestInterface;
import io.silverware.microservices.silver.CdiSilverService;
import io.silverware.microservices.util.Utils;

Expand All @@ -39,6 +38,13 @@
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.Priority;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
Expand All @@ -50,13 +56,6 @@
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* @author <a href="mailto:[email protected]">Martin Večeřa</a>
*/
Expand All @@ -81,12 +80,12 @@ public void initialize(final Context context) {

@Override
public Context getContext() {
return context;
return this.context;
}

@Override
public boolean isDeployed() {
return deployed;
return this.deployed;
}

@Override
Expand All @@ -95,49 +94,44 @@ public void run() {
log.info("Hello from CDI microservice provider!");

final Weld weld = new Weld();
final RestInterface rest = new RestInterface(context);
final MicroservicesCDIExtension microservicesCDIExtension = new MicroservicesCDIExtension(context, rest);
final MicroservicesCDIExtension microservicesCDIExtension = new MicroservicesCDIExtension(this.context);
System.setProperty("org.jboss.weld.se.archive.isolation", "false");
weld.addExtension(microservicesCDIExtension);

final WeldContainer container = weld.initialize();
context.getProperties().put(BEAN_MANAGER, container.getBeanManager());
context.getProperties().put(CDI_CONTAINER, container);
context.getProperties().put(Storage.STORAGE, new HashMap<String, Object>());
this.context.getProperties().put(BEAN_MANAGER, container.getBeanManager());
this.context.getProperties().put(CDI_CONTAINER, container);
this.context.getProperties().put(Storage.STORAGE, new HashMap<String, Object>());

log.info("Discovered the following microservice implementations:");
context.getMicroservices().forEach(metaData -> log.info((" - " + metaData.toString())));
this.context.getMicroservices().forEach(metaData -> log.info((" - " + metaData.toString())));

log.info("Total count of discovered microservice injection points: " + microservicesCDIExtension.getInjectionPointsCount());
this.deployed = true;

log.info("Deploying REST gateway services.");
rest.deploy();

deployed = true;

container.event().select(MicroservicesInitEvent.class).fire(new MicroservicesInitEvent(context, container.getBeanManager(), container));
container.event().select(MicroservicesStartedEvent.class).fire(new MicroservicesStartedEvent(context, container.getBeanManager(), container));
container.event().select(MicroservicesInitEvent.class).fire(new MicroservicesInitEvent(this.context, container.getBeanManager(), container));
container.event().select(MicroservicesStartedEvent.class).fire(new MicroservicesStartedEvent(this.context, container.getBeanManager(), container));

try {
while (!Thread.currentThread().isInterrupted()) {
Thread.sleep(1000);
}
} catch (InterruptedException ie) {
} catch (final InterruptedException ie) {
Utils.shutdownLog(log, ie);
} finally {
deployed = false;
rest.undeploy();
this.deployed = false;
try {
weld.shutdown();
} catch (IllegalStateException e) {
} catch (final IllegalStateException e) {
// nothing, this is just fine, weld was already terminated
}
}
} catch (Exception e) {
} catch (final Exception e) {
log.error("CDI microservice provider failed: ", e);
}
}

@Override
@SuppressWarnings("checkstyle:JavadocMethod")
public Set<Object> lookupMicroservice(final MicroserviceMetaData microserviceMetaData) {
final String name = microserviceMetaData.getName();
Expand All @@ -158,9 +152,9 @@ public Set<Object> lookupMicroservice(final MicroserviceMetaData microserviceMet
In all cases, there must be precisely one result or an error is thrown.
*/

final BeanManager beanManager = ((BeanManager) context.getProperties().get(BEAN_MANAGER));
Set<Bean<?>> beans = beanManager.getBeans(type, qualifiers.toArray(new Annotation[qualifiers.size()]));
for (Bean<?> bean : beans) {
final BeanManager beanManager = ((BeanManager) this.context.getProperties().get(BEAN_MANAGER));
final Set<Bean<?>> beans = beanManager.getBeans(type, qualifiers.toArray(new Annotation[qualifiers.size()]));
for (final Bean<?> bean : beans) {
if (bean.getBeanClass().isAnnotationPresent(Microservice.class) && !(bean instanceof MicroserviceProxyBean)) {
final Bean<?> theBean = beanManager.resolve(Collections.singleton(bean));
final Microservice microserviceAnnotation = theBean.getBeanClass().getAnnotation(Microservice.class);
Expand Down Expand Up @@ -223,17 +217,17 @@ static Object getMicroserviceProxy(final Context context, final Class clazz, fin
}

public <T> T lookupBean(final Class<T> type) {
return ((WeldContainer) context.getProperties().get(CDI_CONTAINER)).instance().select(type).get();
return ((WeldContainer) this.context.getProperties().get(CDI_CONTAINER)).instance().select(type).get();
}

@Override
public <T> T findByType(Class<T> type) {
BeanManager beanManager = (BeanManager) context.getProperties().get(BEAN_MANAGER);
Set<T> beans = new HashSet<T>();
Set<Bean<?>> definitions = beanManager.getBeans(type);
Bean<?> bean = beanManager.resolve(definitions);
CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
Object result = beanManager.getReference(bean, type, creationalContext);
public <T> T findByType(final Class<T> type) {
final BeanManager beanManager = (BeanManager) this.context.getProperties().get(BEAN_MANAGER);
final Set<T> beans = new HashSet<T>();
final Set<Bean<?>> definitions = beanManager.getBeans(type);
final Bean<?> bean = beanManager.resolve(definitions);
final CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
final Object result = beanManager.getReference(bean, type, creationalContext);

return result == null ? null : type.cast(result);
}
Expand All @@ -248,7 +242,7 @@ public MicroserviceReferenceLiteral(final String name) {

@Override
public String value() {
return name;
return this.name;
}
}

Expand All @@ -273,10 +267,10 @@ public static class SilverWareConfiguration implements Configuration {

@Override
public Object getProperty(final String propertyName) {
return context.getProperties().get(propertyName);
return this.context.getProperties().get(propertyName);
}

public void eventObserver(@Observes MicroservicesInitEvent event) {
public void eventObserver(@Observes final MicroservicesInitEvent event) {
this.context = event.getContext();
}
}
Expand All @@ -288,7 +282,7 @@ public static class SilverWareStorage implements Storage {
private Context context;

private Map<String, Object> getStorage() {
return (Map<String, Object>) context.getProperties().get(STORAGE);
return (Map<String, Object>) this.context.getProperties().get(STORAGE);
}

@Override
Expand All @@ -306,7 +300,7 @@ public boolean drop(final String key) {
return getStorage().remove(key) != null;
}

public void eventObserver(@Observes MicroservicesInitEvent event) {
public void eventObserver(@Observes final MicroservicesInitEvent event) {
this.context = event.getContext();
}
}
Expand All @@ -319,10 +313,10 @@ public static class SilverWareCurrentContext implements CurrentContext {

@Override
public Context getContext() {
return context;
return this.context;
}

public void eventObserver(@Observes MicroservicesInitEvent event) {
public void eventObserver(@Observes final MicroservicesInitEvent event) {
this.context = event.getContext();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import io.silverware.microservices.Context;
import io.silverware.microservices.MicroserviceMetaData;
import io.silverware.microservices.annotations.Gateway;
import io.silverware.microservices.annotations.Microservice;
import io.silverware.microservices.annotations.MicroserviceReference;
import io.silverware.microservices.providers.cdi.MicroserviceContext;
Expand Down Expand Up @@ -70,11 +69,6 @@ public class MicroservicesCDIExtension implements Extension {
*/
private long injectionPointsCount = 0;

/**
* Rest interface to register gateway annotated beans.
*/
private RestInterface restInterface;

/**
* Microservices context.
*/
Expand All @@ -83,11 +77,10 @@ public class MicroservicesCDIExtension implements Extension {
/**
* List of created {@link MicroserviceProxyBean} instances.
*/
private List<MicroserviceProxyBean> microserviceProxyBeans = new ArrayList<>();
private final List<MicroserviceProxyBean> microserviceProxyBeans = new ArrayList<>();

public MicroservicesCDIExtension(final Context context, final RestInterface restInterface) {
public MicroservicesCDIExtension(final Context context) {
this.context = context;
this.restInterface = restInterface;
}

/**
Expand All @@ -98,7 +91,7 @@ public MicroservicesCDIExtension(final Context context, final RestInterface rest
* @param beanManager
* CDI Bean Manager instance.
*/
public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery beforeEvent, BeanManager beanManager) {
public void beforeBeanDiscovery(@Observes final BeforeBeanDiscovery beforeEvent, final BeanManager beanManager) {
if (log.isDebugEnabled()) {
log.debug("CDI Bean discovery process started.");
}
Expand All @@ -119,20 +112,16 @@ public void processBean(@Observes final ProcessBean processBean, final BeanManag
if (bean.getBeanClass().isAnnotationPresent(Microservice.class)) {
// we do not want to register the injection points
if (bean.getQualifiers().stream().filter(qualifier -> qualifier.annotationType().isAssignableFrom(MicroserviceReference.class)).count() == 0) {
Microservice annotation = bean.getBeanClass().getAnnotation(Microservice.class);
final Microservice annotation = bean.getBeanClass().getAnnotation(Microservice.class);
final String microserviceName = annotation.value().length() > 0 ? annotation.value() : bean.getBeanClass().getSimpleName();

context.registerMicroservice(getMicroserviceMetaData(microserviceName, bean));

if (bean.getBeanClass().isAnnotationPresent(Gateway.class)) {
restInterface.registerGateway(microserviceName, bean);
}
this.context.registerMicroservice(getMicroserviceMetaData(microserviceName, bean));
}
}

// Create proxies for the corresponding injection points
final Set<InjectionPoint> injectionPoints = bean.getInjectionPoints();
for (InjectionPoint injectionPoint : injectionPoints) {
for (final InjectionPoint injectionPoint : injectionPoints) {
final Set<Annotation> annotations = injectionPoint.getAnnotated().getAnnotations();
injectionPoint.getQualifiers().stream().filter(qualifier -> MicroserviceReference.class.isAssignableFrom(qualifier.annotationType())).forEach(qualifier -> {
final Member member = injectionPoint.getMember();
Expand All @@ -141,15 +130,15 @@ public void processBean(@Observes final ProcessBean processBean, final BeanManag
log.trace("Creating proxy bean for injection point: " + injectionPoint.toString());
}
addInjectableClientProxyBean((Field) member, (MicroserviceReference) qualifier, preProcessQualifiers(injectionPoint.getQualifiers()), annotations, beanManager);
injectionPointsCount = injectionPointsCount + 1;
this.injectionPointsCount = this.injectionPointsCount + 1;
}
});
}
}

private Set<Annotation> preProcessQualifiers(final Set<Annotation> qualifiers) {
Set<Annotation> processed = new HashSet<>(qualifiers);
List<String> current = qualifiers.stream().map(q -> q.annotationType().getName()).collect(Collectors.toList());
final Set<Annotation> processed = new HashSet<>(qualifiers);
final List<String> current = qualifiers.stream().map(q -> q.annotationType().getName()).collect(Collectors.toList());


if (!current.contains(Any.class.getName())) {
Expand All @@ -173,7 +162,7 @@ private Set<Annotation> preProcessQualifiers(final Set<Annotation> qualifiers) {
public void afterBeanDiscovery(@Observes final AfterBeanDiscovery afterEvent) {
afterEvent.addContext(new MicroserviceContext());

microserviceProxyBeans.forEach(proxyBean -> {
this.microserviceProxyBeans.forEach(proxyBean -> {
if (log.isTraceEnabled()) {
log.trace(String.format("Registering client proxy bean for bean service %s. Microservice type is %s.", proxyBean.getMicroserviceName(), proxyBean.getServiceInterface().getName()));
}
Expand Down Expand Up @@ -206,10 +195,10 @@ private void addInjectableClientProxyBean(final Field injectionPointField, final

private void addClientProxyBean(final String microserviceName, final Class<?> beanClass, final Set<Annotation> qualifiers, final Set<Annotation> annotations) {
// Do we already have a proxy with this service name and type?
for (MicroserviceProxyBean microserviceProxyBean : microserviceProxyBeans) {
for (final MicroserviceProxyBean microserviceProxyBean : this.microserviceProxyBeans) {
if (microserviceName.equals(microserviceProxyBean.getMicroserviceName()) && beanClass == microserviceProxyBean.getBeanClass()) {
List<String> required = qualifiers.stream().map(q -> q.annotationType().getCanonicalName()).collect(Collectors.toList());
List<String> available = microserviceProxyBean.getQualifiers().stream().map(q -> q.annotationType().getCanonicalName()).collect(Collectors.toList());
final List<String> required = qualifiers.stream().map(q -> q.annotationType().getCanonicalName()).collect(Collectors.toList());
final List<String> available = microserviceProxyBean.getQualifiers().stream().map(q -> q.annotationType().getCanonicalName()).collect(Collectors.toList());

required.forEach(available::remove);

Expand All @@ -221,16 +210,16 @@ private void addClientProxyBean(final String microserviceName, final Class<?> be
}

// No, we don't. Give us one please!
final MicroserviceProxyBean microserviceProxyBean = new MicroserviceProxyBean(microserviceName, beanClass, qualifiers, annotations, context);
microserviceProxyBeans.add(microserviceProxyBean);
final MicroserviceProxyBean microserviceProxyBean = new MicroserviceProxyBean(microserviceName, beanClass, qualifiers, annotations, this.context);
this.microserviceProxyBeans.add(microserviceProxyBean);
}

/**
* Gets the number of discovered injection points.
* @return The number of discovered injection points.
*/
public long getInjectionPointsCount() {
return injectionPointsCount;
return this.injectionPointsCount;
}

/**
Expand Down
Loading

0 comments on commit 18946af

Please sign in to comment.