ASysCore: Init module
This commit is contained in:
93
core/src/main/java/eu/arcadex/system/core/ASysCore.java
Normal file
93
core/src/main/java/eu/arcadex/system/core/ASysCore.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package eu.arcadex.system.core;
|
||||
|
||||
import eu.arcadex.system.core.api.ICore;
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.BundleException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016
|
||||
*/
|
||||
class ASysCore implements ICore {
|
||||
private Logger logger = LoggerFactory.getLogger(ASysCore.class.getName());
|
||||
private BundleContext bundleContext;
|
||||
private List<Bundle> modules;
|
||||
|
||||
ASysCore(BundleContext bundleContext) {
|
||||
this.bundleContext = bundleContext;
|
||||
}
|
||||
|
||||
void init() {
|
||||
// Загрузка библиотек
|
||||
logger.trace("Load libraries");
|
||||
startBundles(loadJars("lib"));
|
||||
|
||||
// Загрузка модулей
|
||||
logger.trace("Load modules");
|
||||
modules = loadJars("modules");
|
||||
startBundles(modules);
|
||||
}
|
||||
|
||||
private List<Bundle> loadJars(String directory) {
|
||||
List<Bundle> bundlesList = new ArrayList<>();
|
||||
|
||||
File libsDir = new File(directory);
|
||||
String[] jarsList = libsDir.list();
|
||||
for (String jar : jarsList) {
|
||||
if (!jar.endsWith(".jar")) continue;
|
||||
try {
|
||||
logger.trace("Load jar \"{}/{}\"", directory, jar);
|
||||
Bundle bundle = bundleContext.installBundle(String.format("file:%s/%s", directory, jar));
|
||||
bundlesList.add(bundle);
|
||||
} catch (BundleException e) {
|
||||
logger.error(String.format("Error load jar \"%s/%s\"", directory, jar), e);
|
||||
}
|
||||
}
|
||||
|
||||
return bundlesList;
|
||||
}
|
||||
|
||||
private void startBundles(List<Bundle> bundleList) {
|
||||
for (Bundle bundle : bundleList) {
|
||||
try {
|
||||
logger.trace("Start module \"{}\"", bundle.getSymbolicName());
|
||||
bundle.start();
|
||||
} catch (BundleException e) {
|
||||
logger.error(String.format("Error start bundle \"%s\"", bundle.getSymbolicName()), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void stopBundles(List<Bundle> bundleList) {
|
||||
for (Bundle bundle : bundleList) {
|
||||
try {
|
||||
bundle.stop();
|
||||
} catch (BundleException e) {
|
||||
logger.error(String.format("Error stop bundle \"%s\"", bundle.getSymbolicName()), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reloadMoludes() {
|
||||
logger.trace("Reload modules - start");
|
||||
|
||||
// Остановка модулей
|
||||
stopBundles(modules);
|
||||
|
||||
// Загрузка модулей по-новой
|
||||
modules = loadJars("modules");
|
||||
|
||||
// Запуск модулей
|
||||
startBundles(modules);
|
||||
|
||||
logger.trace("Reload modules - end");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package eu.arcadex.system.core;
|
||||
|
||||
import eu.arcadex.system.core.api.ICore;
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
|
||||
/**
|
||||
* @author DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016
|
||||
*/
|
||||
public class ASysCoreActivator implements BundleActivator {
|
||||
private ServiceRegistration<?> service;
|
||||
|
||||
@Override
|
||||
public void start(BundleContext bundleContext) throws Exception {
|
||||
ASysCore core = new ASysCore(bundleContext);
|
||||
|
||||
// Регистрация сервиса доступа к Ядру
|
||||
service = bundleContext.registerService(ICore.class.getName(), core, null);
|
||||
|
||||
// Инициализация Ядра
|
||||
core.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(BundleContext bundleContext) throws Exception {
|
||||
service.unregister();
|
||||
}
|
||||
}
|
||||
9
core/src/main/java/eu/arcadex/system/core/api/ICore.java
Normal file
9
core/src/main/java/eu/arcadex/system/core/api/ICore.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package eu.arcadex.system.core.api;
|
||||
|
||||
/**
|
||||
* @author DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016
|
||||
*/
|
||||
public interface ICore {
|
||||
void reloadMoludes();
|
||||
}
|
||||
Reference in New Issue
Block a user