This is an automated email from the ASF dual-hosted git repository.

radcortez pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git


The following commit(s) were added to refs/heads/master by this push:
     new c69eec4  TOMEE-2462 - MicroProfile Configuration ConfigSource Example.
c69eec4 is described below

commit c69eec4917c897292ea5dd890bde1527c373dd0c
Author: Roberto Cortez <radcor...@yahoo.com>
AuthorDate: Wed Jan 30 15:45:55 2019 +0000

    TOMEE-2462 - MicroProfile Configuration ConfigSource Example.
---
 examples/mp-config-source-database/README.adoc     |  14 ++
 examples/mp-config-source-database/pom.xml         | 144 +++++++++++++++++++++
 .../config/source/database/ApplicationBean.java    |  47 +++++++
 .../source/database/DatabaseConfigSource.java      |  93 +++++++++++++
 ...rg.eclipse.microprofile.config.spi.ConfigSource |  17 +++
 .../src/main/tomee/conf/tomee.xml                  |  21 +++
 .../tomee/lib/import-config-source-database.sql    |  19 +++
 .../source/database/DatabaseConfigSourceTest.java  |  55 ++++++++
 .../src/test/resources/arquillian.xml              |  37 ++++++
 examples/pom.xml                                   |   1 +
 10 files changed, 448 insertions(+)

diff --git a/examples/mp-config-source-database/README.adoc 
b/examples/mp-config-source-database/README.adoc
new file mode 100644
index 0000000..98db897
--- /dev/null
+++ b/examples/mp-config-source-database/README.adoc
@@ -0,0 +1,14 @@
+= MicroProfile Configuration ConfigSource Database
+:index-group: MicroProfile
+:jbake-type: page
+:jbake-status: published
+
+This is an example on how to implement a custom MicroProfile Configuration 
ConfigSource. The custom ConfigSource is
+going to read configuration values from a Database.
+
+== Run the application:
+
+[source,bash]
+----
+mvn clean install tomee:run
+----
diff --git a/examples/mp-config-source-database/pom.xml 
b/examples/mp-config-source-database/pom.xml
new file mode 100644
index 0000000..2528f82
--- /dev/null
+++ b/examples/mp-config-source-database/pom.xml
@@ -0,0 +1,144 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one or more
+  ~ contributor license agreements.  See the NOTICE file distributed with
+  ~ this work for additional information regarding copyright ownership.
+  ~ The ASF licenses this file to You under the Apache License, Version 2.0
+  ~ (the "License"); you may not use this file except in compliance with
+  ~ the License.  You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<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>
+
+  <groupId>org.superbiz</groupId>
+  <artifactId>mp-config-source-database</artifactId>
+  <version>8.0.0-SNAPSHOT</version>
+  <packaging>war</packaging>
+
+  <name>MicroProfile :: Examples :: Config Source Database</name>
+
+  <properties>
+    <version.javaee-api>8.0</version.javaee-api>
+    <version.microprofile>2.0.1</version.microprofile>
+    <version.arquillian>1.1.13.Final</version.arquillian>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.tomee</groupId>
+      <artifactId>javaee-api</artifactId>
+      <version>${version.javaee-api}</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.eclipse.microprofile</groupId>
+      <artifactId>microprofile</artifactId>
+      <version>${version.microprofile}</version>
+      <type>pom</type>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>commons-dbutils</groupId>
+      <artifactId>commons-dbutils</artifactId>
+      <version>1.7</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.tomee</groupId>
+      <artifactId>apache-tomee</artifactId>
+      <version>${project.version}</version>
+      <type>zip</type>
+      <classifier>microprofile</classifier>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.jboss.arquillian.junit</groupId>
+      <artifactId>arquillian-junit-container</artifactId>
+      <version>${version.arquillian}</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.tomee</groupId>
+      <artifactId>ziplock</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.jboss.shrinkwrap.resolver</groupId>
+      <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
+      <version>2.1.0</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.tomee</groupId>
+      <artifactId>arquillian-tomee-remote</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.11</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-war-plugin</artifactId>
+        <configuration>
+          <failOnMissingWebXml>false</failOnMissingWebXml>
+        </configuration>
+      </plugin>
+
+      <plugin>
+        <groupId>org.apache.tomee.maven</groupId>
+        <artifactId>tomee-maven-plugin</artifactId>
+        <version>${project.version}</version>
+        <configuration>
+          <tomeeClassifier>microprofile</tomeeClassifier>
+          <context>${project.artifactId}</context>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.8</source>
+          <target>1.8</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+  <!-- This section allows you to configure where to publish libraries for
+  sharing. It is not required and may be deleted. For more information see:
+  http://maven.apache.org/plugins/maven-deploy-plugin/ -->
+  <distributionManagement>
+    <repository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/repo/</url>
+    </repository>
+    <snapshotRepository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/snapshot-repo/</url>
+    </snapshotRepository>
+  </distributionManagement>
+</project>
diff --git 
a/examples/mp-config-source-database/src/main/java/org/superbiz/microprofile/config/source/database/ApplicationBean.java
 
b/examples/mp-config-source-database/src/main/java/org/superbiz/microprofile/config/source/database/ApplicationBean.java
new file mode 100644
index 0000000..88a3ca4
--- /dev/null
+++ 
b/examples/mp-config-source-database/src/main/java/org/superbiz/microprofile/config/source/database/ApplicationBean.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.microprofile.config.source.database;
+
+import org.eclipse.microprofile.config.inject.ConfigProperty;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Initialized;
+import javax.enterprise.event.Observes;
+import javax.inject.Inject;
+
+@ApplicationScoped
+public class ApplicationBean {
+    @Inject
+    @ConfigProperty(name = "application.currency")
+    private String applicationCurrrency;
+    @Inject
+    @ConfigProperty(name = "application.country")
+    private String applicationCountry;
+
+    public void init(@Observes @Initialized(ApplicationScoped.class) final 
Object init) {
+        System.out.println("applicationCurrrency = " + applicationCurrrency);
+        System.out.println("applicationCountry = " + applicationCountry);
+    }
+
+    public String getApplicationCurrrency() {
+        return applicationCurrrency;
+    }
+
+    public String getApplicationCountry() {
+        return applicationCountry;
+    }
+}
diff --git 
a/examples/mp-config-source-database/src/main/java/org/superbiz/microprofile/config/source/database/DatabaseConfigSource.java
 
b/examples/mp-config-source-database/src/main/java/org/superbiz/microprofile/config/source/database/DatabaseConfigSource.java
new file mode 100644
index 0000000..53a0ebe
--- /dev/null
+++ 
b/examples/mp-config-source-database/src/main/java/org/superbiz/microprofile/config/source/database/DatabaseConfigSource.java
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.microprofile.config.source.database;
+
+import org.apache.commons.dbutils.DbUtils;
+import org.eclipse.microprofile.config.spi.ConfigSource;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class DatabaseConfigSource implements ConfigSource {
+    private DataSource dataSource;
+
+    public DatabaseConfigSource() {
+        try {
+            dataSource = (DataSource) new 
InitialContext().lookup("openejb:Resource/config-source-database");
+        } catch (final NamingException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    @Override
+    public Map<String, String> getProperties() {
+        final Map<String, String> properties = new HashMap<>();
+
+        try {
+            final Connection connection = dataSource.getConnection();
+            final PreparedStatement query = 
connection.prepareStatement("SELECT NAME, VALUE FROM CONFIGURATIONS");
+            final ResultSet names = query.executeQuery();
+
+            while (names.next()) {
+                properties.put(names.getString(0), names.getString(1));
+            }
+
+            DbUtils.closeQuietly(names);
+            DbUtils.closeQuietly(query);
+            DbUtils.closeQuietly(connection);
+        } catch (final SQLException e) {
+            e.printStackTrace();
+        }
+
+        return properties;
+    }
+
+    @Override
+    public String getValue(final String propertyName) {
+        try {
+            final Connection connection = dataSource.getConnection();
+            final PreparedStatement query =
+                    connection.prepareStatement("SELECT VALUE FROM 
CONFIGURATIONS WHERE NAME = ?");
+            query.setString(1, propertyName);
+            final ResultSet value = query.executeQuery();
+
+            if (value.next()) {
+                return value.getString(1);
+            }
+
+            DbUtils.closeQuietly(value);
+            DbUtils.closeQuietly(query);
+            DbUtils.closeQuietly(connection);
+        } catch (final SQLException e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
+
+    @Override
+    public String getName() {
+        return DatabaseConfigSource.class.getSimpleName();
+    }
+}
diff --git 
a/examples/mp-config-source-database/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource
 
b/examples/mp-config-source-database/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource
new file mode 100644
index 0000000..5c9d64e
--- /dev/null
+++ 
b/examples/mp-config-source-database/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource
@@ -0,0 +1,17 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+org.superbiz.microprofile.config.source.database.DatabaseConfigSource
diff --git a/examples/mp-config-source-database/src/main/tomee/conf/tomee.xml 
b/examples/mp-config-source-database/src/main/tomee/conf/tomee.xml
new file mode 100644
index 0000000..9841c23
--- /dev/null
+++ b/examples/mp-config-source-database/src/main/tomee/conf/tomee.xml
@@ -0,0 +1,21 @@
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one or more
+  ~ contributor license agreements.  See the NOTICE file distributed with
+  ~ this work for additional information regarding copyright ownership.
+  ~ The ASF licenses this file to You under the Apache License, Version 2.0
+  ~ (the "License"); you may not use this file except in compliance with
+  ~ the License.  You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<tomee>
+  <Resource id="config-source-database" type="DataSource">
+
+  </Resource>
+</tomee>
diff --git 
a/examples/mp-config-source-database/src/main/tomee/lib/import-config-source-database.sql
 
b/examples/mp-config-source-database/src/main/tomee/lib/import-config-source-database.sql
new file mode 100644
index 0000000..0fe45ef
--- /dev/null
+++ 
b/examples/mp-config-source-database/src/main/tomee/lib/import-config-source-database.sql
@@ -0,0 +1,19 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+CREATE TABLE CONFIGURATIONS (NAME VARCHAR (255) NOT NULL PRIMARY KEY, VALUE 
VARCHAR(255) NOT NULL);
+INSERT INTO CONFIGURATIONS(NAME, VALUE) VALUES('application.currency', 'Euro');
+INSERT INTO CONFIGURATIONS(NAME, VALUE) VALUES('application.country', 'PT');
diff --git 
a/examples/mp-config-source-database/src/test/java/org/superbiz/microprofile/config/source/database/DatabaseConfigSourceTest.java
 
b/examples/mp-config-source-database/src/test/java/org/superbiz/microprofile/config/source/database/DatabaseConfigSourceTest.java
new file mode 100644
index 0000000..b22edd8
--- /dev/null
+++ 
b/examples/mp-config-source-database/src/test/java/org/superbiz/microprofile/config/source/database/DatabaseConfigSourceTest.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.microprofile.config.source.database;
+
+import org.apache.ziplock.maven.Mvn;
+import org.eclipse.microprofile.config.Config;
+import org.eclipse.microprofile.config.ConfigProvider;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.inject.Inject;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(Arquillian.class)
+public class DatabaseConfigSourceTest {
+    @Deployment
+    public static WebArchive createDeployment() {
+        return (WebArchive) Mvn.war();
+    }
+
+    @Inject
+    private Config config;
+    @Inject
+    private ApplicationBean applicationBean;
+
+    @Test
+    public void testConfigSourceStatic() throws Exception {
+        assertEquals(config.getValue("application.currency", String.class), 
"Euro");
+        assertEquals(config.getValue("application.country", String.class), 
"PT");
+
+        assertEquals(applicationBean.getApplicationCurrrency(), "Euro");
+        assertEquals(applicationBean.getApplicationCountry(), "PT");
+
+        
assertEquals(ConfigProvider.getConfig().getValue("application.currency", 
String.class), "Euro");
+        
assertEquals(ConfigProvider.getConfig().getValue("application.country", 
String.class), "PT");
+    }
+}
diff --git 
a/examples/mp-config-source-database/src/test/resources/arquillian.xml 
b/examples/mp-config-source-database/src/test/resources/arquillian.xml
new file mode 100644
index 0000000..a90e56e
--- /dev/null
+++ b/examples/mp-config-source-database/src/test/resources/arquillian.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+  <!--
+    ~ Licensed to the Apache Software Foundation (ASF) under one or more
+    ~ contributor license agreements.  See the NOTICE file distributed with
+    ~ this work for additional information regarding copyright ownership.
+    ~ The ASF licenses this file to You under the Apache License, Version 2.0
+    ~ (the "License"); you may not use this file except in compliance with
+    ~ the License.  You may obtain a copy of the License at
+    ~
+    ~     http://www.apache.org/licenses/LICENSE-2.0
+    ~
+    ~ Unless required by applicable law or agreed to in writing, software
+    ~ distributed under the License is distributed on an "AS IS" BASIS,
+    ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    ~ See the License for the specific language governing permissions and
+    ~ limitations under the License.
+    -->
+<arquillian xmlns="http://jboss.org/schema/arquillian";
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+            xsi:schemaLocation="
+              http://jboss.org/schema/arquillian
+              http://jboss.org/schema/arquillian/arquillian_1_0.xsd";>
+<container qualifier="tomee-remote" default="true">
+  <configuration>
+    <property name="debug">false</property>
+    <property name="httpPort">-1</property>
+    <property name="ajpPort">-1</property>
+    <property name="stopPort">-1</property>
+    <property name="classifier">microprofile</property>
+    <property name="conf">src/main/tomee/conf</property>
+    <property name="lib">src/main/tomee/lib</property>
+    <property name="dir">target/tomee</property>
+    <property name="appWorkingDir">target/workdir</property>
+    <property name="cleanOnStartUp">true</property>
+  </configuration>
+</container>
+</arquillian>
diff --git a/examples/pom.xml b/examples/pom.xml
index d195ad5..a13d63d 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -101,6 +101,7 @@
     <module>movies-complete-meta</module>
     <module>movies-complete</module>
     <module>mp-config-example</module>
+    <module>mp-config-source-database</module>
     <module>mp-faulttolerance-fallback</module>
     <module>mp-faulttolerance-retry</module>
     <module>mp-metrics-counted</module>

Reply via email to