Rem
Rem $Header: javavm/install/bug37273789_rollback.sql /st_javavm_19.0.0.0.0ojvmru/7 2025/06/18 09:28:56 etucker Exp $
Rem
Rem bug37273789_rollback.sql
Rem
Rem Copyright (c) 2025, Oracle and/or its affiliates.
Rem
Rem    NAME
Rem      bug37273789_rollback.sql - rollback 37273789
Rem
Rem    DESCRIPTION
Rem      Remove jakarta.mail.jar and jakarta.activation.jar and
Rem      replace with previous jars.
Rem
Rem    NOTES
Rem      removes the CISA files.
Rem
Rem    BEGIN SQL_FILE_METADATA
Rem    SQL_SOURCE_FILE: javavm/install/bug37273789_rollback.sql
Rem    SQL_SHIPPED_FILE: javavm/install/bug37273789_rollback.sql
Rem    SQL_PHASE: PATCH_POSTAPPLY
Rem    SQL_STARTUP_MODE: NORMAL
Rem    SQL_IGNORABLE_ERRORS: NONE
Rem    END SQL_FILE_METADATA
Rem
Rem    MODIFIED   (MM/DD/YY)
Rem    etucker     06/17/25 - fix which version_full is used
Rem    apfwkr      06/10/25 - CI #38054190 of etucker_bug-37875572 from main
Rem                           (apfwkr_ci_backport_37875572_19.28.0.0.250715
Rem                            ojvmru).
Rem    etucker     04/01/25 - CI #37772121 of etucker_bug-37733669 from
Rem                           main
Rem    etucker     03/07/25 - add check to JAVAVM existance
Rem    etucker     02/11/25 - 19c version
Rem    etucker     05/12/25 - check for version and load differently named
Rem                           jars if necessary.
Rem
Rem set echo on
Rem set serveroutput on
@@?/rdbms/admin/sqlsessstart.sql

--
-- This will remove jakarta.mail.jar and jakarta.activation.jar classes 
-- and synonyms from SYS
--
declare 
  stat    varchar2(30);
  full_ver varchar2(256) := 0;
  indx number;
  indy number;
  rel_date varchar2(256)  := 0;
begin
  begin
    select status into stat from dba_registry where comp_id = 'JAVAVM';
  exception when no_data_found then
    stat := 'NOT FOUND';
  end;

  if stat != 'REMOVED' and stat != 'NOT FOUND' then

   execute immediate '
    declare 
      junk varchar2(30);
    begin
      junk := dbms_java.get_jdk_version;
    end;';

    declare
      name varchar2(128);
      cursor c is
      select  o.name from obj$ o, javasnm$ where (short(+) = o.name and nvl(longdbcs,o.name) like 'javax/mail/%')   and o.type#=29 and o.owner#=0;
    begin
      open c;
      loop
        fetch c into name;
        exit when c%NOTFOUND;
        begin
          execute immediate 'drop java class ' || 
            sys.dbms_assert.enquote_name(name, false);
          execute immediate 'drop public synonym ' ||
            sys.dbms_assert.enquote_name(name, false);
        exception when others then 
          if sqlcode not in (-942, -1432, -4043) then
            raise;
          end if;
        end; 
      end loop;
      close c;
    end;

    declare
      name varchar2(128);
      cursor c is
      select  o.name from obj$ o, javasnm$ where (short(+) = o.name and nvl(longdbcs,o.name) like 'com/sun/mail/%')   and o.type#=29 and o.owner#=0;
    begin
      open c;
      loop
        fetch c into name;
        exit when c%NOTFOUND;
        begin
          execute immediate 'drop java class ' || 
            sys.dbms_assert.enquote_name(name, false);
          execute immediate 'drop public synonym ' ||
            sys.dbms_assert.enquote_name(name, false);
        exception when others then 
          if sqlcode not in (-942, -1432, -4043) then
            raise;
          end if;
        end; 
      end loop;
      close c;
    end;


         
    begin
-- jakarta.mail.jar
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/gfprobe-provider.xml', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name(
         'META-INF/javamail.default.address.map', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name(
         'META-INF/javamail.default.providers',false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/hk2-locator/default', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/javamail.charset.map', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name(
         'META-INF/services/javax.mail.Provider',false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/LICENSE.txt', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/mailcap',false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/maven/com.sun.mail/jakarta.mail/pom.xml', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/maven/com.sun.mail/jakarta.mail/pom.properties', false);
-- jakarta.activation.jar
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/ORACLE_C.SF', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/ORACLE_C.RSA', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/LICENSE.md', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/mailcap.default', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/mimetypes.default', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/NOTICE.md', false);
      execute immediate 'drop java resource ' ||
        sys.dbms_assert.enquote_name('META-INF/maven/com.sun.activation/jakarta.activation/pom.xml', false);
    exception when others then 
      if sqlcode not in (-942, -1432, -4043) then
        raise;
      end if;    
    end;

-- restore mail.jar and activation.jar
-- find major and minor version
    begin
      -- get the date of the classes.bin version
      select object_name into full_ver from dba_objects where object_name 
        like '%CBRLS%';
      indx := instrb(full_ver,'.',1,4);
      indy := instrb(full_ver,' ',1,1);
      rel_date := substrb(full_ver,indx+1,indy-indx-1);
    end;

-- This is for 23.x and 19.x where 23.7 and 19.26 will need to load
-- rolled back jars with different names.
--  23.7:
--  lib/javax.mail.jar lib/activaation.jar
--  19.26 
--  lib/mail.jar lib/activation.jar
--  MAIN is really a don't care unless the names change from
--  jlib/jakarta.mail.jar and jlib/jakarta.activation.jar.

    -- 250415 == 19.27
    if (rel_date < '250415') then
      execute immediate 'begin
      dbms_java.loadjava(''-v -force -noverify -resolve -install -synonym -grant PUBLIC lib/mail.jar lib/activation.jar'');
      end;';
    else
      execute immediate 'begin
       dbms_java.loadjava(''-v -force -noverify -resolve -install -synonym -grant PUBLIC jlib/jakarta.mail.jar jlib/jakarta.activation.jar'');
     end;';
    end if;
  end if;
end;  
/
@?/rdbms/admin/sqlsessend.sql

