1111
1212import javax .jms .*;
1313import javax .sql .DataSource ;
14+ import java .sql .CallableStatement ;
1415import java .sql .Connection ;
1516import java .sql .PreparedStatement ;
1617import java .sql .SQLException ;
@@ -31,12 +32,12 @@ class PropagationSetup {
3132 "END;" ;
3233
3334 String CREATE_CREDENTIAL_SQL = " BEGIN" +
34- " DBMS_CLOUD.CREATE_CREDENTIAL(" +
35- " credential_name => 'INVENTORYPDB_CRED' ," +
36- " username => ?," +
37- " password => ?" +
38- " );" +
39- " END;" ;
35+ " DBMS_CLOUD.CREATE_CREDENTIAL(" +
36+ " credential_name => ? ," +
37+ " username => ?," +
38+ " password => ?" +
39+ " );" +
40+ " END;" ;
4041
4142 String CREATE_DBLINK_SQL = "BEGIN " +
4243 "DBMS_CLOUD_ADMIN.CREATE_DATABASE_LINK(" +
@@ -54,7 +55,11 @@ String createInventoryTable(DataSource inventorypdbDataSource) throws SQLExcepti
5455 String returnValue = "createInventoryTable and add items... " ;
5556 try (Connection connection = inventorypdbDataSource .getConnection (inventoryuser , inventorypw )) {
5657 connection .createStatement ().execute ("drop table inventory" );
57- returnValue += " inventory table dropped, about to create inventory table..." ;
58+ } catch (SQLException ex ) {
59+ System .out .println ("PropagationSetup.createInventoryTable inventory table was not dropped. This is expected during initial setup as it doesn't exist yet." );
60+ }
61+ try (Connection connection = inventorypdbDataSource .getConnection (inventoryuser , inventorypw )) {
62+ returnValue += " inventory table dropped (if it existed), about to create inventory table..." ;
5863 connection .createStatement ().execute (
5964 "create table inventory (inventoryid varchar(16) PRIMARY KEY NOT NULL, inventorylocation varchar(32), inventorycount integer CONSTRAINT positive_inventory CHECK (inventorycount >= 0) )" );
6065 returnValue += " inventory table created, about to insert into inventory..." ;
@@ -88,21 +93,28 @@ String createUsers(DataSource orderpdbDataSource, DataSource inventorypdbDataSou
8893
8994 Object createAQUser (DataSource ds , String queueOwner , String queueOwnerPW ) throws SQLException {
9095 String outputString = "createAQUser queueOwner = [" + queueOwner + "]" ;
96+ System .out .println ("PropagationSetup.createAQUser queueOwner = [" + queueOwner + "]" );
9197 try (Connection connection = ds .getConnection ()) {
92- PreparedStatement pstmt = connection .prepareStatement ("grant pdb_dba to ? identified by ?" );
93- pstmt .setString (1 , queueOwner );
94- pstmt .setString (2 , queueOwnerPW );
98+ connection .prepareStatement ("CREATE OR REPLACE PROCEDURE CREATEDATAAPPUSER (userNam NVARCHAR2, userPw NVARCHAR2)" +
99+ " IS" +
100+ " v_sql VARCHAR2 (32767);" +
101+ " BEGIN" +
102+ " v_sql := 'CREATE USER \" ' || userNam || '\" IDENTIFIED BY \" ' || userPw || '\" ';" +
103+ " EXECUTE IMMEDIATE v_sql;" +
104+ " END CREATEDATAAPPUSER;" ).execute ();
105+ CallableStatement pstmt = connection .prepareCall ("{call CREATEDATAAPPUSER(?,?)}" );
106+ pstmt .setNString (1 , queueOwner .trim ());
107+ pstmt .setNString (2 , queueOwnerPW .trim ());
95108 pstmt .execute ();
109+ connection .createStatement ().execute ("GRANT pdb_dba TO " + queueOwner );
96110 connection .createStatement ().execute ("GRANT EXECUTE ON DBMS_CLOUD_ADMIN TO " + queueOwner );
97111 connection .createStatement ().execute ("GRANT EXECUTE ON DBMS_CLOUD TO " + queueOwner );
98112 connection .createStatement ().execute ("GRANT CREATE DATABASE LINK TO " + queueOwner );
99- connection .createStatement ().execute ("grant unlimited tablespace to " + queueOwner );
100- connection .createStatement ().execute ("grant connect, resource TO " + queueOwner );
101- connection .createStatement ().execute ("grant aq_user_role TO " + queueOwner );
113+ connection .createStatement ().execute ("GRANT unlimited tablespace to " + queueOwner );
114+ connection .createStatement ().execute ("GRANT connect, resource TO " + queueOwner );
115+ connection .createStatement ().execute ("GRANT aq_user_role TO " + queueOwner );
102116 connection .createStatement ().execute ("GRANT EXECUTE ON sys.dbms_aqadm TO " + queueOwner );
103117 connection .createStatement ().execute ("GRANT EXECUTE ON sys.dbms_aq TO " + queueOwner );
104- connection .createStatement ().execute ("GRANT EXECUTE ON sys.dbms_aq TO " + queueOwner );
105- // sysDBAConnection.createStatement().execute("create table tracking (state number)");
106118 }
107119 return outputString + " successful" ;
108120 }
@@ -122,36 +134,37 @@ String createDBLinks(DataSource orderpdbDataSource, DataSource inventorypdbDataS
122134 return "DBLinks created and verified successfully" ;
123135 }
124136
125- private void createDBLink (Connection connection , String getobject , String dropcred , String createlink , String linkname ) throws SQLException {
137+ private void createDBLink (Connection connection , String getobject , String credName , String createlink , String linkname ) throws SQLException {
126138 boolean isOrderToInventory = createlink .equals ("ordertoinventory" ); // if it's not OrderToInventory it's InventoryToOrder
127- System .out .println (" creating link:" + linkname );
128- System .out .println ("\n about to " + getobject );
139+ System .out .println (" creating link:" + linkname + " about to " + getobject );
129140 PreparedStatement preparedStatement2 = connection .prepareStatement (GET_OBJECT_CWALLETSSO_DATA_PUMP_DIR );
130141 preparedStatement2 .setString (1 , cwalletobjecturi );
131142 preparedStatement2 .execute ();
132143 try {
133- System .out .println ("\n GET_OBJECT cwalletobjecturi successful, about to (if exists_ " + dropcred );
144+ System .out .println ("About to drop credential (if exists) " + credName );
134145 PreparedStatement preparedStatement = connection .prepareStatement (DROP_CREDENTIAL_SQL );
135- preparedStatement .setString (1 , dropcred );
146+ preparedStatement .setString (1 , credName );
136147 preparedStatement .execute ();
137148 } catch (SQLException ex ) {
138- System .out .println ("SQLException from DROP_CREDENTIAL_INVENTORYPDB_CRED_SQL (likely expected) :" + ex );
149+ System .out .println ("Drop credential failed as expected (if this is initial setup)" );
139150 }
140- System .out .println ("\n GET_OBJECT cwalletobjecturi successful, about to create credential" );
151+ System .out .println ("GET_OBJECT cwalletobjecturi successful, about to create credential" );
141152 PreparedStatement preparedStatement1 = connection .prepareStatement (CREATE_CREDENTIAL_SQL );
142- preparedStatement1 .setString (1 , isOrderToInventory ?inventoryuser :orderuser );
143- preparedStatement1 .setString (1 , isOrderToInventory ?inventorypw .trim ():orderpw .trim ());
153+ preparedStatement1 .setString (1 , credName );
154+ preparedStatement1 .setString (2 , isOrderToInventory ?inventoryuser :orderuser );
155+ preparedStatement1 .setString (3 , isOrderToInventory ?inventorypw .trim ():orderpw .trim ());
144156 preparedStatement1 .execute ();
145- System .out .println ("\n CREATE_CREDENTIAL INVENTORYPDB_CRED successful, about to " + createlink );
146- connection .createStatement ().execute (createlink );
157+ System .out .println ("CREATE_CREDENTIAL successful, about to create link" + createlink );
147158 PreparedStatement preparedStatement = connection .prepareStatement (CREATE_DBLINK_SQL );
148159 preparedStatement .setString (1 , isOrderToInventory ? orderToInventoryLinkName : inventoryToOrderLinkName );
149160 preparedStatement .setString (2 , isOrderToInventory ? inventoryhostname : orderhostname );
150161 preparedStatement .setInt (3 , Integer .valueOf (isOrderToInventory ? inventoryport : orderport ));
151162 preparedStatement .setString (4 , isOrderToInventory ? inventoryservice_name : orderservice_name );
152163 preparedStatement .setString (5 , isOrderToInventory ? inventoryssl_server_cert_dn : orderssl_server_cert_dn );
164+ preparedStatement .setString (6 , credName );
165+ preparedStatement .setString (7 , "DATA_PUMP_DIR" );
153166 preparedStatement .execute ();
154- System .out .println ("\n CREATE_DATABASE_LINK " + linkname + " successful," );
167+ System .out .println (" CREATE_DATABASE_LINK " + linkname + " successful," );
155168 }
156169
157170 String verifyDBLinks (DataSource orderpdbDataSource , DataSource inventorypdbDataSource ) throws SQLException {
@@ -388,7 +401,7 @@ private static void sendMessages(TopicSession topicSession, Topic topic) throws
388401 private static void receiveMessages (QueueSession qsess , AQjmsConsumer [] subs ) throws JMSException {
389402 System .out .println ("Receive test messages..." );
390403 for (int i = 0 ; i < subs .length ; i ++) {
391- System .out .println ("\n \n Messages for subscriber : " + i );
404+ System .out .println ("Messages for subscriber : " + i );
392405 if (subs [i ].getMessageSelector () != null ) {
393406 System .out .println (" with selector: " + subs [i ].getMessageSelector ());
394407 }
0 commit comments