-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ivargrimstad/config
Config
- Loading branch information
Showing
17 changed files
with
287 additions
and
21 deletions.
There are no files selected for viewing
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
91 changes: 91 additions & 0 deletions
91
...config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/Configuration.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,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 + '}'; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...config/snoopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfig.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,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 ""; | ||
} |
99 changes: 99 additions & 0 deletions
99
...noopee-config-client/src/main/java/eu/agilejava/snoopee/config/SnoopEEConfigProducer.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,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()); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
snoopee-config/snoopee-config-client/src/main/resources/snoopee.yml
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,3 @@ | ||
snoopee: | ||
snoopeeService: 10.19.210.62:8081/snoopee-service/ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
} | ||
} |
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
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
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.