Skip to content

Commit

Permalink
[ISSUES alibaba#8622] Add NacosEnvironment and add some unit tests
Browse files Browse the repository at this point in the history
- remove magic number
- refactor all of them
- add some unit tests for env
- modify search rule and reformat
- fix search order
- fix p3c error
  • Loading branch information
onewe committed Jul 8, 2022
1 parent b52fc22 commit 2725bd2
Show file tree
Hide file tree
Showing 15 changed files with 971 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static class SysEnv {

public static final String JM_SNAPSHOT_PATH = "JM.SNAPSHOT.PATH";

public static final String NACOS_ENVS_SEARCH = "nacos.envs.search";

}

public static class Disk {
Expand Down
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);

}
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: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);
}
}
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);
}
}
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 false.
*/
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);

}
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 NEnvs#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);
}

}
Loading

0 comments on commit 2725bd2

Please sign in to comment.