com.github.zhengframework.bootstrap.ZhengApplication Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zheng-bootstrap Show documentation
Show all versions of zheng-bootstrap Show documentation
zheng framework module: bootstrap
package com.github.zhengframework.bootstrap;
import com.github.zhengframework.configuration.Configuration;
import com.github.zhengframework.configuration.ConfigurationAware;
import com.github.zhengframework.configuration.ConfigurationBuilder;
import com.github.zhengframework.configuration.io.FileLocator;
import com.github.zhengframework.configuration.source.ConfigurationSource;
import com.github.zhengframework.configuration.source.EnvironmentVariablesConfigurationSource;
import com.github.zhengframework.configuration.source.FileConfigurationSource;
import com.github.zhengframework.configuration.source.MergeConfigurationSource;
import com.github.zhengframework.configuration.source.SystemPropertiesConfigurationSource;
import com.github.zhengframework.core.ModuleProvider;
import com.github.zhengframework.service.ServiceManager;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Stage;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
import joptsimple.ArgumentAcceptingOptionSpec;
import joptsimple.OptionSet;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Getter
public class ZhengApplication {
public static final String APPLICATION_FILE = "zheng.application.file";
private Configuration configuration;
private Arguments arguments;
private Set moduleList;
private boolean autoLoadModule;
private Set> excludeModuleList;
private Set> excludeModuleProviderList;
private Injector injector;
public ZhengApplication(Configuration configuration,
Arguments arguments, Set moduleList, boolean autoLoadModule,
Set> excludeModuleList,
Set> excludeModuleProviderList) {
this.configuration = configuration;
this.arguments = arguments;
this.moduleList = moduleList;
this.autoLoadModule = autoLoadModule;
this.excludeModuleList = excludeModuleList;
this.excludeModuleProviderList = excludeModuleProviderList;
build();
}
private void build() {
if (configuration == null) {
configuration = buildConfiguration(arguments);
}
Set moduleListCopy = new LinkedHashSet<>(moduleList);
if (autoLoadModule) {
for (ModuleProvider moduleProvider : ServiceLoader.load(ModuleProvider.class)) {
if (!excludeModuleProviderList.contains(moduleProvider.getClass())) {
log.info("find ModuleProvider={}", moduleProvider.getClass());
moduleListCopy.add(moduleProvider.getModule());
}
}
}
Set modules = moduleListCopy.stream()
.filter(module -> !excludeModuleList.contains(module.getClass()))
.collect(Collectors.toSet());
for (Module module : modules) {
if (module instanceof ConfigurationAware) {
ConfigurationAware configurationAware = (ConfigurationAware) module;
configurationAware.initConfiguration(configuration);
}
}
injector = Guice.createInjector(Stage.PRODUCTION, modules);
}
public Injector getInjector() {
return injector;
}
public void start() throws Exception {
injector.getInstance(ServiceManager.class).start();
}
public void stop() {
injector.getInstance(ServiceManager.class).stop();
}
private Configuration buildConfiguration(Arguments arguments) {
ArgumentAcceptingOptionSpec configOpt = arguments.getOptionParser().accepts("config")
.withRequiredArg().ofType(String.class);
OptionSet optionSet = arguments.parse();
Optional config = optionSet.valueOfOptional(configOpt);
String argsConfigFile = null;
if (config.isPresent()) {
argsConfigFile = config.get();
}
Path path = null;
if (argsConfigFile != null) {
path = Paths.get(argsConfigFile);
Preconditions.checkState(Files.exists(path) && Files.isReadable(path),
argsConfigFile + " not exists or not readable");
}
String env = System.getenv(APPLICATION_FILE);
if (path == null && !Strings.isNullOrEmpty(env)) {
path = Paths.get(env);
Preconditions.checkState(Files.exists(path) && Files.isReadable(path),
env + " not exists or not readable");
}
String property = System.getProperty(APPLICATION_FILE);
if (path == null && !Strings.isNullOrEmpty(property)) {
path = Paths.get(property);
Preconditions.checkState(Files.exists(path) && Files.isReadable(path),
property + " not exists or not readable");
}
List sources = new ArrayList<>();
sources.add(new SystemPropertiesConfigurationSource());
sources.add(new EnvironmentVariablesConfigurationSource());
if (path != null) {
sources.add(0, new FileConfigurationSource(
FileLocator.builder().sourceURL(path.toAbsolutePath().toString()).build()));
} else {
sources.add(new FileConfigurationSource("application.properties"));
}
MergeConfigurationSource configurationSource = new MergeConfigurationSource(
sources);
return new ConfigurationBuilder()
.withConfigurationSource(configurationSource)
.build();
}
}