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#34] Added semantic version resolution
- Loading branch information
1 parent
9beef8e
commit 1d85099
Showing
29 changed files
with
920 additions
and
283 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...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,55 @@ | ||
/* | ||
* -----------------------------------------------------------------------\ | ||
* 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 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} | ||
* | ||
* @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. | ||
*/ | ||
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; | ||
|
@@ -33,12 +34,19 @@ | |
import io.silverware.microservices.providers.cdi.internal.RestInterface; | ||
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; | ||
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; | ||
|
@@ -50,13 +58,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> | ||
*/ | ||
|
@@ -140,15 +141,18 @@ public void run() { | |
|
||
@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 | ||
|
@@ -163,33 +167,39 @@ public Set<Object> lookupMicroservice(final MicroserviceMetaData microserviceMet | |
for (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 | ||
} | ||
} | ||
} | ||
|
@@ -229,7 +239,7 @@ public <T> T lookupBean(final Class<T> type) { | |
@Override | ||
public <T> T findByType(Class<T> type) { | ||
BeanManager beanManager = (BeanManager) context.getProperties().get(BEAN_MANAGER); | ||
Set<T> beans = new HashSet<T>(); | ||
Set<T> beans = new HashSet<>(); | ||
Set<Bean<?>> definitions = beanManager.getBeans(type); | ||
Bean<?> bean = beanManager.resolve(definitions); | ||
CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean); | ||
|
@@ -265,7 +275,7 @@ public Object log(final InvocationContext ic) throws Exception { | |
} | ||
} | ||
|
||
@SuppressWarnings({"unused", "checkstyle:JavadocType"}) | ||
@SuppressWarnings({ "unused", "checkstyle:JavadocType" }) | ||
@Microservice | ||
public static class SilverWareConfiguration implements Configuration { | ||
|
||
|
@@ -281,7 +291,7 @@ public void eventObserver(@Observes MicroservicesInitEvent event) { | |
} | ||
} | ||
|
||
@SuppressWarnings({"unused", "unchecked", "checkstyle:JavadocType"}) | ||
@SuppressWarnings({ "unused", "unchecked", "checkstyle:JavadocType" }) | ||
@Microservice | ||
public static class SilverWareStorage implements Storage { | ||
|
||
|
@@ -311,7 +321,7 @@ public void eventObserver(@Observes 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.