ASysCore: Init module
This commit is contained in:
37
core/pom.xml
37
core/pom.xml
@@ -1,14 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<name>Core</name>
|
||||
|
||||
<artifactId>core</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>arcadexsystem</artifactId>
|
||||
<groupId>eu.arcadex.system</groupId>
|
||||
<version>${global.version}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>core</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<build>
|
||||
<finalName>${groupId}.${artifactId}-${version}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<version>2.3.5</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Bundle-Name>Arcadex System: ${name} ${version}</Bundle-Name>
|
||||
<Bundle-SymbolicName>${groupId}.${artifactId}</Bundle-SymbolicName>
|
||||
<Bundle-Activator>eu.arcadex.system.core.ASysCoreActivator</Bundle-Activator>
|
||||
<Export-Package>eu.arcadex.system.core.api</Export-Package>
|
||||
<Import-Package>*</Import-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
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