Rem
Rem $Header: javavm/ojvmwcu/install/configure_ojvmwcu.sql /st_javavm_19.0.0.0.0ojvmru/2 2025/03/11 16:41:41 apfwkr Exp $
Rem
Rem configure_ojvmwcu.sql
Rem
Rem Copyright (c) 2015, 2025, Oracle and/or its affiliates. 
Rem All rights reserved.
Rem
Rem    NAME
Rem      configure_ojvmwcu.sql - <one-line expansion of the name>
Rem
Rem    DESCRIPTION
Rem      <short description of component this file declares/defines>
Rem
Rem    NOTES
Rem      <other useful comments, qualifications, etc.>
Rem
Rem    BEGIN SQL_FILE_METADATA 
Rem    SQL_SOURCE_FILE: javavm/ojvmwcu/configure_ojvmwcu.sql 
Rem    SQL_SHIPPED_FILE: 
Rem    SQL_PHASE: 
Rem    SQL_STARTUP_MODE: NORMAL 
Rem    SQL_IGNORABLE_ERRORS: NONE 
Rem    SQL_CALLING_FILE: 
Rem    END SQL_FILE_METADATA
Rem
Rem    MODIFIED   (MM/DD/YY)
Rem    apfwkr      02/28/25 - CI #37647624 of mohshanm_bug-29867088 from main
Rem                           (apfwkr_ci_backport_29867088_19.27.0.0.250415ojvmru).
Rem    kdere       03/27/17 - Removing creation of install schema
Rem    kdere       03/14/16 - Remove permission createAccessControlContext
Rem    kdere       03/08/16 - Adding user parameter to export procedures
Rem    kdere       10/15/15 - Adding permissions for SOAP part
Rem    kdere       08/13/15 - Created
Rem

whenever sqlerror exit sql.sqlcode
set serveroutput on;

create or replace package ojvmwcu_sys authid current_user as
 procedure require_sys;
 procedure grant_permissions_to_schema;
 procedure create_role;
 procedure configure;
 procedure load_jar(filename varchar2);
 procedure drop_jar(filename varchar2);
end ojvmwcu_sys;
/ 

create or replace package body ojvmwcu_sys as

install_schema VARCHAR2(20) := 'OJVMWCU_INSTALL';

procedure require_sys as
begin
  if login_user != 'SYS' then
    declare
      foo exception;
      pragma exception_init(foo,-1031);
    begin
      raise foo;
    end;
  end if;
end;

procedure grant_permissions_to_schema as
begin
  dbms_java.grant_permission( install_schema, 'SYS:java.net.SocketPermission', '*', 'connect,resolve' );
  dbms_java.grant_permission( install_schema, 'SYS:java.lang.reflect.ReflectPermission', 'suppressAccessChecks', '' );
  dbms_java.grant_permission( install_schema, 'SYS:java.lang.RuntimePermission', 'getClassLoader', '' );
  dbms_java.grant_permission( install_schema, 'SYS:java.util.PropertyPermission', '*', 'read,write' );
  dbms_java.grant_permission( install_schema, 'SYS:java.lang.RuntimePermission','setFactory', '' );
  dbms_java.grant_permission( install_schema, 'SYS:java.net.NetPermission', 'setDefaultAuthenticator', '' );      
  dbms_java.grant_permission( install_schema,'SYS:java.net.NetPermission', 'getProxySelector', '' );
end;

procedure load_jar(filename varchar2) as
  verifier_status number;
  ORACLE_HOME varchar2(100);
  trash varchar2(300) := dbms_java.endsession_and_related_state;
begin
  require_sys;
  verifier_status := dbms_java.option_controller(2,3);
  begin
    dbms_output.put_line('WCU: Loading - '||filename);
    dbms_system.get_env('ORACLE_HOME', ORACLE_HOME);
    if ORACLE_HOME is null then
      dbms_java.loadjava('-v -force -schema ' || install_schema || ' -resolver ((* ' || install_schema || ')(* SYS)(* -)) -resolve  javavm/ojvmwcu/lib/' || filename);
    else
      dbms_java.loadjava('-v -force -schema ' || install_schema || ' -resolver ((* ' || install_schema || ')(* SYS)(* -)) -resolve ' || ORACLE_HOME || '/javavm/ojvmwcu/lib/' || filename);
    end if;
    if verifier_status = 1 then
       verifier_status := dbms_java.option_controller(2,2);
    end if;
  exception when others then
    if verifier_status = 1 then
       verifier_status := dbms_java.option_controller(2,2);
    end if;
    raise;
  end;
end;

procedure drop_jar(filename varchar2) as
  verifier_status number;
  trash varchar2(300) := dbms_java.endsession_and_related_state;
begin
  require_sys;
  verifier_status := dbms_java.option_controller(2,3);
  begin
    dbms_output.put_line('WCU: Dropping - '||filename);
    dbms_java.dropjava('-v -schema ' || install_schema || ' -synonym ojvmwcu/lib/'||filename);
    if verifier_status = 1 then
       verifier_status := dbms_java.option_controller(2,2);
    end if;
  exception when others then
    if verifier_status = 1 then
       verifier_status := dbms_java.option_controller(2,2);
    end if;
    raise;
  end;
end;


procedure create_role as
begin
  require_sys;
  initjvmaux.drp('drop role ojvmwcu');
  initjvmaux.exec('create role ojvmwcu');
  initjvmaux.exec('grant ojvmwcu to SYS');
  -- grant ojvmwcu-specific guarding permisssions
  dbms_java.grant_permission( 'OJVMWCU', 'SYS:java.lang.RuntimePermission', 'ojvmwcuUser', '' );  
  dbms_java.grant_permission( 'OJVMWCU', 'SYS:java.net.SocketPermission', '*', 'connect,resolve' );
  dbms_java.grant_permission( 'OJVMWCU', 'SYS:java.lang.RuntimePermission', 'setContextClassLoader', '' );
  dbms_java.grant_permission( 'OJVMWCU', 'SYS:java.lang.RuntimePermission', 'accessDeclaredMembers', '' );
  dbms_java.grant_permission( 'OJVMWCU', 'SYS:java.lang.RuntimePermission','setFactory', '' );
  dbms_java.grant_permission( 'OJVMWCU','SYS:java.net.NetPermission', 'getProxySelector', '' );
  execute immediate 'grant ojvmwcu to '|| install_schema;
end;

procedure configure as 
begin
  grant_permissions_to_schema;
  create_role;
end;


end ojvmwcu_sys;
/
