From 54c6746862c1a84e8f42541908cc53544c7852dc Mon Sep 17 00:00:00 2001 From: Ivar Grimstad Date: Fri, 20 Jan 2017 13:47:57 +0100 Subject: [PATCH 1/3] Add simple config producer --- .../eu/agilejava/snoopee/config/Config.java | 47 ++++++++++++++++ .../snoopee/config/ConfigProducer.java | 55 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Config.java create mode 100644 snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/ConfigProducer.java diff --git a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Config.java b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Config.java new file mode 100644 index 0000000..74a790d --- /dev/null +++ b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Config.java @@ -0,0 +1,47 @@ +/* + * The MIT License + * + * Copyright 2017 Ivar Grimstad (ivar.grimstad@cybercom.com). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package eu.agilejava.snoopee.config; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import javax.enterprise.util.Nonbinding; +import javax.inject.Qualifier; + +/** + * + * @author Ivar Grimstad (ivar.grimstad@cybercom.com) + */ +@Qualifier +@Retention(RUNTIME) +@Target({METHOD, FIELD, PARAMETER, TYPE}) +public @interface Config { + + @Nonbinding + String key() default ""; +} diff --git a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/ConfigProducer.java b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/ConfigProducer.java new file mode 100644 index 0000000..b415363 --- /dev/null +++ b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/ConfigProducer.java @@ -0,0 +1,55 @@ +/* + * The MIT License + * + * Copyright 2017 Ivar Grimstad (ivar.grimstad@cybercom.com). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package eu.agilejava.snoopee.config; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; + +/** + * + * @author Ivar Grimstad (ivar.grimstad@cybercom.com) + */ +@ApplicationScoped +public class ConfigProducer { + + @Produces + @Config + public String getStringConfigValue(final InjectionPoint ip) { + return getValue(ip.getAnnotated().getAnnotation(Config.class).key()); + } + + @Produces + @Config + public int getIntConfigValue(InjectionPoint ip) { + return Integer.parseInt(getStringConfigValue(ip)); + } + + + private String getValue(final String key) { + + // TODO: + return "jalla"; + } +} From af4a9e7da6cf4fd4f6bd90a884e985122f22e744 Mon Sep 17 00:00:00 2001 From: Ivar Grimstad Date: Tue, 24 Jan 2017 08:40:17 +0100 Subject: [PATCH 2/3] Add SnoopEEConfig Producer --- snoopee-config/snoopee-config-client/pom.xml | 2 +- .../snoopee/config/Configuration.java | 91 +++++++++++++++++++ .../{Config.java => SnoopEEConfig.java} | 2 +- ...oducer.java => SnoopEEConfigProducer.java} | 50 ++++++++-- .../src/main/resources/snoopee.yml | 3 + snoopee-config/snoopee-config-service/pom.xml | 5 + .../config/api/ConfigurationsResource.java | 21 ++++- .../src/main/resources/snoopee.yml | 2 +- snoopee-discovery/FAQ.adoc | 2 +- snoopee-discovery/README.adoc | 2 +- snoopee-discovery/service-discovery.adoc | 2 +- .../snoopee}/SnoopEEExtensionHelper.java | 2 +- .../snoopee/client/SnoopEEProducer.java | 6 +- .../snoopee/client/SnoopEEServiceClient.java | 8 +- .../annotation/EnableSnoopEEClient.java | 6 +- .../scan/SnoopEERegistrationClient.java | 1 + .../snoopee/scan/SnoopEEScannerExtension.java | 1 + 17 files changed, 174 insertions(+), 32 deletions(-) create mode 100644 snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Configuration.java rename snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/{Config.java => SnoopEEConfig.java} (98%) rename snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/{ConfigProducer.java => SnoopEEConfigProducer.java} (54%) create mode 100644 snoopee-config/snoopee-config-client/src/main/resources/snoopee.yml rename snoopee-discovery/{snoopee/src/main/java/eu/agilejava/snoopee/scan => snoopee-client/src/main/java/eu/agilejava/snoopee}/SnoopEEExtensionHelper.java (98%) diff --git a/snoopee-config/snoopee-config-client/pom.xml b/snoopee-config/snoopee-config-client/pom.xml index 86104d9..48c3250 100644 --- a/snoopee-config/snoopee-config-client/pom.xml +++ b/snoopee-config/snoopee-config-client/pom.xml @@ -4,7 +4,7 @@ eu.agilejava snoopee-config-client 2.0.0-SNAPSHOT - war + jar SnoopEE Config Client SnoopEE - A Config Client for Java EE. diff --git a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Configuration.java b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Configuration.java new file mode 100644 index 0000000..c9e6b24 --- /dev/null +++ b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Configuration.java @@ -0,0 +1,91 @@ +/* + * The MIT License + * + * Copyright 2017 Ivar Grimstad (ivar.grimstad@cybercom.com). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package eu.agilejava.snoopee.config; + +import java.util.Objects; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * + * @author Ivar Grimstad (ivar.grimstad@cybercom.com) + */ +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class Configuration { + + private String key; + private String value; + + Configuration() { + } + + public Configuration(final String key, final String value) { + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public String getValue() { + return value; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 79 * hash + Objects.hashCode(this.key); + hash = 79 * hash + Objects.hashCode(this.value); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Configuration other = (Configuration) obj; + if (!Objects.equals(this.key, other.key)) { + return false; + } + if (!Objects.equals(this.value, other.value)) { + return false; + } + return true; + } + + @Override + public String toString() { + return "Configuration{" + "key=" + key + ", value=" + value + '}'; + } +} diff --git a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Config.java b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfig.java similarity index 98% rename from snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Config.java rename to snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfig.java index 74a790d..7066a40 100644 --- a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Config.java +++ b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfig.java @@ -40,7 +40,7 @@ @Qualifier @Retention(RUNTIME) @Target({METHOD, FIELD, PARAMETER, TYPE}) -public @interface Config { +public @interface SnoopEEConfig { @Nonbinding String key() default ""; diff --git a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/ConfigProducer.java b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfigProducer.java similarity index 54% rename from snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/ConfigProducer.java rename to snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfigProducer.java index b415363..c4a6fb0 100644 --- a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/ConfigProducer.java +++ b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfigProducer.java @@ -23,33 +23,63 @@ */ package eu.agilejava.snoopee.config; +import eu.agilejava.snoopee.SnoopEEConfigurationException; +import eu.agilejava.snoopee.annotation.SnoopEE; +import eu.agilejava.snoopee.client.SnoopEEServiceClient; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import static java.util.stream.Collectors.toMap; +import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.InjectionPoint; +import javax.inject.Inject; +import javax.ws.rs.core.GenericType; /** * * @author Ivar Grimstad (ivar.grimstad@cybercom.com) */ @ApplicationScoped -public class ConfigProducer { +public class SnoopEEConfigProducer { + + @Inject + @SnoopEE(serviceName = "snoopee-config") + private SnoopEEServiceClient configService; + + private Map configurations = new HashMap<>(); @Produces - @Config + @SnoopEEConfig public String getStringConfigValue(final InjectionPoint ip) { - return getValue(ip.getAnnotated().getAnnotation(Config.class).key()); + return getValue(ip.getAnnotated().getAnnotation(SnoopEEConfig.class).key()); } - + @Produces - @Config + @SnoopEEConfig public int getIntConfigValue(InjectionPoint ip) { return Integer.parseInt(getStringConfigValue(ip)); } - - + private String getValue(final String key) { - - // TODO: - return "jalla"; + + if (!configurations.containsKey(key)) { + throw new SnoopEEConfigurationException("No value for key=" + key); + } + + return configurations.get(key); + } + + @PostConstruct + private void init() { +// configurations.put("jalla", configService.getServiceRoot().toString()); + + configurations.putAll(configService.simpleGet("services/helloworld/configurations") + .filter(r -> r.getStatus() == 200) + .map(r -> r.readEntity(new GenericType>() {})) + .orElseThrow(SnoopEEConfigurationException::new) + .stream() + .collect(toMap(Configuration::getKey, Configuration::getValue))); } } diff --git a/snoopee-config/snoopee-config-client/src/main/resources/snoopee.yml b/snoopee-config/snoopee-config-client/src/main/resources/snoopee.yml new file mode 100644 index 0000000..a997972 --- /dev/null +++ b/snoopee-config/snoopee-config-client/src/main/resources/snoopee.yml @@ -0,0 +1,3 @@ +snoopee: + snoopeeService: 10.19.210.62:8081/snoopee-service/ + \ No newline at end of file diff --git a/snoopee-config/snoopee-config-service/pom.xml b/snoopee-config/snoopee-config-service/pom.xml index 5b74599..d4133f9 100644 --- a/snoopee-config/snoopee-config-service/pom.xml +++ b/snoopee-config/snoopee-config-service/pom.xml @@ -25,6 +25,11 @@ snoopee 2.0.0-SNAPSHOT + + eu.agilejava + snoopee-config-client + 2.0.0-SNAPSHOT + diff --git a/snoopee-config/snoopee-config-service/src/main/java/eu/agilejava/snoopee/config/api/ConfigurationsResource.java b/snoopee-config/snoopee-config-service/src/main/java/eu/agilejava/snoopee/config/api/ConfigurationsResource.java index 3219663..d25a630 100644 --- a/snoopee-config/snoopee-config-service/src/main/java/eu/agilejava/snoopee/config/api/ConfigurationsResource.java +++ b/snoopee-config/snoopee-config-service/src/main/java/eu/agilejava/snoopee/config/api/ConfigurationsResource.java @@ -23,21 +23,32 @@ */ package eu.agilejava.snoopee.config.api; +import eu.agilejava.snoopee.config.Configuration; +import java.util.ArrayList; +import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.GenericEntity; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; /** * * @author Ivar Grimstad (ivar.grimstad@cybercom.com) */ -@Path("configurations") +@Path("services") public class ConfigurationsResource { - + @GET - @Path("{serviceId}") - public Response getConfigurationsForService(@PathParam("serviceId") String serviceId ) { - return Response.ok().build(); + @Path("{serviceName}/configurations") + @Produces(MediaType.APPLICATION_JSON) + public Response getConfigurationsForService(@PathParam("serviceName") String serviceName) { + + List configurations = new ArrayList<>(); + configurations.add(new Configuration("jalla", "balla")); + + return Response.ok(new GenericEntity>(configurations) {}).build(); } } diff --git a/snoopee-config/snoopee-config-service/src/main/resources/snoopee.yml b/snoopee-config/snoopee-config-service/src/main/resources/snoopee.yml index 9a9a90c..246087c 100644 --- a/snoopee-config/snoopee-config-service/src/main/resources/snoopee.yml +++ b/snoopee-config/snoopee-config-service/src/main/resources/snoopee.yml @@ -4,4 +4,4 @@ snoopee: serviceRoot: snoopee-config-service/api # snoopeeService: localhost:8080/snoopee-service/ - snoopeeService: 192.168.99.100:8081/snoopee-service/ \ No newline at end of file + snoopeeService: 10.19.210.62:8081/snoopee-service/ \ No newline at end of file diff --git a/snoopee-discovery/FAQ.adoc b/snoopee-discovery/FAQ.adoc index fb0520a..0f5d435 100644 --- a/snoopee-discovery/FAQ.adoc +++ b/snoopee-discovery/FAQ.adoc @@ -1,4 +1,4 @@ -= Frequently Asked Questions about Snoop += Frequently Asked Questions about SnoopEE ## Is SnoopEE a Load Balancer? *No*, the intention of SnoopEE is to provide a service registration and lookup diff --git a/snoopee-discovery/README.adoc b/snoopee-discovery/README.adoc index 240e8b8..c129377 100644 --- a/snoopee-discovery/README.adoc +++ b/snoopee-discovery/README.adoc @@ -25,7 +25,7 @@ SnoopEE [ˈsnuːpı] is an experimental registration and discovery service for J == Examples -- link:https://github.com/ivargrimstad/snoop-samples[snoop-samples@GitHub (works with SnoopEE 1.0] +- link:https://github.com/ivargrimstad/snoopee-samples[snoopee-samples@GitHub (works with SnoopEE 1.0] - link:https://github.com/arun-gupta/microservices[https://github.com/arun-gupta/microservices] == FAQ diff --git a/snoopee-discovery/service-discovery.adoc b/snoopee-discovery/service-discovery.adoc index 4b8cb2b..5fe3876 100644 --- a/snoopee-discovery/service-discovery.adoc +++ b/snoopee-discovery/service-discovery.adoc @@ -11,7 +11,7 @@ . Configure the client .. Using `snoopee.yml` - snoop: + snoopee: snoopeeService: 192.168.59.103:8081/snoopee-service/ .. Or by environment variable diff --git a/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEEExtensionHelper.java b/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/SnoopEEExtensionHelper.java similarity index 98% rename from snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEEExtensionHelper.java rename to snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/SnoopEEExtensionHelper.java index 6c51c75..55abef9 100644 --- a/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEEExtensionHelper.java +++ b/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/SnoopEEExtensionHelper.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package eu.agilejava.snoopee.scan; +package eu.agilejava.snoopee; /** * Singleton to store the information gathered from annotation scan. diff --git a/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEProducer.java b/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEProducer.java index 4592de8..0af8a81 100644 --- a/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEProducer.java +++ b/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEProducer.java @@ -47,7 +47,7 @@ public class SnoopEEProducer { private static final Logger LOGGER = Logger.getLogger("eu.agilejava.snoopee"); - private Map snoopConfig = Collections.EMPTY_MAP; + private Map snoopeeConfig = Collections.EMPTY_MAP; /** * Creates a SnoopEEServiceClient for the named service. @@ -64,7 +64,7 @@ public SnoopEEServiceClient lookup(InjectionPoint ip) { LOGGER.config(() -> "producing " + applicationName); - String serviceUrl = "http://" + readProperty("snoopeeService", snoopConfig); + String serviceUrl = "http://" + readProperty("snoopeeService", snoopeeConfig); LOGGER.config(() -> "Service URL: " + serviceUrl); return new SnoopEEServiceClient.Builder(applicationName) @@ -101,7 +101,7 @@ private void init() { Yaml yaml = new Yaml(); Map props = (Map) yaml.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("/snoopee.yml")); - snoopConfig = (Map) props.get("snoopee"); + snoopeeConfig = (Map) props.get("snoopee"); } catch (YAMLException e) { LOGGER.config(() -> "No configuration file. Using env properties."); diff --git a/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEServiceClient.java b/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEServiceClient.java index e507f49..c034625 100644 --- a/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEServiceClient.java +++ b/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEServiceClient.java @@ -82,12 +82,12 @@ private SnoopEEServiceClient(final Builder builder) { */ public WebTarget getServiceRoot() throws SnoopEEServiceUnavailableException { - SnoopEEConfig snoopConfig = getConfigFromSnoop(); + SnoopEEConfig snoopEEConfig = getConfigFromSnoopEE(); LOGGER.fine(() -> "looking up service for " + applicationName); return ClientBuilder.newClient() - .target(snoopConfig.getServiceHome()) - .path(snoopConfig.getServiceRoot()); + .target(snoopEEConfig.getServiceHome()) + .path(snoopEEConfig.getServiceRoot()); } /** @@ -200,7 +200,7 @@ public Optional simplePost(String resourcePath, Object resource) { return returnValue; } - private SnoopEEConfig getConfigFromSnoop() throws SnoopEEServiceUnavailableException { + private SnoopEEConfig getConfigFromSnoopEE() throws SnoopEEServiceUnavailableException { try { Response response = ClientBuilder.newClient() diff --git a/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/annotation/EnableSnoopEEClient.java b/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/annotation/EnableSnoopEEClient.java index 042411b..e8b77f2 100644 --- a/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/annotation/EnableSnoopEEClient.java +++ b/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/annotation/EnableSnoopEEClient.java @@ -34,8 +34,8 @@ import javax.enterprise.util.Nonbinding; /** - * Annotation for enabling application as Snoop client. Use this annotation to register the application (service) with - * Snoop. + * Annotation for enabling application as SnoopEE client. Use this annotation to register the application (service) with + * SnoopEE. * * @author Ivar Grimstad (ivar.grimstad@gmail.com) */ @@ -47,7 +47,7 @@ public @interface EnableSnoopEEClient { /** - * The service name. This is the unique identifier for this service when it is registered with Snoop. + * The service name. This is the unique identifier for this service when it is registered with SnoopEE. * * @return The name of the service */ diff --git a/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEERegistrationClient.java b/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEERegistrationClient.java index d936d3b..96f3046 100644 --- a/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEERegistrationClient.java +++ b/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEERegistrationClient.java @@ -23,6 +23,7 @@ */ package eu.agilejava.snoopee.scan; +import eu.agilejava.snoopee.SnoopEEExtensionHelper; import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml; import com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException; import eu.agilejava.snoopee.SnoopEEConfigurationException; diff --git a/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEEScannerExtension.java b/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEEScannerExtension.java index 85c5f1c..cd80336 100644 --- a/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEEScannerExtension.java +++ b/snoopee-discovery/snoopee/src/main/java/eu/agilejava/snoopee/scan/SnoopEEScannerExtension.java @@ -23,6 +23,7 @@ */ package eu.agilejava.snoopee.scan; +import eu.agilejava.snoopee.SnoopEEExtensionHelper; import java.util.logging.Logger; import javax.enterprise.event.Observes; import javax.enterprise.inject.spi.AfterBeanDiscovery; From 86632d0e410f7c60fc0e2a8cd406fb74a5f914ca Mon Sep 17 00:00:00 2001 From: Ivar Grimstad Date: Tue, 24 Jan 2017 09:50:40 +0100 Subject: [PATCH 3/3] Error handling for configurations Request scoped producer --- .../snoopee/config/SnoopEEConfig.java | 3 ++ .../snoopee/config/SnoopEEConfigProducer.java | 44 ++++++++++++------- .../snoopee/client/SnoopEEProducer.java | 5 +++ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfig.java b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfig.java index 7066a40..107552f 100644 --- a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfig.java +++ b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfig.java @@ -44,4 +44,7 @@ @Nonbinding String key() default ""; + + @Nonbinding + String defaultValue() default ""; } diff --git a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfigProducer.java b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfigProducer.java index c4a6fb0..4e28bc2 100644 --- a/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfigProducer.java +++ b/snoopee-config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfigProducer.java @@ -24,14 +24,17 @@ package eu.agilejava.snoopee.config; import eu.agilejava.snoopee.SnoopEEConfigurationException; +import eu.agilejava.snoopee.SnoopEEExtensionHelper; import eu.agilejava.snoopee.annotation.SnoopEE; import eu.agilejava.snoopee.client.SnoopEEServiceClient; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; +import java.util.logging.Logger; import static java.util.stream.Collectors.toMap; import javax.annotation.PostConstruct; -import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.Dependent; import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.InjectionPoint; import javax.inject.Inject; @@ -41,9 +44,11 @@ * * @author Ivar Grimstad (ivar.grimstad@cybercom.com) */ -@ApplicationScoped +@Dependent public class SnoopEEConfigProducer { + private static final Logger LOGGER = Logger.getLogger(SnoopEEConfigProducer.class.getName()); + @Inject @SnoopEE(serviceName = "snoopee-config") private SnoopEEServiceClient configService; @@ -53,7 +58,7 @@ public class SnoopEEConfigProducer { @Produces @SnoopEEConfig public String getStringConfigValue(final InjectionPoint ip) { - return getValue(ip.getAnnotated().getAnnotation(SnoopEEConfig.class).key()); + return getValue(ip.getAnnotated().getAnnotation(SnoopEEConfig.class)); } @Produces @@ -62,24 +67,33 @@ public int getIntConfigValue(InjectionPoint ip) { return Integer.parseInt(getStringConfigValue(ip)); } - private String getValue(final String key) { + private String getValue(final SnoopEEConfig config) { - if (!configurations.containsKey(key)) { - throw new SnoopEEConfigurationException("No value for key=" + key); + if (configurations.containsKey(config.key())) { + return configurations.get(config.key()); + } else if (!config.defaultValue().isEmpty()) { + LOGGER.warning(() -> "No value for key: " + config.key() + ". Using DEFAULT value: " + config.defaultValue()); + return config.defaultValue(); + } else { + LOGGER.severe(() -> "No value for key: " + config.key()); + throw new SnoopEEConfigurationException("No value for key:" + config.key()); } - - return configurations.get(key); } @PostConstruct private void init() { -// configurations.put("jalla", configService.getServiceRoot().toString()); - configurations.putAll(configService.simpleGet("services/helloworld/configurations") - .filter(r -> r.getStatus() == 200) - .map(r -> r.readEntity(new GenericType>() {})) - .orElseThrow(SnoopEEConfigurationException::new) - .stream() - .collect(toMap(Configuration::getKey, Configuration::getValue))); + try { + + configurations.putAll(configService.simpleGet("services/" + SnoopEEExtensionHelper.getServiceName() + "/configurations") + .filter(r -> r.getStatus() == 200) + .map(r -> r.readEntity(new GenericType>() {})) + .get() + .stream() + .collect(toMap(Configuration::getKey, Configuration::getValue))); + + } catch (NoSuchElementException e) { + LOGGER.warning(() -> "No configurations found for service: " + SnoopEEExtensionHelper.getServiceName()); + } } } diff --git a/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEProducer.java b/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEProducer.java index 0af8a81..a7f2d3c 100644 --- a/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEProducer.java +++ b/snoopee-discovery/snoopee-client/src/main/java/eu/agilejava/snoopee/client/SnoopEEProducer.java @@ -26,6 +26,7 @@ import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml; import com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException; import eu.agilejava.snoopee.SnoopEEConfigurationException; +import eu.agilejava.snoopee.SnoopEEExtensionHelper; import java.util.Collections; import java.util.Map; import java.util.Optional; @@ -103,6 +104,10 @@ private void init() { snoopeeConfig = (Map) props.get("snoopee"); + if (!SnoopEEExtensionHelper.isSnoopEnabled()) { + SnoopEEExtensionHelper.setServiceName(readProperty("serviceName", snoopeeConfig)); + } + } catch (YAMLException e) { LOGGER.config(() -> "No configuration file. Using env properties."); }