Skip to content

Commit

Permalink
#9 added lookup strategy interface
Browse files Browse the repository at this point in the history
  • Loading branch information
marvec committed May 8, 2015
1 parent c96088d commit f4ad3e4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@
import java.util.HashSet;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.util.AnnotationLiteral;

/**
* Client Proxy CDI Bean.
Expand Down Expand Up @@ -61,10 +58,15 @@ public class MicroserviceProxyBean implements Bean {
private final Class<?> serviceInterface;

/**
* CDI bean qualifiers. See CDI Specification.
* CDI bean qualifiers. See CDI Specification.
*/
private final Set<Annotation> qualifiers;

/**
* CDI bean annotations. Can specify invocation strategy, results caching etc.
*/
private final Set<Annotation> annotations;

/**
* The dynamic proxy bean instance created from the supplied {@link #serviceInterface}.
*/
Expand All @@ -82,12 +84,13 @@ public class MicroserviceProxyBean implements Bean {
* @param beanDeploymentMetaData
* Deployment metadata.
*/
public MicroserviceProxyBean(String microserviceName, Class<?> proxyInterface, Set<Annotation> qualifiers, final Context context) {
public MicroserviceProxyBean(final String microserviceName, final Class<?> proxyInterface, final Set<Annotation> qualifiers, final Set<Annotation> annotations, final Context context) {
this.microserviceName = microserviceName;
this.serviceInterface = proxyInterface;
this.context = context;

this.qualifiers = new HashSet<>(qualifiers);
this.annotations = annotations;

proxyBean = MicroserviceProxy.getProxy(this);
}
Expand Down Expand Up @@ -129,6 +132,10 @@ public Set<Annotation> getQualifiers() {
return qualifiers;
}

public Set<Annotation> getAnnotations() {
return annotations;
}

@Override
public String getName() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ public void processBean(@Observes final ProcessBean processBean, final BeanManag
// Create proxies for the corresponding injection points
final Set<InjectionPoint> injectionPoints = bean.getInjectionPoints();
for (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();
if (member instanceof Field) {
if (log.isTraceEnabled()) {
log.trace("Creating proxy bean for injection point: " + injectionPoint.toString());
}
addInjectableClientProxyBean((Field) member, (MicroserviceReference) qualifier, preProcessQualifiers(injectionPoint.getQualifiers()), beanManager);
addInjectableClientProxyBean((Field) member, (MicroserviceReference) qualifier, preProcessQualifiers(injectionPoint.getQualifiers()), annotations, beanManager);
injectionPointsCount = injectionPointsCount + 1;
}
});
Expand Down Expand Up @@ -173,7 +174,7 @@ public void afterBeanDiscovery(@Observes final AfterBeanDiscovery afterEvent) {
}
}

private void addInjectableClientProxyBean(final Field injectionPointField, final MicroserviceReference microserviceReference, final Set<Annotation> qualifiers, final BeanManager beanManager) {
private void addInjectableClientProxyBean(final Field injectionPointField, final MicroserviceReference microserviceReference, final Set<Annotation> qualifiers, final Set<Annotation> annotations, final BeanManager beanManager) {
final String serviceName;

// first try to use a user defined service name
Expand All @@ -188,10 +189,10 @@ private void addInjectableClientProxyBean(final Field injectionPointField, final
//}
}

addClientProxyBean(serviceName, injectionPointField.getType(), qualifiers);
addClientProxyBean(serviceName, injectionPointField.getType(), qualifiers, annotations);
}

private void addClientProxyBean(final String microserviceName, final Class<?> beanClass, final Set<Annotation> qualifiers) {
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) {
if (microserviceName.equals(microserviceProxyBean.getMicroserviceName()) && beanClass == microserviceProxyBean.getBeanClass()) {
Expand All @@ -208,7 +209,7 @@ 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, context);
final MicroserviceProxyBean microserviceProxyBean = new MicroserviceProxyBean(microserviceName, beanClass, qualifiers, annotations, context);
microserviceProxyBeans.add(microserviceProxyBean);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* -----------------------------------------------------------------------\
* SilverWare
*  
* Copyright (C) 2010 - 2013 the original author or authors.
*  
* 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.silverware.microservices.silver.services;

import org.silverware.microservices.Context;
import org.silverware.microservices.MicroserviceMetaData;

import java.lang.annotation.Annotation;
import java.util.Set;

/**
* @author Martin Večeřa <[email protected]>
*/
public class LookupStrategy {

void initialize(final Context context, final MicroserviceMetaData metaData, final Set<Annotation> options);

Object getService();
}

0 comments on commit f4ad3e4

Please sign in to comment.