Skip to content

Commit

Permalink
[ISSUE alibaba#8622] add the scope isolation strategy
Browse files Browse the repository at this point in the history
- add setProperty,addProperties and containsKey methods
- remove PropertySourceSearch
  • Loading branch information
onewe committed Aug 11, 2022
1 parent bf76143 commit 1186028
Show file tree
Hide file tree
Showing 14 changed files with 551 additions and 375 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ 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 final String NACOS_ENV_FIRST = "nacos.env.first";

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.client.env;

import java.util.Properties;

abstract class AbstractPropertySource {

/**
Expand All @@ -31,4 +33,17 @@ abstract class AbstractPropertySource {
*/
abstract String getProperty(String key);

/**
* Tests if the specified object is a key in this propertySource.
* @param key key – possible key
* @return true if and only if the specified object is a key in this propertySource, false otherwise.
*/
abstract boolean containsKey(String key);

/**
* to properties.
* @return properties
*/
abstract Properties asProperties();

}
40 changes: 40 additions & 0 deletions client/src/main/java/com/alibaba/nacos/client/env/ApplyScope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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;

/**
* environment apply scope.
* @author onewe
*/
public enum ApplyScope {
/**
* global scope.
*/
GLOBAL,
/**
* config module scope.
*/
CONFIG,
/**
* naming module scope.
*/
NAMING,
/**
* naming maintain module scope.
*/
NAMING_MAINTAIN
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

class DefaultSettingPropertySource extends AbstractPropertySource {
Expand All @@ -33,12 +32,10 @@ class DefaultSettingPropertySource extends AbstractPropertySource {
private final Properties defaultSetting = new Properties();

DefaultSettingPropertySource() {
try {
final URL resourceUrl = ResourceUtils.getResourceUrl(DEFAULT_SETTING_PATH);
final InputStream inputStream = resourceUrl.openStream();
try (final InputStream inputStream = ResourceUtils.getResourceUrl(DEFAULT_SETTING_PATH).openStream()) {
defaultSetting.load(inputStream);
} catch (Exception e) {
LOGGER.warn("load default setting failed");
LOGGER.error("load default setting failed", e);
}
}

Expand All @@ -51,4 +48,16 @@ SourceType getType() {
String getProperty(String key) {
return defaultSetting.getProperty(key);
}

@Override
boolean containsKey(String key) {
return defaultSetting.containsKey(key);
}

@Override
Properties asProperties() {
Properties properties = new Properties();
properties.putAll(defaultSetting);
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@

class JvmArgsPropertySource extends AbstractPropertySource {

private final Properties properties;

JvmArgsPropertySource() {
this.properties = System.getProperties();
}
private final Properties properties = new Properties(System.getProperties());

@Override
SourceType getType() {
Expand All @@ -35,4 +31,16 @@ SourceType getType() {
String getProperty(String key) {
return properties.getProperty(key);
}

@Override
boolean containsKey(String key) {
return properties.containsKey(key);
}

@Override
Properties asProperties() {
Properties properties = new Properties();
properties.putAll(this.properties);
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.client.env;

import java.util.Properties;

/**
* nacos env interface.
*
Expand Down Expand Up @@ -87,4 +89,29 @@ public interface NacosEnvironment {
*/
Long getLong(String key, Long defaultValue);

/**
* set property.
* @param key key
* @param value value
*/
void setProperty(String key, String value);

/**
* add properties.
* @param properties properties
*/
void addProperties(Properties properties);

/**
* Tests if the specified object is a key in this environment.
* @param key key – possible key
* @return true if and only if the specified object is a key in this environment, false otherwise.
*/
boolean containsKey(String key);

/**
* get properties from environment.
* @return properties
*/
Properties asProperties();
}

This file was deleted.

Loading

0 comments on commit 1186028

Please sign in to comment.