Skip to content

Commit

Permalink
Merge pull request #7 from ctripcorp/master
Browse files Browse the repository at this point in the history
sync forked code
  • Loading branch information
Anilople authored Dec 11, 2020
2 parents c421c98 + e35ce8c commit 4a670e2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,12 @@ The project is licensed under the [Apache 2 license](https://github.com/ctripcor
<tr>
<td><img src="https://raw.githubusercontent.com/ctripcorp/apollo-community/master/images/known-users/czbank.png" alt="浙商银行"></td>
<td><img src="https://raw.githubusercontent.com/ctripcorp/apollo-community/master/images/known-users/czbyqy.png" alt="易企银科技"></td>
<td><img src="https://raw.githubusercontent.com/ctripcorp/apollo-community/master/images/known-users/yundun.jpg" alt="上海云盾"></td>
<td><img src="https://raw.githubusercontent.com/ctripcorp/apollo-community/master/images/known-users/gaiaworks.jpg" alt="苏州盖雅信息技术有限公司"></td>
<td><img src="https://raw.githubusercontent.com/ctripcorp/apollo-community/master/images/known-users/mengxiang.png" alt="爱库存"></td>
</tr>
<tr>
<td><img src="https://raw.githubusercontent.com/ctripcorp/apollo-community/master/images/known-users/jidouauto.png" alt="极豆车联网"></td>
</tr>
</table>

Expand Down
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.google.common.base.Strings;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
Expand All @@ -16,20 +35,47 @@

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";

static final String DEFAULT_SERVER_PROPERTIES_PATH_ON_LINUX = "/opt/settings/server.properties";
static final String DEFAULT_SERVER_PROPERTIES_PATH_ON_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();

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

if (!Strings.isNullOrEmpty(serverPropertiesPath)) {
return serverPropertiesPath;
}

return Utils.isOSWindows() ? DEFAULT_SERVER_PROPERTIES_PATH_ON_WINDOWS : DEFAULT_SERVER_PROPERTIES_PATH_ON_LINUX;
}

private String getCustomizedServerPropertiesPath() {
// 1. Get from System Property
final String serverPropertiesPathFromSystemProperty = System
.getProperty("apollo.path.server.properties");
if (!Strings.isNullOrEmpty(serverPropertiesPathFromSystemProperty)) {
return serverPropertiesPathFromSystemProperty;
}

// 2. Get from OS environment variable
final String serverPropertiesPathFromEnvironment = System
.getenv("APOLLO_PATH_SERVER_PROPERTIES");
if (!Strings.isNullOrEmpty(serverPropertiesPathFromEnvironment)) {
return serverPropertiesPathFromEnvironment;
}

// last, return null if there is no custom value
return null;
}

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

File file = new File(path);
File file = new File(this.getServerPropertiesPath());
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
@@ -1,10 +1,13 @@
package com.ctrip.framework.foundation.internals.provider;

import static com.ctrip.framework.foundation.internals.provider.DefaultServerProvider.DEFAULT_SERVER_PROPERTIES_PATH_ON_LINUX;
import static com.ctrip.framework.foundation.internals.provider.DefaultServerProvider.DEFAULT_SERVER_PROPERTIES_PATH_ON_WINDOWS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.ctrip.framework.foundation.internals.Utils;
import java.io.File;
import java.io.FileInputStream;

Expand All @@ -31,6 +34,20 @@ public void tearDown() throws Exception {
private void cleanUp() {
System.clearProperty("env");
System.clearProperty("idc");
System.clearProperty("apollo.path.server.properties");
}

@Test
public void testGetServerPropertiesPathDefault() {
assertEquals(Utils.isOSWindows() ? DEFAULT_SERVER_PROPERTIES_PATH_ON_WINDOWS
: DEFAULT_SERVER_PROPERTIES_PATH_ON_LINUX, defaultServerProvider.getServerPropertiesPath());
}

@Test
public void testGetServerPropertiesPathCustom() {
final String customPath = "/simple/custom/path";
System.setProperty("apollo.path.server.properties", customPath);
assertEquals(customPath, defaultServerProvider.getServerPropertiesPath());
}

@Test
Expand Down

0 comments on commit 4a670e2

Please sign in to comment.