forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUES alibaba#8622] Add NacosEnvironment and add some unit tests (a…
…libaba#8680) * [ISSUES alibaba#8622] Add NacosEnvironment and add some unit tests - remove magic number - refactor all of them - add some unit tests for env - modify search rule and reformat - fix search order - fix p3c error * fix ci error * add some type converters - enhance search method - default_setting.properties is renamed nacos_default_setting.properties * the property key of system environment replace dot with underscore * add some default values and enhance SystemEnvPropertySource * add the copyright information
- Loading branch information
Showing
21 changed files
with
1,233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
client/src/main/java/com/alibaba/nacos/client/env/AbstractPropertySource.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,34 @@ | ||
/* | ||
* Copyright 1999-2022 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.client.env; | ||
|
||
abstract class AbstractPropertySource { | ||
|
||
/** | ||
* get property's type. | ||
* @return name | ||
*/ | ||
abstract SourceType getType(); | ||
|
||
/** | ||
* get property, if the value can not be got by the special key, the null will be returned. | ||
* @param key special key | ||
* @return value or null | ||
*/ | ||
abstract String getProperty(String key); | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
client/src/main/java/com/alibaba/nacos/client/env/DefaultSettingPropertySource.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,54 @@ | ||
/* | ||
* Copyright 1999-2022 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.client.env; | ||
|
||
import com.alibaba.nacos.common.utils.ResourceUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.util.Properties; | ||
|
||
class DefaultSettingPropertySource extends AbstractPropertySource { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSettingPropertySource.class); | ||
|
||
private static final String DEFAULT_SETTING_PATH = "classpath:nacos_default_setting.properties"; | ||
|
||
private final Properties defaultSetting = new Properties(); | ||
|
||
DefaultSettingPropertySource() { | ||
try { | ||
final URL resourceUrl = ResourceUtils.getResourceUrl(DEFAULT_SETTING_PATH); | ||
final InputStream inputStream = resourceUrl.openStream(); | ||
defaultSetting.load(inputStream); | ||
} catch (Exception e) { | ||
LOGGER.warn("load default setting failed"); | ||
} | ||
} | ||
|
||
@Override | ||
SourceType getType() { | ||
return SourceType.DEFAULT_SETTING; | ||
} | ||
|
||
@Override | ||
String getProperty(String key) { | ||
return defaultSetting.getProperty(key); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
client/src/main/java/com/alibaba/nacos/client/env/JvmArgsPropertySource.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,38 @@ | ||
/* | ||
* Copyright 1999-2022 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.client.env; | ||
|
||
import java.util.Properties; | ||
|
||
class JvmArgsPropertySource extends AbstractPropertySource { | ||
|
||
private final Properties properties; | ||
|
||
JvmArgsPropertySource() { | ||
this.properties = System.getProperties(); | ||
} | ||
|
||
@Override | ||
SourceType getType() { | ||
return SourceType.JVM; | ||
} | ||
|
||
@Override | ||
String getProperty(String key) { | ||
return properties.getProperty(key); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
client/src/main/java/com/alibaba/nacos/client/env/NacosEnvironment.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,90 @@ | ||
/* | ||
* Copyright 1999-2022 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.client.env; | ||
|
||
/** | ||
* nacos env interface. | ||
* | ||
* @author onewe | ||
*/ | ||
public interface NacosEnvironment { | ||
|
||
/** | ||
* get property, if the value can not be got by the special key, the null will be returned. | ||
* | ||
* @param key special key | ||
* @return string value or null. | ||
*/ | ||
String getProperty(String key); | ||
|
||
/** | ||
* get property, if the value can not be got by the special key, the default value will be returned. | ||
* @param key special key | ||
* @param defaultValue default value | ||
* @return string value or default value. | ||
*/ | ||
String getProperty(String key, String defaultValue); | ||
|
||
/** | ||
* get boolean, if the value can not be got by the special key, the null will be returned. | ||
* | ||
* @param key special key | ||
* @return boolean value or null. | ||
*/ | ||
Boolean getBoolean(String key); | ||
|
||
/** | ||
* get boolean, if the value can not be got by the special key, the default value will be returned. | ||
* @param key special key | ||
* @param defaultValue default value | ||
* @return boolean value or defaultValue. | ||
*/ | ||
Boolean getBoolean(String key, Boolean defaultValue); | ||
|
||
/** | ||
* get integer, if the value can not be got by the special key, the null will be returned. | ||
* | ||
* @param key special key | ||
* @return integer value or null | ||
*/ | ||
Integer getInteger(String key); | ||
|
||
/** | ||
* get integer, if the value can not be got by the special key, the default value will be returned. | ||
* @param key special key | ||
* @param defaultValue default value | ||
* @return integer value or default value | ||
*/ | ||
Integer getInteger(String key, Integer defaultValue); | ||
|
||
/** | ||
* get long, if the value can not be got by the special key, the null will be returned. | ||
* | ||
* @param key special key | ||
* @return long value or null | ||
*/ | ||
Long getLong(String key); | ||
|
||
/** | ||
* get long, if the value can not be got by the special key, the default value will be returned. | ||
* @param key special key | ||
* @param defaultValue default value | ||
* @return long value or default value | ||
*/ | ||
Long getLong(String key, Long defaultValue); | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
client/src/main/java/com/alibaba/nacos/client/env/NacosEnvironmentFactory.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 @@ | ||
/* | ||
* Copyright 1999-2022 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.client.env; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.Properties; | ||
|
||
class NacosEnvironmentFactory { | ||
|
||
/** | ||
* create nacos environment. | ||
* @return NacosEnvironment's proxy object, it contains a SearchableEnvironment object. | ||
* @see SearchableEnvironment | ||
*/ | ||
static NacosEnvironment createEnvironment() { | ||
|
||
return (NacosEnvironment) Proxy.newProxyInstance(NacosEnvironmentFactory.class.getClassLoader(), new Class[] {NacosEnvironment.class}, | ||
new NacosEnvironmentDelegate() { | ||
volatile NacosEnvironment environment; | ||
|
||
@Override | ||
public void init(Properties properties) { | ||
if (environment == null) { | ||
synchronized (NacosEnvironmentFactory.class) { | ||
if (environment == null) { | ||
environment = new SearchableEnvironment(properties); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
if (environment == null) { | ||
throw new IllegalStateException( | ||
"Nacos environment doesn't init, please call NacosEnvs#init method then try it again."); | ||
} | ||
return method.invoke(environment, args); | ||
} | ||
}); | ||
} | ||
|
||
interface NacosEnvironmentDelegate extends InvocationHandler { | ||
|
||
/** | ||
* init environment. | ||
* @param properties user customize properties | ||
*/ | ||
void init(Properties properties); | ||
} | ||
|
||
} |
Oops, something went wrong.