You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by st...@apache.org on 2015/05/06 11:13:54 UTC

[13/25] tomee git commit: TOMEE-1575 HerokuDatabasePropertiesProvider

TOMEE-1575 HerokuDatabasePropertiesProvider


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/1767d480
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/1767d480
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/1767d480

Branch: refs/heads/fb_tomee2_owb16
Commit: 1767d4804c05fb9f1579e460e373a9d00ac2cb05
Parents: 9d18256
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Mon May 4 22:44:50 2015 +0200
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Mon May 4 22:44:50 2015 +0200

----------------------------------------------------------------------
 .../HerokuDatabasePropertiesProvider.java       | 61 +++++++++++++
 .../openejb/util/PropertyPlaceHolderHelper.java |  3 +-
 ...uDatabasePropertiesProviderResourceTest.java | 92 ++++++++++++++++++++
 .../HerokuDatabasePropertiesProviderTest.java   | 42 +++++++++
 4 files changed, 197 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/1767d480/container/openejb-core/src/main/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProvider.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProvider.java b/container/openejb-core/src/main/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProvider.java
new file mode 100644
index 0000000..4c95a4a
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProvider.java
@@ -0,0 +1,61 @@
+/*
+ * 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.apache.openejb.resource.heroku;
+
+import org.apache.openejb.api.resource.PropertiesResourceProvider;
+import org.apache.openejb.testng.PropertiesBuilder;
+import org.apache.openejb.util.PropertyPlaceHolderHelper;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+// kind of php to java
+public class HerokuDatabasePropertiesProvider implements PropertiesResourceProvider {
+    private final Map<String, String> jdbcMapping = new HashMap<String, String>() {{
+        put("postgres", "postgresql");
+        put("hsql", "hsqldb:hsql");
+    }};
+
+    @Override
+    public Properties provides() {
+        try {
+            final URI url = new URI(PropertyPlaceHolderHelper.simpleValue("${DATABASE_URL}")); // let it be overridable
+            final String userInfo = url.getUserInfo();
+            final String jdbcUrl =
+                    "jdbc:" +
+                    (jdbcMapping.containsKey(url.getScheme()) ? jdbcMapping.get(url.getScheme()) : url.getScheme()) +
+                    "://" + url.getHost() + (url.getPort() > 0 ? ":" + url.getPort() : "") +
+                    url.getPath();
+            final PropertiesBuilder builder = new PropertiesBuilder().p("JdbcUrl", jdbcUrl);
+            if (userInfo != null) {
+                final int sep = userInfo.indexOf(':');
+                if (sep > 0) {
+                    builder.p("UserName", userInfo.substring(0, sep))
+                            .p("Password", userInfo.substring(sep + 1, userInfo.length()));
+                } else {
+                    builder.p("UserName", userInfo);
+                }
+            }
+            return builder.build();
+        } catch (final URISyntaxException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/1767d480/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java b/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
index adb13aa..b218d82 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
@@ -34,6 +34,7 @@ public final class PropertyPlaceHolderHelper {
 
     private static final PropertiesLookup RESOLVER = new PropertiesLookup();
     public static final StrSubstitutor SUBSTITUTOR = new StrSubstitutor(RESOLVER);
+
     static {
         SUBSTITUTOR.setEnableSubstitutionInVariables(true);
         SUBSTITUTOR.setValueDelimiter(System.getProperty("openejb.placehodler.delimiter", ":-")); // default one of [lang3]
@@ -68,7 +69,7 @@ public final class PropertyPlaceHolderHelper {
     private static String decryptIfNeeded(final String replace) {
         if (replace.startsWith(CIPHER_PREFIX)) {
             final String algo = replace.substring(CIPHER_PREFIX.length(), replace.indexOf(':', CIPHER_PREFIX.length() + 1));
-            PasswordCipher cipher = null;
+            PasswordCipher cipher;
             try {
                 cipher = PasswordCipherFactory.getPasswordCipher(algo);
             } catch (final PasswordCipherException ex) {

http://git-wip-us.apache.org/repos/asf/tomee/blob/1767d480/container/openejb-core/src/test/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProviderResourceTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProviderResourceTest.java b/container/openejb-core/src/test/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProviderResourceTest.java
new file mode 100644
index 0000000..dd20c90
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProviderResourceTest.java
@@ -0,0 +1,92 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.apache.openejb.resource.heroku;
+
+import org.apache.openejb.junit.ApplicationComposerRule;
+import org.apache.openejb.loader.Files;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.ContainerProperties;
+import org.apache.openejb.testing.SimpleLog;
+import org.apache.openejb.util.NetworkUtil;
+import org.hsqldb.Server;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import javax.annotation.Resource;
+import javax.sql.DataSource;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+@Classes
+@ContainerProperties({
+        @ContainerProperties.Property(name = "DATABASE_URL", value = "hsql://SA@localhost:${hsqldb}/adb"),
+        @ContainerProperties.Property(name = "db", value = "new://Resource?type=DataSource&properties-provider=org.apache.openejb.resource.heroku.HerokuDatabasePropertiesProvider")
+})
+@SimpleLog
+public class HerokuDatabasePropertiesProviderResourceTest {
+    @Rule
+    public final TestRule rule = RuleChain
+            .outerRule(new TestRule() {
+                @Override
+                public Statement apply(final Statement base, final Description description) {
+                    return new Statement() {
+                        @Override
+                        public void evaluate() throws Throwable {
+                            final int port = NetworkUtil.getNextAvailablePort();
+                            final Server server = new Server();
+                            server.setAddress("localhost");
+                            server.setPort(port);
+                            server.setDatabaseName(0, "adb");
+                            server.setDatabasePath(0, Files.mkdirs(new File("target/HerokuDatabasePropertiesProviderResourceTest")).getAbsolutePath());
+                            server.start();
+                            System.setProperty("hsqldb", Integer.toString(port));
+                            try {
+                                base.evaluate();
+                            } finally {
+                                server.stop();
+                            }
+                        }
+                    };
+                }
+            })
+            .around(new ApplicationComposerRule(this));
+
+    @Resource(name = "db")
+    private DataSource db;
+
+    @Test
+    public void herokuToJava() throws Exception {
+        assertNotNull(db);
+
+        final Connection connection = db.getConnection();
+        final DatabaseMetaData metaData = connection.getMetaData();
+        final String url = metaData.getURL();
+        assertTrue(url.startsWith("jdbc:hsqldb:hsql://localhost:"));
+        assertTrue(url.endsWith("/adb"));
+        assertEquals("SA", metaData.getUserName());
+        connection.close();
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/1767d480/container/openejb-core/src/test/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProviderTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProviderTest.java b/container/openejb-core/src/test/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProviderTest.java
new file mode 100644
index 0000000..d96dff3
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/resource/heroku/HerokuDatabasePropertiesProviderTest.java
@@ -0,0 +1,42 @@
+/**
+ *
+ * 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.apache.openejb.resource.heroku;
+
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.testng.PropertiesBuilder;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class HerokuDatabasePropertiesProviderTest {
+    @Before
+    @After
+    public void reset() {
+        SystemInstance.reset();
+    }
+
+    @Test
+    public void herokuToJava() {
+        SystemInstance.get().setProperty("DATABASE_URL", "postgres://user:pwd@host.com:5432/db");
+        assertEquals(
+                new PropertiesBuilder().p("Password", "pwd").p("JdbcUrl", "jdbc:postgresql://host.com:5432/db").p("UserName", "user").build(),
+                new HerokuDatabasePropertiesProvider().provides());
+    }
+}