-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #594 from nobodyiam/merge-framework-foundation
merge framework foundation
- Loading branch information
Showing
42 changed files
with
1,892 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ServiceBootstrap.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
apollo-core/src/main/java/com/ctrip/framework/apollo/tracer/Tracer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
apollo-core/src/main/java/com/ctrip/framework/foundation/Foundation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.ctrip.framework.foundation; | ||
|
||
import com.ctrip.framework.foundation.internals.NullProviderManager; | ||
import com.ctrip.framework.foundation.internals.ServiceBootstrap; | ||
import com.ctrip.framework.foundation.spi.ProviderManager; | ||
import com.ctrip.framework.foundation.spi.provider.ApplicationProvider; | ||
import com.ctrip.framework.foundation.spi.provider.NetworkProvider; | ||
import com.ctrip.framework.foundation.spi.provider.ServerProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public abstract class Foundation { | ||
private static final Logger logger = LoggerFactory.getLogger(Foundation.class); | ||
private static Object lock = new Object(); | ||
|
||
private static ProviderManager s_manager; | ||
|
||
// Encourage early initialization and fail early if it happens. | ||
static { | ||
getManager(); | ||
} | ||
|
||
private static ProviderManager getManager() { | ||
try { | ||
if (s_manager == null) { | ||
// Double locking to make sure only one thread initializes ProviderManager. | ||
synchronized (lock) { | ||
if (s_manager == null) { | ||
s_manager = ServiceBootstrap.loadFirst(ProviderManager.class); | ||
} | ||
} | ||
} | ||
|
||
return s_manager; | ||
} catch (Throwable ex) { | ||
s_manager = new NullProviderManager(); | ||
logger.error("Initialize ProviderManager failed.", ex); | ||
return s_manager; | ||
} | ||
} | ||
|
||
public static String getProperty(String name, String defaultValue) { | ||
try { | ||
return getManager().getProperty(name, defaultValue); | ||
} catch (Throwable ex) { | ||
logger.error("getProperty for {} failed.", name, ex); | ||
return defaultValue; | ||
} | ||
} | ||
|
||
public static NetworkProvider net() { | ||
try { | ||
return getManager().provider(NetworkProvider.class); | ||
} catch (Exception ex) { | ||
logger.error("Initialize NetworkProvider failed.", ex); | ||
return NullProviderManager.provider; | ||
} | ||
} | ||
|
||
public static ServerProvider server() { | ||
try { | ||
return getManager().provider(ServerProvider.class); | ||
} catch (Exception ex) { | ||
logger.error("Initialize ServerProvider failed.", ex); | ||
return NullProviderManager.provider; | ||
} | ||
} | ||
|
||
public static ApplicationProvider app() { | ||
try { | ||
return getManager().provider(ApplicationProvider.class); | ||
} catch (Exception ex) { | ||
logger.error("Initialize ApplicationProvider failed.", ex); | ||
return NullProviderManager.provider; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...o-core/src/main/java/com/ctrip/framework/foundation/internals/DefaultProviderManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.ctrip.framework.foundation.internals; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import com.ctrip.framework.foundation.internals.provider.DefaultApplicationProvider; | ||
import com.ctrip.framework.foundation.internals.provider.DefaultNetworkProvider; | ||
import com.ctrip.framework.foundation.internals.provider.DefaultServerProvider; | ||
import com.ctrip.framework.foundation.spi.ProviderManager; | ||
import com.ctrip.framework.foundation.spi.provider.Provider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class DefaultProviderManager implements ProviderManager { | ||
private static final Logger logger = LoggerFactory.getLogger(DefaultProviderManager.class); | ||
private Map<Class<? extends Provider>, Provider> m_providers = | ||
new LinkedHashMap<Class<? extends Provider>, Provider>(); | ||
|
||
public DefaultProviderManager() { | ||
// Load per-application configuration, like app id, from classpath://META-INF/app.properties | ||
Provider applicationProvider = new DefaultApplicationProvider(); | ||
applicationProvider.initialize(); | ||
register(applicationProvider); | ||
|
||
// Load network parameters | ||
Provider networkProvider = new DefaultNetworkProvider(); | ||
networkProvider.initialize(); | ||
register(networkProvider); | ||
|
||
// Load environment (fat, fws, uat, prod ...) and dc, from /opt/settings/server.properties, JVM property and/or OS | ||
// environment variables. | ||
Provider serverProvider = new DefaultServerProvider(); | ||
serverProvider.initialize(); | ||
register(serverProvider); | ||
} | ||
|
||
public synchronized void register(Provider provider) { | ||
m_providers.put(provider.getType(), provider); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T extends Provider> T provider(Class<T> clazz) { | ||
Provider provider = m_providers.get(clazz); | ||
|
||
if (provider != null) { | ||
return (T) provider; | ||
} else { | ||
logger.error("No provider [{}] found in DefaultProviderManager, please make sure it is registered in DefaultProviderManager ", | ||
clazz.getName()); | ||
return (T) NullProviderManager.provider; | ||
} | ||
} | ||
|
||
@Override | ||
public String getProperty(String name, String defaultValue) { | ||
for (Provider provider : m_providers.values()) { | ||
String value = provider.getProperty(name, null); | ||
|
||
if (value != null) { | ||
return value; | ||
} | ||
} | ||
|
||
return defaultValue; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(512); | ||
if (null != m_providers) { | ||
for (Map.Entry<Class<? extends Provider>, Provider> entry : m_providers.entrySet()) { | ||
sb.append(entry.getValue()).append("\n"); | ||
} | ||
} | ||
sb.append("(DefaultProviderManager)").append("\n"); | ||
return sb.toString(); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...-core/src/main/java/com/ctrip/framework/foundation/internals/NetworkInterfaceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.ctrip.framework.foundation.internals; | ||
|
||
import java.net.Inet4Address; | ||
import java.net.InetAddress; | ||
import java.net.NetworkInterface; | ||
import java.net.SocketException; | ||
import java.net.UnknownHostException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public enum NetworkInterfaceManager { | ||
INSTANCE; | ||
|
||
private InetAddress m_local; | ||
|
||
private InetAddress m_localHost; | ||
|
||
private NetworkInterfaceManager() { | ||
load(); | ||
} | ||
|
||
public InetAddress findValidateIp(List<InetAddress> addresses) { | ||
InetAddress local = null; | ||
int maxWeight = -1; | ||
for (InetAddress address : addresses) { | ||
if (address instanceof Inet4Address) { | ||
int weight = 0; | ||
|
||
if (address.isSiteLocalAddress()) { | ||
weight += 8; | ||
} | ||
|
||
if (address.isLinkLocalAddress()) { | ||
weight += 4; | ||
} | ||
|
||
if (address.isLoopbackAddress()) { | ||
weight += 2; | ||
} | ||
|
||
// has host name | ||
// TODO fix performance issue when calling getHostName | ||
if (!Objects.equals(address.getHostName(), address.getHostAddress())) { | ||
weight += 1; | ||
} | ||
|
||
if (weight > maxWeight) { | ||
maxWeight = weight; | ||
local = address; | ||
} | ||
} | ||
} | ||
return local; | ||
} | ||
|
||
public String getLocalHostAddress() { | ||
return m_local.getHostAddress(); | ||
} | ||
|
||
public String getLocalHostName() { | ||
try { | ||
if (null == m_localHost) { | ||
m_localHost = InetAddress.getLocalHost(); | ||
} | ||
return m_localHost.getHostName(); | ||
} catch (UnknownHostException e) { | ||
return m_local.getHostName(); | ||
} | ||
} | ||
|
||
private String getProperty(String name) { | ||
String value = null; | ||
|
||
value = System.getProperty(name); | ||
|
||
if (value == null) { | ||
value = System.getenv(name); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private void load() { | ||
String ip = getProperty("host.ip"); | ||
|
||
if (ip != null) { | ||
try { | ||
m_local = InetAddress.getByName(ip); | ||
return; | ||
} catch (Exception e) { | ||
System.err.println(e); | ||
// ignore | ||
} | ||
} | ||
|
||
try { | ||
List<NetworkInterface> nis = Collections.list(NetworkInterface.getNetworkInterfaces()); | ||
List<InetAddress> addresses = new ArrayList<InetAddress>(); | ||
InetAddress local = null; | ||
|
||
try { | ||
for (NetworkInterface ni : nis) { | ||
if (ni.isUp() && !ni.isLoopback()) { | ||
addresses.addAll(Collections.list(ni.getInetAddresses())); | ||
} | ||
} | ||
local = findValidateIp(addresses); | ||
} catch (Exception e) { | ||
// ignore | ||
} | ||
m_local = local; | ||
} catch (SocketException e) { | ||
// ignore it | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
apollo-core/src/main/java/com/ctrip/framework/foundation/internals/NullProviderManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.ctrip.framework.foundation.internals; | ||
|
||
import com.ctrip.framework.foundation.internals.provider.NullProvider; | ||
import com.ctrip.framework.foundation.spi.ProviderManager; | ||
|
||
public class NullProviderManager implements ProviderManager { | ||
public static final NullProvider provider = new NullProvider(); | ||
|
||
@Override | ||
public String getProperty(String name, String defaultValue) { | ||
return defaultValue; | ||
} | ||
|
||
@Override | ||
public NullProvider provider(Class clazz) { | ||
return provider; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return provider.toString(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
apollo-core/src/main/java/com/ctrip/framework/foundation/internals/ServiceBootstrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ctrip.framework.foundation.internals; | ||
|
||
import java.util.Iterator; | ||
import java.util.ServiceLoader; | ||
|
||
public class ServiceBootstrap { | ||
public static <S> S loadFirst(Class<S> clazz) { | ||
Iterator<S> iterator = loadAll(clazz); | ||
if (!iterator.hasNext()) { | ||
throw new IllegalStateException(String.format( | ||
"No implementation defined in /META-INF/services/%s, please check whether the file exists and has the right implementation class!", | ||
clazz.getName())); | ||
} | ||
return iterator.next(); | ||
} | ||
|
||
private static <S> Iterator<S> loadAll(Class<S> clazz) { | ||
ServiceLoader<S> loader = ServiceLoader.load(clazz); | ||
|
||
return loader.iterator(); | ||
} | ||
} |
Oops, something went wrong.