# Instance.pm: Class Used for Managing Oracle Instances package RDA::Target::Instance; # $Id: Instance.pm,v 1.11 2015/05/05 13:08:42 RDA Exp $ # ARCS: $Header: /home/cvs/cvs/RDA_8/src/scripting/lib/RDA/Target/Instance.pm,v 1.11 2015/05/05 13:08:42 RDA Exp $ # # Change History # 20150505 MSC Improve the documentation. =head1 NAME RDA::Target::Instance - Class Used for Managing Oracle Instances =head1 SYNOPSIS require RDA::Target::Instance; =head1 DESCRIPTION The objects of the C class are used to interface with Oracle instances. It is a subclass of L. The following methods are available: =cut use strict; BEGIN { use Exporter; use IO::File; use RDA::Text qw(get_string); use RDA::Object; use RDA::Object::Rda; use RDA::Object::Target; } # Define the global public variables use vars qw($STRINGS $VERSION @DUMP @ISA %SDCL); $VERSION = sprintf('%d.%02d', q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/); @DUMP = ( hsh => {'RDA::Target::Base' => 1, 'RDA::Target::Common' => 1, 'RDA::Target::Database' => 1, 'RDA::Target::Db' => 1, 'RDA::Target::Dbi' => 1, 'RDA::Target::Domain' => 1, 'RDA::Target::Home' => 1, 'RDA::Target::Instance' => 1, 'RDA::Target::MwHome' => 1, 'RDA::Target::System' => 1, 'RDA::Target::WlHome' => 1, }, ); @ISA = qw(RDA::Object::Target RDA::Object Exporter); %SDCL = ( dep => [qw(RDA::Target::Home)], inc => [qw(RDA::Object::Target RDA::Object)], ); # Define the global private constants # Define the global private variables my %tb_nat = map {$_ => 1} qw(ins tns); # Report the package version sub Version { return $VERSION; } =head2 S<$h = RDA::Target::Instance-Enew($oid,$col,$def,$par[,$edt])> The object constructor. It takes the object identifier, the collector object reference, the definition item reference, the parent target reference, and an optional initial attribute hash reference as arguments. Do not use this constructor directly. Create all targets using the L methods. C is represented by a blessed hash reference. The following special keys are used: =over 12 =item S< B<'col' > > Reference to the collector object =item S< B<'ins' > > Instance home directory =item S< B<'oid' > > Object identifier =item S< B<'par' > > Reference to the parent target =item S< B<'raw' > > Raw value indicator =item S< B<'tns' > > TNS_ADMIN specification =item S< B<'_abr'> > Symbol definition hash =item S< B<'_bkp'> > Backup of environment variables =item S< B<'_chg'> > Symbol change hash =item S< B<'_chl'> > List of the child keys =item S< B<'_def'> > Reference to the target definition item =item S< B<'_env'> > Environment specifications =item S< B<'_fcs'> > Focus hash =item S< B<'_prs'> > Symbol detection parse tree =item S< B<'_shr'> > Share indicator =item S< B<'_typ'> > Target type =back Internal keys are prefixed by an underscore. =cut sub new { my ($cls, $oid, $col, $def, $par, $edt) = @_; my ($flg, $key, $raw, $slf, $tgt, $val); # Create the Oracle instance object $raw = $def->get_first('B_RAW', 0); $slf = bless { col => $col, oid => $par->get_unique($oid), par => $par, raw => $raw, _chl => [], _def => $def, _fcs => {}, _shr => $def->get_first(['B_DEDICATED_INSTANCE','B_DEDICATED']) ? 0 : 1, _typ => 'OI', }, ref($cls) || $cls; # Load the target definition $slf->{'ins'} = $val if defined($val = $def->get_first('D_ORACLE_INSTANCE', undef, $raw)); $slf->{'tns'} = undef if $def->get_first('B_NO_TNS_ADMIN'); $slf->{'tns'} = $val if defined($val = $def->get_first('D_TNS_ADMIN', undef, $raw)); # Add the initial attributes if ($edt) { foreach my $key (keys(%{$edt})) { $slf->{$key} = exists($tb_nat{$key}) ? RDA::Object::Rda->native($edt->{$key}) : $edt->{$key}; } } # Load the associated Oracle home target unless ($def->get_first('B_MISSING_HOME')) { if (defined($val = $def->get_first('I_ORACLE_HOME'))) { $slf->{'_hom'} = $slf->add_target($val); push(@{$slf->{'_chl'}}, '_hom'); } elsif (defined($val = $def->get_first('W_ORACLE_HOME'))) { $slf->{'_hom'} = $slf->get_target($val); push(@{$slf->{'_chl'}}, '_hom'); } elsif (defined($val = _find_home($slf, $def))) { $oid =~ s/^OI_/OH_/i; $tgt = ($flg = $def->get_prime('B_DEDICATED_HOME')) ? undef : $par->find_target('OH', hom => RDA::Object::Rda->native($val)); $slf->{'_hom'} = $tgt || $slf->add_target($oid, {B_DEDICATED_HOME => $flg, B_RAW => $raw, D_ORACLE_HOME => $val, T_OH_ABBR => $def->get_prime('T_OH_ABBR'), }); push(@{$slf->{'_chl'}}, '_hom'); } } # Initiate the symbol management when applicable unless (RDA::Object::Rda->is_vms) { $slf->{'_abr'} = {}; exists($slf->{'ins'}) ? $slf->set_symbol($def->get_first('T_OI_ABBR'), $slf->{'ins'}) : $slf->init_symbols; delete($slf->{'_chg'}); } # Return the object reference return $slf; } =head2 S<$h-Eget_env> This method returns the environment variable specifications as a hash reference. =cut sub get_env { my ($slf) = @_; # Determine the environment specifications on first usage unless (exists($slf->{'_env'})) { my ($dft, $dir, $env, $ins, $lib, $sep, %tbl); # Get the default specifications $dft = exists($slf->{'_hom'}) ? $slf->{'_hom'} : $slf->get_default; $slf->{'_env'} = $env = {%{$dft->get_env}}; # Add the target specifications if (exists($slf->{'ins'}) && !RDA::Object::Rda->is_vms) { $sep = RDA::Object::Rda->get_separator; # Align the environment and the settings $env->{'ORACLE_INSTANCE'} = $ins = $slf->{'ins'}; $env->{'TNS_ADMIN'} = defined($slf->{'tns'}) ? $slf->{'tns'} : undef if exists($slf->{'tns'}); # Adapt the command path %tbl = map {$_ => 1} split(/$sep/, $env->{'PATH'}); $dir = RDA::Object::Rda->cat_dir($ins, 'bin'); $env->{'PATH'} = join($sep, $dir, $env->{'PATH'}) unless exists($tbl{$dir}) ## no critic (Unless) || !defined($dir = $slf->get_top('sys')->is_restricted($dir)); # Adapt the shared library path if (defined($lib = RDA::Object::Rda->get_shlib)) { %tbl = map {$_ => 1} split(/$sep/, $env->{$lib}); $slf->{'_env'}->{$lib} = join($sep, $dir, $env->{$lib}) if -d ($dir = RDA::Object::Rda->cat_dir($ins, 'lib')) && !exists($tbl{$dir}); } } } # Return the environment specifications return $slf->{'_env'}; } # --- Internal routines ------------------------------------------------------- # Find the Oracle home directory sub _find_home { my ($slf, $def) = @_; my ($dir, $ifh); # Check the definition return $dir if defined($dir = $def->get_prime('D_ORACLE_HOME', undef, $slf->{'raw'})); # Try to detect the Oracle home directory unless ($def->get_first('B_NO_DETECT')) { $ifh = IO::File->new; if (RDA::Object::Rda->is_windows || RDA::Object::Rda->is_cygwin) { if ($ifh->open('<'.RDA::Object::Rda->cat_file($slf->{'ins'}, 'bin', 'opmnctl.bat'))) { while (<$ifh>) { if (m/^\s*set\s+ORACLE_HOME=(.*?)[\n\r\s]*$/) { $dir = RDA::Object::Rda->cat_dir($1) if length($1); } } $ifh->close; } } else { if ($ifh->open('<'.RDA::Object::Rda->cat_file($slf->{'ins'}, 'bin', 'opmnctl'))) { while (<$ifh>) { if (m/^\s*\$OracleHome\s*=([\042\047])(.*?)\1/) { $dir = RDA::Object::Rda->cat_dir($2) if length($2); } } $ifh->close; } } } return $dir; } 1; __END__ =head1 SEE ALSO L, L, L, L, L, L, L, L =head1 COPYRIGHT NOTICE Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved. =head1 TRADEMARK NOTICE Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. =cut