org.sysfoundry.kiln.base.ss.sys.SysSubsys 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.Provides;
import com.google.inject.TypeLiteral;
import com.google.inject.multibindings.Multibinder;
import org.sysfoundry.kiln.base.cfg.CompositeConfigurationSource;
import org.sysfoundry.kiln.base.cfg.ConfigurationSource;
import org.sysfoundry.kiln.base.cfg.PropertiesConfigurationSource;
import org.sysfoundry.kiln.base.srv.Server;
import org.sysfoundry.kiln.base.ss.evt.EventSubsys;
import org.sysfoundry.kiln.base.ss.srv.ServerSubsys;
import org.sysfoundry.kiln.base.sys.*;
import javax.inject.Singleton;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public class SysSubsys extends Subsys {
//public static final String NAME = SysSubsys.class.getName();
private Optional> externalConfigurationSourcesOptional = Optional.empty();
private String[] args;
private Optional> singletonClassListOptional = Optional.empty();
private Optional>> serverClassListOptional = Optional.empty();
public SysSubsys(SubsysInfo subsysInfo){
super(subsysInfo);
}
public SysSubsys(SubsysInfo subsysInfo,ConfigurationSource... configurationSources){
super(subsysInfo);
List configurationSourceList = Arrays.asList(configurationSources);
externalConfigurationSourcesOptional = Optional.ofNullable(configurationSourceList);
}
public SysSubsys(String[] args, SubsysInfo subsysInfo,
List configurationSources, List singletonClassList,
List> serverClassList){
super(subsysInfo);
externalConfigurationSourcesOptional = Optional.ofNullable(configurationSources);
this.args = args;
this.singletonClassListOptional = Optional.ofNullable(singletonClassList);
this.serverClassListOptional = Optional.ofNullable(serverClassList);
}
@Override
protected void configure() {
//call super configure so that moduleinfo binding happens
super.configure();
registerArgs();
registerDefaultSys();
registerSysConfigSourceSet();
registerServerClasses();
install(new EventSubsys());
install(new ServerSubsys());
registerSingletonClasses();
}
private void registerServerClasses() {
serverClassListOptional.ifPresent(serverClassList->{
bindServers(serverClassList.toArray(new Class[0]));
});
}
private void registerSingletonClasses() {
singletonClassListOptional.ifPresent(singletonClassList->{
singletonClassList.forEach(clz->{
bind(clz).asEagerSingleton();
});
});
}
private void registerArgs() {
bind(new TypeLiteral(){}).annotatedWith(Args.class).toInstance(args);
}
private void registerSysConfigSourceSet() {
Multibinder sysConfigSourceSetMultibinder = Multibinder.newSetBinder(binder(),
ConfigurationSource.class, SysConfigSourceSet.class);
externalConfigurationSourcesOptional.ifPresent(configs->{
for (ConfigurationSource config : configs) {
if(config != null) {
sysConfigSourceSetMultibinder.addBinding().toInstance(config);
}
}
});
Multibinder subsysConfigSourceSetMultibinder = Multibinder.newSetBinder(binder(),
ConfigurationSource.class, SubsysConfigSourceSet.class);
}
private void registerDefaultSys() {
bind(Sys.class).to(DefaultSys.class).in(Singleton.class);
}
@Provides
@Singleton
@RuntimeSysConfigSource
public ConfigurationSource provideRuntimeSysConfigurationSource(){
PropertiesConfigurationSource systemPropertiesConfigSource = new PropertiesConfigurationSource(System.getProperties());
return systemPropertiesConfigSource;
}
@Provides
@Singleton
@SysConfigSource
public ConfigurationSource provideSysConfigurationSource(@RuntimeSysConfigSource ConfigurationSource runtimeSysConfigurationSource,
@SubsysConfigSourceSet Set subsysConfigSourceSet,
@SysConfigSourceSet Set sysConfigSourceSet){
return new CompositeConfigurationSource(runtimeSysConfigurationSource,
subsysConfigSourceSet,sysConfigSourceSet);
}
}