forked from SilverThings/SilverWare
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SilverThings#47] [SilverThings#34] Implemented version lookup and cl…
…ustering fixes
- Loading branch information
1 parent
abcd287
commit b7c7c00
Showing
43 changed files
with
1,014 additions
and
333 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...e-provider/src/main/java/io/silverware/microservices/annotations/MicroserviceVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* -----------------------------------------------------------------------\ | ||
* SilverWare | ||
* | ||
* Copyright (C) 2010 - 2016 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 io.silverware.microservices.annotations; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* This annotation can be used in two different scenarios: | ||
* <ul> | ||
* <li><strong>Microservice:</strong> can specify version of Microservice Implementation and API. </li> | ||
* <li><strong>Injection point:</strong> can specify supported version of a Microservice API. </li> | ||
* </ul> | ||
* These versions are used in the lookup of the Microservices in the cluster. | ||
* See @{@link io.silverware.microservices.providers.cdi.util.VersionResolver} | ||
* See @{@link io.silverware.microservices.util.VersionComparator} | ||
* | ||
* @author Slavomír Krupa ([email protected]) | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Inherited | ||
@Target({ ElementType.TYPE }) | ||
public @interface MicroserviceVersion { | ||
|
||
/** | ||
* Gets the API version of the Microservice. | ||
*/ | ||
String api() default ""; | ||
|
||
/** | ||
* Gets the implementation version of the Microservice. | ||
* If not defined then {@link io.silverware.microservices.providers.cdi.util.VersionResolver} continues in search for microservice version. | ||
*/ | ||
String implementation() default ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import io.silverware.microservices.MicroserviceMetaData; | ||
import io.silverware.microservices.annotations.Microservice; | ||
import io.silverware.microservices.annotations.MicroserviceReference; | ||
import io.silverware.microservices.annotations.MicroserviceVersion; | ||
import io.silverware.microservices.providers.MicroserviceProvider; | ||
import io.silverware.microservices.providers.cdi.builtin.Configuration; | ||
import io.silverware.microservices.providers.cdi.builtin.CurrentContext; | ||
|
@@ -32,6 +33,7 @@ | |
import io.silverware.microservices.providers.cdi.internal.MicroservicesInitEvent; | ||
import io.silverware.microservices.silver.CdiSilverService; | ||
import io.silverware.microservices.util.Utils; | ||
import io.silverware.microservices.util.VersionComparator; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
@@ -44,7 +46,6 @@ | |
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; | ||
|
@@ -58,6 +59,8 @@ | |
|
||
/** | ||
* @author <a href="mailto:[email protected]">Martin Večeřa</a> | ||
* Changes in version resolution in lookupMicroservice | ||
* @author Slavomir Krupa ([email protected]) | ||
*/ | ||
public class CdiMicroserviceProvider implements MicroserviceProvider, CdiSilverService { | ||
|
||
|
@@ -134,15 +137,18 @@ public void run() { | |
@Override | ||
@SuppressWarnings("checkstyle:JavadocMethod") | ||
public Set<Object> lookupMicroservice(final MicroserviceMetaData microserviceMetaData) { | ||
// name can not be null - contract of MicroserviceMetaData | ||
final String name = microserviceMetaData.getName(); | ||
final Class<?> type = microserviceMetaData.getType(); | ||
final Set<Annotation> qualifiers = microserviceMetaData.getQualifiers(); | ||
final String apiVersion = microserviceMetaData.getApiVersion(); | ||
final Set<Object> matchingBeansByName = new HashSet<>(); | ||
final Set<Object> matchingBeansByType = new HashSet<>(); | ||
boolean wasAlternative = false; | ||
|
||
/* | ||
We are in search for a CDI bean that meets the provided meta-data. | ||
If there is a MicroserviceVersion annotation and it is not matching the version in metadata we skip that bean automatically. | ||
Input and corresponding output is as follows: | ||
* name specified in MicroserviceReference or derived from data type (both class or interface) | ||
* beans having the same name in Microservice annotation | ||
|
@@ -157,33 +163,39 @@ public Set<Object> lookupMicroservice(final MicroserviceMetaData microserviceMet | |
for (final Bean<?> bean : beans) { | ||
if (bean.getBeanClass().isAnnotationPresent(Microservice.class) && !(bean instanceof MicroserviceProxyBean)) { | ||
final Bean<?> theBean = beanManager.resolve(Collections.singleton(bean)); | ||
|
||
if (theBean.getBeanClass().isAnnotationPresent(MicroserviceVersion.class)) { | ||
final MicroserviceVersion versionAnnotation = theBean.getBeanClass().getAnnotation(MicroserviceVersion.class); | ||
String implementationVersion = versionAnnotation.implementation(); | ||
if (!VersionComparator.forVersion(implementationVersion).satisfies(apiVersion)) { | ||
continue; | ||
} | ||
} | ||
final Microservice microserviceAnnotation = theBean.getBeanClass().getAnnotation(Microservice.class); | ||
|
||
if (name != null) { | ||
if ((!microserviceAnnotation.value().isEmpty() && name.equals(microserviceAnnotation.value())) || | ||
(microserviceAnnotation.value().isEmpty() && name.equals(theBean.getName()))) { | ||
matchingBeansByName.add(beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean))); | ||
} else if (type.isAssignableFrom(theBean.getBeanClass())) { | ||
final Set<Annotation> qualifiersToCompare = new HashSet<>(theBean.getQualifiers()); | ||
qualifiers.stream().forEach(qualifiersToCompare::remove); | ||
|
||
if (qualifiersToCompare.size() == 0) { | ||
|
||
if (bean.isAlternative()) { | ||
if (!wasAlternative) { | ||
matchingBeansByType.clear(); | ||
matchingBeansByType.add(beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean))); | ||
wasAlternative = true; | ||
} else { | ||
matchingBeansByType.add(beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean))); | ||
throw new IllegalStateException(String.format("There are more than alternate beans matching the query: %s. The beans are: %s.", microserviceMetaData.toString(), matchingBeansByType.toString())); | ||
} | ||
if ((!microserviceAnnotation.value().isEmpty() && name.equals(microserviceAnnotation.value())) || | ||
(microserviceAnnotation.value().isEmpty() && name.equals(theBean.getName()))) { | ||
matchingBeansByName.add(beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean))); | ||
} else if (type.isAssignableFrom(theBean.getBeanClass())) { | ||
final Set<Annotation> qualifiersToCompare = new HashSet<>(theBean.getQualifiers()); | ||
qualifiers.stream().forEach(qualifiersToCompare::remove); | ||
|
||
if (qualifiersToCompare.size() == 0) { | ||
|
||
if (bean.isAlternative()) { | ||
if (!wasAlternative) { | ||
matchingBeansByType.clear(); | ||
matchingBeansByType.add(beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean))); | ||
wasAlternative = true; | ||
} else { | ||
matchingBeansByType.add(beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean))); | ||
throw new IllegalStateException(String.format("There are more than alternate beans matching the query: %s. The beans are: %s.", microserviceMetaData.toString(), matchingBeansByType.toString())); | ||
} | ||
} else { | ||
if (!wasAlternative) { | ||
matchingBeansByType.add(beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean))); | ||
} else { | ||
if (!wasAlternative) { | ||
matchingBeansByType.add(beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean))); | ||
} else { | ||
// ignore this bean | ||
} | ||
// ignore this bean | ||
} | ||
} | ||
} | ||
|
@@ -223,7 +235,7 @@ public <T> T lookupBean(final Class<T> type) { | |
@Override | ||
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<T> beans = new HashSet<>(); | ||
final Set<Bean<?>> definitions = beanManager.getBeans(type); | ||
final Bean<?> bean = beanManager.resolve(definitions); | ||
final CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean); | ||
|
@@ -259,7 +271,7 @@ public Object log(final InvocationContext ic) throws Exception { | |
} | ||
} | ||
|
||
@SuppressWarnings({"unused", "checkstyle:JavadocType"}) | ||
@SuppressWarnings({ "unused", "checkstyle:JavadocType" }) | ||
@Microservice | ||
public static class SilverWareConfiguration implements Configuration { | ||
|
||
|
@@ -275,7 +287,7 @@ public void eventObserver(@Observes final MicroservicesInitEvent event) { | |
} | ||
} | ||
|
||
@SuppressWarnings({"unused", "unchecked", "checkstyle:JavadocType"}) | ||
@SuppressWarnings({ "unused", "unchecked", "checkstyle:JavadocType" }) | ||
@Microservice | ||
public static class SilverWareStorage implements Storage { | ||
|
||
|
@@ -305,7 +317,7 @@ public void eventObserver(@Observes final MicroservicesInitEvent event) { | |
} | ||
} | ||
|
||
@SuppressWarnings({"unused", "checkstyle:JavadocMethod"}) | ||
@SuppressWarnings({ "unused", "checkstyle:JavadocMethod" }) | ||
@Microservice | ||
public static class SilverWareCurrentContext implements CurrentContext { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.