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 all 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
@@ -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