-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: custom server file location #3401
Merged
nobodyiam
merged 7 commits into
apolloconfig:master
from
Anilople:feature/custom-server-file-location
Nov 29, 2020
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dc246c2
feat(apollo-core): user can custom the path of config file 'server.pr…
Anilople 7f382d8
test(apollo-core): ConfigUtils
Anilople c8f8005
test(apollo-core): PathUtils
Anilople 7b9be71
docs(apollo-core): Apache-2.0 License
Anilople f5a24a5
feat(apollo-core): use simpler way to custom the path of 'server.prop…
Anilople 45abe5f
test(apollo-core): getServerPropertiesPath
Anilople 7f25ef3
refactor(apollo-core): delete
Anilople File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...o-core/src/main/java/com/ctrip/framework/foundation/internals/constant/PathConstants.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,68 @@ | ||
/* | ||
* 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 com.ctrip.framework.foundation.internals.constant; | ||
|
||
import com.ctrip.framework.foundation.internals.util.PathUtils; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* @author wang | ||
*/ | ||
public interface PathConstants { | ||
|
||
interface ResolvedPaths { | ||
|
||
/** | ||
* Default value {@code /opt/settings/server.properties} on linux or {@code | ||
* C:/opt/settings/server.properties} on windows | ||
*/ | ||
Path SERVER_PROPERTIES = PathUtils | ||
.resolve(SystemPropertyKeys.APOLLO_OPT, EnvironmentKeys.APOLLO_OPT, | ||
DefaultValuesOnLinux.APOLLO_OPT, DefaultValuesOnWindows.APOLLO_OPT) | ||
.resolve("settings") | ||
.resolve("server.properties"); | ||
} | ||
|
||
interface DefaultValuesOnLinux { | ||
|
||
String APOLLO_OPT = "/opt"; | ||
} | ||
|
||
interface DefaultValuesOnWindows { | ||
|
||
String APOLLO_OPT = "C:/opt"; | ||
} | ||
|
||
/** | ||
* key in {@link System#getProperty(String)} | ||
*/ | ||
interface SystemPropertyKeys { | ||
|
||
String APOLLO_OPT = "APOLLO_OPT"; | ||
} | ||
|
||
/** | ||
* key in {@link System#getenv(String)} | ||
*/ | ||
interface EnvironmentKeys { | ||
|
||
String APOLLO_OPT = "apollo.opt"; | ||
} | ||
|
||
} |
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
55 changes: 55 additions & 0 deletions
55
apollo-core/src/main/java/com/ctrip/framework/foundation/internals/util/ConfigUtils.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,55 @@ | ||
/* | ||
* 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 com.ctrip.framework.foundation.internals.util; | ||
|
||
import com.google.common.base.Strings; | ||
|
||
/** | ||
* resolve the config. | ||
* | ||
* @author wxq | ||
*/ | ||
public class ConfigUtils { | ||
|
||
/** | ||
* Gets the value indicated by the specified key. | ||
* | ||
* @param keyInSystemProperty the name of the system property | ||
* @param keyInEnvironment the name of the environment variable | ||
* @return the string value of the variable, or <code>null</code> if the variable is not defined | ||
* in the system property or the system environment | ||
*/ | ||
public static String getValue(String keyInSystemProperty, String keyInEnvironment) { | ||
// Get from System Property | ||
final String valueInSystemProperty = System.getProperty(keyInSystemProperty); | ||
if (!Strings.isNullOrEmpty(valueInSystemProperty)) { | ||
// return if value exists | ||
return valueInSystemProperty; | ||
} | ||
|
||
// Get from OS environment variable | ||
final String valueInEnvironment = System.getenv(keyInEnvironment); | ||
if (!Strings.isNullOrEmpty(valueInEnvironment)) { | ||
// return if value exists | ||
return valueInEnvironment; | ||
} | ||
|
||
return null; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
apollo-core/src/main/java/com/ctrip/framework/foundation/internals/util/PathUtils.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,43 @@ | ||
/* | ||
* 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 com.ctrip.framework.foundation.internals.util; | ||
|
||
import com.ctrip.framework.foundation.internals.Utils; | ||
import com.google.common.base.Strings; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* resolve path. | ||
* | ||
* @author wxq | ||
*/ | ||
public class PathUtils { | ||
|
||
public static Path resolve(String keyInSystemProperty, String keyInEnvironment, | ||
String defaultValueOnLinux, String defaultValueOnWindows) { | ||
String value = ConfigUtils.getValue(keyInSystemProperty, keyInEnvironment); | ||
if (!Strings.isNullOrEmpty(value)) { | ||
return Paths.get(value); | ||
} | ||
|
||
// default path | ||
return Utils.isOSWindows() ? Paths.get(defaultValueOnWindows) : Paths.get(defaultValueOnLinux); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
apollo-core/src/test/java/com/ctrip/framework/foundation/internals/util/ConfigUtilsTest.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,57 @@ | ||
/* | ||
* 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 com.ctrip.framework.foundation.internals.util; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import static com.ctrip.framework.foundation.internals.util.ConfigUtilsTest.SystemPropertyKeys.KEY; | ||
import static com.ctrip.framework.foundation.internals.util.ConfigUtilsTest.SystemPropertyValues.CUSTOM_VALUE; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
public class ConfigUtilsTest { | ||
|
||
@After | ||
public void tearDown() { | ||
System.clearProperty(KEY); | ||
} | ||
|
||
@Test | ||
public void testGetValue_null() { | ||
assertNull(ConfigUtils.getValue(KEY, "nothing")); | ||
} | ||
|
||
@Test | ||
public void testGetValue_custom() { | ||
System.setProperty(KEY, CUSTOM_VALUE); | ||
String value = ConfigUtils.getValue(KEY, "nothing"); | ||
assertEquals(CUSTOM_VALUE, value); | ||
} | ||
|
||
protected static class SystemPropertyKeys { | ||
|
||
static final String KEY = "system.property.key.2020.11.28"; | ||
} | ||
|
||
protected static class SystemPropertyValues { | ||
|
||
static final String CUSTOM_VALUE = "/simple/custom/path"; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
apollo-core/src/test/java/com/ctrip/framework/foundation/internals/util/PathUtilsTest.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,59 @@ | ||
/* | ||
* 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 com.ctrip.framework.foundation.internals.util; | ||
|
||
import static com.ctrip.framework.foundation.internals.util.PathUtilsTest.SystemPropertyKeys.PATH_KEY; | ||
import static com.ctrip.framework.foundation.internals.util.PathUtilsTest.SystemPropertyValues.CUSTOM_VALUE; | ||
import static com.ctrip.framework.foundation.internals.util.PathUtilsTest.SystemPropertyValues.DEFAULT_VALUE; | ||
import static org.junit.Assert.*; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
public class PathUtilsTest { | ||
|
||
@After | ||
public void tearDown() { | ||
System.clearProperty(PATH_KEY); | ||
} | ||
|
||
@Test | ||
public void testResolveWithDefaultValue() { | ||
Path path = PathUtils.resolve(PATH_KEY, "nothing", DEFAULT_VALUE, DEFAULT_VALUE); | ||
assertEquals(Paths.get(DEFAULT_VALUE), path); | ||
} | ||
|
||
@Test | ||
public void testResolveWithCustomValue() { | ||
System.setProperty(PATH_KEY, CUSTOM_VALUE); | ||
Path path = PathUtils.resolve(PATH_KEY, "nothing", DEFAULT_VALUE, DEFAULT_VALUE); | ||
assertEquals(Paths.get(CUSTOM_VALUE), path); | ||
} | ||
|
||
protected static class SystemPropertyKeys { | ||
static final String PATH_KEY = "system.property.key.2020.11.28"; | ||
} | ||
|
||
protected static class SystemPropertyValues { | ||
static final String CUSTOM_VALUE = "/simple/custom/path"; | ||
static final String DEFAULT_VALUE = "/simple/default/path"; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we make it a little simpler?
e.g.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has Changed to simpler implemention.
How about use
APOLLO_PATH_SERVER_PROPERTIES
to instead ofAPOLLO_SERVERPROPERTIESPATH
for better readability?