@ -17,6 +17,7 @@
package com.tencent.cloud.polaris.context ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Objects ;
@ -25,6 +26,7 @@ import com.tencent.polaris.api.control.Destroyable;
import com.tencent.polaris.api.core.ConsumerAPI ;
import com.tencent.polaris.api.core.LosslessAPI ;
import com.tencent.polaris.api.core.ProviderAPI ;
import com.tencent.polaris.api.utils.CollectionUtils ;
import com.tencent.polaris.assembly.api.AssemblyAPI ;
import com.tencent.polaris.assembly.factory.AssemblyAPIFactory ;
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI ;
@ -53,7 +55,8 @@ public class PolarisSDKContextManager {
* Constant for checking before destroy SDK context .
* /
public volatile static boolean isRegistered = false ;
private volatile static SDKContext sdkContext ;
private volatile static SDKContext configSDKContext ;
private volatile static SDKContext serviceSdkContext ;
private volatile static ProviderAPI providerAPI ;
private volatile static ConsumerAPI consumerAPI ;
private volatile static LosslessAPI losslessAPI ;
@ -75,7 +78,7 @@ public class PolarisSDKContextManager {
* Don ' t call this method directly .
* /
public static void innerDestroy ( ) {
if ( Objects . nonNull ( s dkContext) ) {
if ( Objects . nonNull ( s erviceS dkContext) ) {
try {
// destroy ProviderAPI
if ( Objects . nonNull ( providerAPI ) ) {
@ -119,9 +122,9 @@ public class PolarisSDKContextManager {
assemblyAPI = null ;
}
if ( Objects . nonNull ( s dkContext) ) {
s dkContext. destroy ( ) ;
s dkContext = null ;
if ( Objects . nonNull ( s erviceS dkContext) ) {
s erviceS dkContext. destroy ( ) ;
s erviceS dkContext = null ;
}
LOG . info ( "Polaris SDK context is destroyed." ) ;
}
@ -131,35 +134,58 @@ public class PolarisSDKContextManager {
}
}
/ * *
* Used for config data .
* /
public static SDKContext innerGetConfigSDKContext ( ) {
if ( configSDKContext = = null ) {
throw new IllegalArgumentException ( "configSDKContext is not initialized." ) ;
}
return configSDKContext ;
}
public static void innerConfigDestroy ( ) {
try {
if ( Objects . nonNull ( configSDKContext ) ) {
configSDKContext . destroy ( ) ;
configSDKContext = null ;
}
LOG . info ( "Polaris SDK config context is destroyed." ) ;
}
catch ( Throwable throwable ) {
LOG . info ( "Polaris SDK config context is destroyed failed." , throwable ) ;
}
}
public void init ( ) {
if ( null = = sdkContext ) {
if ( null = = s erviceS dkContext) {
try {
// init SDKContext
sdkContext = SDKContext . initContextByConfig ( properties . configuration ( modifierList ,
s erviceS dkContext = SDKContext . initContextByConfig ( properties . configuration ( modifierList ,
( ) - > environment . getProperty ( "spring.cloud.client.ip-address" ) ,
( ) - > environment . getProperty ( "spring.cloud.polaris.local-port" , Integer . class , 0 ) ) ) ;
sdkContext . init ( ) ;
s erviceS dkContext. init ( ) ;
// init ProviderAPI
providerAPI = DiscoveryAPIFactory . createProviderAPIByContext ( sdkContext ) ;
providerAPI = DiscoveryAPIFactory . createProviderAPIByContext ( s erviceS dkContext) ;
// init losslessAPI
losslessAPI = DiscoveryAPIFactory . createLosslessAPIByContext ( sdkContext ) ;
losslessAPI = DiscoveryAPIFactory . createLosslessAPIByContext ( s erviceS dkContext) ;
// init ConsumerAPI
consumerAPI = DiscoveryAPIFactory . createConsumerAPIByContext ( sdkContext ) ;
consumerAPI = DiscoveryAPIFactory . createConsumerAPIByContext ( s erviceS dkContext) ;
// init RouterAPI
routerAPI = RouterAPIFactory . createRouterAPIByContext ( sdkContext ) ;
routerAPI = RouterAPIFactory . createRouterAPIByContext ( s erviceS dkContext) ;
// init CircuitBreakAPI
circuitBreakAPI = CircuitBreakAPIFactory . createCircuitBreakAPIByContext ( sdkContext ) ;
circuitBreakAPI = CircuitBreakAPIFactory . createCircuitBreakAPIByContext ( s erviceS dkContext) ;
// init LimitAPI
limitAPI = LimitAPIFactory . createLimitAPIByContext ( s dkContext) ;
limitAPI = LimitAPIFactory . createLimitAPIByContext ( s erviceS dkContext) ;
// init AssemblyAPI
assemblyAPI = AssemblyAPIFactory . createAssemblyAPIByContext ( s dkContext) ;
assemblyAPI = AssemblyAPIFactory . createAssemblyAPIByContext ( s erviceS dkContext) ;
// add shutdown hook
Runtime . getRuntime ( ) . addShutdownHook ( new Thread ( ( ) - > {
@ -182,11 +208,25 @@ public class PolarisSDKContextManager {
throw throwable ;
}
}
initConfig ( ) ;
}
/ * *
* Used for config data .
* /
public static void setConfigSDKContext ( SDKContext context ) {
if ( configSDKContext = = null ) {
configSDKContext = context ;
// add shutdown hook
Runtime . getRuntime ( ) . addShutdownHook ( new Thread ( PolarisSDKContextManager : : innerConfigDestroy ) ) ;
LOG . info ( "create Polaris config SDK context successfully." ) ;
}
}
public SDKContext getSDKContext ( ) {
init ( ) ;
return sdkContext ;
return s erviceS dkContext;
}
public ProviderAPI getProviderAPI ( ) {
@ -222,4 +262,36 @@ public class PolarisSDKContextManager {
public AssemblyAPI getAssemblyAPI ( ) {
return assemblyAPI ;
}
public SDKContext getConfigSDKContext ( ) {
initConfig ( ) ;
return configSDKContext ;
}
public void initConfig ( ) {
// get modifiers for configuration.
List < PolarisConfigModifier > configModifierList = new ArrayList < > ( ) ;
for ( PolarisConfigModifier modifier : modifierList ) {
if ( modifier instanceof PolarisConfigurationConfigModifier ) {
configModifierList . add ( modifier ) ;
}
}
if ( null = = configSDKContext & & CollectionUtils . isNotEmpty ( configModifierList ) ) {
try {
// init config SDKContext
configSDKContext = SDKContext . initContextByConfig ( properties . configuration ( configModifierList ,
( ) - > environment . getProperty ( "spring.cloud.client.ip-address" ) ,
( ) - > environment . getProperty ( "spring.cloud.polaris.local-port" , Integer . class , 0 ) ) ) ;
configSDKContext . init ( ) ;
// add shutdown hook
Runtime . getRuntime ( ) . addShutdownHook ( new Thread ( PolarisSDKContextManager : : innerConfigDestroy ) ) ;
LOG . info ( "create Polaris config SDK context successfully. properties: {}, " , properties ) ;
}
catch ( Throwable throwable ) {
LOG . error ( "create Polaris config SDK context failed. properties: {}, " , properties , throwable ) ;
throw throwable ;
}
}
}
}