Skip to content

Commit 1be095f

Browse files
authored
CosmosDB implementation Java (#685)
1 parent e9364c2 commit 1be095f

File tree

9 files changed

+1321
-0
lines changed

9 files changed

+1321
-0
lines changed

gxcosmosdb/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.genexus</groupId>
9+
<artifactId>parent</artifactId>
10+
<version>${revision}${changelist}</version>
11+
</parent>
12+
13+
<artifactId>gxcosmosdb</artifactId>
14+
<name>GeneXus CosmosDB</name>
15+
16+
<dependencyManagement>
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.azure</groupId>
20+
<artifactId>azure-cosmos</artifactId>
21+
<version>${software.azure.cosmos.version}</version>
22+
</dependency>
23+
</dependencies>
24+
</dependencyManagement>
25+
<dependencies>
26+
<dependency>
27+
<groupId>com.azure</groupId>
28+
<artifactId>azure-cosmos</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>${project.groupId}</groupId>
32+
<artifactId>gxclassR</artifactId>
33+
<version>${project.version}</version>
34+
</dependency>
35+
</dependencies>
36+
37+
<build>
38+
<finalName>gxcosmosdb</finalName>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-compiler-plugin</artifactId>
43+
<configuration>
44+
<source>8</source>
45+
<target>8</target>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
</project>
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package com.genexus.db.cosmosdb;
2+
3+
import com.genexus.db.service.ServiceConnection;
4+
import com.azure.cosmos.ConsistencyLevel;
5+
import com.azure.cosmos.CosmosAsyncClient;
6+
import com.azure.cosmos.CosmosAsyncContainer;
7+
import com.azure.cosmos.CosmosAsyncDatabase;
8+
import com.azure.cosmos.CosmosClientBuilder;
9+
10+
import org.apache.commons.lang.StringUtils;
11+
12+
import java.sql.ResultSet;
13+
import java.util.Collections;
14+
import java.util.Enumeration;
15+
import java.util.Properties;
16+
import java.util.concurrent.Executor;
17+
18+
public class CosmosDBConnection extends ServiceConnection
19+
{
20+
private static final String GXCOSMOSDB_PRODUCT_NAME = "CosmosDB";
21+
private static final String GXCOSMOSDB_VERSION = "1.0";
22+
private static final String REGION = "applicationregion";
23+
private static final String DATABASE = "database";
24+
private static final String SERVICE_URI = "serviceuri";
25+
private static final String ACCOUNT_KEY = "accountkey";
26+
27+
private String mregion;
28+
private String mdatabase;
29+
private String maccountKey;
30+
private String maccountEndpoint;
31+
CosmosAsyncClient cosmosClient;
32+
CosmosAsyncDatabase cosmosDatabase = null;
33+
public CosmosDBConnection(String connUrl, Properties initialConnProps) throws Exception {
34+
super(connUrl, initialConnProps);
35+
initializeDBConnection(connUrl);
36+
37+
}
38+
private void initializeDBConnection(String connUrl) throws Exception
39+
{
40+
for(Enumeration<Object> keys = props.keys(); keys.hasMoreElements(); )
41+
{
42+
String key = (String)keys.nextElement();
43+
String value = props.getProperty(key, key);
44+
switch(key.toLowerCase())
45+
{
46+
case SERVICE_URI: maccountEndpoint = value.replace("AccountEndpoint=",""); break;
47+
case ACCOUNT_KEY: maccountKey = value; break;
48+
case REGION: mregion = value; break;
49+
case DATABASE: mdatabase = value; break;
50+
default: break;
51+
}
52+
}
53+
if (maccountEndpoint == null)
54+
{
55+
String accountURI = "";
56+
if (connUrl.contains("AccountEndpoint="))
57+
{
58+
int pos1 = connUrl.indexOf("AccountEndpoint=",0);
59+
int pos2 = connUrl.indexOf(";",pos1);
60+
accountURI = connUrl.substring(pos1,pos2);
61+
maccountEndpoint = accountURI != "" ? accountURI.replace("AccountEndpoint=",""):null;
62+
}
63+
}
64+
65+
//Consistency Level: https://learn.microsoft.com/en-us/java/api/com.azure.cosmos.consistencylevel?view=azure-java-stable
66+
67+
if (maccountEndpoint == null || maccountKey == null)
68+
throw(new IllegalArgumentException("Missing required credentials parameters. Enter Host Name and Key."));
69+
70+
if (mdatabase == null || mregion == null)
71+
throw(new IllegalArgumentException("Missing additional connection options. Enter databasename and region."));
72+
73+
cosmosClient = new CosmosClientBuilder()
74+
.endpoint(maccountEndpoint)
75+
.key(maccountKey)
76+
.consistencyLevel(ConsistencyLevel.EVENTUAL)
77+
.contentResponseOnWriteEnabled(true)
78+
.preferredRegions(Collections.singletonList(mregion))
79+
.buildAsyncClient();
80+
81+
cosmosDatabase = cosmosClient.getDatabase(mdatabase);
82+
}
83+
84+
private CosmosAsyncContainer GetContainer(String containerName)
85+
{
86+
if (cosmosDatabase != null && StringUtils.isNotEmpty(containerName))
87+
return cosmosDatabase.getContainer(containerName);
88+
return null;
89+
}
90+
91+
//----------------------------------------------------------------------------------------------------
92+
@Override
93+
public void close()
94+
{
95+
cosmosClient.close();
96+
cosmosClient = null;
97+
}
98+
@Override
99+
public boolean isClosed()
100+
{
101+
return cosmosClient != null;
102+
}
103+
@Override
104+
public String getDatabaseProductName()
105+
{
106+
return GXCOSMOSDB_PRODUCT_NAME;
107+
}
108+
109+
@Override
110+
public String getDatabaseProductVersion()
111+
{
112+
return "";
113+
}
114+
115+
@Override
116+
public String getDriverName()
117+
{
118+
return cosmosClient.getClass().getName();
119+
}
120+
121+
@Override
122+
public String getDriverVersion()
123+
{
124+
return String.format("%s/%s", GXCOSMOSDB_PRODUCT_NAME, GXCOSMOSDB_VERSION);
125+
}
126+
127+
// JDK8:
128+
@Override
129+
public void setSchema(String schema)
130+
{
131+
throw new UnsupportedOperationException("Not supported yet.");
132+
}
133+
134+
@Override
135+
public String getSchema()
136+
{
137+
throw new UnsupportedOperationException("Not supported yet.");
138+
}
139+
140+
@Override
141+
public void abort(Executor executor)
142+
{
143+
throw new UnsupportedOperationException("Not supported yet.");
144+
}
145+
146+
@Override
147+
public void setNetworkTimeout(Executor executor, int milliseconds)
148+
{
149+
throw new UnsupportedOperationException("Not supported yet.");
150+
}
151+
152+
@Override
153+
public int getNetworkTimeout()
154+
{
155+
throw new UnsupportedOperationException("Not supported yet.");
156+
}
157+
158+
@Override
159+
public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
160+
{
161+
throw new UnsupportedOperationException("Not supported yet.");
162+
}
163+
164+
@Override
165+
public boolean generatedKeyAlwaysReturned()
166+
{
167+
throw new UnsupportedOperationException("Not supported yet.");
168+
}
169+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.genexus.db.cosmosdb;
2+
3+
import java.sql.*;
4+
import java.util.Properties;
5+
import java.util.logging.Logger;
6+
public class CosmosDBDriver implements Driver
7+
{
8+
private static final int MAJOR_VERSION = 1;
9+
private static final int MINOR_VERSION = 0;
10+
private static final String DRIVER_ID = "cosmosdb:";
11+
12+
private static final CosmosDBDriver COSMOSDB_DRIVER;
13+
static
14+
{
15+
COSMOSDB_DRIVER = new CosmosDBDriver();
16+
try
17+
{
18+
DriverManager.registerDriver(COSMOSDB_DRIVER);
19+
}catch(SQLException e)
20+
{
21+
e.printStackTrace();
22+
}
23+
}
24+
25+
public CosmosDBDriver()
26+
{
27+
}
28+
29+
@Override
30+
public Connection connect(String url, Properties info)
31+
{
32+
if(!acceptsURL(url))
33+
return null;
34+
try {
35+
return new CosmosDBConnection(url.substring(DRIVER_ID.length()), info);
36+
} catch (Exception e) {
37+
throw new RuntimeException(e);
38+
}
39+
}
40+
41+
@Override
42+
public boolean acceptsURL(String url)
43+
{
44+
return url.startsWith(DRIVER_ID);
45+
}
46+
47+
@Override
48+
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
49+
{
50+
return new DriverPropertyInfo[0];
51+
}
52+
53+
@Override
54+
public int getMajorVersion()
55+
{
56+
return MAJOR_VERSION;
57+
}
58+
59+
@Override
60+
public int getMinorVersion()
61+
{
62+
return MINOR_VERSION;
63+
}
64+
65+
@Override
66+
public boolean jdbcCompliant()
67+
{
68+
return false;
69+
}
70+
71+
@Override
72+
public Logger getParentLogger()
73+
{
74+
throw new UnsupportedOperationException("Not supported yet.");
75+
}
76+
}

0 commit comments

Comments
 (0)