Skip to content

Commit cf08792

Browse files
Add TNS descriptor sample code
1 parent 2ed864e commit cf08792

File tree

5 files changed

+161
-43
lines changed

5 files changed

+161
-43
lines changed

sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencies {
3636
sourceCompatibility = 1.11
3737
targetCompatibility = 1.11
3838

39-
mainClassName = 'TcpsConnectDemo'
39+
mainClassName = 'oracle.r2dbc.samples.TcpsConnectDemo'
4040

4141
run {
4242
// To enable network debugging:

sample/example-config.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ HOST=db.host.example.com
1111
PORT=1521
1212

1313
# Service name of a test database
14-
DATABASE=db.service.name
14+
SERVICE_NAME=db.service.name
1515

1616
# User name authenticated by a test database
1717
USER=db_user
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright (c) 2020, 2021, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package oracle.r2dbc.samples;
23+
24+
import java.io.IOException;
25+
import java.io.UncheckedIOException;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
import java.util.Properties;
29+
30+
/**
31+
* <p>
32+
* Configuration for connecting code samples to an Oracle Database instance.
33+
* </p><p>
34+
* The configuration is read from a properties file in the current directory
35+
* by default, or from a file specified as
36+
* <code>-DCONFIG_FILE=/path/to/your/config.properties</code>
37+
* </p>
38+
*/
39+
public class DatabaseConfig {
40+
41+
/** Path to a configuration file: config.properties */
42+
private static final Path CONFIG_PATH =
43+
Path.of(System.getProperty("CONFIG_FILE", "config.properties"));
44+
45+
/** Configuration that is read from a file at {@link #CONFIG_PATH} */
46+
private static final Properties CONFIG;
47+
static {
48+
try (var fileStream = Files.newInputStream(CONFIG_PATH)) {
49+
CONFIG = new Properties();
50+
CONFIG.load(fileStream);
51+
}
52+
catch (IOException readFailure) {
53+
throw new UncheckedIOException(readFailure);
54+
}
55+
}
56+
57+
/** Host name where an Oracle Database instance is running */
58+
static final String HOST = CONFIG.getProperty("HOST");
59+
60+
/** Port number where an Oracle Database instance is listening */
61+
static final int PORT = Integer.parseInt(CONFIG.getProperty("PORT"));
62+
63+
/** Service name of an Oracle Database */
64+
static final String SERVICE_NAME = CONFIG.getProperty("SERVICE_NAME");
65+
66+
/** User name that connects to an Oracle Database */
67+
static final String USER = CONFIG.getProperty("USER");
68+
69+
/** Password of the user that connects to an Oracle Database */
70+
static final String PASSWORD = CONFIG.getProperty("PASSWORD");
71+
72+
/** The file system path of a wallet directory */
73+
static final String WALLET_LOCATION =
74+
CONFIG.getProperty("WALLET_LOCATION");
75+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright (c) 2020, 2021, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package oracle.r2dbc.samples;
23+
24+
import io.r2dbc.spi.ConnectionFactories;
25+
import io.r2dbc.spi.ConnectionFactoryOptions;
26+
import reactor.core.publisher.Mono;
27+
28+
import static oracle.r2dbc.samples.DatabaseConfig.HOST;
29+
import static oracle.r2dbc.samples.DatabaseConfig.PORT;
30+
import static oracle.r2dbc.samples.DatabaseConfig.SERVICE_NAME;
31+
import static oracle.r2dbc.samples.DatabaseConfig.USER;
32+
import static oracle.r2dbc.samples.DatabaseConfig.PASSWORD;
33+
34+
/**
35+
* This code example shows how to use TNS descriptor URLs with Oracle R2DBC.
36+
* The TNS descriptor has the form:
37+
* <pre>
38+
* (DESCRIPTION=...)
39+
* </pre>
40+
* The full syntax of the TNS descriptor is described in the
41+
* <a href=https://docs.oracle.com/en/database/oracle/oracle-database/21/netag/identifying-and-accessing-database.html#GUID-8D28E91B-CB72-4DC8-AEFC-F5D583626CF6>
42+
* Oracle Net Services Administrator's Guide
43+
* </a>
44+
*/
45+
public class DescriptorURL {
46+
47+
/**
48+
* A TNS descriptor specifying the HOST, PORT, and SERVICE_NAME read from
49+
* {@link DatabaseConfig}.
50+
*/
51+
private static final String DESCRIPTOR = "(DESCRIPTION=" +
52+
"(ADDRESS=(HOST="+HOST+")(PORT="+PORT+")(PROTOCOL=tcp))" +
53+
"(CONNECT_DATA=(SERVICE_NAME="+SERVICE_NAME+")))";
54+
55+
public static void main(String[] args) {
56+
// A descriptor may appear in the host section of an R2DBC URL:
57+
String r2dbcUrl = "r2dbc:oracle://"+DESCRIPTOR;
58+
Mono.from(ConnectionFactories.get(ConnectionFactoryOptions.parse(r2dbcUrl)
59+
.mutate()
60+
.option(ConnectionFactoryOptions.USER, USER)
61+
.option(ConnectionFactoryOptions.PASSWORD, PASSWORD)
62+
.build())
63+
.create())
64+
.flatMapMany(connection ->
65+
Mono.from(connection.createStatement(
66+
"SELECT 'Connected with TNS descriptor' FROM sys.dual")
67+
.execute())
68+
.flatMapMany(result ->
69+
result.map((row, metadata) -> row.get(0, String.class)))
70+
.concatWith(Mono.from(connection.close()).cast(String.class)))
71+
.toStream()
72+
.forEach(System.out::println);
73+
}
74+
}

sample/src/main/java/TcpsConnectDemo.java renamed to sample/src/main/java/oracle/r2dbc/samples/TcpsConnectDemo.java

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
limitations under the License.
2020
*/
2121

22+
package oracle.r2dbc.samples;
23+
2224
import io.r2dbc.spi.ConnectionFactories;
2325
import io.r2dbc.spi.ConnectionFactory;
2426
import io.r2dbc.spi.ConnectionFactoryOptions;
@@ -28,14 +30,15 @@
2830
import reactor.core.publisher.Flux;
2931
import reactor.core.publisher.Mono;
3032

31-
import java.io.IOException;
32-
import java.io.UncheckedIOException;
3333
import java.net.URI;
3434
import java.net.URISyntaxException;
35-
import java.nio.file.Files;
36-
import java.nio.file.Path;
3735
import java.time.Duration;
38-
import java.util.Properties;
36+
37+
import static oracle.r2dbc.samples.DatabaseConfig.HOST;
38+
import static oracle.r2dbc.samples.DatabaseConfig.PASSWORD;
39+
import static oracle.r2dbc.samples.DatabaseConfig.PORT;
40+
import static oracle.r2dbc.samples.DatabaseConfig.USER;
41+
import static oracle.r2dbc.samples.DatabaseConfig.WALLET_LOCATION;
3942

4043
/**
4144
* Sample code that uses an Oracle Wallet to authenticate with an Oracle
@@ -67,40 +70,6 @@
6770
*/
6871
public class TcpsConnectDemo {
6972

70-
/** Path to a configuration file: config.properties */
71-
private static final Path CONFIG_PATH =
72-
Path.of(System.getProperty("CONFIG_FILE", "config.properties"));
73-
74-
/** Configuration that is read from a file at {@link #CONFIG_PATH} */
75-
private static final Properties CONFIG;
76-
static {
77-
try (var fileStream = Files.newInputStream(CONFIG_PATH)) {
78-
CONFIG = new Properties();
79-
CONFIG.load(fileStream);
80-
}
81-
catch (IOException readFailure) {
82-
throw new UncheckedIOException(readFailure);
83-
}
84-
}
85-
86-
/** Host name where an Oracle Database instance is running */
87-
private static final String HOST = CONFIG.getProperty("HOST");
88-
89-
/** Port number where an Oracle Database instance is listening */
90-
private static final int PORT = Integer.parseInt(CONFIG.getProperty("PORT"));
91-
92-
/** Service name of an Oracle Database */
93-
private static final String SERVICE_NAME = CONFIG.getProperty("SERVICE_NAME");
94-
95-
/** User name that connects to an Oracle Database */
96-
private static final String USER = CONFIG.getProperty("USER");
97-
98-
/** Password of the user that connects to an Oracle Database */
99-
private static final String PASSWORD = CONFIG.getProperty("PASSWORD");
100-
101-
/** The file system path of a wallet directory */
102-
private static final String WALLET_LOCATION =
103-
CONFIG.getProperty("WALLET_LOCATION");
10473

10574
public static void main(String[] args) throws URISyntaxException {
10675

@@ -122,7 +91,7 @@ public static void main(String[] args) throws URISyntaxException {
12291
USER + ":" + PASSWORD, // userInfo
12392
HOST, // host
12493
PORT, // port
125-
"/" + SERVICE_NAME, // path
94+
"/" + DatabaseConfig.SERVICE_NAME, // path
12695
"oracle.net.wallet_location=" + WALLET_LOCATION
12796
+ "&oracle.jdbc.fanEnabled=false", // query
12897
null) // fragment
@@ -145,7 +114,7 @@ public static void main(String[] args) throws URISyntaxException {
145114
.option(ConnectionFactoryOptions.DRIVER, "oracle")
146115
.option(ConnectionFactoryOptions.HOST, HOST)
147116
.option(ConnectionFactoryOptions.PORT, PORT)
148-
.option(ConnectionFactoryOptions.DATABASE, SERVICE_NAME)
117+
.option(ConnectionFactoryOptions.DATABASE, DatabaseConfig.SERVICE_NAME)
149118
.option(ConnectionFactoryOptions.USER, USER)
150119
.option(ConnectionFactoryOptions.PASSWORD, PASSWORD)
151120
// To configure a TCPS/SSL/TLS enabled ConnectionFactory, set the SSL

0 commit comments

Comments
 (0)