org.sysfoundry.kiln.base.ss.sys.BaseSysBuilder Maven / Gradle / Ivy
/*
* Copyright 2019 Sysfoundry (www.sysfoundry.org)
*
* Licensed 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 org.sysfoundry.kiln.base.ss.sys;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import org.sysfoundry.kiln.base.cfg.ConfigurationSource;
import org.sysfoundry.kiln.base.srv.Server;
import org.sysfoundry.kiln.base.sys.SubsysInfo;
import org.sysfoundry.kiln.base.sys.Sys;
import org.sysfoundry.kiln.base.sys.SysBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static org.sysfoundry.kiln.base.util.CollectionUtils.KV;
import static org.sysfoundry.kiln.base.util.CollectionUtils.MAP;
public class BaseSysBuilder implements SysBuilder {
private Optional sysArgsOptional = Optional.empty();
private Optional subsysInfoOptional = Optional.empty();
private Optional> subsystemsOptional = Optional.empty();
private Optional> configurationSourcesOptional = Optional.empty();
private SubsysInfo defaultSubsysInfo = new SubsysInfo("anonymous-sys",MAP(KV("name","anonymous-sys")));
private List defaultConfigurSourcesList;
private String[] sysArgsDefault = new String[]{}; // default args
private Optional> singletonClassListOptional = Optional.empty();
private Optional>> serverClassListOptional = Optional.empty();
private List defaultSingletonClassList = new ArrayList<>();
private List> defaultServerClassList = new ArrayList<>();
public BaseSysBuilder(){
this(null);
}
public BaseSysBuilder(String[] args) {
sysArgsOptional = Optional.ofNullable(args);
}
public BaseSysBuilder withAttributes(SubsysInfo subsysInfo) {
subsysInfoOptional = Optional.ofNullable(subsysInfo);
return this;
}
public BaseSysBuilder withSubsystems(Module... subsystems) {
Optional tempModules = Optional.ofNullable(subsystems);
tempModules.ifPresent(modules -> {
List moduleList = Arrays.asList(modules);
subsystemsOptional = Optional.ofNullable(moduleList);
});
return this;
}
public BaseSysBuilder withConfigurations(ConfigurationSource... configurationSources) {
Optional tempConfigSourcesOptional = Optional.ofNullable(configurationSources);
tempConfigSourcesOptional.ifPresent(configSources -> {
List configurationSourceList = Arrays.asList(configSources);
configurationSourcesOptional = Optional.ofNullable(configurationSourceList);
});
return this;
}
@Override
public Sys build(){
Module[] finalModuleList = getFinalModuleList();
Injector mainInjector = Guice.createInjector(finalModuleList);
Sys sys = mainInjector.getInstance(Sys.class);
return sys;
}
private Module[] getFinalModuleList() {
List finalModuleList = new ArrayList<>();
//add the SysSubsys automatically
SysSubsys sysSubsys = new SysSubsys(sysArgsOptional.orElse(sysArgsDefault),subsysInfoOptional.orElse(defaultSubsysInfo),
configurationSourcesOptional.orElse(defaultConfigurSourcesList),
singletonClassListOptional.orElse(defaultSingletonClassList),
serverClassListOptional.orElse(defaultServerClassList));
finalModuleList.add(sysSubsys);
//add the rest of the modules if present
subsystemsOptional.ifPresent(modules -> {
finalModuleList.addAll(modules);
});
return finalModuleList.toArray(new Module[0]);
}
public BaseSysBuilder withSingletons(Class... singletonClasses) {
List classList = Arrays.asList(singletonClasses);
singletonClassListOptional = Optional.ofNullable(classList);
return this;
}
public BaseSysBuilder withServers(Class extends Server>... servers){
List> serverClassList = Arrays.asList(servers);
serverClassListOptional = Optional.ofNullable(serverClassList);
return this;
}
}