adding_os_command_capability_to_plsql_with_java_stored_procedures

32
Adding OS Command Capability to PLSQL With Java Stored Procedures Mary Wagner Oracle Database Administrator [email protected] https:// www.eclouddesk.com 1 Mary Wagner [email protected]

Upload: mary-wagner

Post on 12-Apr-2017

241 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: adding_os_command_capability_to_plsql_with_java_stored_procedures

Adding OS Command Capability to PLSQL With Java Stored Procedures

Mary WagnerOracle Database Administrator

[email protected]://www.eclouddesk.com

1Mary Wagner [email protected]

Page 2: adding_os_command_capability_to_plsql_with_java_stored_procedures

What are Java Stored Procedures?• Java source / class files can be loaded into the

database and their methods can then be called in PLSQL functions and procedures.

2Mary Wagner [email protected]

Page 3: adding_os_command_capability_to_plsql_with_java_stored_procedures

Why use them?Extend your PLSQL stored procedures to do things that PLSQL

is unable to do like…

• Create directories on the server• Copy and delete files from the operating system• Run a batch file, shell script or operating system executable

All from within the database…

3Mary Wagner [email protected]

Page 4: adding_os_command_capability_to_plsql_with_java_stored_procedures

4 ExamplesExample 1 – Create a New Directory on the Server

Example 2 – Copy an Output File to a New Directory

Example 3 – Run a Zip utility to Compress an Output File

Example 4 – Remove Directories and Files That are more than 3 Days Old

4Mary Wagner [email protected]

Page 5: adding_os_command_capability_to_plsql_with_java_stored_procedures

Setting Up for the Examples.1. Create the USERS table. - create_users_table.sql

2. Create the DUMP_CSV function. - create_dump_csv_function.sql

3. Create the DUMP_USERS package. - create_dump_users_package.sql

4. Create directory objectcreate or replace directory OUTPUT as

'/app01/oracle/local/output';

5. Test the DUMP_USERS procedure. - EXEC PKG_DUMP_USERS.DUMP_USERS_TO_CSV('OUTPUT',

'users_test.txt')

5Mary Wagner [email protected]

Page 6: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 1 – Create a New Directory.

JAVA:FileUtil.javamakeDirectory(String directoryName)

PLSQL:pkg_file_util.make_directory(p_directoryname in

varchar2)pkg_dump_users. example1

6Mary Wagner [email protected]

Page 7: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 1 => Step 1.) – Create FileUtil java source.

• Script - \example1\create_file_util_java_source.sql

…………………set echo onset define off

create or replace and compilejava source named "FileUtil“as…………….

7Mary Wagner [email protected]

Page 8: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 1 => Step 2.) – Create FILE_UTIL package.• Script - \example1\create_file_util_package.sql

CREATE OR REPLACE PACKAGE PKG_FILE_UTIL as function make_directory(p_directoryname in varchar2) return number;end;/

CREATE OR REPLACE PACKAGE BODY PKG_FILE_UTIL as function make_directory(p_directoryname in varchar2) return number is language java name 'FileUtil.makeDirectory(java.lang.String) return integer';end;/

8Mary Wagner [email protected]

Page 9: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 1 => Step 3.) – Modify

DUMP_USERS package.• Script - \example1\create_dump_users_package.sql

CREATE OR REPLACE PACKAGE PKG_DUMP_USERS as procedure dump_users_to_csv(p_directory in varchar2, p_filename in

varchar2); procedure example1; end;

CREATE OR REPLACE PACKAGE BODY PKG_DUMP_USERS as…………………………….

9Mary Wagner [email protected]

Page 10: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 1 => Step 4.) – Grant java related permissions.

• Script - \example1\grant_java.sqlgrant create any directory to MWAGNER;grant select on dba_directories to MWAGNER;

BEGINDBMS_JAVA.GRANT_PERMISSION( 'MWAGNER','java.io.FilePermission','c:\data\output\users\-','read');

DBMS_JAVA.GRANT_PERMISSION( 'MWAGNER','java.io.FilePermission','c:\data\output\users\-','write'); END;/

10Mary Wagner [email protected]

Page 11: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 1 => Step 5.) – Run the

example.

• Script - \example1\run_example.sql

set serveroutput on size 1000000exec dbms_java.set_output( 1000000 )

EXEC PKG_DUMP_USERS.EXAMPLE1

11Mary Wagner [email protected]

Page 12: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 2 – Copy Output File to a New Directory

JAVA:FileUtil.javacopyFile(String from, String to)

PLSQL:pkg_file_util.copy_file(p_filename1 in varchar2,

p_filename2 in varchar2)pkg_dump_users. example2

12Mary Wagner [email protected]

Page 13: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 2 => Step 1.) – Create FileUtil java source.

• Script - \example2\create_file_util_java_source.sql

…………………set echo onset define off

create or replace and compilejava source named "FileUtil“as…………….

13Mary Wagner [email protected]

Page 14: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 2 => Step 2.) – Modify FILE_UTIL package.

• Script - \example2\create_file_util_package.sql

PACKAGE ADDITION -- function copy_file(p_filename1 in varchar2, p_filename2 in varchar2) return

number;

PACKAGE BODY ADDITION -- function copy_file(p_filename1 in varchar2, p_filename2 in varchar2) return

number is language java name 'FileUtil.copyFile(java.lang.String, java.lang.String)

return integer';

14Mary Wagner [email protected]

Page 15: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 2 => Step 3.) – Modify

DUMP_USERS package.• Script - \example2\create_dump_users_package.sql

PACKAGE ADDITION -- procedure example2;

PACKAGE BODY ADDITION -- procedure example2 is …………..

15Mary Wagner [email protected]

Page 16: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 2 => Step 4.) – Grant java related permissions.• Script - \example2\grant_java.sql

Run as SYS or other DBA user-- The below permission is needed in addition to the grants run for EXAMPLE

1.

BEGINDBMS_JAVA.GRANT_PERMISSION( ‘MWAGNER’,‘java.io.FilePermission’,‘c:\temp\*’,‘read’); END;/

16Mary Wagner [email protected]

Page 17: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 2 => Step 5.) – Run the

example.

• Script - \example2\run_example.sql

set serveroutput on size 1000000exec dbms_java.set_output( 1000000 )

EXEC PKG_DUMP_USERS.EXAMPLE2

17Mary Wagner [email protected]

Page 18: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 3 – Run a Zip utility to Compress Output File

JAVA:RunOSCmdUtil.javarunCommand(String command)

PLSQL:pkg_file_util. run_command(p_command in varchar2)pkg_dump_users. example3

18Mary Wagner [email protected]

Page 19: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 3 => Step 1.) – Create RunOSCmdUtil java source.

• Script - \example3\create_run_cmd_util_java_source.sql

…………………set echo onset define off

create or replace and compilejava source named "RunOSCmdUtil“as………………….

19Mary Wagner [email protected]

Page 20: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 3 => Step 2.) – Modify FILE_UTIL package.

• Script - \example3\create_file_util_package.sql

PACKAGE ADDITION -- function run_command(p_command in varchar2) return number;

PACKAGE BODY ADDITION -- function run_command(p_command in varchar2) return number is language java name 'RunOSCmdUtil.runCommand(java.lang.String) return

integer';

20Mary Wagner [email protected]

Page 21: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 3 => Step 3.) – Modify

DUMP_USERS package.• Script - \example3\create_dump_users_package.sql

PACKAGE ADDITION -- procedure example3;

PACKAGE BODY ADDITION -- procedure example3 is …………..

21Mary Wagner [email protected]

Page 22: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 3 => Step 4.) – Grant java related permissions.• Script - \example3\grant_java.sql

Run as SYS or other DBA user-- The below permissions are needed in addition to the grants run for EXAMPLES 1 and 2.BEGINDBMS_JAVA.GRANT_PERMISSION( 'MWAGNER','java.io.FilePermission','c:\data\fsoug\example3\rar.exe','execute');

DBMS_JAVA.GRANT_PERMISSION ('MWAGNER','java.lang.RuntimePermission','*','writeFileDescriptor' );END;/

22Mary Wagner [email protected]

Page 23: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 3 => Step 5.) – Run the

example.

• Script - \example3\run_example.sql

set serveroutput on size 1000000exec dbms_java.set_output( 1000000 )

EXEC PKG_DUMP_USERS.EXAMPLE3

23Mary Wagner [email protected]

Page 24: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 4 – Remove Directories and Files That are more than 3

Days OldJAVA:FileUtil.javadeleteOldDirectories(String directory) – main methoddeleteDirectory(String directoryname)deleteDirectoryandFiles(String directory)

PLSQL:pkg_file_util.delete_old_directories(p_directoryname in

varchar2)pkg_dump_users.example4

24Mary Wagner [email protected]

Page 25: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 4 => Step 1.) – Modify FileUtil java source.

• Script - \example4\create_file_util_java_source.sql

…………………set echo onset define off

create or replace and compilejava source named "FileUtil“as…………….

25Mary Wagner [email protected]

Page 26: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 4 => Step 2.) – Modify FILE_UTIL package.

• Script - \example4\create_file_util_package.sql

PACKAGE ADDITION -- function delete_old_directories(p_directory_name in varchar2) return number;

PACKAGE BODY ADDITION -- function delete_old_directories(p_directoryname in varchar2) return number is language java name 'FileUtil.deleteOldDirectories(java.lang.String) return

integer';

26Mary Wagner [email protected]

Page 27: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 4 => Step 3.) – Modify

DUMP_USERS package.• Script - \example4\create_dump_users_package.sql

PACKAGE ADDITION -- procedure example4;

PACKAGE BODY ADDITION -- procedure example4 is …………..

27Mary Wagner [email protected]

Page 28: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 4 => Step 4.) – Grant java related permissions.• Script - \example4\grant_java.sqlRun as SYS or other DBA user-- The below permissions are needed in addition to the grants run for EXAMPLES 1, 2 and 3.BEGINDBMS_JAVA.GRANT_PERMISSION( 'MWAGNER','java.io.FilePermission','c:\data\output\users\','read');

DBMS_JAVA.GRANT_PERMISSION( 'MWAGNER','java.io.FilePermission','c:\data\output\users\*','delete');

DBMS_JAVA.GRANT_PERMISSION( 'MWAGNER','java.io.FilePermission','c:\data\output\users\-','delete'); END;/

28Mary Wagner [email protected]

Page 29: adding_os_command_capability_to_plsql_with_java_stored_procedures

Example 4 => Step 5.) – Run the

example.

• Script - \example4\run_example.sql

set serveroutput on size 1000000exec dbms_java.set_output( 1000000 )

EXEC PKG_DUMP_USERS.EXAMPLE4

29Mary Wagner [email protected]

Page 30: adding_os_command_capability_to_plsql_with_java_stored_procedures

Miscellaneous Notes• BE CAREFUL WITH JAVA PERMISSIONS – When granting java

file system permissions, be very careful as to what you allow users to do. If they have too many privileges they could delete critical files mistakenly (like database files) or run destructive operating system commands or scripts. Only give users the permissions that are needed.

• SET DEFINE OFF – When loading java source through sql plus, always execute “set define off” before loading the java, in case there are && or other sql plus characters in your java source code.

• ORA 29531- See metalink article - 1014301.102 – “All Java Stored Procedures with methods to be exposed to PL/SQL must be defined with the 'static' attribute.“

30Mary Wagner [email protected]

Page 31: adding_os_command_capability_to_plsql_with_java_stored_procedures

Acknowledgements / More Information• http://asktom.oracle.com

• http://java.sun.com

• http://forum.java.sun.com

• http://java.sun.com/products/javamail

• http://metalink.oracle.com

• http://www.jguru.com

31Mary Wagner [email protected]

Page 32: adding_os_command_capability_to_plsql_with_java_stored_procedures

Scripts / Contact Info

Use the scripts at your own risk and test them fully in your own test environment before using them in a production database.

Please let me know if there are any errors in the scripts or other issues.

Mary [email protected] https://www.eclouddesk.com

32Mary Wagner [email protected]