Skip to content

armgit / core

Public Imported from GitHub
0

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

Switch to Postgres

Christian Bauer282c5401a0cc
diff --git a/README.md b/README.md
index 0d8b82eb2..cff770872 100644
--- a/README.md
+++ b/README.md
@@ -45,11 +45,15 @@ function dcleanup(){
}
```
-We are using the [Orion Context Broker](https://fiware-orion.readthedocs.org/en/develop/) with a MongoDB backend. For development, this is an instance with a non-persistent data store.
+For authentication and authorization we are using [Keycloak](http://keycloak.jboss.org/).
-For authentication and authorization we are using [Keycloak](http://keycloak.jboss.org/) with a non-persistent data store.
+We are using the PostgreSQL as a database backend. Test and sample data will be installed fresh every time you start the services. Note that this is not suitable for a production configuration!
-Test and sample data will be installed fresh every time you start the server(s).
+To access the database directly with an SQL console, run:
+
+```
+docker exec -it openremote_postgres_dev_1 su -s /bin/sh - postgres -c "sh -c 'psql openremote'"
+```
### Run required services
@@ -86,7 +90,7 @@ You can now open http://localhost:8080/ in your browser.
You can run the tests of the `manager` project with `./gradlew test` or run the individual test classes in your IDE directly. The working directory must always be set to the project root diretory.
-Note that some of these tests are end-to-end tests that require the whole environment to be running. This means your Keycloak and Orion containers will be used, and data will be inserted and deleted during a test run. You might want to start with clean containers before running tests and you might have to restart containers after (failed) tests.
+Note that some of these tests are end-to-end tests that require the whole environment to be running. This means your Keycloak and PostgreSQL containers will be used, and data will be inserted and deleted during a test run. You might want to start with clean containers before running tests and you might have to restart containers after (failed) tests.
### Updating map data
diff --git a/container/build.gradle b/container/build.gradle
index 6c27c9533..e8f5fc8fc 100644
--- a/container/build.gradle
+++ b/container/build.gradle
@@ -31,7 +31,7 @@ dependencies {
compile "com.google.gwt:gwt-elemental:$gwtVersion"
- compile "org.mariadb.jdbc:mariadb-java-client:$mariadbConnectorVersion"
+ compile "org.postgresql:postgresql:$postgresJdbcVersion"
compile "com.zaxxer:HikariCP:$hikaricpVersion"
compile "org.hibernate:hibernate-entitymanager:$hibernateVersion"
compile "org.hibernate:hibernate-java8:$hibernateVersion"
diff --git a/container/src/main/java/org/openremote/container/Constants.java b/container/src/main/java/org/openremote/container/Constants.java
index 21d64959b..3d3bda13d 100644
--- a/container/src/main/java/org/openremote/container/Constants.java
+++ b/container/src/main/java/org/openremote/container/Constants.java
@@ -23,4 +23,6 @@ public interface Constants {
String PERSISTENCE_SEQUENCE_ID_GENERATOR = "SEQUENCE_ID_GENERATOR";
String PERSISTENCE_UNIQUE_ID_GENERATOR = "UNIQUE_ID_GENERATOR";
+ String PERSISTENCE_JSON_OBJECT_TYPE = "json_object";
+ String PERSISTENCE_JSON_ARRAY_TYPE = "json_array";
}
diff --git a/container/src/main/java/org/openremote/container/json/JsonStringType.java b/container/src/main/java/org/openremote/container/json/ElementalJsonArrayType.java
similarity index 60%
rename from container/src/main/java/org/openremote/container/json/JsonStringType.java
rename to container/src/main/java/org/openremote/container/json/ElementalJsonArrayType.java
index 269f8f1a4..c4f29c7ae 100644
--- a/container/src/main/java/org/openremote/container/json/JsonStringType.java
+++ b/container/src/main/java/org/openremote/container/json/ElementalJsonArrayType.java
@@ -19,22 +19,27 @@
*/
package org.openremote.container.json;
-import elemental.json.JsonValue;
-import org.hibernate.type.AbstractSingleColumnStandardBasicType;
+import elemental.json.JsonArray;
+import org.hibernate.HibernateException;
-public class JsonStringType extends AbstractSingleColumnStandardBasicType<JsonValue> {
+import java.io.Serializable;
- public JsonStringType() {
- super(JsonStringSqlTypeDescriptor.INSTANCE, new JsonTypeDescriptor());
+public class ElementalJsonArrayType extends PostgreSQLJsonType {
+
+ @Override
+ public Serializable disassemble(Object value) throws HibernateException {
+ return value == null ? null : ((JsonArray) value).toJson();
}
- public String getName() {
- return "json";
+ @Override
+ public Object assemble(Serializable cached, Object owner) throws HibernateException {
+ return cached == null ? null : elemental.json.impl.JsonUtil.parse(cached.toString());
}
@Override
- protected boolean registerUnderJavaType() {
- return true;
+ public Class returnedClass() {
+ return JsonArray.class;
}
-}
\ No newline at end of file
+}
+
diff --git a/container/src/main/java/org/openremote/container/json/ElementalJsonObjectType.java b/container/src/main/java/org/openremote/container/json/ElementalJsonObjectType.java
new file mode 100644
index 000000000..7346e252d
--- /dev/null
+++ b/container/src/main/java/org/openremote/container/json/ElementalJsonObjectType.java
@@ -0,0 +1,45 @@
+/*
+ * 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.json;
+
+import elemental.json.JsonObject;
+import org.hibernate.HibernateException;
+
+import java.io.Serializable;
+
+public class ElementalJsonObjectType extends PostgreSQLJsonType {
+
+ @Override
+ public Serializable disassemble(Object value) throws HibernateException {
+ return value == null ? null : ((JsonObject) value).toJson();
+ }
+
+ @Override
+ public Object assemble(Serializable cached, Object owner) throws HibernateException {
+ return cached == null ? null : elemental.json.impl.JsonUtil.parse(cached.toString());
+ }
+
+ @Override
+ public Class returnedClass() {
+ return JsonObject.class;
+ }
+
+}
+
diff --git a/container/src/main/java/org/openremote/container/json/JsonStringSqlTypeDescriptor.java b/container/src/main/java/org/openremote/container/json/JsonStringSqlTypeDescriptor.java
deleted file mode 100644
index 162072c38..000000000
--- a/container/src/main/java/org/openremote/container/json/JsonStringSqlTypeDescriptor.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * 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.json;
-
-import org.hibernate.type.descriptor.ValueBinder;
-import org.hibernate.type.descriptor.ValueExtractor;
-import org.hibernate.type.descriptor.WrapperOptions;
-import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
-import org.hibernate.type.descriptor.sql.BasicBinder;
-import org.hibernate.type.descriptor.sql.BasicExtractor;
-import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
-
-import java.sql.*;
-
-public class JsonStringSqlTypeDescriptor implements SqlTypeDescriptor {
-
- public static final JsonStringSqlTypeDescriptor INSTANCE = new JsonStringSqlTypeDescriptor();
-
- @Override
- public int getSqlType() {
- return Types.OTHER;
- }
-
- @Override
- public boolean canBeRemapped() {
- return true;
- }
-
- @Override
- public <X> ValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) {
- return new BasicExtractor<X>(javaTypeDescriptor, this) {
- @Override
- protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
- return javaTypeDescriptor.wrap(rs.getString(name), options);
- }
-
- @Override
- protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
- return javaTypeDescriptor.wrap(statement.getString(index), options);
- }
-
- @Override
- protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
- return javaTypeDescriptor.wrap(statement.getString(name), options);
- }
- };
- }
-
-
- @Override
- public <X> ValueBinder<X> getBinder(JavaTypeDescriptor<X> javaTypeDescriptor) {
- return new BasicBinder<X>(javaTypeDescriptor, this) {
- @Override
- protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
- st.setString(
- index,
- javaTypeDescriptor.unwrap(value, String.class, options)
- );
- }
- };
- }
-}
\ No newline at end of file
diff --git a/container/src/main/java/org/openremote/container/json/JsonTypeDescriptor.java b/container/src/main/java/org/openremote/container/json/JsonTypeDescriptor.java
deleted file mode 100644
index 5d9635eca..000000000
--- a/container/src/main/java/org/openremote/container/json/JsonTypeDescriptor.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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.json;
-
-import elemental.json.JsonValue;
-import elemental.json.impl.JsonUtil;
-import org.hibernate.type.descriptor.WrapperOptions;
-import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
-import org.hibernate.type.descriptor.java.MutableMutabilityPlan;
-
-public class JsonTypeDescriptor extends AbstractTypeDescriptor<JsonValue> {
-
- public JsonTypeDescriptor() {
- super(JsonValue.class, new MutableMutabilityPlan<JsonValue>() {
- @Override
- protected JsonValue deepCopyNotNull(JsonValue value) {
- return JsonUtil.parse(value.toJson());
- }
- });
- }
-
- @Override
- public boolean areEqual(JsonValue one, JsonValue another) {
- if (one == another) {
- return true;
- }
- if (one == null || another == null) {
- return false;
- }
- return one.toJson().equals(another.toJson());
- }
-
- @Override
- public String toString(JsonValue value) {
- return value.toJson();
- }
-
- @Override
- public JsonValue fromString(String string) {
- return JsonUtil.parse(string);
- }
-
- @SuppressWarnings({"unchecked"})
- @Override
- public <X> X unwrap(JsonValue value, Class<X> type, WrapperOptions options) {
- if (value == null) {
- return null;
- }
- if (String.class.isAssignableFrom(type)) {
- return (X) toString(value);
- }
- throw unknownUnwrap(type);
- }
-
- @Override
- public <X> JsonValue wrap(X value, WrapperOptions options) {
- if (value == null) {
- return null;
- }
- return fromString(value.toString());
- }
-
-}
\ No newline at end of file
diff --git a/container/src/main/java/org/openremote/container/json/PostgreSQLJsonType.java b/container/src/main/java/org/openremote/container/json/PostgreSQLJsonType.java
new file mode 100644
index 000000000..4dab8f858
--- /dev/null
+++ b/container/src/main/java/org/openremote/container/json/PostgreSQLJsonType.java
@@ -0,0 +1,79 @@
+/*
+ * 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.json;
+
+import org.hibernate.HibernateException;
+import org.hibernate.engine.spi.SessionImplementor;
+import org.hibernate.usertype.UserType;
+import org.postgresql.util.PGobject;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+
+public abstract class PostgreSQLJsonType implements UserType {
+
+ @Override
+ public boolean isMutable() {
+ return true;
+ }
+
+ @Override
+ public Object deepCopy(Object value) throws HibernateException {
+ return assemble(disassemble(value), null);
+ }
+
+ @Override
+ public int[] sqlTypes() {
+ return new int[]{Types.JAVA_OBJECT};
+ }
+
+ @Override
+ public boolean equals(Object x, Object y) throws HibernateException {
+ return x == null && y == null || x != null && y != null && disassemble(x).equals(disassemble(y));
+ }
+
+ @Override
+ public int hashCode(Object x) throws HibernateException {
+ return disassemble(x).hashCode();
+ }
+
+ @Override
+ public Object replace(Object original, Object target, Object owner) throws HibernateException {
+ return assemble(disassemble(original), owner);
+ }
+
+ @Override
+ public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
+ String json = resultSet.getString(names[0]);
+ return json == null ? null : assemble(json, null);
+ }
+
+ @Override
+ public void nullSafeSet(PreparedStatement st, Object value, int index,
+ SessionImplementor session) throws HibernateException, SQLException {
+ final String json = value == null ? null : disassemble(value).toString();
+ PGobject pgo = new PGobject();
+ pgo.setType("jsonb");
+ pgo.setValue(json);
+ st.setObject(index, pgo);
+ }
+}
diff --git a/container/src/main/java/org/openremote/container/persistence/Database.java b/container/src/main/java/org/openremote/container/persistence/Database.java
index c2ec16c94..b144e355e 100644
--- a/container/src/main/java/org/openremote/container/persistence/Database.java
+++ b/container/src/main/java/org/openremote/container/persistence/Database.java
@@ -22,7 +22,7 @@ package org.openremote.container.persistence;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.hibernate.cfg.AvailableSettings;
-import org.hibernate.dialect.MySQL57InnoDBDialect;
+import org.hibernate.dialect.PostgreSQL94Dialect;
import java.util.HashMap;
import java.util.Map;
@@ -35,7 +35,7 @@ public interface Database {
enum Product implements Database {
- MYSQL {
+ POSTGRES {
protected HikariConfig hikariConfig;
protected HikariDataSource hikariDataSource;
@@ -43,7 +43,7 @@ public interface Database {
public Map<String, Object> open(String connectionUrl, String username, String password, int minIdle, int maxPoolSize) {
hikariConfig = new HikariConfig();
- hikariConfig.setDataSourceClassName("org.mariadb.jdbc.MySQLDataSource");
+ hikariConfig.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
hikariConfig.addDataSourceProperty("url", connectionUrl);
hikariConfig.setUsername(username);
hikariConfig.setPassword(password);
@@ -53,7 +53,7 @@ public interface Database {
hikariDataSource = new HikariDataSource(hikariConfig);
Map<String, Object> properties = new HashMap<>();
- properties.put(AvailableSettings.DIALECT, MySQL57InnoDBDialect.class.getName());
+ properties.put(AvailableSettings.DIALECT, PostgreSQL94Dialect.class.getName());
properties.put(AvailableSettings.DATASOURCE, hikariDataSource);
return properties;
diff --git a/container/src/main/java/org/openremote/container/persistence/PersistenceEventInterceptor.java b/container/src/main/java/org/openremote/container/persistence/PersistenceEventInterceptor.java
index 7214d193e..91b3578cf 100644
--- a/container/src/main/java/org/openremote/container/persistence/PersistenceEventInterceptor.java
+++ b/container/src/main/java/org/openremote/container/persistence/PersistenceEventInterceptor.java
@@ -23,19 +23,24 @@ import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;
import org.hibernate.CallbackException;
import org.hibernate.EmptyInterceptor;
+import org.hibernate.Transaction;
import org.hibernate.type.Type;
import org.openremote.container.message.MessageBrokerService;
+import javax.transaction.Status;
+import javax.transaction.Synchronization;
import java.io.Serializable;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.Set;
+import java.util.logging.Logger;
/**
* Intercept Hibernate lifecycle events and publish a message.
*/
public class PersistenceEventInterceptor extends EmptyInterceptor {
+ private static final Logger LOG = Logger.getLogger(PersistenceEventInterceptor.class.getName());
+
protected MessageBrokerService messageBrokerService;
protected Set<Object> inserts = new HashSet<>();
protected Set<Object> updates = new HashSet<>();
@@ -76,26 +81,42 @@ public class PersistenceEventInterceptor extends EmptyInterceptor {
deletes.add(entity);
}
- public void postFlush(Iterator iterator) throws CallbackException {
- try {
- ProducerTemplate producerTemplate =
- messageBrokerService.getContext().createProducerTemplate();
-
- for (Object insert : inserts) {
- publishEvent(producerTemplate, insert, PersistenceEvent.INSERT);
- }
- for (Object update : updates) {
- publishEvent(producerTemplate, update, PersistenceEvent.UPDATE);
+ @Override
+ public void afterTransactionBegin(Transaction tx) {
+ tx.registerSynchronization(new Synchronization() {
+ @Override
+ public void beforeCompletion() {
}
- for (Object delete : deletes) {
- publishEvent(producerTemplate, delete, PersistenceEvent.DELETE);
+ @Override
+ public void afterCompletion(int status) {
+ try {
+ if (status != Status.STATUS_COMMITTED)
+ return;
+ ProducerTemplate producerTemplate =
+ messageBrokerService.getContext().createProducerTemplate();
+
+ for (Object insert : inserts) {
+ publishEvent(producerTemplate, insert, PersistenceEvent.INSERT);
+ }
+ for (Object update : updates) {
+ publishEvent(producerTemplate, update, PersistenceEvent.UPDATE);
+ }
+ for (Object delete : deletes) {
+ publishEvent(producerTemplate, delete, PersistenceEvent.DELETE);
+
+ }
+ } finally {
+ inserts.clear();
+ updates.clear();
+ deletes.clear();
+ }
}
- } finally {
- inserts.clear();
- updates.clear();
- deletes.clear();
- }
+ });
+ }
+
+ @Override
+ public void afterTransactionCompletion(Transaction tx) {
}
protected void publishEvent(ProducerTemplate producerTemplate, Object object, PersistenceEvent eventAction) {
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 64cb9c74d..df0b0248f 100644
--- a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
+++ b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
@@ -43,9 +43,9 @@ public class PersistenceService implements ContainerService {
public static final String PERSISTENCE_UNIT_NAME = "PERSISTENCE_UNIT_NAME";
public static final String PERSISTENCE_UNIT_NAME_DEFAULT = "OpenRemotePU";
public static final String DATABASE_PRODUCT = "DATABASE_PRODUCT";
- public static final String DATABASE_PRODUCT_DEFAULT = Database.Product.MYSQL.name();
+ public static final String DATABASE_PRODUCT_DEFAULT = Database.Product.POSTGRES.name();
public static final String DATABASE_CONNECTION_URL = "DATABASE_CONNECTION_URL";
- public static final String DATABASE_CONNECTION_URL_DEFAULT = "jdbc:mysql://192.168.99.100:8082/openremote?useSSL=false";
+ public static final String DATABASE_CONNECTION_URL_DEFAULT = "jdbc:postgresql://192.168.99.100:8082/openremote";
public static final String DATABASE_USERNAME = "DATABASE_USERNAME";
public static final String DATABASE_USERNAME_DEFAULT = "openremote";
public static final String DATABASE_PASSWORD = "DATABASE_PASSWORD";
diff --git a/container/src/main/java/org/openremote/container/persistence/package-info.java b/container/src/main/java/org/openremote/container/persistence/package-info.java
index cfbb32b66..e4f2364fb 100644
--- a/container/src/main/java/org/openremote/container/persistence/package-info.java
+++ b/container/src/main/java/org/openremote/container/persistence/package-info.java
@@ -20,12 +20,17 @@
@org.hibernate.annotations.TypeDefs({
@org.hibernate.annotations.TypeDef(
- name = "json",
- typeClass = JsonStringType.class
+ name = Constants.PERSISTENCE_JSON_OBJECT_TYPE,
+ typeClass = ElementalJsonObjectType.class
+ ),
+ @org.hibernate.annotations.TypeDef(
+ name = Constants.PERSISTENCE_JSON_ARRAY_TYPE,
+ typeClass = ElementalJsonArrayType.class
)
})
package org.openremote.container.persistence;
import org.openremote.container.Constants;
-import org.openremote.container.json.JsonStringType;
+import org.openremote.container.json.ElementalJsonArrayType;
+import org.openremote.container.json.ElementalJsonObjectType;
diff --git a/docker-compose.yml b/docker-compose.yml
index e1bb14199..a7078ce14 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -8,24 +8,27 @@ services:
volumes:
- ./keycloak/theme:/opt/jboss/keycloak/themes/openremote
links:
- - mysql_dev:mysql
+ - postgres_dev:postgresql
environment:
TZ: "Europe/Zurich"
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: admin
KEYCLOAK_LOGLEVEL: DEBUG
- MYSQL_DATABASE: openremote
- MYSQL_USERNAME: openremote
- MYSQL_PASSWORD: secret
+ POSTGRES_DATABASE: openremote
+ POSTGRES_USER: openremote
+ POSTGRES_PASSWORD: secret
ports:
- "8081:8080"
- mysql_dev:
- image: mysql:5.7
+ postgres_dev:
+ build:
+ context: ./postgres
+ dockerfile: Dockerfile-dev
environment:
TZ: "Europe/Zurich"
- MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
- MYSQL_DATABASE: openremote
- MYSQL_USER: openremote
- MYSQL_PASSWORD: secret
+ # This is the database superuser
+ POSTGRES_USER: postgres
+ POSTGRES_PASSWORD: secret
+ # Create default database when started (also see init.sh files)
+ POSTGRES_DB: openremote
ports:
- - "8082:3306"
+ - "8082:5432"
diff --git a/gradle.properties b/gradle.properties
index 828dbaf5e..39b3bb4d3 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -24,7 +24,7 @@ groovyVersion = 2.4.1
sqliteVersion = 3.8.11.2
hibernateVersion = 5.0.7.Final
hibernateValidatorVersion = 5.2.3.Final
-mariadbConnectorVersion = 1.4.6
+postgresJdbcVersion = 9.4.1209
hikaricpVersion = 2.4.3
junitVersion = 4.12
spockVersion = 1.0-groovy-2.4
diff --git a/keycloak/Dockerfile-demo b/keycloak/Dockerfile-demo
index d7ff98f30..c0807dec5 100644
--- a/keycloak/Dockerfile-demo
+++ b/keycloak/Dockerfile-demo
@@ -2,8 +2,8 @@ FROM jboss/keycloak:2.1.0.Final
ADD changeDatabase.xsl /opt/jboss/keycloak/
RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone.xml -xsl:/opt/jboss/keycloak/changeDatabase.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone.xml; java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone-ha.xml -xsl:/opt/jboss/keycloak/changeDatabase.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone-ha.xml; rm /opt/jboss/keycloak/changeDatabase.xsl
-RUN mkdir -p /opt/jboss/keycloak/modules/system/layers/base/com/mysql/jdbc/main; curl -o /opt/jboss/keycloak/modules/system/layers/base/com/mysql/jdbc/main/mysql-connector-java.jar http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar
-ADD module.xml /opt/jboss/keycloak/modules/system/layers/base/com/mysql/jdbc/main/
+RUN mkdir -p /opt/jboss/keycloak/modules/system/layers/base/org/postgresql/jdbc/main; curl -o /opt/jboss/keycloak/modules/system/layers/base/org/postgresql/jdbc/main/postgresql-jdbc.jar http://repo1.maven.org/maven2/org/postgresql/postgresql/9.4.1209/postgresql-9.4.1209.jar
+ADD module.xml /opt/jboss/keycloak/modules/system/layers/base/org/postgresql/jdbc/main/
ADD setProxyForwarding.xsl /opt/jboss/keycloak/
RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone.xml -xsl:/opt/jboss/keycloak/setProxyForwarding.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone.xml
diff --git a/keycloak/Dockerfile-dev b/keycloak/Dockerfile-dev
index 434008a12..7c97762a3 100644
--- a/keycloak/Dockerfile-dev
+++ b/keycloak/Dockerfile-dev
@@ -2,8 +2,8 @@ FROM jboss/keycloak:2.1.0.Final
ADD changeDatabase.xsl /opt/jboss/keycloak/
RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone.xml -xsl:/opt/jboss/keycloak/changeDatabase.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone.xml; java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone-ha.xml -xsl:/opt/jboss/keycloak/changeDatabase.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone-ha.xml; rm /opt/jboss/keycloak/changeDatabase.xsl
-RUN mkdir -p /opt/jboss/keycloak/modules/system/layers/base/org/mariadb/jdbc/main; curl -o /opt/jboss/keycloak/modules/system/layers/base/org/mariadb/jdbc/main/mariadb-java-client.jar http://central.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/1.4.6/mariadb-java-client-1.4.6.jar
-ADD module.xml /opt/jboss/keycloak/modules/system/layers/base/org/mariadb/jdbc/main/
+RUN mkdir -p /opt/jboss/keycloak/modules/system/layers/base/org/postgresql/jdbc/main; curl -o /opt/jboss/keycloak/modules/system/layers/base/org/postgresql/jdbc/main/postgresql-jdbc.jar http://repo1.maven.org/maven2/org/postgresql/postgresql/9.4.1209/postgresql-9.4.1209.jar
+ADD module.xml /opt/jboss/keycloak/modules/system/layers/base/org/postgresql/jdbc/main/
ADD setProxyForwarding.xsl /opt/jboss/keycloak/
RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone.xml -xsl:/opt/jboss/keycloak/setProxyForwarding.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone.xml
diff --git a/keycloak/changeDatabase.xsl b/keycloak/changeDatabase.xsl
index e2fc23ad6..f7fffa79a 100644
--- a/keycloak/changeDatabase.xsl
+++ b/keycloak/changeDatabase.xsl
@@ -7,12 +7,12 @@
<xsl:output method="xml" indent="yes"/>
<xsl:template match="//ds:subsystem/ds:datasources/ds:datasource[@jndi-name='java:jboss/datasources/KeycloakDS']">
- <ds:datasource jndi-name="java:jboss/datasources/KeycloakDS" pool-name="KeycloakDS" enabled="true" use-java-context="true" use-ccm="true">
- <ds:connection-url>jdbc:mysql://mysql:3306/${env.MYSQL_DATABASE:keycloak}?useSSL=false</ds:connection-url>
- <ds:driver>mariadb</ds:driver>
+ <ds:datasource jndi-name="java:jboss/datasources/KeycloakDS" enabled="true" use-java-context="true" pool-name="KeycloakDS" use-ccm="true">
+ <ds:connection-url>jdbc:postgresql://postgresql:5432/${env.POSTGRES_DATABASE:keycloak}</ds:connection-url>
+ <ds:driver>postgresql</ds:driver>
<ds:security>
- <ds:user-name>${env.MYSQL_USERNAME:keycloak}</ds:user-name>
- <ds:password>${env.MYSQL_PASSWORD:password}</ds:password>
+ <ds:user-name>${env.POSTGRES_USER:keycloak}</ds:user-name>
+ <ds:password>${env.POSTGRES_PASSWORD:password}</ds:password>
</ds:security>
<ds:validation>
<ds:check-valid-connection-sql>SELECT 1</ds:check-valid-connection-sql>
@@ -28,8 +28,8 @@
<xsl:template match="//ds:subsystem/ds:datasources/ds:drivers">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
- <ds:driver name="mariadb" module="org.mariadb.jdbc">
- <ds:xa-datasource-class>org.mariadb.jdbc.MariaDbDataSource</ds:xa-datasource-class>
+ <ds:driver name="postgresql" module="org.postgresql.jdbc">
+ <ds:xa-datasource-class>org.postgresql.xa.PGXADataSource</ds:xa-datasource-class>
</ds:driver>
</xsl:copy>
</xsl:template>
diff --git a/keycloak/module.xml b/keycloak/module.xml
index 8d50a33b8..f61ff5166 100644
--- a/keycloak/module.xml
+++ b/keycloak/module.xml
@@ -20,9 +20,9 @@
~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
-<module xmlns="urn:jboss:module:1.0" name="org.mariadb.jdbc">
+<module xmlns="urn:jboss:module:1.0" name="org.postgresql.jdbc">
<resources>
- <resource-root path="mariadb-java-client.jar"/>
+ <resource-root path="postgresql-jdbc.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
diff --git a/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java b/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java
index f4623c7a1..7326ffe6f 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java
@@ -255,16 +255,27 @@ public class SampleDataService implements ContainerService {
});
// TODO Remove tests
-/*
- List hosts = em.createNativeQuery(
- "SELECT a.CONNECTOR_SETTINGS -> \"$.host\" " +
- "FROM AGENT a " +
- "WHERE JSON_EXTRACT(a.CONNECTOR_SETTINGS, \"$.port.value\") = 8080")
- .getResultList();
-
- for (Object host : hosts) {
- LOG.info("### GOT: " + host);
- }
-*/
+ persistenceService.doTransaction(em -> {
+ List<String> hosts = em.createNativeQuery(
+ "SELECT text(a.CONNECTOR_SETTINGS #> '{host,value}') FROM AGENT a " +
+ "WHERE a.CONNECTOR_SETTINGS #> '{port,value}' = '8080'"
+ ).getResultList();
+
+ if (hosts.size() == 0) {
+ throw new RuntimeException("Text query failed! TODO Remove this test...");
+ }
+
+ List<Agent> agents = em.createNativeQuery(
+ "SELECT a.* FROM AGENT a " +
+ "WHERE a.CONNECTOR_SETTINGS #> '{port,value}' = '8080'",
+ Agent.class
+ ).getResultList();
+
+ if (agents.size() == 0) {
+ throw new RuntimeException("Text query failed! TODO Remove this test...");
+ }
+
+ });
+
}
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/Constants.java b/manager/shared/src/main/java/org/openremote/manager/shared/Constants.java
index d4a598d83..cd9fe05b1 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/Constants.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/Constants.java
@@ -27,4 +27,6 @@ public interface Constants {
String MASTER_REALM_ADMIN_USER = "admin";
String PERSISTENCE_SEQUENCE_ID_GENERATOR = "SEQUENCE_ID_GENERATOR";
String PERSISTENCE_UNIQUE_ID_GENERATOR = "UNIQUE_ID_GENERATOR";
+ String PERSISTENCE_JSON_OBJECT_TYPE = "json_object";
+ String PERSISTENCE_JSON_ARRAY_TYPE = "json_array";
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/agent/Agent.java b/manager/shared/src/main/java/org/openremote/manager/shared/agent/Agent.java
index 6798cd3c2..e219707e6 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/agent/Agent.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/agent/Agent.java
@@ -25,6 +25,7 @@ import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
+import static org.openremote.manager.shared.Constants.PERSISTENCE_JSON_OBJECT_TYPE;
import static org.openremote.manager.shared.Constants.PERSISTENCE_UNIQUE_ID_GENERATOR;
@Entity
@@ -64,8 +65,8 @@ public class Agent {
@Column(name = "CONNECTOR_TYPE")
protected String connectorType = NO_CONNECTOR_ASSIGNED_TYPE;
- @Column(name = "CONNECTOR_SETTINGS", columnDefinition = "json")
- @org.hibernate.annotations.Type(type = "json")
+ @Column(name = "CONNECTOR_SETTINGS", columnDefinition = "jsonb")
+ @org.hibernate.annotations.Type(type = PERSISTENCE_JSON_OBJECT_TYPE)
protected JsonObject connectorSettings;
public Agent() {
diff --git a/postgres/01-init-user.sh b/postgres/01-init-user.sh
new file mode 100644
index 000000000..dc25ce2f3
--- /dev/null
+++ b/postgres/01-init-user.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+set -e
+psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
+ CREATE USER openremote PASSWORD 'secret';
+ GRANT ALL PRIVILEGES ON DATABASE openremote TO openremote;
+EOSQL
diff --git a/postgres/Dockerfile-demo b/postgres/Dockerfile-demo
new file mode 100644
index 000000000..ba068cfaf
--- /dev/null
+++ b/postgres/Dockerfile-demo
@@ -0,0 +1,3 @@
+FROM postgres:9.5
+
+ADD 01-init-db.sh /docker-entrypoint-initdb.d/01-init-db.sh
diff --git a/postgres/Dockerfile-dev b/postgres/Dockerfile-dev
new file mode 100644
index 000000000..ba068cfaf
--- /dev/null
+++ b/postgres/Dockerfile-dev
@@ -0,0 +1,3 @@
+FROM postgres:9.5
+
+ADD 01-init-db.sh /docker-entrypoint-initdb.d/01-init-db.sh
diff --git a/postgres/build.gradle b/postgres/build.gradle
new file mode 100644
index 000000000..7a12d4563
--- /dev/null
+++ b/postgres/build.gradle
@@ -0,0 +1,12 @@
+import com.bmuschko.gradle.docker.tasks.image.*
+
+task buildImage(type: DockerBuildImage) {
+ inputDir = projectDir
+ dockerFile = file("$projectDir/Dockerfile-demo")
+ tag = dockerImageName
+}
+
+task pushImage(type: DockerPushImage) {
+ dependsOn buildImage
+ imageName = dockerImageName
+}
\ No newline at end of file