This is an automated email from the ASF dual-hosted git repository. wlieurance pushed a commit to branch TAMAYA-394-sonar-errors in repository https://gitbox.apache.org/repos/asf/incubator-tamaya-extensions.git
commit 457956b33dfe161fcf53bdc81ca66d9d11cf6c23 Author: William Lieurance <[email protected]> AuthorDate: Thu Jun 13 22:32:50 2019 -0500 TAMAYA-394 Sonar bugs --- .../tamaya/examples/distributed/Display.java | 1 + .../examples/distributed/DisplayManager.java | 1 + .../examples/distributed/DisplayRegistration.java | 6 +- modules/etcd/nbactions.xml | 15 ++ .../java/org/apache/tamaya/etcd/EtcdAccessor.java | 182 ++++++++++----------- .../org/apache/tamaya/etcd/EtcdBackendConfig.java | 6 +- .../org/apache/tamaya/etcd/EtcdAccessorTest.java | 31 ++-- .../apache/tamaya/etcd/EtcdBackendConfigTest.java | 8 +- .../apache/tamaya/etcd/EtcdPropertySourceTest.java | 14 +- .../java/org/apache/tamaya/json/JSONFormat.java | 19 ++- .../org/apache/tamaya/json/JSONPropertySource.java | 66 +++++--- .../apache/tamaya/inject/spi/InjectionUtils.java | 8 +- .../tamaya/inject/internal/InjectionHelper.java | 2 +- .../tamaya/microprofile/MicroprofileConfig.java | 12 -- .../converter/BooleanAsIntegerConverterFix.java | 3 + .../microprofile/converter/ProviderConverter.java | 3 + .../main/java/org/apache/tamaya/osgi/Backups.java | 4 +- .../java/org/apache/tamaya/osgi/ConfigHistory.java | 4 +- 18 files changed, 212 insertions(+), 173 deletions(-) diff --git a/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/Display.java b/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/Display.java index 490def0..ba0e8db 100644 --- a/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/Display.java +++ b/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/Display.java @@ -109,6 +109,7 @@ public class Display extends Application{ Thread.sleep(100L); } catch (InterruptedException e) { e.printStackTrace(); + Thread.currentThread().interrupt(); } } titleField.getStyleClass().add("title"); diff --git a/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/DisplayManager.java b/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/DisplayManager.java index 51770bc..b5cf2a2 100644 --- a/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/DisplayManager.java +++ b/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/DisplayManager.java @@ -96,6 +96,7 @@ public class DisplayManager extends Application{ Thread.sleep(100L); } catch (InterruptedException e) { e.printStackTrace(); + Thread.currentThread().interrupt(); } } monitorField.getStyleClass().add("monitor"); diff --git a/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/DisplayRegistration.java b/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/DisplayRegistration.java index d7354ba..a09c8ba 100644 --- a/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/DisplayRegistration.java +++ b/examples/06-distributed/src/main/java/org/apache/tamaya/examples/distributed/DisplayRegistration.java @@ -56,7 +56,11 @@ public class DisplayRegistration implements Serializable{ adr = InetAddress.getLocalHost(); this.host = adr.getCanonicalHostName(); } catch(Exception e){ - this.host = adr.getHostName(); + if (adr == null){ + this.host = "localhost"; + }else{ + this.host = adr.getHostName(); + } } } diff --git a/modules/etcd/nbactions.xml b/modules/etcd/nbactions.xml new file mode 100644 index 0000000..7a42ed5 --- /dev/null +++ b/modules/etcd/nbactions.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<actions> + <action> + <actionName>test</actionName> + <packagings> + <packaging>*</packaging> + </packagings> + <goals> + <goal>test</goal> + </goals> + <properties> + <etcd.server.urls>http://localhost:2379</etcd.server.urls> + </properties> + </action> + </actions> diff --git a/modules/etcd/src/main/java/org/apache/tamaya/etcd/EtcdAccessor.java b/modules/etcd/src/main/java/org/apache/tamaya/etcd/EtcdAccessor.java index fcc5f49..c44a755 100644 --- a/modules/etcd/src/main/java/org/apache/tamaya/etcd/EtcdAccessor.java +++ b/modules/etcd/src/main/java/org/apache/tamaya/etcd/EtcdAccessor.java @@ -88,10 +88,6 @@ class EtcdAccessor { * The base server url. */ private final String serverURL; - /** - * The http client. - */ - private final CloseableHttpClient httpclient = HttpClients.createDefault(); /** * Creates a new instance with the basic access url. @@ -118,8 +114,7 @@ class EtcdAccessor { */ public String getVersion() { String version = "<ERROR>"; - try { - final CloseableHttpClient httpclient = HttpClients.createDefault(); + try (CloseableHttpClient httpclient = HttpClients.createDefault()) { final HttpGet httpGet = new HttpGet(serverURL + "/version"); httpGet.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectTimeout(timeout).build()); @@ -139,9 +134,9 @@ class EtcdAccessor { } /** - * Ask etcd for a single key, createValue pair. Hereby the response returned from - * etcd: - * + * Ask etcd for a single key, createValue pair. Hereby the response returned + * from etcd: + * * <pre> * { * "action": "current", @@ -153,9 +148,9 @@ class EtcdAccessor { * } * } * </pre> - * + * * is mapped to: - * + * * <pre> * key=value * _key.source=[etcd]http://127.0.0.1:4001 @@ -170,34 +165,35 @@ class EtcdAccessor { */ public Map<String, String> get(String key) { final Map<String, String> result = new HashMap<>(); - try { + try (CloseableHttpClient httpclient = HttpClients.createDefault()) { final HttpGet httpGet = new HttpGet(serverURL + "/v2/keys/" + key); httpGet.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(timeout).setConnectTimeout(connectTimeout).build()); - try (CloseableHttpResponse response = httpclient.execute(httpGet)) { + try (CloseableHttpResponse response = httpclient.execute(httpGet);) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); - final JsonReader reader = readerFactory - .createReader(new StringReader(EntityUtils.toString(entity))); - final JsonObject o = reader.readObject(); - final JsonObject node = o.getJsonObject("value"); - if (node.containsKey("key")) { - result.put(key, node.getString("key")); - result.put("_" + key + ".source", "[etcd]" + serverURL); - } - if (node.containsKey("createdIndex")) { - result.put("_" + key + ".createdIndex", String.valueOf(node.getInt("createdIndex"))); + try (JsonReader reader = readerFactory + .createReader(new StringReader(EntityUtils.toString(entity)))) { + final JsonObject o = reader.readObject(); + final JsonObject node = o.getJsonObject("node"); + if (node.containsKey("value")) { + result.put(key, node.getString("value")); + result.put("_" + key + ".source", "[etcd]" + serverURL); + } + if (node.containsKey("createdIndex")) { + result.put("_" + key + ".createdIndex", String.valueOf(node.getInt("createdIndex"))); + } + if (node.containsKey("modifiedIndex")) { + result.put("_" + key + ".modifiedIndex", String.valueOf(node.getInt("modifiedIndex"))); + } + if (node.containsKey("expiration")) { + result.put("_" + key + ".expiration", String.valueOf(node.getString("expiration"))); + } + if (node.containsKey("ttl")) { + result.put("_" + key + ".ttl", String.valueOf(node.getInt("ttl"))); + } + EntityUtils.consume(entity); } - if (node.containsKey("modifiedIndex")) { - result.put("_" + key + ".modifiedIndex", String.valueOf(node.getInt("modifiedIndex"))); - } - if (node.containsKey("expiration")) { - result.put("_" + key + ".expiration", String.valueOf(node.getString("expiration"))); - } - if (node.containsKey("ttl")) { - result.put("_" + key + ".ttl", String.valueOf(node.getInt("ttl"))); - } - EntityUtils.consume(entity); } else { result.put("_" + key + ".NOT_FOUND.target", "[etcd]" + serverURL); } @@ -212,7 +208,7 @@ class EtcdAccessor { /** * Creates/updates an entry in etcd without any ttl setCurrent. * - * @param key the property key, not null + * @param key the property key, not null * @param value the createValue to be setCurrent * @return the result mapProperties as described above. * @see #set(String, String, Integer) @@ -223,7 +219,7 @@ class EtcdAccessor { /** * Creates/updates an entry in etcd. The response as follows: - * + * * <pre> * { * "action": "setCurrent", @@ -241,9 +237,9 @@ class EtcdAccessor { * } * } * </pre> - * + * * is mapped to: - * + * * <pre> * key=createValue * _key.source=[etcd]http://127.0.0.1:4001 @@ -258,14 +254,14 @@ class EtcdAccessor { * _key.prevNode.expiration=... * </pre> * - * @param key the property key, not null - * @param value the createValue to be setCurrent + * @param key the property key, not null + * @param value the createValue to be setCurrent * @param ttlSeconds the ttl in seconds (optional) * @return the result mapProperties as described above. */ public Map<String, String> set(String key, String value, Integer ttlSeconds) { final Map<String, String> result = new HashMap<>(); - try { + try (CloseableHttpClient httpclient = HttpClients.createDefault()) { final HttpPut put = new HttpPut(serverURL + "/v2/keys/" + key); put.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(timeout).setConnectTimeout(connectTimeout).build()); @@ -279,26 +275,27 @@ class EtcdAccessor { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED || response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); - final JsonReader reader = readerFactory - .createReader(new StringReader(EntityUtils.toString(entity))); - final JsonObject o = reader.readObject(); - final JsonObject node = o.getJsonObject("value"); - if (node.containsKey("createdIndex")) { - result.put("_" + key + ".createdIndex", String.valueOf(node.getInt("createdIndex"))); - } - if (node.containsKey("modifiedIndex")) { - result.put("_" + key + ".modifiedIndex", String.valueOf(node.getInt("modifiedIndex"))); - } - if (node.containsKey("expiration")) { - result.put("_" + key + ".expiration", String.valueOf(node.getString("expiration"))); - } - if (node.containsKey("ttl")) { - result.put("_" + key + ".ttl", String.valueOf(node.getInt("ttl"))); + try (JsonReader reader = readerFactory + .createReader(new StringReader(EntityUtils.toString(entity)))) { + final JsonObject o = reader.readObject(); + final JsonObject node = o.getJsonObject("node"); + if (node.containsKey("createdIndex")) { + result.put("_" + key + ".createdIndex", String.valueOf(node.getInt("createdIndex"))); + } + if (node.containsKey("modifiedIndex")) { + result.put("_" + key + ".modifiedIndex", String.valueOf(node.getInt("modifiedIndex"))); + } + if (node.containsKey("expiration")) { + result.put("_" + key + ".expiration", String.valueOf(node.getString("expiration"))); + } + if (node.containsKey("ttl")) { + result.put("_" + key + ".ttl", String.valueOf(node.getInt("ttl"))); + } + result.put(key, node.getString("value")); + result.put("_" + key + ".source", "[etcd]" + serverURL); + parsePrevNode(key, result, node); + EntityUtils.consume(entity); } - result.put(key, node.getString("key")); - result.put("_" + key + ".source", "[etcd]" + serverURL); - parsePrevNode(key, result, node); - EntityUtils.consume(entity); } } } catch (final Exception e) { @@ -310,7 +307,7 @@ class EtcdAccessor { /** * Deletes a given key. The response is as follows: - * + * * <pre> * _key.source=[etcd]http://127.0.0.1:4001 * _key.createdIndex=12 @@ -330,31 +327,32 @@ class EtcdAccessor { */ public Map<String, String> delete(String key) { final Map<String, String> result = new HashMap<>(); - try { + try (CloseableHttpClient httpclient = HttpClients.createDefault()) { final HttpDelete delete = new HttpDelete(serverURL + "/v2/keys/" + key); delete.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(timeout).setConnectTimeout(connectTimeout).build()); try (CloseableHttpResponse response = httpclient.execute(delete)) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); - final JsonReader reader = readerFactory - .createReader(new StringReader(EntityUtils.toString(entity))); - final JsonObject o = reader.readObject(); - final JsonObject node = o.getJsonObject("value"); - if (node.containsKey("createdIndex")) { - result.put("_" + key + ".createdIndex", String.valueOf(node.getInt("createdIndex"))); - } - if (node.containsKey("modifiedIndex")) { - result.put("_" + key + ".modifiedIndex", String.valueOf(node.getInt("modifiedIndex"))); + try (JsonReader reader = readerFactory + .createReader(new StringReader(EntityUtils.toString(entity)))) { + final JsonObject o = reader.readObject(); + final JsonObject node = o.getJsonObject("node"); + if (node.containsKey("createdIndex")) { + result.put("_" + key + ".createdIndex", String.valueOf(node.getInt("createdIndex"))); + } + if (node.containsKey("modifiedIndex")) { + result.put("_" + key + ".modifiedIndex", String.valueOf(node.getInt("modifiedIndex"))); + } + if (node.containsKey("expiration")) { + result.put("_" + key + ".expiration", String.valueOf(node.getString("expiration"))); + } + if (node.containsKey("ttl")) { + result.put("_" + key + ".ttl", String.valueOf(node.getInt("ttl"))); + } + parsePrevNode(key, result, o); + EntityUtils.consume(entity); } - if (node.containsKey("expiration")) { - result.put("_" + key + ".expiration", String.valueOf(node.getString("expiration"))); - } - if (node.containsKey("ttl")) { - result.put("_" + key + ".ttl", String.valueOf(node.getInt("ttl"))); - } - parsePrevNode(key, result, o); - EntityUtils.consume(entity); } } } catch (final Exception e) { @@ -399,7 +397,7 @@ class EtcdAccessor { /** * Access all properties. The response of: - * + * * <pre> * { * "action": "current", @@ -423,9 +421,9 @@ class EtcdAccessor { * } * } * </pre> - * + * * is mapped to a regular Tamaya properties mapProperties as follows: - * + * * <pre> * key1=myvalue * _key1.source=[etcd]http://127.0.0.1:4001 @@ -445,12 +443,13 @@ class EtcdAccessor { * </pre> * * @param directory remote directory to query. - * @param recursive allows to setCurrent if querying is performed recursively + * @param recursive allows to setCurrent if querying is performed + * recursively * @return all properties read from the remote server. */ public Map<String, String> getProperties(String directory, boolean recursive) { final Map<String, String> result = new HashMap<>(); - try { + try (CloseableHttpClient httpclient = HttpClients.createDefault()) { final HttpGet get = new HttpGet(serverURL + "/v2/keys/" + directory + "?recursive=" + recursive); get.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(timeout).setConnectTimeout(connectTimeout).build()); @@ -458,13 +457,14 @@ class EtcdAccessor { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); - final JsonReader reader = readerFactory.createReader(new StringReader(EntityUtils.toString(entity))); - final JsonObject o = reader.readObject(); - final JsonObject node = o.getJsonObject("value"); - if (node != null) { - addNodes(result, node); + try (JsonReader reader = readerFactory.createReader(new StringReader(EntityUtils.toString(entity)))) { + final JsonObject o = reader.readObject(); + final JsonObject node = o.getJsonObject("node"); + if (node != null){ + addNodes(result, node); + } + EntityUtils.consume(entity); } - EntityUtils.consume(entity); } } } catch (final Exception e) { @@ -479,7 +479,7 @@ class EtcdAccessor { * Recursively read out all key/values from this etcd JSON array. * * @param result mapProperties with key, values and metadata. - * @param node the getValue to parse. + * @param node the getValue to parse. */ private void addNodes(Map<String, String> result, JsonObject node) { if (!node.containsKey("dir") || "false".equals(node.get("dir").toString())) { @@ -499,7 +499,7 @@ class EtcdAccessor { } result.put("_" + key + ".source", "[etcd]" + serverURL); } else { - final JsonArray nodes = node.getJsonArray("values"); + final JsonArray nodes = node.getJsonArray("nodes"); if (nodes != null) { for (int i = 0; i < nodes.size(); i++) { addNodes(result, nodes.getJsonObject(i)); diff --git a/modules/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackendConfig.java b/modules/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackendConfig.java index 696f187..25a633a 100644 --- a/modules/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackendConfig.java +++ b/modules/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackendConfig.java @@ -72,9 +72,9 @@ final class EtcdBackendConfig { } /** - * Evaluate the etcd target servers fomr system/environment property {@code tamaya.etcd.server}. + * Evaluate the etcd target servers for system/environment property {@code tamaya.etcd.server}. * - * @return the servers configured, or {@code http://127.0.0.1:4001} (default). + * @return the servers configured, or {@code http://127.0.0.1:2379} (default). */ public static List<String> getServers() { String serverURLs = System.getProperty(TAMAYA_ETCD_SERVER_URLS); @@ -82,7 +82,7 @@ final class EtcdBackendConfig { serverURLs = System.getenv(TAMAYA_ETCD_SERVER_URLS); } if (serverURLs == null) { - serverURLs = "http://127.0.0.1:4001"; + serverURLs = "http://127.0.0.1:2379"; } List<String> servers = new ArrayList<>(); for (String url : serverURLs.split("\\,")) { diff --git a/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdAccessorTest.java b/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdAccessorTest.java index 0b5c1ae..43cca83 100644 --- a/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdAccessorTest.java +++ b/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdAccessorTest.java @@ -28,7 +28,8 @@ import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for the etcd backend integration. You must have setCurrent a system property so, theses tests are executed, e.g. + * Tests for the etcd backend integration. You must have setCurrent a system + * property so, theses tests are executed, e.g. * {@code -Detcd.url=http://127.0.0.1:4001}. */ public class EtcdAccessorTest { @@ -38,12 +39,16 @@ public class EtcdAccessorTest { @BeforeClass public static void setup() throws MalformedURLException { - accessor = new EtcdAccessor("http://192.168.99.105:4001"); - if (!accessor.getVersion().contains("etcd")) { + String servers = System.getProperty("etcd.server.urls"); + if (servers != null) { + accessor = new EtcdAccessor(servers); + if (accessor.getVersion().contains("etcd")) { + execute = true; + } + } + if (!execute) { System.out.println("Disabling etcd tests, etcd not accessible at: " + System.getProperty("etcd.server.urls")); System.out.println("Configure etcd with -Detcd.server.urls=http://<IP>:<PORT>"); - } else { - execute = true; } } @@ -52,7 +57,7 @@ public class EtcdAccessorTest { if (!execute) { return; } - assertThat(accessor.getVersion()).isEqualTo("etcd 0.4.9"); + assertThat(accessor.getVersion()).contains("etcd"); } @Test @@ -60,7 +65,7 @@ public class EtcdAccessorTest { if (!execute) { return; } - Map<String,String> result = accessor.get("test1"); + Map<String, String> result = accessor.get("test1"); assertThat(result).isNotNull(); } @@ -70,7 +75,7 @@ public class EtcdAccessorTest { return; } String value = UUID.randomUUID().toString(); - Map<String,String> result = accessor.set("testSetNormal", value); + Map<String, String> result = accessor.set("testSetNormal", value); assertThat(result.get("_testSetNormal.ttl")).isNull(); assertThat(value).isEqualTo(accessor.get("testSetNormal").get("testSetNormal")); } @@ -81,7 +86,7 @@ public class EtcdAccessorTest { return; } String value = UUID.randomUUID().toString(); - Map<String,String> result = accessor.set("testSetNormal2", value, null); + Map<String, String> result = accessor.set("testSetNormal2", value, null); assertThat(result.get("_testSetNormal2.ttl")).isNull(); assertThat(value).isEqualTo(accessor.get("testSetNormal2").get("testSetNormal2")); } @@ -92,7 +97,7 @@ public class EtcdAccessorTest { return; } String value = UUID.randomUUID().toString(); - Map<String,String> result = accessor.set("testSetWithTTL", value, 1); + Map<String, String> result = accessor.set("testSetWithTTL", value, 1); assertThat(result.get("_testSetWithTTL.ttl")).isNotNull(); assertThat(value).isEqualTo(accessor.get("testSetWithTTL").get("testSetWithTTL")); Thread.sleep(2000L); @@ -106,11 +111,11 @@ public class EtcdAccessorTest { return; } String value = UUID.randomUUID().toString(); - Map<String,String> result = accessor.set("testDelete", value, null); + Map<String, String> result = accessor.set("testDelete", value, null); assertThat(value).isEqualTo(accessor.get("testDelete").get("testDelete")); assertThat(result.get("_testDelete.createdIndex")).isNotNull(); result = accessor.delete("testDelete"); - assertThat(value).isEqualTo(result.get("_testDelete.prevNode.createValue")); + assertThat(value).isEqualTo(result.get("_testDelete.prevNode.value")); assertThat(accessor.get("testDelete").get("testDelete")).isNull(); } @@ -121,7 +126,7 @@ public class EtcdAccessorTest { } String value = UUID.randomUUID().toString(); accessor.set("testGetProperties1", value); - Map<String,String> result = accessor.getProperties(""); + Map<String, String> result = accessor.getProperties(""); assertThat(result).isNotNull(); assertThat(value).isEqualTo(result.get("testGetProperties1")); assertThat(result.get("_testGetProperties1.createdIndex")).isNotNull(); diff --git a/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdBackendConfigTest.java b/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdBackendConfigTest.java index f28aee6..5855cdf 100644 --- a/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdBackendConfigTest.java +++ b/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdBackendConfigTest.java @@ -48,9 +48,13 @@ public class EtcdBackendConfigTest { @Test public void testEtcdServerProperty() throws Exception { - final String server = "http://localhost:4001"; + final String server = System.getProperty("etcd.server.urls"); + if (server == null){ + return; + } try { - assertThat(EtcdBackendConfig.getServers()).contains("http://127.0.0.1:4001"); + System.clearProperty("tamaya.etcd.server"); + assertThat(EtcdBackendConfig.getServers()).contains("http://127.0.0.1:2379"); //the default System.setProperty("tamaya.etcd.server", server); assertThat(EtcdBackendConfig.getServers()).contains(server); } finally { diff --git a/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdPropertySourceTest.java b/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdPropertySourceTest.java index 1ec1ce7..d16fa49 100644 --- a/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdPropertySourceTest.java +++ b/modules/etcd/src/test/java/org/apache/tamaya/etcd/EtcdPropertySourceTest.java @@ -19,7 +19,6 @@ package org.apache.tamaya.etcd; import org.apache.tamaya.spi.PropertyValue; -import org.junit.BeforeClass; import org.junit.Test; import java.util.Map; @@ -34,11 +33,6 @@ public class EtcdPropertySourceTest { private final EtcdPropertySource propertySource = new EtcdPropertySource(); - @BeforeClass - public static void setup(){ - System.setProperty("etcd.server.urls", "http://8.8.8.8:4001,http://192.168.99.105:4001"); - } - @Test public void testGetOrdinal() throws Exception { assertThat(1000).isEqualTo(propertySource.getOrdinal()); @@ -75,13 +69,13 @@ public class EtcdPropertySourceTest { @Test public void testPropertySourceConstructorParams() throws Exception { - EtcdPropertySource propertySource = new EtcdPropertySource("http://8.8.8.8:4001", "http://192.168.99.105:4001"); - assertThat(propertySource.getProperties()).isNotNull(); + EtcdPropertySource localpropertySource = new EtcdPropertySource("http://1.1.1.1:2379", "http://2.2.2.2:2379"); + assertThat(localpropertySource.getProperties()).isNotNull(); } @Test public void testPropertySourceConstructorList() throws Exception { - EtcdPropertySource propertySource = new EtcdPropertySource(asList("http://8.8.8.8:4001", "http://192.168.99.105:4001")); - assertThat(propertySource.getProperties()).isNotNull(); + EtcdPropertySource localpropertySource = new EtcdPropertySource(asList("http://3.3.3.3:2379", "http://4.4.4.4:2379")); + assertThat(localpropertySource.getProperties()).isNotNull(); } } diff --git a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONFormat.java b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONFormat.java index a145ad8..dd0c61e 100644 --- a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONFormat.java +++ b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONFormat.java @@ -41,15 +41,20 @@ import javax.json.JsonReaderFactory; * @see <a href="http://www.json.org">JSON format specification</a> */ public class JSONFormat implements ConfigurationFormat { - /** Property that makes Johnzon accept comments. */ + + /** + * Property that makes Johnzon accept comments. + */ public static final String JOHNZON_SUPPORTS_COMMENTS_PROP = "org.apache.johnzon.supports-comments"; - /** The reader factory used. */ + /** + * The reader factory used. + */ private final JsonReaderFactory readerFactory; /** * Constructor, initializing the JSON reader factory. */ - public JSONFormat(){ + public JSONFormat() { Map<String, Object> config = new HashMap<>(); config.put(JOHNZON_SUPPORTS_COMMENTS_PROP, true); this.readerFactory = Json.createReaderFactory(config); @@ -66,14 +71,12 @@ public class JSONFormat implements ConfigurationFormat { } @Override - public ConfigurationData readConfiguration(String resource, InputStream inputStream) - throws IOException{ - try{ - final JsonReader reader = this.readerFactory.createReader(inputStream, Charset.forName("UTF-8")); + public ConfigurationData readConfiguration(String resource, InputStream inputStream) throws IOException { + try (JsonReader reader = this.readerFactory.createReader(inputStream, Charset.forName("UTF-8"))) { JsonObject root = reader.readObject(); JSONDataBuilder dataBuilder = new JSONDataBuilder(resource, root); return new ConfigurationData(resource, this, dataBuilder.build()); - } catch(Exception e) { + } catch (Exception e) { throw new IOException("Failed to read data from " + resource, e); } } diff --git a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java index 39f2b95..a7fbd75 100644 --- a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java +++ b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java @@ -38,46 +38,62 @@ import javax.json.JsonReaderFactory; import javax.json.JsonStructure; import static java.lang.String.format; +import javax.json.JsonReader; /** * Property source based on a JSON file. */ public class JSONPropertySource implements PropertySource { - /** Constant for enabling comments in Johnzon. */ + + /** + * Constant for enabling comments in Johnzon. + */ public static final String JOHNZON_SUPPORTS_COMMENTS_PROP = "org.apache.johnzon.supports-comments"; - /** The underlying resource. */ + /** + * The underlying resource. + */ private final URL urlResource; - /** The values read. */ + /** + * The values read. + */ private final Map<String, PropertyValue> values; - /** The evaluated ordinal. */ + /** + * The evaluated ordinal. + */ private int ordinal; - /** The JSON reader factory used. */ + /** + * The JSON reader factory used. + */ private JsonReaderFactory readerFactory = initReaderFactory(); - /** Initializes the factory to be used for creating readers. */ + /** + * Initializes the factory to be used for creating readers. + */ private JsonReaderFactory initReaderFactory() { Map<String, Object> config = new HashMap<>(); config.put(JOHNZON_SUPPORTS_COMMENTS_PROP, true); - return Json.createReaderFactory(config); + return Json.createReaderFactory(config); } /** * Constructor, hereby using 0 as the default ordinal. + * * @param resource the resource modelled as URL, not null. * @throws IOException if reading the resource fails. */ - public JSONPropertySource(URL resource)throws IOException { + public JSONPropertySource(URL resource) throws IOException { this(resource, 0); } /** * Constructor. + * * @param resource the resource modelled as URL, not null. * @param defaultOrdinal the defaultOrdinal to be used. * @throws IOException if reading the resource fails. */ - public JSONPropertySource(URL resource, int defaultOrdinal)throws IOException { + public JSONPropertySource(URL resource, int defaultOrdinal) throws IOException { urlResource = Objects.requireNonNull(resource); this.ordinal = defaultOrdinal; // may be overriden by read... this.values = readConfig(urlResource); @@ -86,13 +102,12 @@ public class JSONPropertySource implements PropertySource { } } - public int getOrdinal() { PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL); - if(configuredOrdinal!=null){ - try{ + if (configuredOrdinal != null) { + try { return Integer.parseInt(configuredOrdinal.getValue()); - } catch(Exception e){ + } catch (Exception e) { Logger.getLogger(getClass().getName()).log(Level.WARNING, "Configured Ordinal is not an int number: " + configuredOrdinal, e); } @@ -118,25 +133,28 @@ public class JSONPropertySource implements PropertySource { /** * Reads the configuration. + * * @param urlResource soure of the configuration. * @return the configuration read from the given resource URL. * @throws ConfigException if resource URL cannot be read. * @throws IOException if reading the urlResource fails. */ - protected Map<String, PropertyValue> readConfig(URL urlResource) throws IOException{ + protected Map<String, PropertyValue> readConfig(URL urlResource) throws IOException { try (InputStream is = urlResource.openStream()) { - JsonStructure root = this.readerFactory.createReader(is, StandardCharsets.UTF_8).read(); - - JSONDataBuilder visitor = new JSONDataBuilder(urlResource.toString(), root); - Map<String, String> values = visitor.build().toMap(); - Map<String, PropertyValue> result = new HashMap<>(); - for(Map.Entry<String,String> en:values.entrySet()){ - result.put(en.getKey(), PropertyValue.createValue(en.getKey(), en.getValue()).setMeta("source", getName())); + try (JsonReader reader = this.readerFactory.createReader(is, StandardCharsets.UTF_8)) { + JsonStructure root = reader.read(); + + JSONDataBuilder visitor = new JSONDataBuilder(urlResource.toString(), root); + Map<String, String> values = visitor.build().toMap(); + Map<String, PropertyValue> result = new HashMap<>(); + for (Map.Entry<String, String> en : values.entrySet()) { + result.put(en.getKey(), PropertyValue.createValue(en.getKey(), en.getValue()).setMeta("source", getName())); + } + return result; } - return result; - }catch(IOException ioe){ + } catch (IOException ioe) { throw ioe; - }catch (Exception t) { + } catch (Exception t) { throw new IOException(format("Failed to read properties from %s", urlResource.toExternalForm()), t); } } diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/InjectionUtils.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/InjectionUtils.java index b0ef112..2068d34 100644 --- a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/InjectionUtils.java +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/InjectionUtils.java @@ -102,14 +102,14 @@ public final class InjectionUtils { try { keyResolver = sectionAnnot.keyResolver().getDeclaredConstructor().newInstance(); } catch (Exception e) { - LOG.log(Level.SEVERE, "Cannot create KeyResolver: " + sectionAnnot.keyResolver().getName(), e); + LOG.log(Level.SEVERE, "Cannot create KeyResolver from section resolver: " + sectionAnnot.keyResolver().getName(), e); } } if(configAnnot!=null && !configAnnot.keyResolver().equals(KeyResolver.class)){ try { keyResolver = configAnnot.keyResolver().getDeclaredConstructor().newInstance(); } catch (Exception e) { - LOG.log(Level.SEVERE, "Cannot create KeyResolver: " + sectionAnnot.keyResolver().getName(), e); + LOG.log(Level.SEVERE, "Cannot create KeyResolver from config resolver: " + configAnnot.keyResolver().getName(), e); } } List<String> alternateKeys = new ArrayList<>(); @@ -134,14 +134,14 @@ public final class InjectionUtils { try { keyResolver = sectionAnnot.keyResolver().getDeclaredConstructor().newInstance(); } catch (Exception e) { - LOG.log(Level.SEVERE, "Cannot create KeyResolver: " + sectionAnnot.keyResolver().getName(), e); + LOG.log(Level.SEVERE, "Cannot create KeyResolver from section resolver: " + sectionAnnot.keyResolver().getName(), e); } } if(configAnnot!=null && !configAnnot.keyResolver().equals(KeyResolver.class)){ try { keyResolver = configAnnot.keyResolver().getDeclaredConstructor().newInstance(); } catch (Exception e) { - LOG.log(Level.SEVERE, "Cannot create KeyResolver: " + sectionAnnot.keyResolver().getName(), e); + LOG.log(Level.SEVERE, "Cannot create KeyResolver from config resolver: " + configAnnot.keyResolver().getName(), e); } } List<String> alternateKeys = new ArrayList<>(); diff --git a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java index a057708..3f1d600 100644 --- a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java +++ b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java @@ -156,7 +156,7 @@ final class InjectionHelper { Class<? extends PropertyConverter<T>> converterType; if (converterAnnot != null) { converterType = (Class<? extends PropertyConverter<T>>) converterAnnot.value(); - if (!converterType.getName().equals(WithPropertyConverter.class.getName())) { + if (!converterType.isAssignableFrom(WithPropertyConverter.class)) { try { // TODO cache here... ConversionContext ctx = new ConversionContext.Builder(key,targetType) diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfig.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfig.java index 04f6132..92f8666 100644 --- a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfig.java +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfig.java @@ -24,9 +24,6 @@ import org.apache.tamaya.spi.PropertySource; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.spi.ConfigSource; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.*; @@ -84,13 +81,4 @@ public class MicroprofileConfig implements Config, Serializable { "delegate=" + delegate + '}'; } - - private void writeObject(ObjectOutputStream out) throws IOException { - out.writeObject(this.delegate.getSnapshot()); - } - - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - this.delegate = (Configuration) in.readObject(); - } - } diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/BooleanAsIntegerConverterFix.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/BooleanAsIntegerConverterFix.java index 8f292bb..f7ef8b7 100644 --- a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/BooleanAsIntegerConverterFix.java +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/BooleanAsIntegerConverterFix.java @@ -48,6 +48,9 @@ public class BooleanAsIntegerConverterFix implements PropertyConverter<Boolean> @Override public boolean equals(Object o){ + if (o == null){ + return false; + } return getClass().equals(o.getClass()); } diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/ProviderConverter.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/ProviderConverter.java index b6d1c05..f779b96 100644 --- a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/ProviderConverter.java +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/ProviderConverter.java @@ -59,6 +59,9 @@ public class ProviderConverter implements PropertyConverter<Provider> { @Override public boolean equals(Object o) { + if (o == null){ + return false; + } return getClass().equals(o.getClass()); } diff --git a/modules/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java b/modules/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java index 57e94a0..472bb7e 100644 --- a/modules/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java +++ b/modules/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java @@ -36,7 +36,7 @@ final class Backups { private static final Logger LOG = Logger.getLogger(Backups.class.getName()); public static final String TAMAYA_BACKUP = "tamaya.backup"; - private static Map<String, Hashtable<String, ?>> initialConfigState = new ConcurrentHashMap<>(); + private static ConcurrentHashMap<String, Hashtable<String, ?>> initialConfigState = new ConcurrentHashMap<>(); private Backups() { } @@ -158,7 +158,7 @@ final class Backups { if (serialized != null) { ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(serialized)); ObjectInputStream ois = new ObjectInputStream(bis); - initialConfigState = (Map<String, Hashtable<String, ?>>) ois.readObject(); + initialConfigState = (ConcurrentHashMap<String, Hashtable<String, ?>>) ois.readObject(); ois.close(); } } catch (Exception e) { diff --git a/modules/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigHistory.java b/modules/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigHistory.java index d8a4fa0..c0dd459 100644 --- a/modules/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigHistory.java +++ b/modules/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigHistory.java @@ -52,7 +52,7 @@ public final class ConfigHistory implements Serializable { /** * The overall history. */ - private static List<ConfigHistory> history = new LinkedList<>(); + private static LinkedList<ConfigHistory> history = new LinkedList<>(); /** * The entry timestamp. @@ -285,7 +285,7 @@ public final class ConfigHistory implements Serializable { if (serialized != null) { ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(serialized)); ObjectInputStream ois = new ObjectInputStream(bis); - ConfigHistory.history = (List<ConfigHistory>) ois.readObject(); + ConfigHistory.history = (LinkedList<ConfigHistory>) ois.readObject(); ois.close(); } } catch (Exception e) {
