Repository: metamodel
Updated Branches:
  refs/heads/master 57e48be23 -> 37d72a8f9


METAMODEL-1151: Added DataContextFactory for Cassandra

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

Branch: refs/heads/master
Commit: f64a3726dfb5d3d1af1d63b1381f8a36254c0da8
Parents: 2fe6a2b
Author: Kasper Sørensen <i.am.kasper.soren...@gmail.com>
Authored: Sat Aug 5 22:41:58 2017 -0700
Committer: Kasper Sørensen <i.am.kasper.soren...@gmail.com>
Committed: Sat Aug 5 22:41:58 2017 -0700

----------------------------------------------------------------------
 .../cassandra/CassandraDataContextFactory.java  | 75 ++++++++++++++++++++
 ....apache.metamodel.factory.DataContextFactory |  1 +
 2 files changed, 76 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/metamodel/blob/f64a3726/cassandra/src/main/java/org/apache/metamodel/cassandra/CassandraDataContextFactory.java
----------------------------------------------------------------------
diff --git 
a/cassandra/src/main/java/org/apache/metamodel/cassandra/CassandraDataContextFactory.java
 
b/cassandra/src/main/java/org/apache/metamodel/cassandra/CassandraDataContextFactory.java
new file mode 100644
index 0000000..482c912
--- /dev/null
+++ 
b/cassandra/src/main/java/org/apache/metamodel/cassandra/CassandraDataContextFactory.java
@@ -0,0 +1,75 @@
+/**
+ * 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.metamodel.cassandra;
+
+import java.util.Map;
+
+import org.apache.metamodel.ConnectionException;
+import org.apache.metamodel.DataContext;
+import org.apache.metamodel.factory.DataContextFactory;
+import org.apache.metamodel.factory.DataContextProperties;
+import org.apache.metamodel.factory.ResourceFactoryRegistry;
+import org.apache.metamodel.factory.UnsupportedDataContextPropertiesException;
+
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.Cluster.Builder;
+import com.google.common.base.Objects;
+import com.google.common.base.Strings;
+
+public class CassandraDataContextFactory implements DataContextFactory {
+
+    public static final String PROPERTY_TYPE = "cassandra";
+
+    @Override
+    public boolean accepts(DataContextProperties properties, 
ResourceFactoryRegistry resourceFactoryRegistry) {
+        return PROPERTY_TYPE.equals(properties.getDataContextType());
+    }
+
+    @Override
+    public DataContext create(DataContextProperties properties, 
ResourceFactoryRegistry resourceFactoryRegistry)
+            throws UnsupportedDataContextPropertiesException, 
ConnectionException {
+
+        final Map<String, Object> map = properties.toMap();
+        final Builder clusterBuilder = Cluster.builder();
+
+        final String hostname = properties.getHostname();
+        if (!Strings.isNullOrEmpty(hostname)) {
+            clusterBuilder.addContactPoints(hostname.split(","));
+        }
+
+        if (properties.getPort() != null) {
+            clusterBuilder.withPort(properties.getPort());
+        }
+
+        if (map.containsKey("cluster-name")) {
+            clusterBuilder.withClusterName((String) map.get("cluster-name"));
+        }
+
+        if (properties.getUsername() != null && properties.getPassword() != 
null) {
+            clusterBuilder.withCredentials(properties.getUsername(), 
properties.getPassword());
+        }
+
+        final Cluster cluster = clusterBuilder.build();
+
+        final String keySpace = Objects.firstNonNull((String) 
map.get("keyspace"), properties.getDatabaseName());
+
+        return new CassandraDataContext(cluster, keySpace, 
properties.getTableDefs());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/metamodel/blob/f64a3726/cassandra/src/main/resources/META-INF/services/org.apache.metamodel.factory.DataContextFactory
----------------------------------------------------------------------
diff --git 
a/cassandra/src/main/resources/META-INF/services/org.apache.metamodel.factory.DataContextFactory
 
b/cassandra/src/main/resources/META-INF/services/org.apache.metamodel.factory.DataContextFactory
new file mode 100644
index 0000000..270957f
--- /dev/null
+++ 
b/cassandra/src/main/resources/META-INF/services/org.apache.metamodel.factory.DataContextFactory
@@ -0,0 +1 @@
+org.apache.metamodel.cassandra.CassandraDataContextFactory
\ No newline at end of file

Reply via email to