Repository: incubator-tamaya Updated Branches: refs/heads/master 9fd1cee1f -> 5701c27e7
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/5701c27e/modules/optional/src/main/java/org/apache/tamaya/optional/OptionalConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/optional/src/main/java/org/apache/tamaya/optional/OptionalConfiguration.java b/modules/optional/src/main/java/org/apache/tamaya/optional/OptionalConfiguration.java index 8cc5d4f..4868bc1 100644 --- a/modules/optional/src/main/java/org/apache/tamaya/optional/OptionalConfiguration.java +++ b/modules/optional/src/main/java/org/apache/tamaya/optional/OptionalConfiguration.java @@ -19,12 +19,12 @@ package org.apache.tamaya.optional; -import org.apache.tamaya.ConfigurationProvider; - import java.util.Objects; +import org.apache.tamaya.ConfigurationProvider; + /** - * Simplified configuration API, that can be used by code that only want Tamaya to optionally enhance its configuration + * Simplified configuration API, that can be used by code that only wants Tamaya to optionally enhance its configuration * mechanism, but by default uses its own configuration by default. */ public final class OptionalConfiguration { @@ -48,7 +48,7 @@ public final class OptionalConfiguration { try { Class.forName(TAMAYA_CONFIGURATION); return true; - } catch (Exception e) { + } catch (final Exception e) { return false; } } @@ -58,6 +58,7 @@ public final class OptionalConfiguration { * In all other cases {@code null} is returned. */ public static final ValueProvider DEFAULT_PROVIDER = new ValueProvider() { + @SuppressWarnings("unchecked") @Override public <T> T get(String key, Class<T> type) { if (String.class == type) { @@ -93,7 +94,8 @@ public final class OptionalConfiguration { /** * Creates a new instance. - * @param policy the policy how a value should be evaluated depending ig Tamaya is availalbe or not. + * + * @param policy the policy how a value should be evaluated depending if Tamaya is available or not. * @param provider the non Tamaya-based provider to be used to evaluate values. */ private OptionalConfiguration(EvaluationPolicy policy, ValueProvider provider) { @@ -104,6 +106,8 @@ public final class OptionalConfiguration { /** * Returns an instance of OptionalConfiguration, which uses the given provider and policy for evaluating the values. * + * @param policy the policy how a value should be evaluated depending if Tamaya is available or not. + * @param provider the non Tamaya-based provider to be used to evaluate values. * @return a default OptionalConfiguration instance, never null. */ public static OptionalConfiguration of(EvaluationPolicy policy, ValueProvider provider) { @@ -114,6 +118,7 @@ public final class OptionalConfiguration { * Returns a default instance, which uses a default provider returning values from system properties and environment * only. * + * @param policy the policy how a value should be evaluated depending if Tamaya is available or not. * @return a default OptionalConfiguration instance, never null. */ public static OptionalConfiguration of(EvaluationPolicy policy) { @@ -133,13 +138,13 @@ public final class OptionalConfiguration { /** * Access a String value. * - * @param key the key, not null. + * @param key the key, not null. * @param defaultValue the default value, returned if no such key is found in the configuration. * @return the value found, or null. */ public String getOrDefault(String key, String defaultValue) { - String value = get(key, String.class); - if(value==null){ + final String value = get(key, String.class); + if (value == null) { return defaultValue; } return value; @@ -155,8 +160,8 @@ public final class OptionalConfiguration { * @return the value, or null. */ public <T> T get(String key, Class<T> type) { - T value = provider.get(key, type); - T tamayaValue = getTamaya(key, type); + final T value = provider.get(key, type); + final T tamayaValue = getTamaya(key, type); switch (policy) { case OTHER_OVERRIDES_TAMAYA: return value != null ? value : tamayaValue; @@ -178,15 +183,15 @@ public final class OptionalConfiguration { /** * Access a String value. * - * @param key the key, not null. - * @param type the target type, not null. - * @param <T> the type param. + * @param key the key, not null. + * @param type the target type, not null. + * @param <T> the type param. * @param defaultValue the default value, returned if no such key is found in the configuration. * @return the value found, or null. */ public <T> T getOrDefault(String key, Class<T> type, T defaultValue) { - T value = get(key, type); - if(value==null){ + final T value = get(key, type); + if (value == null) { return defaultValue; } return value; @@ -194,13 +199,14 @@ public final class OptionalConfiguration { /** * Internal method that evaluates a value from Tamaya, when Tamaya is loaded. - * @param key the key, not null. + * + * @param key the key, not null. * @param type the target type, not null. - * @param <T> The type param + * @param <T> The type param * @return the corresponding value from Tamaya, or null. */ private <T> T getTamaya(String key, Class<T> type) { - if(TAMAYA_LOADED){ + if (TAMAYA_LOADED) { return ConfigurationProvider.getConfiguration().get(key, type); } return null; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/5701c27e/modules/server/pom.xml ---------------------------------------------------------------------- diff --git a/modules/server/pom.xml b/modules/server/pom.xml index f5b1e70..28e26e5 100644 --- a/modules/server/pom.xml +++ b/modules/server/pom.xml @@ -134,7 +134,6 @@ under the License. <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> - <version>2.4</version> <configuration> <archive> <manifest> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/5701c27e/modules/server/src/main/java/org/apache/tamaya/server/ConfigurationResource.java ---------------------------------------------------------------------- diff --git a/modules/server/src/main/java/org/apache/tamaya/server/ConfigurationResource.java b/modules/server/src/main/java/org/apache/tamaya/server/ConfigurationResource.java index a9575fe..f38184e 100644 --- a/modules/server/src/main/java/org/apache/tamaya/server/ConfigurationResource.java +++ b/modules/server/src/main/java/org/apache/tamaya/server/ConfigurationResource.java @@ -18,9 +18,10 @@ */ package org.apache.tamaya.server; -import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.functions.ConfigurationFunctions; +import java.io.StringWriter; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicLong; import javax.json.Json; import javax.json.JsonArray; @@ -38,22 +39,22 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; -import java.io.StringWriter; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.atomic.AtomicLong; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.functions.ConfigurationFunctions; /** - * Spring boot based configuration service that behavious compatible with etcd REST API (excluded the blocking API - * calls). + * Spring boot based configuration service that is compatible with etcd REST API + * (excluding the blocking API calls). */ @Path("/") @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN}) public class ConfigurationResource { private final String scope; - private final AtomicLong readCounter = new AtomicLong(); - private final AtomicLong writeCounter = new AtomicLong(); - private final AtomicLong deleteCounter = new AtomicLong(); + private final AtomicLong readCounter = new AtomicLong(); + private final AtomicLong writeCounter = new AtomicLong(); + private final AtomicLong deleteCounter = new AtomicLong(); public ConfigurationResource(String scope) { this.scope = scope; @@ -63,6 +64,7 @@ public class ConfigurationResource { @Path("/version") @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN}) public String version() { + // TODO TAMAYA-139: make version string dynamically calculated based on mvn settings String product = VersionProperties.getProduct().replace("\"", "\\\""); String version = VersionProperties.getVersion().replace("\"", "\\\""); @@ -76,34 +78,37 @@ public class ConfigurationResource { } /** - * * This models a etcd2 compliant access point for getting a property value. - * @return + * + * @param recursive NOT YET IMPLEMENTED! + * @return all configuration property values. */ @GET @Path("/keys") public String readConfig(@QueryParam("recursive") Boolean recursive) { readCounter.incrementAndGet(); - Configuration config = ConfigurationProvider.getConfiguration(); - Map<String,String> children = config.getProperties(); - JsonArrayBuilder ab = Json.createArrayBuilder(); - for(Map.Entry<String,String> en: children.entrySet()){ - Node node = new Node(config, en.getKey(), "node"); + final Configuration config = ConfigurationProvider.getConfiguration(); + final Map<String, String> children = config.getProperties(); + final JsonArrayBuilder ab = Json.createArrayBuilder(); + for (final Map.Entry<String, String> en : children.entrySet()) { + final Node node = new Node(config, en.getKey(), "node"); ab.add(node.createJsonObject()); } - Node node = new Node(config, null, "node", ab.build()); - JsonObjectBuilder root = Json.createObjectBuilder().add("action", "get") + final Node node = new Node(config, null, "node", ab.build()); + final JsonObjectBuilder root = Json.createObjectBuilder().add("action", "get") .add("node", node.createJsonObject()); - StringWriter writer = new StringWriter(); - JsonWriter jwriter = Json.createWriter(writer); + final StringWriter writer = new StringWriter(); + final JsonWriter jwriter = Json.createWriter(writer); jwriter.writeObject(root.build()); return writer.toString(); } /** * This models a etcd2 compliant access point for getting a property value. - * @param key - * @return + * + * @param key name of the key to show + * @param recursive NOT YET IMPLEMENTED! + * @return specific configuration key derived from the given key name. */ @GET @Path("/v2/keys/{key}") @@ -113,44 +118,46 @@ public class ConfigurationResource { /** * This models a etcd2 compliant access point for getting a property value. - * @param key - * @return + * + * @param key name of the key to show + * @param recursive NOT YET IMPLEMENTED! + * @return configuration value of the given key. */ @GET @Path("/keys/{key}") public String readConfig(@PathParam("key") String key, @QueryParam("recursive") Boolean recursive) { readCounter.incrementAndGet(); - Configuration config = ConfigurationProvider.getConfiguration(); - if(key!=null) { + final Configuration config = ConfigurationProvider.getConfiguration(); + if (key != null) { if (key.startsWith("/")) { key = key.substring(1); } if (config.get(key) != null && !recursive) { - Node node = new Node(config, key, "node"); - JsonObjectBuilder root = Json.createObjectBuilder().add("action", "get") + final Node node = new Node(config, key, "node"); + final JsonObjectBuilder root = Json.createObjectBuilder().add("action", "get") .add("node", node.createJsonObject()); - StringWriter writer = new StringWriter(); - JsonGenerator gen = Json.createGenerator(writer); + final StringWriter writer = new StringWriter(); + final JsonGenerator gen = Json.createGenerator(writer); gen.write(root.build()); return writer.toString(); } } - Map<String,String> children = null; - if(key==null){ + Map<String, String> children = null; + if (key == null) { children = config.getProperties(); - } else{ + } else { children = config.with(ConfigurationFunctions.section(key)).getProperties(); } - JsonArrayBuilder ab = Json.createArrayBuilder(); - for(Map.Entry<String,String> en: children.entrySet()){ - Node node = new Node(config, en.getKey(), "node"); + final JsonArrayBuilder ab = Json.createArrayBuilder(); + for (final Map.Entry<String, String> en : children.entrySet()) { + final Node node = new Node(config, en.getKey(), "node"); ab.add(node.createJsonObject()); } - Node node = new Node(config, key, "node", ab.build()); - JsonObjectBuilder root = Json.createObjectBuilder().add("action", "get") + final Node node = new Node(config, key, "node", ab.build()); + final JsonObjectBuilder root = Json.createObjectBuilder().add("action", "get") .add("node", node.createJsonObject()); - StringWriter writer = new StringWriter(); - JsonWriter jwriter = Json.createWriter(writer); + final StringWriter writer = new StringWriter(); + final JsonWriter jwriter = Json.createWriter(writer); jwriter.writeObject(root.build()); return writer.toString(); } @@ -158,48 +165,52 @@ public class ConfigurationResource { @PUT @Path("/v2/keys/{key}") public String writeEtcdConfig(@PathParam("key") String key, @javax.ws.rs.FormParam("value") String value, - @FormParam("ttl") Integer ttl) { + @FormParam("ttl") Integer ttl) { return writeConfig(key, value, ttl); } + /** - * This models a etcd2 compliant access point for getting a property value: + * This models a etcd2 compliant access point for setting a property value: * <pre> * { - "action": "set", - "node": { - "createdIndex": 3, - "key": "/message", - "modifiedIndex": 3, - "value": "Hello etcd" - }, - "prevNode": { - "createdIndex": 2, - "key": "/message", - "value": "Hello world", - "modifiedIndex": 2 - } - } + * "action": "set", + * "node": { + * "createdIndex": 3, + * "key": "/message", + * "modifiedIndex": 3, + * "value": "Hello etcd" + * }, + * "prevNode": { + * "createdIndex": 2, + * "key": "/message", + * "value": "Hello world", + * "modifiedIndex": 2 + * } + * } * </pre> - * @param key - * @return + * + * @param key name of the key to show + * @param value configuration value for the given key + * @param ttl time to live + * @return written configuration value. */ @PUT @Path("/keys/{key}") public String writeConfig(@PathParam("key") String key, @javax.ws.rs.FormParam("value") String value, @FormParam("ttl") Integer ttl) { writeCounter.incrementAndGet(); - Configuration config = ConfigurationProvider.getConfiguration(); - if(key.startsWith("/")){ + final Configuration config = ConfigurationProvider.getConfiguration(); + if (key.startsWith("/")) { key = key.substring(1); } - Node prevNode = new Node(config, key, "prevNode"); + final Node prevNode = new Node(config, key, "prevNode"); // TODO implement write! value and ttl as input - Node node = new Node(config, key, "node"); - JsonObjectBuilder root = Json.createObjectBuilder().add("action", "set") + final Node node = new Node(config, key, "node"); + final JsonObjectBuilder root = Json.createObjectBuilder().add("action", "set") .add("node", node.createJsonObject()) .add("prevNode", prevNode.createJsonObject()); - StringWriter writer = new StringWriter(); - JsonWriter jwriter = Json.createWriter(writer); + final StringWriter writer = new StringWriter(); + final JsonWriter jwriter = Json.createWriter(writer); jwriter.writeObject(root.build()); return writer.toString(); } @@ -209,80 +220,60 @@ public class ConfigurationResource { public String deleteEtcdConfig(@PathParam("key") String key) { return deleteConfig(key); } - /** - * This models a etcd2 compliant access point for getting a property value: - * <pre> - * { - "action": "set", - "node": { - "createdIndex": 3, - "key": "/message", - "modifiedIndex": 3, - "value": "Hello etcd" - }, - "prevNode": { - "createdIndex": 2, - "key": "/message", - "value": "Hello world", - "modifiedIndex": 2 - } - } - * </pre> - * @param key - * @return - */ + @DELETE @Path("/keys/{key}") public String deleteConfig(@PathParam("key") String key) { deleteCounter.incrementAndGet(); - Configuration config = ConfigurationProvider.getConfiguration(); - if(key.startsWith("/")){ + final Configuration config = ConfigurationProvider.getConfiguration(); + if (key.startsWith("/")) { key = key.substring(1); } - Node prevNode = new Node(config, key, "prevNode"); + final Node prevNode = new Node(config, key, "prevNode"); // TODO implement write! value and ttl as input - Node node = new Node(config, key, "node"); - JsonObjectBuilder root = Json.createObjectBuilder().add("action", "delete") + final Node node = new Node(config, key, "node"); + final JsonObjectBuilder root = Json.createObjectBuilder().add("action", "delete") .add("node", node.createJsonObject()) .add("prevNode", prevNode.createJsonObject()); - StringWriter writer = new StringWriter(); - JsonWriter jwriter = Json.createWriter(writer); + final StringWriter writer = new StringWriter(); + final JsonWriter jwriter = Json.createWriter(writer); jwriter.writeObject(root.build()); return writer.toString(); } - public long getDeleteCounter(){ + public long getDeleteCounter() { return deleteCounter.get(); } - public long getReadCounter(){ + public long getReadCounter() { return readCounter.get(); } - public long getWriteCounter(){ + public long getWriteCounter() { return writeCounter.get(); } /** - * Internal representation of a configuration node as modelled by etc. + * Internal representation of a configuration node as modeled by etc. */ - private static final class Node{ + private static final class Node { private Integer createdIndex; private Integer modifiedIndex; - private String key; + private final String key; private String value; - private String nodeId; + private final String nodeId; private Integer ttl; private String expiration; - private JsonArray nodes; + private final JsonArray nodes; - Node(Configuration config, String key, String nodeId){ + Node(Configuration config, String key, String nodeId) { this(config, key, nodeId, null); } - Node(Configuration config, String key, String nodeId, JsonArray nodes){ + + Node(Configuration config, String key, String nodeId, JsonArray nodes) { this.key = key; this.nodeId = Objects.requireNonNull(nodeId); - if(key!=null) { + if (key != null) { value = config.get(key); createdIndex = config.getOrDefault("_" + key + ".createdIndex", Integer.class, null); modifiedIndex = config.getOrDefault("_" + key + ".modifiedIndex", Integer.class, null); @@ -292,29 +283,29 @@ public class ConfigurationResource { this.nodes = nodes; } - JsonObject createJsonObject(){ - JsonObjectBuilder nodeBuilder = Json.createObjectBuilder(); - if(key!=null) { + JsonObject createJsonObject() { + final JsonObjectBuilder nodeBuilder = Json.createObjectBuilder(); + if (key != null) { nodeBuilder.add("key", '/' + key); - }else{ + } else { nodeBuilder.add("dir", true); } - if(value!=null){ + if (value != null) { nodeBuilder.add("value", value); } - if(createdIndex!=null){ + if (createdIndex != null) { nodeBuilder.add("createdIndex", createdIndex.intValue()); } - if(modifiedIndex!=null){ + if (modifiedIndex != null) { nodeBuilder.add("modifiedIndex", modifiedIndex.intValue()); } - if(ttl!=null){ + if (ttl != null) { nodeBuilder.add("ttl", ttl.intValue()); } - if(expiration!=null){ + if (expiration != null) { nodeBuilder.add("expiration", value); } - if(nodes!=null){ + if (nodes != null) { nodeBuilder.add("nodes", nodes); } return nodeBuilder.build(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/5701c27e/modules/server/src/main/java/org/apache/tamaya/server/spi/ScopeManager.java ---------------------------------------------------------------------- diff --git a/modules/server/src/main/java/org/apache/tamaya/server/spi/ScopeManager.java b/modules/server/src/main/java/org/apache/tamaya/server/spi/ScopeManager.java index 3462e40..3d2757a 100644 --- a/modules/server/src/main/java/org/apache/tamaya/server/spi/ScopeManager.java +++ b/modules/server/src/main/java/org/apache/tamaya/server/spi/ScopeManager.java @@ -18,18 +18,18 @@ */ package org.apache.tamaya.server.spi; -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.ConfigOperator; -import org.apache.tamaya.spi.ServiceContextManager; - import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.ConfigOperator; +import org.apache.tamaya.spi.ServiceContextManager; + /** - * Singleton manager for scopes, used by the server component to filtering returned config. + * Singleton manager for scopes, used by the server component to filtering returned configurations. */ public final class ScopeManager { /** The logger used. */ @@ -41,11 +41,11 @@ public final class ScopeManager { * Singleton constructor. */ private static Map<String, ScopeProvider> initProviders(){ - Map<String, ScopeProvider> result = new ConcurrentHashMap<>(); - for(ScopeProvider prov: ServiceContextManager.getServiceContext().getServices(ScopeProvider.class)){ + final Map<String, ScopeProvider> result = new ConcurrentHashMap<>(); + for(final ScopeProvider prov: ServiceContextManager.getServiceContext().getServices(ScopeProvider.class)){ try{ result.put(prov.getScopeType(), prov); - } catch(Exception e){ + } catch(final Exception e){ LOG.log(Level.WARNING, "Error loading scopes from " + prov, e); } } @@ -58,19 +58,20 @@ public final class ScopeManager { private ScopeManager(){} /** - * Get the scope given its name. + * Get the scope given its id and provider. * - * @throws ConfigException if no such scope is defined * @param scopeId the scope name + * @param targetScope name of the targetScope * @return the scope matching + * @throws ConfigException if no such scope is defined */ - public static ConfigOperator getScope(String scopeId, String target) + public static ConfigOperator getScope(String scopeId, String targetScope) throws ConfigException { - ScopeProvider prov = scopeProviders.get(scopeId); + final ScopeProvider prov = scopeProviders.get(scopeId); if(prov==null){ throw new ConfigException("No such scope: " + scopeId); } - return prov.getScope(target); + return prov.getScope(targetScope); } /** http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/5701c27e/modules/server/src/main/java/org/apache/tamaya/server/spi/ScopeProvider.java ---------------------------------------------------------------------- diff --git a/modules/server/src/main/java/org/apache/tamaya/server/spi/ScopeProvider.java b/modules/server/src/main/java/org/apache/tamaya/server/spi/ScopeProvider.java index d74317c..5c247d8 100644 --- a/modules/server/src/main/java/org/apache/tamaya/server/spi/ScopeProvider.java +++ b/modules/server/src/main/java/org/apache/tamaya/server/spi/ScopeProvider.java @@ -21,7 +21,7 @@ package org.apache.tamaya.server.spi; import org.apache.tamaya.ConfigOperator; /** - * Simple registratable provider class to register scopes for the server extension. + * Simple registrable provider class to register scopes for the server extension. */ public interface ScopeProvider { @@ -33,6 +33,7 @@ public interface ScopeProvider { /** * Return the scope operator that implements the scope for the given scope id. + * @param scopeId target scope id. * @return the scope operator, never null. */ ConfigOperator getScope(String scopeId);