Archived
0

Core: регистрация сервиса настроек ASys

This commit is contained in:
2017-04-29 14:37:59 +03:00
parent 75963ae936
commit 50242ec11f
6 changed files with 78 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
group = 'asys' group = 'asys'
version = '0.2-SNAPSHOT' version = '0.3-SNAPSHOT'
apply plugin: 'application' apply plugin: 'application'

View File

@@ -0,0 +1,14 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-04-29
*/
package asys.api;
public interface Config {
String getString(String key);
String getString(String key, String defaultValue);
int getInt(String key);
int getInt(String key, int defaultValue);
long getLong(String key);
long getLong(String key, long defaultValue);
}

View File

@@ -0,0 +1,47 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-04-29
*/
package asys.core;
import asys.api.Config;
import java.util.Properties;
public class ConfigImpl implements Config {
private Properties properties;
public void loadByProperties(Properties properties) {
this.properties = properties;
}
@Override
public String getString(String key) {
return getString(key, null);
}
@Override
public String getString(String key, String defaultValue) {
return properties.getProperty(key, defaultValue);
}
@Override
public int getInt(String key) {
return getInt(key, 0);
}
@Override
public int getInt(String key, int defaultValue) {
return (int) properties.getOrDefault(key, defaultValue);
}
@Override
public long getLong(String key) {
return getLong(key, 0L);
}
@Override
public long getLong(String key, long defaultValue) {
return (long) properties.getOrDefault(key, defaultValue);
}
}

View File

@@ -12,8 +12,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import uk.org.lidalia.sysoutslf4j.context.SysOutOverSLF4J; import uk.org.lidalia.sysoutslf4j.context.SysOutOverSLF4J;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.*; import java.util.*;
import static org.apache.felix.framework.util.FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP; import static org.apache.felix.framework.util.FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP;
@@ -22,18 +20,20 @@ public class Core {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
private Felix felix; private Felix felix;
private BundleActivator hostActivator; private BundleActivator hostActivator;
private Properties properties; private ConfigImpl config = new ConfigImpl();
public Core() { public Core() {
logger.trace("core created"); logger.trace("core created");
} }
public void setProperties(Properties properties) { public void setConfiguration(Properties properties) {
this.properties = properties; config.loadByProperties(properties);
} }
public void start() { public void start() {
hostActivator = new HostActivator(properties.getProperty("asys.bundles.dir")); hostActivator = new HostActivator(
config.getString("asys.bundles.dir", "modules"),
config);
startFelix(); startFelix();
} }

View File

@@ -4,10 +4,8 @@
*/ */
package asys.core; package asys.core;
import org.osgi.framework.Bundle; import asys.api.Config;
import org.osgi.framework.BundleActivator; import org.osgi.framework.*;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -22,13 +20,18 @@ import java.util.List;
public class HostActivator implements BundleActivator { public class HostActivator implements BundleActivator {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final Path BUNDLES_DIR; private final Path BUNDLES_DIR;
private final Config config;
private ServiceRegistration<?> serviceConfig;
public HostActivator(String bundlesDir) { HostActivator(String bundlesDir, Config config) {
this.BUNDLES_DIR = Paths.get(bundlesDir); this.BUNDLES_DIR = Paths.get(bundlesDir);
this.config = config;
} }
@Override @Override
public void start(BundleContext bundleContext) throws Exception { public void start(BundleContext bundleContext) throws Exception {
serviceConfig = bundleContext.registerService(Config.class, config, null);
List<Bundle> bundleList = new ArrayList<>(); List<Bundle> bundleList = new ArrayList<>();
Files.list(BUNDLES_DIR).forEach(path -> { Files.list(BUNDLES_DIR).forEach(path -> {
@@ -56,5 +59,6 @@ public class HostActivator implements BundleActivator {
@Override @Override
public void stop(BundleContext bundleContext) throws Exception { public void stop(BundleContext bundleContext) throws Exception {
logger.trace("call [HostActivator.stop(BundleContext)]"); logger.trace("call [HostActivator.stop(BundleContext)]");
serviceConfig.unregister();
} }
} }

View File

@@ -15,7 +15,7 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Core asysCore = new Core(); Core asysCore = new Core();
asysCore.setProperties(getProperties()); asysCore.setConfiguration(getProperties());
asysCore.start(); asysCore.start();
} }