qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kw...@apache.org
Subject [2/2] qpid-broker-j git commit: QPID-8090: [Broker-J] Support all available BoneCP configuration settings
Date Thu, 01 Feb 2018 17:39:02 GMT
QPID-8090: [Broker-J] Support all available BoneCP configuration settings


Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/7384d4db
Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/7384d4db
Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/7384d4db

Branch: refs/heads/7.0.x
Commit: 7384d4dbcbc7c4c9f8cf5173d7dd6ebcff7ad2d3
Parents: 0d6b089
Author: Alex Rudyy <orudyy@apache.org>
Authored: Wed Jan 31 18:46:37 2018 +0000
Committer: Keith Wall <kwall@apache.org>
Committed: Thu Feb 1 17:38:51 2018 +0000

----------------------------------------------------------------------
 .../jdbc/bonecp/BoneCPConnectionProvider.java   | 58 ++++++++++-------
 .../bonecp/BoneCPConnectionProviderFactory.java | 34 ++++++++--
 .../BoneCPConnectionProviderFactoryTest.java    | 43 +++++++++++++
 .../bonecp/BoneCPConnectionProviderTest.java    | 65 ++++++++++++++++++++
 4 files changed, 172 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/7384d4db/broker-plugins/jdbc-provider-bone/src/main/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProvider.java
----------------------------------------------------------------------
diff --git a/broker-plugins/jdbc-provider-bone/src/main/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProvider.java
b/broker-plugins/jdbc-provider-bone/src/main/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProvider.java
index 2687a32..3ee6881 100644
--- a/broker-plugins/jdbc-provider-bone/src/main/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProvider.java
+++ b/broker-plugins/jdbc-provider-bone/src/main/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProvider.java
@@ -20,17 +20,23 @@
  */
 package org.apache.qpid.server.store.jdbc.bonecp;
 
+import static org.apache.qpid.server.store.jdbc.bonecp.BoneCPConnectionProviderFactory.JDBCSTORE_PREFIX;
 import static org.apache.qpid.server.store.jdbc.bonecp.BoneCPConnectionProviderFactory.MAX_CONNECTIONS_PER_PARTITION;
 import static org.apache.qpid.server.store.jdbc.bonecp.BoneCPConnectionProviderFactory.MIN_CONNECTIONS_PER_PARTITION;
 import static org.apache.qpid.server.store.jdbc.bonecp.BoneCPConnectionProviderFactory.PARTITION_COUNT;
 
-import com.jolbox.bonecp.BoneCP;
-import com.jolbox.bonecp.BoneCPConfig;
-import org.apache.qpid.server.store.jdbc.ConnectionProvider;
-
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.HashMap;
 import java.util.Map;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+import com.jolbox.bonecp.BoneCP;
+import com.jolbox.bonecp.BoneCPConfig;
+
+import org.apache.qpid.server.store.jdbc.ConnectionProvider;
+import org.apache.qpid.server.util.ServerScopedRuntimeException;
 
 public class BoneCPConnectionProvider implements ConnectionProvider
 {
@@ -42,6 +48,14 @@ public class BoneCPConnectionProvider implements ConnectionProvider
 
     public BoneCPConnectionProvider(String connectionUrl, String username, String password,
Map<String, String> providerAttributes) throws SQLException
     {
+        _connectionPool = new BoneCP(createBoneCPConfig(connectionUrl, username, password,
providerAttributes));
+    }
+
+    static BoneCPConfig createBoneCPConfig(final String connectionUrl,
+                                           final String username,
+                                           final String password,
+                                           final Map<String, String> providerAttributes)
+    {
         BoneCPConfig config = new BoneCPConfig();
         config.setJdbcUrl(connectionUrl);
         if (username != null)
@@ -50,31 +64,31 @@ public class BoneCPConnectionProvider implements ConnectionProvider
             config.setPassword(password);
         }
 
-        config.setMinConnectionsPerPartition(convertToIntWithDefault(MIN_CONNECTIONS_PER_PARTITION,
providerAttributes, DEFAULT_MIN_CONNECTIONS_PER_PARTITION));
-        config.setMaxConnectionsPerPartition(convertToIntWithDefault(MAX_CONNECTIONS_PER_PARTITION,
providerAttributes, DEFAULT_MAX_CONNECTIONS_PER_PARTITION));
-        config.setPartitionCount(convertToIntWithDefault(PARTITION_COUNT, providerAttributes,
DEFAULT_PARTITION_COUNT));
+        Map<String, String> attributes = new HashMap<>(providerAttributes);
+        attributes.putIfAbsent(MIN_CONNECTIONS_PER_PARTITION, String.valueOf(DEFAULT_MIN_CONNECTIONS_PER_PARTITION));
+        attributes.putIfAbsent(MAX_CONNECTIONS_PER_PARTITION, String.valueOf(DEFAULT_MAX_CONNECTIONS_PER_PARTITION));
+        attributes.putIfAbsent(PARTITION_COUNT, String.valueOf(DEFAULT_PARTITION_COUNT));
 
-        _connectionPool = new BoneCP(config);
-    }
+        Map<String, String> propertiesMap =
+                attributes.entrySet()
+                          .stream()
+                          .collect(Collectors.toMap(p -> p.getKey().substring(JDBCSTORE_PREFIX.length()),
+                                                    Map.Entry::getValue));
 
-    private int convertToIntWithDefault(String key, Map<String, String> context, int
defaultValue)
-    {
-        if (context.containsKey(key))
+        Properties properties = new Properties();
+        properties.putAll(propertiesMap);
+
+        try
         {
-            try
-            {
-                return Integer.parseInt(context.get(key));
-            }
-            catch (NumberFormatException e)
-            {
-               return defaultValue;
-            }
+            config.setProperties(properties);
         }
-        else
+        catch (Exception e)
         {
-            return defaultValue;
+            throw new ServerScopedRuntimeException("Unexpected exception on applying BoneCP
configuration", e);
         }
+        return config;
     }
+
     @Override
     public Connection getConnection() throws SQLException
     {

http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/7384d4db/broker-plugins/jdbc-provider-bone/src/main/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderFactory.java
----------------------------------------------------------------------
diff --git a/broker-plugins/jdbc-provider-bone/src/main/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderFactory.java
b/broker-plugins/jdbc-provider-bone/src/main/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderFactory.java
index 3f668e9..9040fb4 100644
--- a/broker-plugins/jdbc-provider-bone/src/main/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderFactory.java
+++ b/broker-plugins/jdbc-provider-bone/src/main/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderFactory.java
@@ -20,13 +20,16 @@
  */
 package org.apache.qpid.server.store.jdbc.bonecp;
 
-import static java.util.Arrays.asList;
 import static java.util.Collections.unmodifiableSet;
 
+import java.lang.reflect.Modifier;
 import java.sql.SQLException;
-import java.util.HashSet;
+import java.util.Arrays;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
+
+import com.jolbox.bonecp.BoneCPConfig;
 
 import org.apache.qpid.server.plugin.PluggableService;
 import org.apache.qpid.server.store.jdbc.ConnectionProvider;
@@ -35,11 +38,30 @@ import org.apache.qpid.server.store.jdbc.JDBCConnectionProviderFactory;
 @PluggableService
 public class BoneCPConnectionProviderFactory implements JDBCConnectionProviderFactory
 {
-    public static final String PARTITION_COUNT = "qpid.jdbcstore.bonecp.partitionCount";
-    public static final String MAX_CONNECTIONS_PER_PARTITION = "qpid.jdbcstore.bonecp.maxConnectionsPerPartition";
-    public static final String MIN_CONNECTIONS_PER_PARTITION = "qpid.jdbcstore.bonecp.minConnectionsPerPartition";
+    static final String JDBCSTORE_PREFIX = "qpid.jdbcstore.";
+    static final String BONECP_SETTING_PREFIX = JDBCSTORE_PREFIX + "bonecp.";
+    static final String PARTITION_COUNT = BONECP_SETTING_PREFIX + "partitionCount";
+    static final String MAX_CONNECTIONS_PER_PARTITION = BONECP_SETTING_PREFIX + "maxConnectionsPerPartition";
+    static final String MIN_CONNECTIONS_PER_PARTITION = BONECP_SETTING_PREFIX + "minConnectionsPerPartition";
+
+    private final Set<String> _supportedAttributes;
 
-    private final Set<String> _supportedAttributes = unmodifiableSet(new HashSet<String>(asList(PARTITION_COUNT,
MAX_CONNECTIONS_PER_PARTITION, MIN_CONNECTIONS_PER_PARTITION)));
+    public BoneCPConnectionProviderFactory()
+    {
+        Set<String> names = Arrays.stream(BoneCPConfig.class.getMethods())
+                                  .filter(m -> m.getName().startsWith("set")
+                                               && m.getName().length() > 3
+                                               && Modifier.isPublic(m.getModifiers())
+                                               && m.getParameterCount() == 1
+                                               && (m.getParameterTypes()[0].isPrimitive()
+                                                   || m.getParameterTypes()[0] == String.class))
+                                  .map(m -> {
+                                      String n = m.getName().substring(3);
+                                      n = BONECP_SETTING_PREFIX + Character.toLowerCase(n.charAt(0))
+ n.substring(1);
+                                      return n;
+                                  }).collect(Collectors.toSet());
+        _supportedAttributes = unmodifiableSet(names);
+    }
 
     @Override
     public String getType()

http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/7384d4db/broker-plugins/jdbc-provider-bone/src/test/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderFactoryTest.java
----------------------------------------------------------------------
diff --git a/broker-plugins/jdbc-provider-bone/src/test/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderFactoryTest.java
b/broker-plugins/jdbc-provider-bone/src/test/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderFactoryTest.java
new file mode 100644
index 0000000..f250f7c
--- /dev/null
+++ b/broker-plugins/jdbc-provider-bone/src/test/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderFactoryTest.java
@@ -0,0 +1,43 @@
+/*
+ *
+ * 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.qpid.server.store.jdbc.bonecp;
+
+import static org.apache.qpid.server.store.jdbc.bonecp.BoneCPConnectionProviderFactory.MAX_CONNECTIONS_PER_PARTITION;
+import static org.apache.qpid.server.store.jdbc.bonecp.BoneCPConnectionProviderFactory.MIN_CONNECTIONS_PER_PARTITION;
+import static org.apache.qpid.server.store.jdbc.bonecp.BoneCPConnectionProviderFactory.PARTITION_COUNT;
+
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+public class BoneCPConnectionProviderFactoryTest extends TestCase
+{
+    public void testGetProviderAttributeNames() throws Exception
+    {
+        BoneCPConnectionProviderFactory factory = new BoneCPConnectionProviderFactory();
+        Set<String> supported = factory.getProviderAttributeNames();
+        assertFalse("Supported settings cannot be empty", supported.isEmpty());
+
+        assertTrue("partitionCount is not found", supported.contains(PARTITION_COUNT));
+        assertTrue("maxConnectionsPerPartition is not found", supported.contains(MAX_CONNECTIONS_PER_PARTITION));
+        assertTrue("minConnectionsPerPartition is not found",supported.contains(MIN_CONNECTIONS_PER_PARTITION));
+    }
+}

http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/7384d4db/broker-plugins/jdbc-provider-bone/src/test/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderTest.java
----------------------------------------------------------------------
diff --git a/broker-plugins/jdbc-provider-bone/src/test/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderTest.java
b/broker-plugins/jdbc-provider-bone/src/test/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderTest.java
new file mode 100644
index 0000000..6669974
--- /dev/null
+++ b/broker-plugins/jdbc-provider-bone/src/test/java/org/apache/qpid/server/store/jdbc/bonecp/BoneCPConnectionProviderTest.java
@@ -0,0 +1,65 @@
+/*
+ *
+ * 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.qpid.server.store.jdbc.bonecp;
+
+import static org.apache.qpid.server.store.jdbc.bonecp.BoneCPConnectionProvider.DEFAULT_MAX_CONNECTIONS_PER_PARTITION;
+import static org.apache.qpid.server.store.jdbc.bonecp.BoneCPConnectionProvider.DEFAULT_MIN_CONNECTIONS_PER_PARTITION;
+import static org.apache.qpid.server.store.jdbc.bonecp.BoneCPConnectionProvider.DEFAULT_PARTITION_COUNT;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.jolbox.bonecp.BoneCPConfig;
+import junit.framework.TestCase;
+
+public class BoneCPConnectionProviderTest extends TestCase
+{
+    public void testCreateBoneCPConfig() throws Exception
+    {
+
+        final Map<String, String> attributes = new HashMap<>();
+        attributes.put("qpid.jdbcstore.bonecp.idleMaxAgeInMinutes", "123");
+        attributes.put("qpid.jdbcstore.bonecp.connectionTimeoutInMs", "1234");
+        attributes.put("qpid.jdbcstore.bonecp.connectionTestStatement", "select 1");
+        attributes.put("qpid.jdbcstore.bonecp.logStatementsEnabled", "true");
+        attributes.put("qpid.jdbcstore.bonecp.partitionCount", "12");
+
+        String connectionUrl = "jdbc:mariadb://localhost:3306/test";
+        String username = "usr";
+        String password = "pwd";
+        BoneCPConfig config =
+                BoneCPConnectionProvider.createBoneCPConfig(connectionUrl, username, password,
attributes);
+        assertEquals(connectionUrl, config.getJdbcUrl());
+        assertEquals(username, config.getUsername());
+        assertEquals(password, config.getPassword());
+        assertEquals("Unexpected idleMaxAgeInMinutes", 123, config.getIdleMaxAgeInMinutes());
+        assertEquals("Unexpected connectionTimeout", 1234, config.getConnectionTimeoutInMs());
+        assertEquals("Unexpected connectionTestStatement", "select 1", config.getConnectionTestStatement());
+        assertEquals("Unexpected logStatementsEnabled", true, config.isLogStatementsEnabled());
+        assertEquals("Unexpected maxConnectionsPerPartition",
+                     DEFAULT_MAX_CONNECTIONS_PER_PARTITION,
+                     config.getMaxConnectionsPerPartition());
+        assertEquals("Unexpected minConnectionsPerPartition",
+                     DEFAULT_MIN_CONNECTIONS_PER_PARTITION,
+                     config.getMinConnectionsPerPartition());
+        assertEquals("Unexpected partitionCount", 12, config.getPartitionCount());
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message