Repository: incubator-geode Updated Branches: refs/heads/feature/GEODE-1466 b2d85b4b0 -> d26b03cf8
Added some tests and TODOs for PropertiesResolver Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/47bef6a8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/47bef6a8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/47bef6a8 Branch: refs/heads/feature/GEODE-1466 Commit: 47bef6a85282fb346b86c71e692d7bbab7385665 Parents: b2d85b4 Author: Kirk Lund <kl...@apache.org> Authored: Tue Oct 11 13:27:05 2016 -0700 Committer: Kirk Lund <kl...@apache.org> Committed: Tue Oct 11 13:27:05 2016 -0700 ---------------------------------------------------------------------- .../geode/distributed/DistributedSystem.java | 4 +- .../geode/distributed/LocatorLauncher.java | 2 +- .../geode/internal/PropertiesResolver.java | 81 ++++++++ .../PropertiesResolverIntegrationTest.java | 187 +++++++++++++++++++ 4 files changed, 271 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/47bef6a8/geode-core/src/main/java/org/apache/geode/distributed/DistributedSystem.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/distributed/DistributedSystem.java b/geode-core/src/main/java/org/apache/geode/distributed/DistributedSystem.java index d0c7366..e5c420d 100644 --- a/geode-core/src/main/java/org/apache/geode/distributed/DistributedSystem.java +++ b/geode-core/src/main/java/org/apache/geode/distributed/DistributedSystem.java @@ -569,7 +569,7 @@ public abstract class DistributedSystem implements StatisticsFactory { * @see #getPropertiesFile() * @since Geode 1.0 */ - public static final String PROPERTIES_FILE_DEFAULT = DistributionConfig.GEMFIRE_PREFIX + "properties"; + public static final String PROPERTIES_FILE_DEFAULT = DistributionConfig.GEMFIRE_PREFIX + "properties"; // TODO: GEODE-1466 /** * Returns the current value of {@link #PROPERTIES_FILE_PROPERTY} system @@ -579,7 +579,7 @@ public abstract class DistributedSystem implements StatisticsFactory { * @see #PROPERTIES_FILE_DEFAULT * @since Geode 1.0 */ - public static String getPropertiesFile() { + public static String getPropertiesFile() { // TODO: GEODE-1466 return System.getProperty(PROPERTIES_FILE_PROPERTY, PROPERTIES_FILE_DEFAULT); } http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/47bef6a8/geode-core/src/main/java/org/apache/geode/distributed/LocatorLauncher.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/distributed/LocatorLauncher.java b/geode-core/src/main/java/org/apache/geode/distributed/LocatorLauncher.java index 5db332e..b18ce81 100644 --- a/geode-core/src/main/java/org/apache/geode/distributed/LocatorLauncher.java +++ b/geode-core/src/main/java/org/apache/geode/distributed/LocatorLauncher.java @@ -1732,7 +1732,7 @@ public final class LocatorLauncher extends AbstractLauncher<String> { if (StringUtils.isBlank(getMemberName()) && !isSet(System.getProperties(), DistributionConfig.GEMFIRE_PREFIX + NAME) && !isSet(getDistributedSystemProperties(), NAME) - && !isSet(loadGemFireProperties(DistributedSystem.getPropertyFileURL()), NAME)) + && !isSet(loadGemFireProperties(DistributedSystem.getPropertyFileURL()), NAME)) // TODO: GEODE-1466 { throw new IllegalStateException(LocalizedStrings.Launcher_Builder_MEMBER_NAME_VALIDATION_ERROR_MESSAGE .toLocalizedString("Locator")); http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/47bef6a8/geode-core/src/main/java/org/apache/geode/internal/PropertiesResolver.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/internal/PropertiesResolver.java b/geode-core/src/main/java/org/apache/geode/internal/PropertiesResolver.java new file mode 100644 index 0000000..ce5c3e5 --- /dev/null +++ b/geode-core/src/main/java/org/apache/geode/internal/PropertiesResolver.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.geode.internal; + +import java.io.File; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.apache.geode.distributed.DistributedSystem; +import org.apache.geode.internal.util.IOUtils; + +public class PropertiesResolver { + + static final String GEODE_PROPERTIES_FILE_PROPERTY = "geodePropertyFile"; + static final String GEMFIRE_PROPERTIES_FILE_PROPERTY = DistributedSystem.PROPERTIES_FILE_PROPERTY; + static final String DEFAULT_GEODE_PROPERTIES_FILE_NAME = "geode.properties"; + static final String DEFAULT_GEMFIRE_PROPERTIES_FILE_NAME = "gemfire.properties"; + + private static URI propertiesFileURL = findPropertiesFileLocation(); + + static URI findPropertiesFileLocation() { + List<URI> possibleUrls = new ArrayList<>(); + possibleUrls.add(getFileURI(System.getProperty(GEODE_PROPERTIES_FILE_PROPERTY))); + possibleUrls.add(getFileURI(System.getProperty(GEMFIRE_PROPERTIES_FILE_PROPERTY))); + possibleUrls.add(getFileURI(DEFAULT_GEODE_PROPERTIES_FILE_NAME)); + possibleUrls.add(getFileURI(DEFAULT_GEMFIRE_PROPERTIES_FILE_NAME)); + + return possibleUrls.stream().filter(s -> s != null).findFirst().orElse(null); + } + + static boolean hasProperty(final String key) { + return System.getProperties().containsKey(DistributedSystem.PROPERTIES_FILE_PROPERTY); + } + + private static URI getFileURI(String fileName) { + if (fileName == null) { + return null; + } + + File file = new File(fileName); // absolute path or current dir + + if (file.exists()) { + return IOUtils.tryGetCanonicalFileElseGetAbsoluteFile(file).toURI(); + } + + file = new File(System.getProperty("user.home"), fileName); // user home + + if (file.exists()) { + return IOUtils.tryGetCanonicalFileElseGetAbsoluteFile(file).toURI(); + } + + return getResourceOrNull(fileName); // resource + } + + private static URI getResourceOrNull(final String name) { + try { + URL url = ClassPathLoader.getLatest().getResource(DistributedSystem.class, name); // resource + return (url == null) ? null : url.toURI(); + } catch (URISyntaxException ignore) { + return null; + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/47bef6a8/geode-core/src/test/java/org/apache/geode/internal/PropertiesResolverIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/internal/PropertiesResolverIntegrationTest.java b/geode-core/src/test/java/org/apache/geode/internal/PropertiesResolverIntegrationTest.java new file mode 100644 index 0000000..c866e10 --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/internal/PropertiesResolverIntegrationTest.java @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.geode.internal; + +import static org.apache.geode.internal.PropertiesResolver.*; +import static org.assertj.core.api.Assertions.*; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.RestoreSystemProperties; +import org.junit.experimental.categories.Category; +import org.junit.rules.TemporaryFolder; + +import org.apache.geode.test.junit.categories.IntegrationTest; + +@Category(IntegrationTest.class) +public class PropertiesResolverIntegrationTest { + + private File geodeCustomProperties; + private File gemfireCustomProperties; + + List<File> files = new ArrayList<>(); + + + @Rule + public RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties(); + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Before + public void createFiles() throws Exception { + this.geodeCustomProperties = this.temporaryFolder.newFile("geodeCustom.properties"); + this.gemfireCustomProperties = this.temporaryFolder.newFile("gemfireCustom.properties"); + } + + @After + public void cleanup() throws Exception { + for (File file: files) { + FileUtils.forceDelete(file); + } + } + + @Test + public void canSpecifyGeodePropertiesFileAbsolutePath() throws Exception { + System.setProperty(GEODE_PROPERTIES_FILE_PROPERTY, this.geodeCustomProperties.getCanonicalPath()); + assertThat(findPropertiesFileLocation()).isEqualTo(this.geodeCustomProperties.getCanonicalFile().toURI()); + } + + @Test + public void canSpecifyGeodePropertiesFileInCurrentDir() throws Exception { + System.setProperty(GEODE_PROPERTIES_FILE_PROPERTY, geodeCustomFileInCurrentDir().getName()); + assertThat(findPropertiesFileLocation()).isEqualTo(geodeCustomFileInCurrentDir().getCanonicalFile().toURI()); + } + + @Test + public void canSpecifyGeodePropertiesFileInUserHomeDir() throws Exception { + System.setProperty(GEODE_PROPERTIES_FILE_PROPERTY, geodeCustomFileInHomeDir().getName()); + assertThat(findPropertiesFileLocation()).isEqualTo(geodeCustomFileInHomeDir().getCanonicalFile().toURI()); + } + + @Test + public void canSpecifyGemFirePropertiesFileAbsolutePath() throws Exception { + System.setProperty(GEMFIRE_PROPERTIES_FILE_PROPERTY, this.gemfireCustomProperties.getCanonicalPath()); + assertThat(findPropertiesFileLocation()).isEqualTo(this.gemfireCustomProperties.getCanonicalFile().toURI()); + } + + @Test + public void canSpecifyGemFirePropertiesFileInCurrentDir() throws Exception { + System.setProperty(GEMFIRE_PROPERTIES_FILE_PROPERTY, gemfireCustomFileInCurrentDir().getName()); + assertThat(findPropertiesFileLocation()).isEqualTo(gemfireCustomFileInCurrentDir().getCanonicalFile().toURI()); + } + + @Test + public void canSpecifyGemFirePropertiesFileInUserHomeDir() throws Exception { + System.setProperty(GEMFIRE_PROPERTIES_FILE_PROPERTY, gemfireCustomFileInHomeDir().getName()); + assertThat(findPropertiesFileLocation()).isEqualTo(gemfireCustomFileInHomeDir().getCanonicalFile().toURI()); + } + + @Test + public void findPrefersGeodePropertiesFileFirst() throws Exception { + System.setProperty(GEODE_PROPERTIES_FILE_PROPERTY, this.geodeCustomProperties.getCanonicalPath()); + System.setProperty(GEMFIRE_PROPERTIES_FILE_PROPERTY, this.gemfireCustomProperties.getCanonicalPath()); + geodeDefaultFileInCurrentDir(); + gemfireDefaultFileInCurrentDir(); + + assertThat(findPropertiesFileLocation()).isEqualTo(this.geodeCustomProperties.getCanonicalFile().toURI()); + } + + @Test + public void findPrefersGemFirePropertiesFileSecond() throws Exception { + System.setProperty(GEMFIRE_PROPERTIES_FILE_PROPERTY, this.gemfireCustomProperties.getCanonicalPath()); + geodeDefaultFileInCurrentDir(); + gemfireDefaultFileInCurrentDir(); + + assertThat(findPropertiesFileLocation()).isEqualTo(this.gemfireCustomProperties.getCanonicalFile().toURI()); + } + + @Test + public void findPrefersGeodeDefaultThird() throws Exception { + geodeDefaultFileInCurrentDir(); + gemfireDefaultFileInCurrentDir(); + + assertThat(findPropertiesFileLocation()).isEqualTo(geodeDefaultFileInCurrentDir().getCanonicalFile().toURI()); + } + + @Test + public void findPrefersGemFireDefaultFourth() throws Exception { + gemfireDefaultFileInCurrentDir(); + + assertThat(findPropertiesFileLocation()).isEqualTo(gemfireDefaultFileInCurrentDir().getCanonicalFile().toURI()); + } + + private File geodeDefaultFileInHomeDir() { + return createFileInHomeDir("geode.properties"); + } + + private File geodeDefaultFileInCurrentDir() { + return createFileInCurrentDir("geode.properties"); + } + private File gemfireDefaultFileInHomeDir() { + return createFileInHomeDir("gemfire.properties"); + } + + private File gemfireDefaultFileInCurrentDir() { + return createFileInCurrentDir("gemfire.properties"); + } + private File gemfireCustomFileInHomeDir() { + return createFileInHomeDir("gemfireCustomFileInHomeDir.properties"); + } + + private File gemfireCustomFileInCurrentDir() { + return createFileInCurrentDir("gemfireCustomFileInCurrentDir.properties"); + } + + private File geodeCustomFileInHomeDir() { + return createFileInHomeDir("geodeCustomFileInHomeDir.properties"); + } + private File geodeCustomFileInCurrentDir() { + return createFileInCurrentDir("geodeCustomFileInCurrentDir.properties"); + } + + private File createFileInCurrentDir(String name) { + return createFile(System.getProperty("user.dir"), name); + } + private File createFileInHomeDir(String name){ + return createFile(System.getProperty("user.home"), name); + } + + private File createFile(String parent, String child) { + File file = new File(parent, child); + + if (file.exists()) { + return file; + } + try { + assertThat(file.createNewFile()).isTrue(); + files.add(file); + file.deleteOnExit(); + } catch(IOException e) { + throw new AssertionError(e); + } + return file; + } +} \ No newline at end of file