Archived
0

ASysCore: Init module

This commit is contained in:
2016-04-06 13:25:49 +03:00
parent 1f8bfaa0d8
commit 57c03d3323
5 changed files with 173 additions and 42 deletions

View File

@@ -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>

View 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");
}
}

View File

@@ -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();
}
}

View File

@@ -0,0 +1,9 @@
package eu.arcadex.system.core.api;
/**
* @author DmitriyMX <mail@dmitriymx.ru>
* 2016
*/
public interface ICore {
void reloadMoludes();
}

46
pom.xml
View File

@@ -3,9 +3,11 @@
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">
<modelVersion>4.0.0</modelVersion>
<name>Arcadex System</name>
<properties>
<global.version>0.1</global.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<groupId>eu.arcadex.system</groupId>
@@ -43,47 +45,19 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/*.java</exclude>
<exclude>**/*.SF</exclude>
<exclude>**/*.DSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<!-- ToDo: Change main class -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>eu.arcadex.system.ArcadexSystemImpl</mainClass>
</transformer>
</transformers>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.2</version>
<scope>provided</scope>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.20</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
</dependency>
</dependencies>
</project>