11/*
2-
2+
33 **
44 ** Copyright (c) 2021 Oracle and/or its affiliates.
55 ** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
2020class PropagationSetup {
2121 String GET_OBJECT_CWALLETSSO_DATA_PUMP_DIR = "BEGIN " +
2222 "DBMS_CLOUD.GET_OBJECT(" +
23- "object_uri => '" + cwalletobjecturi + "' , " +
23+ "object_uri => ? , " +
2424 "directory_name => 'DATA_PUMP_DIR'); " +
2525 "END;" ;
2626
27- String DROP_CREDENTIAL_INVENTORYPDB_CRED_SQL = "BEGIN " +
27+ String DROP_CREDENTIAL_SQL = "BEGIN " +
2828 "DBMS_CLOUD.DROP_CREDENTIAL(" +
29- "credential_name => 'INVENTORYPDB_CRED' " +
29+ "credential_name => ? " +
3030 ");" +
3131 "END;" ;
3232
33- String CREATE_CREDENTIAL_INVENTORYPDB_CRED_SQL = " BEGIN" +
33+ String CREATE_CREDENTIAL_SQL = " BEGIN" +
3434 " DBMS_CLOUD.CREATE_CREDENTIAL(" +
3535 " credential_name => 'INVENTORYPDB_CRED'," +
36- " username => '" + inventoryuser + "' ," +
37- " password => '" + inventorypw . trim () + "' " +
36+ " username => ? ," +
37+ " password => ? " +
3838 " );" +
3939 " END;" ;
4040
41- String CREATE_DBLINK_ORDERTOINVENTORY_SQL = "BEGIN " +
41+ String CREATE_DBLINK_SQL = "BEGIN " +
4242 "DBMS_CLOUD_ADMIN.CREATE_DATABASE_LINK(" +
43- "db_link_name => '" + orderToInventoryLinkName + "'," +
44- "hostname => '" + inventoryhostname + "'," +
45- "port => '" + inventoryport + "'," +
46- "service_name => '" + inventoryservice_name + "'," +
47- "ssl_server_cert_dn => '" + inventoryssl_server_cert_dn + "'," +
48- "credential_name => 'INVENTORYPDB_CRED'," +
49- "directory_name => 'DATA_PUMP_DIR');" +
50- "END;" ;
51-
52- String DROP_CREDENTIAL_ORDERPDB_CRED_SQL = "BEGIN " +
53- "DBMS_CLOUD.DROP_CREDENTIAL(" +
54- "credential_name => 'ORDERPDB_CRED'" +
55- ");" +
56- "END;" ;
57-
58- String CREATE_CREDENTIAL_ORDERPDB_CRED_SQL = " BEGIN" +
59- " DBMS_CLOUD.CREATE_CREDENTIAL(" +
60- " credential_name => 'ORDERPDB_CRED'," +
61- " username => '" + orderuser + "'," +
62- " password => '" + orderpw .trim () + "'" +
63- " );" +
64- " END;" ;
65-
66- String CREATE_DBLINK_INVENTORYTOORDER_SQL = "BEGIN " +
67- "DBMS_CLOUD_ADMIN.CREATE_DATABASE_LINK(" +
68- "db_link_name => '" + inventoryToOrderLinkName + "'," +
69- "hostname => '" + orderhostname + "'," +
70- "port => '" + orderport + "'," +
71- "service_name => '" + orderservice_name + "'," +
72- "ssl_server_cert_dn => '" + orderssl_server_cert_dn + "'," +
73- "credential_name => 'ORDERPDB_CRED'," +
74- "directory_name => 'DATA_PUMP_DIR');" +
43+ "db_link_name => ?," +
44+ "hostname => ?," +
45+ "port => ?," +
46+ "service_name => ?," +
47+ "ssl_server_cert_dn => ?," +
48+ "credential_name => ?," +
49+ "directory_name => ?);" +
7550 "END;" ;
7651
7752 String createInventoryTable (DataSource inventorypdbDataSource ) throws SQLException {
7853 System .out .println ("createInventoryTable and add items" );
7954 String returnValue = "createInventoryTable and add items... " ;
80- try (Connection connection = inventorypdbDataSource .getConnection (inventoryuser , inventorypw )){
55+ try (Connection connection = inventorypdbDataSource .getConnection (inventoryuser , inventorypw )) {
8156 connection .createStatement ().execute ("drop table inventory" );
8257 returnValue += " inventory table dropped, about to create inventory table..." ;
8358 connection .createStatement ().execute (
@@ -94,7 +69,7 @@ String createInventoryTable(DataSource inventorypdbDataSource) throws SQLExcepti
9469 return returnValue ;
9570 }
9671
97- String createUsers (DataSource orderpdbDataSource , DataSource inventorypdbDataSource ) throws SQLException {
72+ String createUsers (DataSource orderpdbDataSource , DataSource inventorypdbDataSource ) {
9873 String returnValue = "" ;
9974 try {
10075 returnValue += createAQUser (orderpdbDataSource , orderuser , orderpw );
@@ -114,7 +89,7 @@ String createUsers(DataSource orderpdbDataSource, DataSource inventorypdbDataSou
11489 Object createAQUser (DataSource ds , String queueOwner , String queueOwnerPW ) throws SQLException {
11590 String outputString = "createAQUser queueOwner = [" + queueOwner + "]" ;
11691 System .out .println (outputString + "queueOwnerPW = [" + queueOwnerPW + "]" );
117- try (Connection connection = ds .getConnection ()){
92+ try (Connection connection = ds .getConnection ()) {
11893 PreparedStatement pstmt = connection .prepareStatement ("grant pdb_dba to ? identified by ?" );
11994 pstmt .setString (1 , queueOwner );
12095 pstmt .setString (2 , queueOwnerPW );
@@ -134,43 +109,55 @@ Object createAQUser(DataSource ds, String queueOwner, String queueOwnerPW) throw
134109
135110 String createDBLinks (DataSource orderpdbDataSource , DataSource inventorypdbDataSource ) throws SQLException {
136111 System .out .println ("createDBLinks..." );
137- try (Connection connection = orderpdbDataSource .getConnection (orderuser , orderpw )){
112+ try (Connection connection = orderpdbDataSource .getConnection (orderuser , orderpw )) {
138113 // create link from order to inventory...
139114 createDBLink (connection ,
140- GET_OBJECT_CWALLETSSO_DATA_PUMP_DIR , DROP_CREDENTIAL_INVENTORYPDB_CRED_SQL ,
141- CREATE_CREDENTIAL_INVENTORYPDB_CRED_SQL , CREATE_DBLINK_ORDERTOINVENTORY_SQL , orderToInventoryLinkName );
115+ GET_OBJECT_CWALLETSSO_DATA_PUMP_DIR , "INVENTORYPDB_CRED" , "ordertoinventory" , orderToInventoryLinkName );
142116 }
143117 try (Connection connection = inventorypdbDataSource .getConnection (inventoryuser , inventorypw )) {
144118 createDBLink (connection ,
145- GET_OBJECT_CWALLETSSO_DATA_PUMP_DIR , DROP_CREDENTIAL_ORDERPDB_CRED_SQL ,
146- CREATE_CREDENTIAL_ORDERPDB_CRED_SQL , CREATE_DBLINK_INVENTORYTOORDER_SQL , inventoryToOrderLinkName );
119+ GET_OBJECT_CWALLETSSO_DATA_PUMP_DIR , "ORDERPDB_CRED" , "inventorytoorder" , inventoryToOrderLinkName );
147120 }
148121 verifyDBLinks (orderpdbDataSource , inventorypdbDataSource );
149122 return "DBLinks created and verified successfully" ;
150123 }
151124
152- private void createDBLink (Connection connection , String getobject , String dropcred ,
153- String createcred , String createlink , String linkname ) throws SQLException {
125+ private void createDBLink (Connection connection , String getobject , String dropcred , String createlink , String linkname ) throws SQLException {
126+ boolean isOrderToInventory = createlink . equals ( "ordertoinventory" ); // if it's not OrderToInventory it's InventoryToOrder
154127 System .out .println (" creating link:" + linkname );
155128 System .out .println ("\n about to " + getobject );
156- connection .createStatement ().execute (getobject );
129+ PreparedStatement preparedStatement2 = connection .prepareStatement (GET_OBJECT_CWALLETSSO_DATA_PUMP_DIR );
130+ preparedStatement2 .setString (1 , cwalletobjecturi );
131+ preparedStatement2 .execute ();
157132 try {
158133 System .out .println ("\n GET_OBJECT cwalletobjecturi successful, about to (if exists_" + dropcred );
159- connection .createStatement ().execute (dropcred );
134+ PreparedStatement preparedStatement = connection .prepareStatement (DROP_CREDENTIAL_SQL );
135+ preparedStatement .setString (1 , dropcred );
136+ preparedStatement .execute ();
160137 } catch (SQLException ex ) {
161138 System .out .println ("SQLException from DROP_CREDENTIAL_INVENTORYPDB_CRED_SQL (likely expected) :" + ex );
162139 }
163- System .out .println ("\n GET_OBJECT cwalletobjecturi successful, about to " + createcred );
164- connection .createStatement ().execute (createcred );
140+ System .out .println ("\n GET_OBJECT cwalletobjecturi successful, about to create credential" );
141+ PreparedStatement preparedStatement1 = connection .prepareStatement (CREATE_CREDENTIAL_SQL );
142+ preparedStatement1 .setString (1 , isOrderToInventory ?inventoryuser :orderuser );
143+ preparedStatement1 .setString (1 , isOrderToInventory ?inventorypw .trim ():orderpw .trim ());
144+ preparedStatement1 .execute ();
165145 System .out .println ("\n CREATE_CREDENTIAL INVENTORYPDB_CRED successful, about to " + createlink );
166146 connection .createStatement ().execute (createlink );
147+ PreparedStatement preparedStatement = connection .prepareStatement (CREATE_DBLINK_SQL );
148+ preparedStatement .setString (1 , isOrderToInventory ? orderToInventoryLinkName : inventoryToOrderLinkName );
149+ preparedStatement .setString (2 , isOrderToInventory ? inventoryhostname : orderhostname );
150+ preparedStatement .setInt (3 , Integer .valueOf (isOrderToInventory ? inventoryport : orderport ));
151+ preparedStatement .setString (4 , isOrderToInventory ? inventoryservice_name : orderservice_name );
152+ preparedStatement .setString (5 , isOrderToInventory ? inventoryssl_server_cert_dn : orderssl_server_cert_dn );
153+ preparedStatement .execute ();
167154 System .out .println ("\n CREATE_DATABASE_LINK " + linkname + " successful," );
168155 }
169156
170157 String verifyDBLinks (DataSource orderpdbDataSource , DataSource inventorypdbDataSource ) throws SQLException {
171158 String returnString = "" ;
172159 returnString += "orderuser select on inventorypdb using link..." ;
173- try (Connection orderconnection = orderpdbDataSource .getConnection (orderuser , orderpw )){
160+ try (Connection orderconnection = orderpdbDataSource .getConnection (orderuser , orderpw )) {
174161 System .out .println ("verifyDBLinks orderconnection:" + orderconnection );
175162 orderconnection .createStatement ().execute ("create table templinktest (id varchar(32))" );
176163 System .out .println ("verifyDBLinks temp table created on order" );
@@ -183,7 +170,7 @@ String verifyDBLinks(DataSource orderpdbDataSource, DataSource inventorypdbDataS
183170 returnString += ex ;
184171 }
185172 returnString += "inventoryuser select on orderpdb using link..." ;
186- try (Connection inventoryconnection = inventorypdbDataSource .getConnection (inventoryuser , inventorypw )){
173+ try (Connection inventoryconnection = inventorypdbDataSource .getConnection (inventoryuser , inventorypw )) {
187174 System .out .println ("verifyDBLinks inventoryconnection:" + inventoryconnection );
188175 inventoryconnection .createStatement ().execute ("create table templinktest (id varchar(32))" );
189176 System .out .println ("verifyDBLinks temp table created on inventory" );
0 commit comments