Archived
0

Переход на Gradle

This commit is contained in:
2017-03-06 21:46:55 +03:00
parent 8d54ef0cd0
commit 5c34dbc17f
11 changed files with 140 additions and 115 deletions

View File

@@ -0,0 +1,78 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-03-02
*/
package asys.core;
import org.apache.felix.framework.Felix;
import org.apache.felix.framework.util.ImmutableList;
import org.apache.felix.framework.util.ImmutableMap;
import org.osgi.framework.BundleActivator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.*;
import static org.apache.felix.framework.util.FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP;
public class Core {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private Felix felix;
private BundleActivator hostActivator;
private Properties properties;
public Core() {
logger.trace("core created");
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@PostConstruct
public void start() {
hostActivator = new HostActivator(properties.getProperty("asys.bundles.dir"));
startFelix();
}
private void startFelix() {
logger.trace("starting Apache Felix (OSGi)");
try {
felix = new Felix(createFelixConfig());
felix.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private Map<String, Object> createFelixConfig() {
Map<String, Object> config = new HashMap<>();
config.put(SYSTEMBUNDLE_ACTIVATORS_PROP, ImmutableList.newInstance(this.hostActivator));
config.put("felix.fileinstall.poll", 250);
config.put("felix.fileinstall.log.level", 2);
config.put("felix.fileinstall.noInitialDelay", true);
config.put("org.osgi.framework.system.packages.extra", "com.sun.net.httpserver");
return ImmutableMap.newInstance(config);
}
@PreDestroy
public void stop() {
startFelix();
}
private void stopFelix() {
logger.trace("shutdown Apache Felix (OSGi)");
try {
felix.stop();
felix.waitForStop(5000L);
} catch (Exception e) {
logger.error("Error shutdown Felix", e);
}
}
}

View File

@@ -0,0 +1,59 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-03-02
*/
package asys.core;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class HostActivator implements BundleActivator {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final Path BUNDLES_DIR;
public HostActivator(String bundlesDir) {
this.BUNDLES_DIR = Paths.get(bundlesDir);
}
@Override
public void start(BundleContext bundleContext) throws Exception {
logger.trace("call [HostActivator.start(BundleContext)]");
List<Bundle> bundleList = new ArrayList<>();
Files.list(BUNDLES_DIR).forEach(path -> {
try {
InputStream inputStream = Files.newInputStream(path);
if (inputStream != null) {
bundleList.add(bundleContext.installBundle(path.toUri().toString(), inputStream));
}
} catch (IOException | BundleException e) {
e.printStackTrace();
}
});
for (Bundle bundle : bundleList) {
logger.trace("{} | {} | State: {}", bundle.getBundleId(),
bundle.getSymbolicName(), bundle.getState());
if (bundle.getState() == Bundle.INSTALLED) {
logger.trace("\tStarted");
}
}
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
logger.trace("call [HostActivator.stop(BundleContext)]");
}
}

View File

@@ -0,0 +1,23 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-03-02
*/
package asys.core;
import java.io.IOException;
import java.util.Properties;
public class Main {
public static void main(String[] args) {
Properties asysProperties = new Properties();
try {
asysProperties.load(Main.class.getResourceAsStream("/asys.properties"));
} catch (IOException e) {
throw new RuntimeException(e);
}
Core asysCore = new Core();
asysCore.setProperties(asysProperties);
asysCore.start();
}
}

View File

@@ -0,0 +1 @@
asys.bundles.dir = modules