diff --git a/apollo-core/src/main/java/com/ctrip/framework/foundation/internals/provider/DefaultServerProvider.java b/apollo-core/src/main/java/com/ctrip/framework/foundation/internals/provider/DefaultServerProvider.java index 90076c0ca78..d02e098e5d0 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/foundation/internals/provider/DefaultServerProvider.java +++ b/apollo-core/src/main/java/com/ctrip/framework/foundation/internals/provider/DefaultServerProvider.java @@ -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; @@ -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); diff --git a/apollo-core/src/test/java/com/ctrip/framework/foundation/internals/provider/DefaultServerProviderTest.java b/apollo-core/src/test/java/com/ctrip/framework/foundation/internals/provider/DefaultServerProviderTest.java index e18086fa2a0..612d7b836a7 100644 --- a/apollo-core/src/test/java/com/ctrip/framework/foundation/internals/provider/DefaultServerProviderTest.java +++ b/apollo-core/src/test/java/com/ctrip/framework/foundation/internals/provider/DefaultServerProviderTest.java @@ -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; @@ -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