com.github.bogieclj.molecule.mods.main.DefaultLifecycleManager Maven / Gradle / Ivy
/*
* Copyright 2019 Vijayakumar Mohan
*
* 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 com.github.bogieclj.molecule.mods.main;
import lombok.extern.slf4j.Slf4j;
import com.github.bogieclj.molecule.system.LifecycleException;
import com.github.bogieclj.molecule.system.LifecycleManager;
import com.github.bogieclj.molecule.system.annotations.LifecycleManagers;
import javax.inject.Inject;
import java.util.Map;
@Slf4j
class DefaultLifecycleManager implements LifecycleManager{
private boolean started;
private Map lifecycleManagerMap;
@Inject
DefaultLifecycleManager(@LifecycleManagers Map lifecycleManagerMap){
this.lifecycleManagerMap = lifecycleManagerMap;
}
@Override
public void start() throws LifecycleException {
log.info("Starting default lifecycle behaviour");
//first start the main lifecycle manager so that base framework services are started in order
getLifecycleManager("main").start();
started = true;
}
@Override
public void stop() {
if(started){
log.info("Stopping default lifecycle behaviour");
//first start the main lifecycle manager so that base framework services are started in order
try {
getLifecycleManager("main").stop();
} catch (LifecycleException e) {
e.printStackTrace();
}
started = false;
}
}
private LifecycleManager getLifecycleManager(String name) throws LifecycleException {
if(lifecycleManagerMap.containsKey(name)){
LifecycleManager lifecycleManager = lifecycleManagerMap.get(name);
return lifecycleManager;
}else{
String message = String.format("Unable to locate %s lifecycle manager!",name);
throw new LifecycleException(message);
}
}
}