org.beifengtz.jvmm.server.ServerBootstrap Maven / Gradle / Ivy
Show all versions of jvmm-server Show documentation
package org.beifengtz.jvmm.server;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.Promise;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.beifengtz.jvmm.common.factory.LoggerFactory;
import org.beifengtz.jvmm.common.logger.LoggerLevel;
import org.beifengtz.jvmm.convey.DefaultInternalLoggerFactory;
import org.beifengtz.jvmm.server.entity.conf.Configuration;
import org.beifengtz.jvmm.server.entity.conf.ServerConf;
import org.beifengtz.jvmm.server.enums.ServerType;
import org.beifengtz.jvmm.server.logger.DefaultILoggerFactory;
import org.beifengtz.jvmm.server.logger.DefaultJvmmILoggerFactory;
import org.beifengtz.jvmm.server.service.JvmmHttpServerService;
import org.beifengtz.jvmm.server.service.JvmmSentinelService;
import org.beifengtz.jvmm.server.service.JvmmServerService;
import org.beifengtz.jvmm.server.service.JvmmService;
import org.beifengtz.jvmm.server.service.ServiceManager;
import org.slf4j.Logger;
import java.lang.instrument.Instrumentation;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
/**
*
* Description: TODO
*
*
* Created in 10:11 2021/5/22
*
* @author beifengtz
*/
public class ServerBootstrap {
public static final String AGENT_BOOT_CLASS = "org.beifengtz.jvmm.agent.AgentBootStrap";
private static volatile ServerBootstrap bootstrap;
private static boolean fromAgent;
private Instrumentation instrumentation;
private Thread shutdownHook;
private volatile ServiceManager serviceManager;
private ServerBootstrap(Instrumentation inst) {
this.instrumentation = inst;
}
public static ServerBootstrap getInstance() {
return getInstance(null, (Configuration) null);
}
public synchronized static ServerBootstrap getInstance(Configuration config) {
return getInstance(null, config);
}
public synchronized static ServerBootstrap getInstance(Instrumentation inst, String args) {
String[] argKv = args.split(";");
String configFileUrl = null;
for (String s : argKv) {
String[] split = s.split("=");
if (split.length > 1) {
if ("config".equalsIgnoreCase(split[0])) {
configFileUrl = split[1];
} else if ("fromAgent".equalsIgnoreCase(split[0])) {
fromAgent = Boolean.parseBoolean(split[1]);
}
}
}
if (configFileUrl == null) {
System.err.println("No config file, about to use default configuration");
}
return getInstance(inst, configFileUrl == null ? null : Configuration.parseFromUrl(configFileUrl));
}
public synchronized static ServerBootstrap getInstance(Instrumentation inst, Configuration config) {
if (bootstrap != null) {
if (config != null) {
ServerContext.setConfiguration(config);
}
return bootstrap;
}
if (config == null) {
config = new Configuration();
}
if (config.getLog().isUseJvmm()) {
initJvmmLogger(config.getLog().getLevel());
} else {
if (fromAgent) {
initAgentLogger(config.getLog().getLevel());
} else {
initBaseLogger();
}
}
bootstrap = new ServerBootstrap(inst);
ServerContext.setConfiguration(config);
return bootstrap;
}
private static void initJvmmLogger(String levelStr) {
LoggerLevel level = LoggerLevel.valueOf(levelStr.toUpperCase(Locale.ROOT));
InternalLoggerFactory.setDefaultFactory(DefaultInternalLoggerFactory.newInstance(level));
LoggerFactory.register(DefaultJvmmILoggerFactory.newInstance(level));
}
private static void initBaseLogger() {
LoggerFactory.register(org.slf4j.LoggerFactory.getILoggerFactory());
}
private static void initAgentLogger(String levelStr) {
LoggerLevel level = LoggerLevel.valueOf(levelStr.toUpperCase(Locale.ROOT));
DefaultILoggerFactory defaultILoggerFactory = DefaultILoggerFactory.newInstance(level);
InternalLoggerFactory.setDefaultFactory(defaultILoggerFactory);
LoggerFactory.register(defaultILoggerFactory);
}
private static Logger logger() {
return LoggerFactory.logger(ServerBootstrap.class);
}
public void start(Function