Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Unifed configuration properties handling

Christian Bauer38ff194882a1
diff --git a/beehive/ccs/src/main/java/org/openremote/ccs/DemoDataService.java b/beehive/ccs/src/main/java/org/openremote/ccs/DemoDataService.java
index 00ba1b037..0eda6d27c 100644
--- a/beehive/ccs/src/main/java/org/openremote/ccs/DemoDataService.java
+++ b/beehive/ccs/src/main/java/org/openremote/ccs/DemoDataService.java
@@ -30,6 +30,7 @@ import org.openremote.container.security.AuthForm;
import rx.Observable;
import static org.openremote.ccs.Constants.*;
+import static org.openremote.container.util.MapAccess.getBoolean;
import static rx.Observable.fromCallable;
public class DemoDataService implements ContainerService {
@@ -60,7 +61,7 @@ public class DemoDataService implements ContainerService {
@Override
public void start(Container container) {
- if (!container.isDevMode() && !container.getConfigBoolean(IMPORT_DEMO_DATA, IMPORT_DEMO_DATA_DEFAULT)) {
+ if (!container.isDevMode() && !getBoolean(container.getConfig(), IMPORT_DEMO_DATA, IMPORT_DEMO_DATA_DEFAULT)) {
return;
}
diff --git a/container/src/main/java/org/openremote/container/Container.java b/container/src/main/java/org/openremote/container/Container.java
index 7d3a44d1b..abdcd18a6 100644
--- a/container/src/main/java/org/openremote/container/Container.java
+++ b/container/src/main/java/org/openremote/container/Container.java
@@ -25,22 +25,19 @@ import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
-import com.fasterxml.jackson.databind.node.ObjectNode;
import org.openremote.container.json.ElementalJsonModule;
import org.openremote.container.util.LogUtil;
-import java.io.InputStream;
-import java.nio.file.Files;
-import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
-import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
+import static org.openremote.container.util.MapAccess.getBoolean;
+
public class Container {
public static final Logger LOG;
@@ -65,7 +62,7 @@ public class Container {
.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.NONE)
.registerModule(new ElementalJsonModule());
- protected final ObjectNode config;
+ protected final Map<String, String> config = new HashMap<>();
protected final boolean devMode;
protected boolean running;
@@ -96,12 +93,11 @@ public class Container {
}
public Container(Map<String, String> config, Stream<ContainerService> servicesStream) {
- this.config = JSON.createObjectNode();
for (Map.Entry<String, String> entry : config.entrySet()) {
this.config.put(entry.getKey(), entry.getValue());
}
- this.devMode = getConfigBoolean(DEV_MODE, DEV_MODE_DEFAULT);
+ this.devMode = getBoolean(this.config, DEV_MODE, DEV_MODE_DEFAULT);
if (this.devMode) {
JSON.enable(SerializationFeature.INDENT_OUTPUT);
@@ -114,26 +110,8 @@ public class Container {
Runtime.getRuntime().addShutdownHook(new Thread(this::stop));
}
- public void putConfig(String variable, String value) {
- config.put(variable, value);
- }
-
- public void putConfigIfEmpty(String variable, String value) {
- if (!config.hasNonNull(variable)) {
- putConfig(variable, value);
- }
- }
-
- public String getConfig(String variable, String defaultValue) {
- return config.has(variable) ? config.get(variable).asText() : defaultValue;
- }
-
- public boolean getConfigBoolean(String variable, boolean defaultValue) {
- return config.has(variable) ? config.get(variable).asBoolean() : defaultValue;
- }
-
- public int getConfigInteger(String variable, int defaultValue) {
- return config.has(variable) ? config.get(variable).asInt() : defaultValue;
+ public Map<String, String> getConfig() {
+ return config;
}
public boolean isDevMode() {
diff --git a/container/src/main/java/org/openremote/container/message/MessageBrokerService.java b/container/src/main/java/org/openremote/container/message/MessageBrokerService.java
index a6d76523b..ec91bcd53 100644
--- a/container/src/main/java/org/openremote/container/message/MessageBrokerService.java
+++ b/container/src/main/java/org/openremote/container/message/MessageBrokerService.java
@@ -33,6 +33,8 @@ import org.openremote.container.web.socket.WebsocketComponent;
import java.util.logging.Logger;
+import static org.openremote.container.util.MapAccess.getString;
+
public class MessageBrokerService implements ContainerService {
private static final Logger LOG = Logger.getLogger(MessageBrokerService.class.getName());
@@ -77,7 +79,7 @@ public class MessageBrokerService implements ContainerService {
context.getRegistry().put(Container.class.getName(), container);
- String allowedOrigin = container.getConfig(MESSAGE_SESSION_ALLOWED_ORIGIN, MESSAGE_SESSION_ALLOWED_ORIGIN_DEFAULT);
+ String allowedOrigin = getString(container.getConfig(), MESSAGE_SESSION_ALLOWED_ORIGIN, MESSAGE_SESSION_ALLOWED_ORIGIN_DEFAULT);
WebsocketComponent websocketComponent = new DefaultWebsocketComponent(
container.getService(WebService.class),
allowedOrigin
diff --git a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
index a883209a8..f27f0200f 100644
--- a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
+++ b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
@@ -36,6 +36,8 @@ import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
+import static org.openremote.container.util.MapAccess.*;
+
public class PersistenceService implements ContainerService {
private static final Logger LOG = Logger.getLogger(PersistenceService.class.getName());
@@ -69,21 +71,21 @@ public class PersistenceService implements ContainerService {
public void init(Container container) throws Exception {
this.messageBrokerService = container.getService(MessageBrokerService.class);
- String databaseProduct = container.getConfig(DATABASE_PRODUCT, DATABASE_PRODUCT_DEFAULT);
+ String databaseProduct = getString(container.getConfig(), DATABASE_PRODUCT, DATABASE_PRODUCT_DEFAULT);
LOG.info("Preparing persistence service for database: " + databaseProduct);
database = Database.Product.valueOf(databaseProduct);
}
@Override
public void configure(Container container) throws Exception {
- String connectionUrl = container.getConfig(DATABASE_CONNECTION_URL, DATABASE_CONNECTION_URL_DEFAULT);
+ String connectionUrl = getString(container.getConfig(), DATABASE_CONNECTION_URL, DATABASE_CONNECTION_URL_DEFAULT);
LOG.info("Using database connection URL: " + connectionUrl);
- String databaseUsername = container.getConfig(DATABASE_USERNAME, DATABASE_USERNAME_DEFAULT);
- String databasePassword = container.getConfig(DATABASE_PASSWORD, DATABASE_PASSWORD_DEFAULT);
- int databaseMinPoolSize = container.getConfigInteger(DATABASE_MIN_POOL_SIZE, DATABASE_MIN_POOL_SIZE_DEFAULT);
- int databaseMaxPoolSize = container.getConfigInteger(DATABASE_MAX_POOL_SIZE, DATABASE_MAX_POOL_SIZE_DEFAULT);
- int connectionTimeoutSeconds = container.getConfigInteger(DATABASE_CONNECTION_TIMEOUT_SECONDS, DATABASE_CONNECTION_TIMEOUT_SECONDS_DEFAULT);
+ String databaseUsername = getString(container.getConfig(), DATABASE_USERNAME, DATABASE_USERNAME_DEFAULT);
+ String databasePassword = getString(container.getConfig(), DATABASE_PASSWORD, DATABASE_PASSWORD_DEFAULT);
+ int databaseMinPoolSize = getInteger(container.getConfig(), DATABASE_MIN_POOL_SIZE, DATABASE_MIN_POOL_SIZE_DEFAULT);
+ int databaseMaxPoolSize = getInteger(container.getConfig(), DATABASE_MAX_POOL_SIZE, DATABASE_MAX_POOL_SIZE_DEFAULT);
+ int connectionTimeoutSeconds = getInteger(container.getConfig(), DATABASE_CONNECTION_TIMEOUT_SECONDS, DATABASE_CONNECTION_TIMEOUT_SECONDS_DEFAULT);
persistenceUnitProperties =
database.open(connectionUrl, databaseUsername, databasePassword, connectionTimeoutSeconds, databaseMinPoolSize, databaseMaxPoolSize);
@@ -93,7 +95,7 @@ public class PersistenceService implements ContainerService {
PersistenceEventInterceptor.class.getName()
);
- persistenceUnitName = container.getConfig(PERSISTENCE_UNIT_NAME, PERSISTENCE_UNIT_NAME_DEFAULT);
+ persistenceUnitName = getString(container.getConfig(), PERSISTENCE_UNIT_NAME, PERSISTENCE_UNIT_NAME_DEFAULT);
}
@Override
@@ -102,7 +104,7 @@ public class PersistenceService implements ContainerService {
Persistence.createEntityManagerFactory(persistenceUnitName, persistenceUnitProperties);
if (container.isDevMode()
- || container.getConfigBoolean(DATABASE_CREATE_SCHEMA, DATABASE_CREATE_SCHEMA_DEFAULT)) {
+ || getBoolean(container.getConfig(), DATABASE_CREATE_SCHEMA, DATABASE_CREATE_SCHEMA_DEFAULT)) {
LOG.info("Dropping and re-creating database schema");
dropSchema();
createSchema();
diff --git a/container/src/main/java/org/openremote/container/security/IdentityService.java b/container/src/main/java/org/openremote/container/security/IdentityService.java
index bb6083c6e..ae05b40fd 100644
--- a/container/src/main/java/org/openremote/container/security/IdentityService.java
+++ b/container/src/main/java/org/openremote/container/security/IdentityService.java
@@ -28,8 +28,6 @@ import io.undertow.server.handlers.proxy.SimpleProxyClientProvider;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.keycloak.adapters.KeycloakDeployment;
-import org.keycloak.admin.client.resource.ClientResource;
-import org.keycloak.admin.client.resource.ClientsResource;
import org.keycloak.admin.client.resource.RealmsResource;
import org.keycloak.admin.client.resource.ServerInfoResource;
import org.keycloak.common.enums.SslRequired;
@@ -37,12 +35,11 @@ import org.keycloak.common.util.PemUtils;
import org.keycloak.representations.adapters.config.AdapterConfig;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
-import org.openremote.container.Constants;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.observable.RetryWithDelay;
-import org.openremote.container.web.WebClient;
import org.openremote.container.web.ProxyWebClientBuilder;
+import org.openremote.container.web.WebClient;
import org.openremote.container.web.WebService;
import rx.Observable;
@@ -62,6 +59,7 @@ import java.util.logging.Logger;
import static java.util.concurrent.TimeUnit.MINUTES;
import static javax.ws.rs.core.Response.Status.Family.REDIRECTION;
import static javax.ws.rs.core.Response.Status.Family.SUCCESSFUL;
+import static org.openremote.container.util.MapAccess.*;
import static org.openremote.container.web.WebClient.getTarget;
public class IdentityService implements ContainerService {
@@ -104,10 +102,10 @@ public class IdentityService implements ContainerService {
@Override
public void init(Container container) throws Exception {
- boolean identityNetworkSecure = container.getConfigBoolean(IDENTITY_NETWORK_SECURE, IDENTITY_NETWORK_SECURE_DEFAULT);
- String identityNetworkHost = container.getConfig(IDENTITY_NETWORK_HOST, IDENTITY_NETWORK_HOST_DEFAULT);
- int identityNetworkPort = container.getConfigInteger(IDENTITY_NETWORK_WEBSERVER_PORT, IDENTITY_NETWORK_WEBSERVER_PORT_DEFAULT);
- sessionTimeoutSeconds = container.getConfigInteger(IDENTITY_SESSION_TIMEOUT_SECONDS, IDENTITY_SESSION_TIMEOUT_SECONDS_DEFAULT);
+ boolean identityNetworkSecure = getBoolean(container.getConfig(), IDENTITY_NETWORK_SECURE, IDENTITY_NETWORK_SECURE_DEFAULT);
+ String identityNetworkHost = getString(container.getConfig(), IDENTITY_NETWORK_HOST, IDENTITY_NETWORK_HOST_DEFAULT);
+ int identityNetworkPort = getInteger(container.getConfig(), IDENTITY_NETWORK_WEBSERVER_PORT, IDENTITY_NETWORK_WEBSERVER_PORT_DEFAULT);
+ sessionTimeoutSeconds = getInteger(container.getConfig(), IDENTITY_SESSION_TIMEOUT_SECONDS, IDENTITY_SESSION_TIMEOUT_SECONDS_DEFAULT);
UriBuilder externalAuthServerUrl = UriBuilder.fromUri("")
.scheme(identityNetworkSecure ? "https" : "http")
@@ -127,8 +125,8 @@ public class IdentityService implements ContainerService {
keycloakHostUri =
UriBuilder.fromPath("/")
.scheme("http")
- .host(container.getConfig(KEYCLOAK_HOST, KEYCLOAK_HOST_DEFAULT))
- .port(container.getConfigInteger(KEYCLOAK_PORT, KEYCLOAK_PORT_DEFAULT));
+ .host(getString(container.getConfig(), KEYCLOAK_HOST, KEYCLOAK_HOST_DEFAULT))
+ .port(getInteger(container.getConfig(), KEYCLOAK_PORT, KEYCLOAK_PORT_DEFAULT));
keycloakServiceUri = keycloakHostUri.clone().replacePath(KeycloakResource.KEYCLOAK_CONTEXT_PATH);
@@ -140,22 +138,22 @@ public class IdentityService implements ContainerService {
ResteasyClientBuilder clientBuilder =
new ProxyWebClientBuilder(externalAuthServerUri.getHost(), externalAuthServerUri.getPort())
.establishConnectionTimeout(
- container.getConfigInteger(KEYCLOAK_CONNECT_TIMEOUT, KEYCLOAK_CONNECT_TIMEOUT_DEFAULT),
+ getInteger(container.getConfig(), KEYCLOAK_CONNECT_TIMEOUT, KEYCLOAK_CONNECT_TIMEOUT_DEFAULT),
TimeUnit.MILLISECONDS
)
.socketTimeout(
- container.getConfigInteger(KEYCLOAK_REQUEST_TIMEOUT, KEYCLOAK_REQUEST_TIMEOUT_DEFAULT),
+ getInteger(container.getConfig(), KEYCLOAK_REQUEST_TIMEOUT, KEYCLOAK_REQUEST_TIMEOUT_DEFAULT),
TimeUnit.MILLISECONDS
)
.connectionPoolSize(
- container.getConfigInteger(KEYCLOAK_CLIENT_POOL_SIZE, KEYCLOAK_CLIENT_POOL_SIZE_DEFAULT)
+ getInteger(container.getConfig(), KEYCLOAK_CLIENT_POOL_SIZE, KEYCLOAK_CLIENT_POOL_SIZE_DEFAULT)
);
this.httpClient = WebClient.registerDefaults(container, clientBuilder).build();
clientApplicationCache = createClientApplicationCache();
- if (container.getConfigBoolean(DISABLE_API_SECURITY, DISABLE_API_SECURITY_DEFAULT)) {
+ if (getBoolean(container.getConfig(), DISABLE_API_SECURITY, DISABLE_API_SECURITY_DEFAULT)) {
LOG.warning("###################### API SECURITY DISABLED! ######################");
} else {
if (getClientId() == null)
@@ -189,7 +187,7 @@ public class IdentityService implements ContainerService {
SimpleProxyClientProvider proxyClient = new SimpleProxyClientProvider(keycloakHostUri.build());
ProxyHandler proxyHandler = new ProxyHandler(
proxyClient,
- container.getConfigInteger(KEYCLOAK_REQUEST_TIMEOUT, KEYCLOAK_REQUEST_TIMEOUT_DEFAULT),
+ getInteger(container.getConfig(), KEYCLOAK_REQUEST_TIMEOUT, KEYCLOAK_REQUEST_TIMEOUT_DEFAULT),
ResponseCodeHandler.HANDLE_404
);
container.getService(WebService.class).getPrefixRoutes().put(AUTH_PATH, proxyHandler);
diff --git a/container/src/main/java/org/openremote/container/util/MapAccess.java b/container/src/main/java/org/openremote/container/util/MapAccess.java
new file mode 100644
index 000000000..d7d4be955
--- /dev/null
+++ b/container/src/main/java/org/openremote/container/util/MapAccess.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.container.util;
+
+import java.util.Map;
+
+/**
+ * Adds convenience getters to HashMap.
+ */
+public class MapAccess {
+
+ static public void putIfEmpty(Map<String, String> map, String variable, String value) {
+ if (!map.containsKey(variable)) {
+ map.put(variable, value);
+ }
+ }
+
+ static public String getString(Map<String, String> map, String key, String defaultValue) {
+ return map.containsKey(key) ? map.get(key) : defaultValue;
+ }
+
+ static public boolean getBoolean(Map<String, String> map, String key, boolean defaultValue) {
+ if (map.containsKey(key)) {
+ try {
+ return Boolean.valueOf(map.get(key));
+ } catch (Exception ex) {
+ return defaultValue;
+ }
+ }
+ return defaultValue;
+ }
+
+ static public int getInteger(Map<String, String> map, String key, int defaultValue) {
+ if (map.containsKey(key)) {
+ try {
+ return Integer.valueOf(map.get(key));
+ } catch (Exception ex) {
+ return defaultValue;
+ }
+ }
+ return defaultValue;
+ }
+
+}
\ No newline at end of file
diff --git a/container/src/main/java/org/openremote/container/web/DefaultWebsocketComponent.java b/container/src/main/java/org/openremote/container/web/DefaultWebsocketComponent.java
index 03b026917..443dac1f8 100644
--- a/container/src/main/java/org/openremote/container/web/DefaultWebsocketComponent.java
+++ b/container/src/main/java/org/openremote/container/web/DefaultWebsocketComponent.java
@@ -119,6 +119,7 @@ public class DefaultWebsocketComponent extends WebsocketComponent {
webService.getPrefixRoutes().remove(MessageBrokerService.WEBSOCKET_PATH);
}
+ /* TODO: Security - fix/use this! */
protected void addCORSFilter() {
if (allowedOrigin == null)
return;
diff --git a/container/src/main/java/org/openremote/container/web/WebService.java b/container/src/main/java/org/openremote/container/web/WebService.java
index b5d09e0cf..e1fd002bc 100644
--- a/container/src/main/java/org/openremote/container/web/WebService.java
+++ b/container/src/main/java/org/openremote/container/web/WebService.java
@@ -59,6 +59,8 @@ import java.util.regex.Pattern;
import static javax.ws.rs.core.HttpHeaders.CONTENT_ENCODING;
import static javax.ws.rs.core.UriBuilder.fromUri;
+import static org.openremote.container.util.MapAccess.getInteger;
+import static org.openremote.container.util.MapAccess.getString;
public abstract class WebService implements ContainerService {
@@ -92,8 +94,8 @@ public abstract class WebService implements ContainerService {
@Override
public void init(Container container) throws Exception {
- host = container.getConfig(WEBSERVER_LISTEN_HOST, WEBSERVER_LISTEN_HOST_DEFAULT);
- port = container.getConfigInteger(WEBSERVER_LISTEN_PORT, WEBSERVER_LISTEN_PORT_DEFAULT);
+ host = getString(container.getConfig(), WEBSERVER_LISTEN_HOST, WEBSERVER_LISTEN_HOST_DEFAULT);
+ port = getInteger(container.getConfig(), WEBSERVER_LISTEN_PORT, WEBSERVER_LISTEN_PORT_DEFAULT);
String containerHost = host.equalsIgnoreCase("localhost") || host.indexOf("127") == 0 || host.indexOf("0.0.0.0") == 0 ? getLocalIpAddress() : host;
containerHostUri =
diff --git a/controller/src/main/java/org/openremote/controller/ControllerService.java b/controller/src/main/java/org/openremote/controller/ControllerService.java
index b41a6888c..5b4b79580 100644
--- a/controller/src/main/java/org/openremote/controller/ControllerService.java
+++ b/controller/src/main/java/org/openremote/controller/ControllerService.java
@@ -35,7 +35,7 @@ public class ControllerService implements ContainerService {
public void configure(Container container) throws Exception {
// TODO Support booting of multiple controller instances
Deployment deployment = new Deployment(
- controllerDOMParser.parse(deploymentXml, true),
+ controllerDOMParser.parse(deploymentXml, true).getDeploymentDefinition(),
commandBuilder,
new InMemorySensorStateStorage(),
rulesProvider
diff --git a/controller/src/main/java/org/openremote/controller/context/ControllerContext.java b/controller/src/main/java/org/openremote/controller/context/ControllerContext.java
index a4fa3934b..3b103bdee 100644
--- a/controller/src/main/java/org/openremote/controller/context/ControllerContext.java
+++ b/controller/src/main/java/org/openremote/controller/context/ControllerContext.java
@@ -85,7 +85,7 @@ public class ControllerContext {
public synchronized void update(SensorState sensorState) {
LOG.fine("==> Update: " + sensorState);
if (shutdownInProgress) {
- LOG.fine("<== Shutting down. Ignoring update from: " + sensorState.getSensorName());
+ LOG.fine("<== Shutting down. Ignoring update for: " + sensorState);
return;
}
@@ -93,7 +93,6 @@ public class ControllerContext {
ruleEngine.process(sensorStateUpdate);
- // Early exist if one of the processors decided to terminate the chain
if (sensorStateUpdate.hasTerminated()) {
LOG.fine("<== Update complete, processing terminated, no update was made for: " + sensorStateUpdate.getSensorState());
return;
diff --git a/controller/src/main/java/org/openremote/controller/deploy/Deployment.java b/controller/src/main/java/org/openremote/controller/deploy/Deployment.java
index 0527f41aa..2114806aa 100644
--- a/controller/src/main/java/org/openremote/controller/deploy/Deployment.java
+++ b/controller/src/main/java/org/openremote/controller/deploy/Deployment.java
@@ -1,6 +1,10 @@
package org.openremote.controller.deploy;
-import org.openremote.controller.command.*;
+import org.openremote.container.util.MapAccess;
+import org.openremote.controller.command.Command;
+import org.openremote.controller.command.CommandBuilder;
+import org.openremote.controller.command.Commands;
+import org.openremote.controller.command.SensorUpdateCommand;
import org.openremote.controller.context.SensorStateStorage;
import org.openremote.controller.rules.RulesProvider;
import org.openremote.controller.sensor.*;
@@ -11,6 +15,7 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* Encapsulates:
* <pre>
+ * - General configuration properties
* - How protocol-specific commands are build
* - How controller sensor state is stored
* - Which rules process sensor state updates
@@ -19,6 +24,7 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class Deployment {
+ final protected Map<String, String> config;
final protected CommandBuilder commandBuilder;
final protected Commands commands;
final protected SensorStateStorage sensorStateStorage;
@@ -31,6 +37,7 @@ public class Deployment {
SensorStateStorage sensorStateStorage,
RulesProvider rulesProvider) {
+ this.config = deploymentDefinition.getConfig();
this.commandBuilder = commandBuilder;
this.commands = new Commands(this);
this.sensorStateStorage = sensorStateStorage;
@@ -55,6 +62,10 @@ public class Deployment {
}
}
+ public Map<String, String> getConfig() {
+ return config;
+ }
+
public CommandBuilder getCommandBuilder() {
return commandBuilder;
}
diff --git a/controller/src/main/java/org/openremote/controller/deploy/DeploymentDefinition.java b/controller/src/main/java/org/openremote/controller/deploy/DeploymentDefinition.java
index bc2d9fed4..76eb4dc4f 100644
--- a/controller/src/main/java/org/openremote/controller/deploy/DeploymentDefinition.java
+++ b/controller/src/main/java/org/openremote/controller/deploy/DeploymentDefinition.java
@@ -1,12 +1,41 @@
package org.openremote.controller.deploy;
+import java.util.HashMap;
+import java.util.Map;
+
/**
- * The deployment model.
+ * The deployment definition model, source for building a {@link Deployment}.
*/
-public interface DeploymentDefinition {
+public class DeploymentDefinition {
+
+ protected CommandDefinition[] commandDefinitions = new CommandDefinition[0];
+ protected SensorDefinition[] sensorDefinitions = new SensorDefinition[0];
+ protected Map<String, String> config = new HashMap<>();
+
+ public DeploymentDefinition() {
+ }
+
+ public CommandDefinition[] getCommandDefinitions() {
+ return commandDefinitions;
+ }
+
+ public void setCommandDefinitions(CommandDefinition[] commandDefinitions) {
+ this.commandDefinitions = commandDefinitions;
+ }
+
+ public SensorDefinition[] getSensorDefinitions() {
+ return sensorDefinitions;
+ }
- CommandDefinition[] getCommandDefinitions();
+ public void setSensorDefinitions(SensorDefinition[] sensorDefinitions) {
+ this.sensorDefinitions = sensorDefinitions;
+ }
- SensorDefinition[] getSensorDefinitions();
+ public Map<String, String> getConfig() {
+ return config;
+ }
+ public void setConfig(Map<String, String> config) {
+ this.config = config;
+ }
}
diff --git a/controller/src/main/java/org/openremote/controller/deploy/xml/ControllerDOM.java b/controller/src/main/java/org/openremote/controller/deploy/xml/ControllerDOM.java
index f502fadff..f6a6d92b3 100644
--- a/controller/src/main/java/org/openremote/controller/deploy/xml/ControllerDOM.java
+++ b/controller/src/main/java/org/openremote/controller/deploy/xml/ControllerDOM.java
@@ -1,33 +1,38 @@
package org.openremote.controller.deploy.xml;
import org.openremote.container.xml.DOM;
-import org.openremote.controller.deploy.DeploymentDefinition;
import org.openremote.controller.deploy.CommandDefinition;
+import org.openremote.controller.deploy.DeploymentDefinition;
import org.openremote.controller.deploy.SensorDefinition;
import org.w3c.dom.Document;
import javax.xml.xpath.XPath;
import java.util.*;
+import java.util.logging.Logger;
/**
* Usage:
- *
+ * <p>
* <pre><code>
* ControllerDOM dom = new ControllerDOMParser().parse(deploymentXml);
+ * Deployment deployment = new Deployment(dom.getDeploymentDefinition(), ...);
* </code></pre>
*/
-public class ControllerDOM extends DOM implements DeploymentDefinition {
+public class ControllerDOM extends DOM {
+
+ private static final Logger LOG = Logger.getLogger(ControllerDOM.class.getName());
public static final String NAMESPACE_URI = "http://www.openremote.org";
final protected XPath xpath;
- final protected CommandDefinition[] commandDefinitions;
- final protected SensorDefinition[] sensorDefinitions;
+ final protected DeploymentDefinition deploymentDefinition;
public ControllerDOM(Document dom, XPath xpath) {
super(dom);
this.xpath = xpath;
+ this.deploymentDefinition = new DeploymentDefinition();
+
try {
List<CommandDefinition> commandDefinitionList = new ArrayList<>();
ControllerElement[] commandElements = getRoot(xpath).getFirstChild("commands").getChildren();
@@ -42,7 +47,10 @@ public class ControllerDOM extends DOM implements DeploymentDefinition {
new CommandDefinition(commandID, protocolType, properties)
);
}
- commandDefinitions = commandDefinitionList.toArray(new CommandDefinition[commandDefinitionList.size()]);
+ deploymentDefinition.setCommandDefinitions(
+ commandDefinitionList.toArray(new CommandDefinition[commandDefinitionList.size()]
+ )
+ );
List<SensorDefinition> sensorDefinitionList = new ArrayList<>();
ControllerElement[] sensorElements = getRoot(xpath).getFirstChild("sensors").getChildren();
@@ -91,7 +99,24 @@ public class ControllerDOM extends DOM implements DeploymentDefinition {
new SensorDefinition(sensorID, sensorName, sensorType, commandDefinition, properties)
);
}
- this.sensorDefinitions = sensorDefinitionList.toArray(new SensorDefinition[sensorDefinitionList.size()]);
+ deploymentDefinition.setSensorDefinitions(
+ sensorDefinitionList.toArray(new SensorDefinition[sensorDefinitionList.size()])
+ );
+
+ Map<String, String> config = new HashMap<>();
+ ControllerElement[] configPropertyElements = getRoot(xpath).getFirstChild("config").getChildren();
+ for (ControllerElement configPropertyElement : configPropertyElements) {
+ String name = configPropertyElement.getAttribute("name");
+ String value = configPropertyElement.getAttribute("value");
+ if (name != null) {
+ if (value != null) {
+ config.put(name, value);
+ } else {
+ LOG.info("Ignoring config property without value: " + name);
+ }
+ }
+ }
+ deploymentDefinition.setConfig(config);
} catch (Exception ex) {
throw new RuntimeException("Error building controller model from XML document", ex);
@@ -113,13 +138,7 @@ public class ControllerDOM extends DOM implements DeploymentDefinition {
return new ControllerDOM((Document) getW3CDocument().cloneNode(true), xpath);
}
- @Override
- public CommandDefinition[] getCommandDefinitions() {
- return commandDefinitions;
- }
-
- @Override
- public SensorDefinition[] getSensorDefinitions() {
- return sensorDefinitions;
+ public DeploymentDefinition getDeploymentDefinition() {
+ return deploymentDefinition;
}
}
\ No newline at end of file
diff --git a/controller/src/main/java/org/openremote/controller/sensor/Sensor.java b/controller/src/main/java/org/openremote/controller/sensor/Sensor.java
index 669b44aae..aaf5dfcb1 100644
--- a/controller/src/main/java/org/openremote/controller/sensor/Sensor.java
+++ b/controller/src/main/java/org/openremote/controller/sensor/Sensor.java
@@ -40,9 +40,9 @@ public abstract class Sensor {
return value.equals(UNKNOWN_STATE_VALUE);
}
- protected SensorDefinition sensorDefinition;
+ final protected SensorDefinition sensorDefinition;
+ final protected SensorUpdateCommand sensorUpdateCommand;
protected ControllerContext controllerContext;
- protected SensorUpdateCommand sensorUpdateCommand;
/**
* This is a polling thread implementation for sensors that use {@link PullCommand}.
@@ -61,19 +61,12 @@ public abstract class Sensor {
return sensorDefinition;
}
- /**
- * Callback for {@link PushCommand} implementations. Before updating the controller context, the
- * given value is first validated and/or transformed by concrete sensor implementation's
- * {@link Sensor#process} method.
- */
- public void update(String state) {
- if (controllerContext == null) {
- LOG.fine("Ignoring update, sensor is not running: " + getSensorDefinition());
- return;
- }
- SensorState evt = process(state);
- LOG.fine("Update on ID " + getSensorDefinition().getSensorID() + ", processed '" + state + "', created: " + evt);
- controllerContext.update(evt);
+ public SensorUpdateCommand getSensorUpdateCommand() {
+ return sensorUpdateCommand;
+ }
+
+ public ControllerContext getControllerContext() {
+ return controllerContext;
}
/**
@@ -128,8 +121,19 @@ public abstract class Sensor {
}
}
- public boolean isRunning() {
- return sensorReader != null && sensorReader.pollingThreadRunning;
+ /**
+ * Callback for {@link PushCommand} implementations. Before updating the controller context, the
+ * given value is first validated and/or transformed by concrete sensor implementation's
+ * {@link Sensor#process} method.
+ */
+ public void update(String state) {
+ if (controllerContext == null) {
+ LOG.fine("Ignoring update, sensor is not running: " + getSensorDefinition());
+ return;
+ }
+ SensorState evt = process(state);
+ LOG.fine("Update on ID " + getSensorDefinition().getSensorID() + ", processed '" + state + "', created: " + evt);
+ controllerContext.update(evt);
}
/**
diff --git a/controller/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy b/controller/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy
index 6584495ee..9dd3ea788 100644
--- a/controller/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy
+++ b/controller/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy
@@ -70,6 +70,9 @@ class SimpleRuleTest extends Specification implements ContainerTrait {
deployment.getDevices()[0].getDeviceID() == 111
deployment.getDevice("TestDevice").getName() == "TestDevice"
deployment.getDevice("TestDevice").getDeviceID() == 111
+ deployment.getConfig().get("someString") == "BAR"
+ deployment.getConfig().get("someBoolean") == "true"
+ deployment.getConfig().get("someInteger") == "333"
cleanup: "the server should be stopped"
stopContainer(container)
diff --git a/controller/src/test/resources/org/openremote/test/rules/simple/controller.xml b/controller/src/test/resources/org/openremote/test/rules/simple/controller.xml
index e9c4e73fc..9ad372224 100755
--- a/controller/src/test/resources/org/openremote/test/rules/simple/controller.xml
+++ b/controller/src/test/resources/org/openremote/test/rules/simple/controller.xml
@@ -24,6 +24,9 @@
</commands>
<config>
+ <property name="someString" value="BAR"/>
+ <property name="someBoolean" value="true"/>
+ <property name="someInteger" value="333"/>
</config>
</openremote>
diff --git a/manager/server/src/main/java/org/openremote/manager/server/DemoDataService.java b/manager/server/src/main/java/org/openremote/manager/server/DemoDataService.java
index 29d179c00..7918478cd 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/DemoDataService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/DemoDataService.java
@@ -44,6 +44,7 @@ import rx.Observable;
import java.util.Arrays;
import java.util.List;
+import static org.openremote.container.util.MapAccess.*;
import static org.openremote.manager.shared.Constants.*;
import static rx.Observable.fromCallable;
@@ -94,7 +95,7 @@ public class DemoDataService implements ContainerService {
@Override
public void start(Container container) {
- if (!container.isDevMode() && !container.getConfigBoolean(IMPORT_DEMO_DATA, IMPORT_DEMO_DATA_DEFAULT)) {
+ if (!container.isDevMode() && !getBoolean(container.getConfig(), IMPORT_DEMO_DATA, IMPORT_DEMO_DATA_DEFAULT)) {
return;
}
@@ -264,19 +265,19 @@ public class DemoDataService implements ContainerService {
}
agent.getAttributes().get("host").setValue(
- container.getConfig(DEMO_CONTROLLER_HOST, DEMO_CONTROLLER_HOST_DEFAULT)
+ getString(container.getConfig(), DEMO_CONTROLLER_HOST, DEMO_CONTROLLER_HOST_DEFAULT)
);
agent.getAttributes().get("port").setValue(
- container.getConfigInteger(DEMO_CONTROLLER_PORT, DEMO_CONTROLLER_PORT_DEFAULT)
+ getInteger(container.getConfig(), DEMO_CONTROLLER_PORT, DEMO_CONTROLLER_PORT_DEFAULT)
);
agent.getAttributes().get("username").setValue(
- container.getConfig(DEMO_CONTROLLER_USERNAME, DEMO_CONTROLLER_USERNAME_DEFAULT)
+ getString(container.getConfig(), DEMO_CONTROLLER_USERNAME, DEMO_CONTROLLER_USERNAME_DEFAULT)
);
agent.getAttributes().get("password").setValue(
- container.getConfig(DEMO_CONTROLLER_PASSWORD, DEMO_CONTROLLER_PASSWORD_DEFAULT)
+ getString(container.getConfig(), DEMO_CONTROLLER_PASSWORD, DEMO_CONTROLLER_PASSWORD_DEFAULT)
);
agent.getAttributes().get("secure").setValue(
- container.getConfigBoolean(DEMO_CONTROLLER_SECURE, DEMO_CONTROLLER_SECURE_DEFAULT)
+ getBoolean(container.getConfig(), DEMO_CONTROLLER_SECURE, DEMO_CONTROLLER_SECURE_DEFAULT)
);
agentAsset.setAttributes(agent.getAttributes().getJsonObject());
diff --git a/manager/server/src/main/java/org/openremote/manager/server/map/MapService.java b/manager/server/src/main/java/org/openremote/manager/server/map/MapService.java
index 9b9370d67..5aefd779c 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/map/MapService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/map/MapService.java
@@ -29,11 +29,15 @@ import org.openremote.container.web.WebService;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.sql.*;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
+import static org.openremote.container.util.MapAccess.getString;
import static org.openremote.container.web.WebService.STATIC_PATH;
public class MapService implements ContainerService {
@@ -57,14 +61,14 @@ public class MapService implements ContainerService {
public void init(Container container) throws Exception {
this.devMode = container.isDevMode();
- mapTilesPath = Paths.get(container.getConfig(MAP_TILES_PATH, MAP_TILES_PATH_DEFAULT));
+ mapTilesPath = Paths.get(getString(container.getConfig(), MAP_TILES_PATH, MAP_TILES_PATH_DEFAULT));
if (!Files.isRegularFile(mapTilesPath)) {
throw new IllegalStateException(
"MapWidget tiles data file not found: " + mapTilesPath.toAbsolutePath()
);
}
- mapSettingsPath = Paths.get(container.getConfig(MAP_SETTINGS_PATH, MAP_SETTINGS_PATH_DEFAULT));
+ mapSettingsPath = Paths.get(getString(container.getConfig(), MAP_SETTINGS_PATH, MAP_SETTINGS_PATH_DEFAULT));
if (!Files.isRegularFile(mapSettingsPath)) {
throw new IllegalStateException(
"MapWidget settings file not found: " + mapSettingsPath.toAbsolutePath()
diff --git a/manager/server/src/main/java/org/openremote/manager/server/ngsi/EntityService.java b/manager/server/src/main/java/org/openremote/manager/server/ngsi/EntityService.java
index 105991d52..743ac70fc 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/ngsi/EntityService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/ngsi/EntityService.java
@@ -27,7 +27,9 @@ import org.openremote.container.observable.RetryWithDelay;
import org.openremote.container.web.WebClient;
import org.openremote.container.web.WebService;
import org.openremote.manager.shared.Consumer;
-import org.openremote.manager.shared.ngsi.*;
+import org.openremote.manager.shared.ngsi.Attribute;
+import org.openremote.manager.shared.ngsi.Entity;
+import org.openremote.manager.shared.ngsi.EntryPoint;
import org.openremote.manager.shared.ngsi.params.SubscriptionParams;
import rx.Observable;
@@ -35,10 +37,12 @@ import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.UriBuilder;
import java.net.URI;
-import java.util.*;
+import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
+import static org.openremote.container.util.MapAccess.getInteger;
+import static org.openremote.container.util.MapAccess.getString;
import static org.openremote.container.web.WebClient.getTarget;
public class EntityService implements ContainerService {
@@ -67,23 +71,23 @@ public class EntityService implements ContainerService {
contextBrokerHostUri =
UriBuilder.fromPath("/")
.scheme("http")
- .host(container.getConfig(CONTEXTBROKER_HOST, CONTEXTBROKER_HOST_DEFAULT))
- .port(container.getConfigInteger(CONTEXTBROKER_PORT, CONTEXTBROKER_PORT_DEFAULT));
+ .host(getString(container.getConfig(), CONTEXTBROKER_HOST, CONTEXTBROKER_HOST_DEFAULT))
+ .port(getInteger(container.getConfig(), CONTEXTBROKER_PORT, CONTEXTBROKER_PORT_DEFAULT));
LOG.info("Preparing entity service for broker host: " + contextBrokerHostUri.build());
ResteasyClientBuilder clientBuilder =
new ResteasyClientBuilder()
.establishConnectionTimeout(
- container.getConfigInteger(CONTEXTBROKER_CONNECT_TIMEOUT, CONTEXTBROKER_CONNECT_TIMEOUT_DEFAULT),
+ getInteger(container.getConfig(), CONTEXTBROKER_CONNECT_TIMEOUT, CONTEXTBROKER_CONNECT_TIMEOUT_DEFAULT),
TimeUnit.MILLISECONDS
)
.socketTimeout(
- container.getConfigInteger(CONTEXTBROKER_REQUEST_TIMEOUT, CONTEXTBROKER_REQUEST_TIMEOUT_DEFAULT),
+ getInteger(container.getConfig(), CONTEXTBROKER_REQUEST_TIMEOUT, CONTEXTBROKER_REQUEST_TIMEOUT_DEFAULT),
TimeUnit.MILLISECONDS
)
.connectionPoolSize(
- container.getConfigInteger(CONTEXTBROKER_CLIENT_POOL_SIZE, CONTEXTBROKER_CLIENT_POOL_SIZE_DEFAULT)
+ getInteger(container.getConfig(), CONTEXTBROKER_CLIENT_POOL_SIZE, CONTEXTBROKER_CLIENT_POOL_SIZE_DEFAULT)
);
this.httpClient = WebClient
diff --git a/manager/server/src/main/java/org/openremote/manager/server/web/ManagerWebService.java b/manager/server/src/main/java/org/openremote/manager/server/web/ManagerWebService.java
index 11b495873..edd96a3a8 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/web/ManagerWebService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/web/ManagerWebService.java
@@ -24,6 +24,7 @@ import org.openremote.container.web.WebService;
import java.nio.file.Paths;
+import static org.openremote.container.util.MapAccess.getString;
import static org.openremote.manager.shared.Constants.MASTER_REALM;
public class ManagerWebService extends WebService {
@@ -35,6 +36,8 @@ public class ManagerWebService extends WebService {
public void init(Container container) throws Exception {
super.init(container);
setDefaultRealm(MASTER_REALM);
- setStaticResourceDocRoot(Paths.get(container.getConfig(WEBSERVER_DOCROOT, WEBSERVER_DOCROOT_DEFAULT)));
+ setStaticResourceDocRoot(Paths.get(
+ getString(container.getConfig(), WEBSERVER_DOCROOT, WEBSERVER_DOCROOT_DEFAULT))
+ );
}
}