21 April, 2012

Print Java System Properties in PLSQL

I will present how to print System.getProperties() in Java and call it from PLSQL.
I can print output in DBMS Output Console or Java Console.

The steps of this practice as below
1-Create Java Class
2-Create wrapper Procedure and Function
3-Call Wrapper Procedure and Function From PLSQL

1-Create Java Class
I create two method in Java Class

a-printProperties method which return output of system properties as string
b-printPropertiesJavaConsole method which print output of system properties in Java console

 CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED SystemProperties  
   AS import java.util.Enumeration;  
 import java.util.Properties;  
 public class SystemProperties {  
   public static String printProperties() {  
     StringBuilder sb = new StringBuilder();  
     Properties properties = System.getProperties();  
     Enumeration properiesEnum = properties.propertyNames();  
     while (properiesEnum.hasMoreElements()) {  
       String propertyKey = (String)properiesEnum.nextElement();  
       sb.append(propertyKey + " = " + System.getProperty(propertyKey) + "\n");  
     }  
     return sb.toString();  
   }  
    public static void printPropertiesJavaConsole() {  
     System.out.println(printProperties());  
   }  
   public static void main(String[] args) {  
     System.out.println(printProperties());  
   }  
 }  
 /  



2-Create wrapper Procedure and Function
a- I create wrapper function printProperties that map to printProperties method in Java Class.
b- I create wrapper procedure printPropertiesJavaConsole that map printPropertiesJavaConsole method in Java Class.

 CREATE OR REPLACE FUNCTION printProperties printPropertiesJavaConsole  
   RETURN VARCHAR2  
 AS  
   LANGUAGE JAVA  
   NAME 'SystemProperties.printProperties() return java.lang.String'; 
 CREATE OR REPLACE PROCEDURE printPropertiesJavaConsole  
 AS  
   LANGUAGE JAVA  
   NAME 'SystemProperties.printPropertiesJavaConsole()';  

3-Call Wrapper Procedure and Function From PLSQL
To get system properties in java class from PLSQL You should have  java.util.PropertyPermission permission in DBMS Java. You can grant it to users using below script

 BEGIN  
   DBMS_JAVA.grant_permission ('SCOTT',  
                 'SYS:java.util.PropertyPermission',  
                 '*',  
                 'read,write');  
 END;  

a- Print output in DBMS Output using printProperties function

 BEGIN  
   DBMS_OUTPUT.put_line (printProperties);  
 END;  

b- Print output in Java Console using printPropertiesJavaConsole procedure
To display output of Java Console in DBMS Output you should set output of java using below procedure
   DBMS_JAVA.SET_OUTPUT (1000000);


 BEGIN  
   DBMS_JAVA.SET_OUTPUT (1000000);  
   printPropertiesJavaConsole;  
 END;   

The output in DBMS Output is like below

 oracle.aurora.ncomp.lib.permission =   
 java.protocol.handler.pkgs = oracle.aurora.rdbms.url  
 sun.boot.library.path = /u01/app/oracle/product/11.1.0/db_1/lib  
 java.vm.version = 1.5.0_01  
 oracle.aurora.ncomp.lib.component.prefix = jtc  
 java.vm.vendor = Oracle Corporation  
 java.vendor.url = http://www.oracle.com/java/  
 path.separator = :  
 java.vm.name = JServer VM  
 file.encoding.pkg = sun.io  
 java.vm.specification.name = Java Virtual Machine Specification  
 user.dir = /u01/app/oracle/product/11.1.0/db_1  
 java.awt.graphicsenv = oracle.aurora.awt.OracleGraphicsEnvironment  
 os.arch = i686  
 java.io.tmpdir = /tmp  
 line.separator =   
 java.vm.specification.vendor = Sun Microsystems Inc.  
 java.naming.factory.url.pkgs = com.sun.jndi.url  
 os.name = Linux  
 sun.java2d.fontpath =   
 oracle.aurora.ncomp.file.obj.suffix = o  
 java.library.path =   
 java.specification.name = Java Platform API Specification  
 java.class.version = 48.0  
 java.net.preferIPv4Stack = TRUE  
 oracle.aurora.ncomp.file.dll.suffix = so  
 java.util.prefs.PreferencesFactory = java.util.prefs.OraclePreferencesFactory  
 os.version = 2.6.18-155.el5PAE  
 user.home =   
 file.encoding = WINDOWS-1256  
 java.specification.version = 1.5  
 oracle.aurora.ncomp.lib.os.prefix = lib  
 user.name =   
 java.class.path =   
 oracle.aurora.rdbms.SID = TEST  
 java.vm.specification.version = 1.0  
 oracle.server.version = 11.1.0.7.0  
 java.home = /u01/app/oracle/product/11.1.0/db_1/javavm/  
 java.specification.vendor = Sun Microsystems Inc.  
 user.language = en  
 oracle.aurora.rdbms.oracle_home = /u01/app/oracle/product/11.1.0/db_1  
 awt.toolkit = oracle.aurora.awt.OracleToolkit  
 oracle.aurora.vm.environment.name = rdbms  
 java.version = 1.5.0_10  
 java.vendor = Oracle Corporation  
 java.awt.headless = true  
 file.separator = /  
 sqlj.runtime = sqlj.framework.ide.aurora.rdbms.OracleSQLJRuntime  
 java.compiler =   
 sun.cpu.endian = little  
 sun.io.unicode.encoding = UnicodeLittle  
 oracle.jserver.version = 11.1.0.7.0  
 oracle.aurora.system_subdirectory = lib  


Thanks
Recommended Post Slide Out For Blogger