Skip to content
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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
/*
* 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.provider;

import com.ctrip.framework.foundation.internals.constant.PathConstants.ResolvedPaths;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
Expand All @@ -16,20 +35,16 @@

public class DefaultServerProvider implements ServerProvider {
private static final Logger logger = LoggerFactory.getLogger(DefaultServerProvider.class);
private static final String SERVER_PROPERTIES_LINUX = "/opt/settings/server.properties";
private static final String SERVER_PROPERTIES_WINDOWS = "C:/opt/settings/server.properties";

private String m_env;
private String m_dc;

private Properties m_serverProperties = new Properties();
private final Properties m_serverProperties = new Properties();

@Override
public void initialize() {
try {
String path = Utils.isOSWindows() ? SERVER_PROPERTIES_WINDOWS : SERVER_PROPERTIES_LINUX;

File file = new File(path);
File file = ResolvedPaths.SERVER_PROPERTIES.toFile();
Copy link
Member

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.

  ...
  File file = new File(getServerPropertiesPath());
  ...

  String getServerPropertiesPath() {
    String serverPropertiesPath = getCustomizedServerPropertiesPath();

    if (Strings.isNullOrEmpty(serverPropertiesPath)) {
      serverPropertiesPath = Utils.isOSWindows() ? SERVER_PROPERTIES_WINDOWS : SERVER_PROPERTIES_LINUX;
    }

    return serverPropertiesPath;
  }

  private String getCustomizedServerPropertiesPath() {
    // 1. Get from System Property
    String serverPropertiesPath = System.getProperty("apollo.serverPropertiesPath");
    if (Strings.isNullOrEmpty(serverPropertiesPath)) {
      // 2. Get from OS environment variable
      serverPropertiesPath = System.getenv("APOLLO_SERVERPROPERTIESPATH");
    }

    return serverPropertiesPath;
  }

Copy link
Contributor Author

@Anilople Anilople Nov 28, 2020

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 of APOLLO_SERVERPROPERTIESPATH for better readability?

if (file.exists() && file.canRead()) {
logger.info("Loading {}", file.getAbsolutePath());
FileInputStream fis = new FileInputStream(file);
Expand Down
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;
}
}
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);
}
}
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";
}
}
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";
}
}