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+ }
0 commit comments