Skip to content

Commit

Permalink
Merge pull request #3 from ivargrimstad/config
Browse files Browse the repository at this point in the history
Config
  • Loading branch information
ivargrimstad authored Jan 24, 2017
2 parents 21535e8 + 86632d0 commit 0302e2d
Show file tree
Hide file tree
Showing 17 changed files with 287 additions and 21 deletions.
2 changes: 1 addition & 1 deletion snoopee-config/snoopee-config-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>eu.agilejava</groupId>
<artifactId>snoopee-config-client</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<packaging>jar</packaging>

<name>SnoopEE Config Client</name>
<description>SnoopEE - A Config Client for Java EE.</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* The MIT License
*
* Copyright 2017 Ivar Grimstad ([email protected]).
*
* 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 ([email protected])
*/
@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 + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* The MIT License
*
* Copyright 2017 Ivar Grimstad ([email protected]).
*
* 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 ([email protected])
*/
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface SnoopEEConfig {

@Nonbinding
String key() default "";

@Nonbinding
String defaultValue() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* The MIT License
*
* Copyright 2017 Ivar Grimstad ([email protected]).
*
* 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 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.Dependent;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;
import javax.ws.rs.core.GenericType;

/**
*
* @author Ivar Grimstad ([email protected])
*/
@Dependent
public class SnoopEEConfigProducer {

private static final Logger LOGGER = Logger.getLogger(SnoopEEConfigProducer.class.getName());

@Inject
@SnoopEE(serviceName = "snoopee-config")
private SnoopEEServiceClient configService;

private Map<String, String> configurations = new HashMap<>();

@Produces
@SnoopEEConfig
public String getStringConfigValue(final InjectionPoint ip) {
return getValue(ip.getAnnotated().getAnnotation(SnoopEEConfig.class));
}

@Produces
@SnoopEEConfig
public int getIntConfigValue(InjectionPoint ip) {
return Integer.parseInt(getStringConfigValue(ip));
}

private String getValue(final SnoopEEConfig config) {

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());
}
}

@PostConstruct
private void init() {

try {

configurations.putAll(configService.simpleGet("services/" + SnoopEEExtensionHelper.getServiceName() + "/configurations")
.filter(r -> r.getStatus() == 200)
.map(r -> r.readEntity(new GenericType<List<Configuration>>() {}))
.get()
.stream()
.collect(toMap(Configuration::getKey, Configuration::getValue)));

} catch (NoSuchElementException e) {
LOGGER.warning(() -> "No configurations found for service: " + SnoopEEExtensionHelper.getServiceName());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
snoopee:
snoopeeService: 10.19.210.62:8081/snoopee-service/

5 changes: 5 additions & 0 deletions snoopee-config/snoopee-config-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<artifactId>snoopee</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>eu.agilejava</groupId>
<artifactId>snoopee-config-client</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([email protected])
*/
@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<Configuration> configurations = new ArrayList<>();
configurations.add(new Configuration("jalla", "balla"));

return Response.ok(new GenericEntity<List<Configuration>>(configurations) {}).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ snoopee:
serviceRoot: snoopee-config-service/api

# snoopeeService: localhost:8080/snoopee-service/
snoopeeService: 192.168.99.100:8081/snoopee-service/
snoopeeService: 10.19.210.62:8081/snoopee-service/
2 changes: 1 addition & 1 deletion snoopee-discovery/FAQ.adoc
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion snoopee-discovery/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion snoopee-discovery/service-discovery.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
. Configure the client
.. Using `snoopee.yml`

snoop:
snoopee:
snoopeeService: 192.168.59.103:8081/snoopee-service/

.. Or by environment variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,7 +48,7 @@ public class SnoopEEProducer {

private static final Logger LOGGER = Logger.getLogger("eu.agilejava.snoopee");

private Map<String, Object> snoopConfig = Collections.EMPTY_MAP;
private Map<String, Object> snoopeeConfig = Collections.EMPTY_MAP;

/**
* Creates a SnoopEEServiceClient for the named service.
Expand All @@ -64,7 +65,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)
Expand Down Expand Up @@ -101,7 +102,11 @@ private void init() {
Yaml yaml = new Yaml();
Map<String, Object> props = (Map<String, Object>) yaml.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("/snoopee.yml"));

snoopConfig = (Map<String, Object>) props.get("snoopee");
snoopeeConfig = (Map<String, Object>) props.get("snoopee");

if (!SnoopEEExtensionHelper.isSnoopEnabled()) {
SnoopEEExtensionHelper.setServiceName(readProperty("serviceName", snoopeeConfig));
}

} catch (YAMLException e) {
LOGGER.config(() -> "No configuration file. Using env properties.");
Expand Down
Loading

0 comments on commit 0302e2d

Please sign in to comment.