package RDA::Profile; # $Id: Profile.pm,v 1.12 2009/08/05 12:34:52 mschenke Exp $ # ARCS: $Header: /u02/webapps/workbench/cvs/RDA_4/src/scripting/lib/RDA/Profile.pm,v 1.12 2009/08/05 12:34:52 mschenke Exp $ # # Change History # 20090805 MSC Improve profile file checking. =head1 NAME RDA::Profile - Class Used for Objects to Manage Profiles =head1 SYNOPSIS require RDA::Profile; =head1 DESCRIPTION The objects of the C class are used to manage profiles. The following methods are available: =cut use strict; BEGIN { use Exporter; use File::Spec; use IO::File; } # Define the global public variables use vars qw($VERSION); $VERSION = sprintf("%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/); # Define the global private variables # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Profile-Enew($dir,$agt)> The object constructor. It takes the module directory and the agent reference as arguments. C is represented by a blessed hash reference. The following special keys are used: =over 12 =item S< B<'_agt'> > Reference to the agent object =item S< B<'_cas'> > Indicates a case sensitive context =item S< B<'_dir'> > Module directory =item S< B<'_lvl'> > Level definitions =item S< B<'_prf'> > Profile definitions =back Internal keys are prefixed by an underscore. =cut sub new { my $slf = shift; my $cls = ref($slf) || $slf; my $dir = shift; my $agt = shift; # Create the render object $slf = bless {}, $cls; # Get some setup information $slf->{'_agt'} = $agt; $slf->{'_cas'} = $agt ? $agt->get_setting('RDA_CASE', 1) : 1; $slf->{'_dir'} = $dir; # Return the object reference $slf; } =head2 S<$h-Echk_profile([$name])> This method indicates if the specified setting level and profile name are current. =cut sub chk_profile { my ($slf, $nam) = @_; my ($agt, $lvl, @prf); # No change by default return 1 unless $nam; # Load the profile definition when not yet done $slf->load unless exists($slf->{'_lvl'}); # Separate profile and setting level $lvl = 0; foreach my $itm (split(/\-/, $nam)) { if (exists($slf->{'_lvl'}->{$itm})) { $lvl = $slf->{'_lvl'}->{$itm}; } elsif (exists($slf->{'_prf'}->{$itm})) { push(@prf, $itm); } else { die "RDA-00122: Invalid profile name '$itm'\n"; } } # Compare setting level and profile name $agt = $slf->{'_agt'}; $lvl == $agt->get_setting('RDA_LEVEL',0) && (scalar(@prf) == 0 || join('-', @prf) eq $agt->get_setting('RDA_PROFILE','')); } =head2 S<$h-Eget_levels> This method returns the list of all setting levels. It loads the profile definitions when not yet done. =cut sub get_levels { my $slf = shift; # Load the profile definition when not yet done $slf->load unless exists($slf->{'_lvl'}); # Return the setting level list keys(%{$slf->{'_lvl'}}); } =head2 S<$h-Eget_profile> This method returns the list of the current profile components. =cut sub get_profile { split(/-/, shift->{'_agt'}->get_setting('RDA_PROFILE','')); } =head2 S<$h-Eget_profiles> This method returns the list of all defined profiles. It loads the profile definitions when not yet done. =cut sub get_profiles { my $slf = shift; # Load the profile definition when not yet done $slf->load unless exists($slf->{'_prf'}); # Return the profile list keys(%{$slf->{'_prf'}}); } =head2 S<$h-Eget_title($name,[$default])> This method returns the description of the specified profile, or the default value when not found. =cut sub get_title { my ($slf, $nam, $ttl) = @_; return $ttl unless $nam; my $cur = $slf->{'_prf'}->{$nam}; exists($cur->{"?$nam"}) ? $cur->{"?$nam"} : exists($cur->{"?"}) ? $cur->{"?"} : $ttl; } =head2 S<$h-Eload([$file[,$flag]])> This method loads the setup profile definitions from the specified file. When no file is specified, it uses the file specified by the C environment variable or C by default. When the flag is set, it raises an exception when encountering load errors. It returns the reference to the profile object. =cut sub load { my ($slf, $fil, $flg) = @_; my ($cas, $cur, $err, $key, $lin, $pos, $val); # Select the profile definition file if ($fil) { $fil = File::Spec->catfile($slf->{'_dir'}, $fil) unless -r $fil; } else { $fil = File::Spec->catfile($slf->{'_dir'}, 'rda.cfg') unless ($fil = $ENV{'RDA_PROFILE'}) && -r $fil; } open(IN, "<$fil") || die "RDA-00112: Cannot open the profile definition file $fil:\n $!\n"; # Load the profile definition $slf->{'_lvl'} = {}; $slf->{'_prf'} = {}; $cas = $slf->{'_cas'}; $pos = $err = 0; $lin = ''; while () { # Trim leading spaces s/^\s+//; s/[\r\n]+$//; $lin .= $_; # Join continuation line $pos++; next if $lin =~ s/\\$//; $lin =~ s/\s+$//; # Parse the line eval { if ($lin =~ s/^(\w+|\?\w*)\s*=\s*//) { $key = $1; if ($lin =~ s/^'([^']*)'// || $lin =~ s/^"([^"]*)"//) { $val = $1; $val =~ s/&\#34;/"/; $val =~ s/&\#39;/'/; } else { $val = $lin; $lin = ''; } die "RDA-00113: Invalid profile specification value\n" unless $lin =~ m/^\s*(#.*)?$/; if ($cur) { $cur->{$key} = $val; } elsif ($key !~ m/^\?/) { $slf->{'_lvl'}->{$key} = $val; } } elsif ($lin =~ s/^\[([\w\|]+)\]$//) { $cur = {}; foreach $key (split(/\|/, $1)) { $slf->{'_prf'}->{$key} = $cur; $slf->{'_prf'}->{lc($key)} = $cur unless $cas; } } elsif ($cur && $lin =~ s/^\*\s*=\s*([\w\,]+)//) { $cur->{'*'} = $1; die "RDA-00114: Invalid profile module list\n" unless $lin =~ m/^\s*(#.*)?$/; } elsif ($lin !~ m/^(#.*)?$/) { die "RDA-00115: Unexpected profile specification\n"; } }; # Report an error if ($@) { my $msg = $@; $msg =~ s/\n$//; last if $msg =~ m/^last/; $err++; print $msg; print " in the profile definition file near line $pos" if $pos; print "\n"; } # Prepare the next line $lin = ''; } close(IN); # Terminate if errors are encountered die "RDA-00116: Error(s) in profile definition file $fil\n" if $flg && $err; # Return the object reference $slf; } =head2 S<$h-Eset_profile([$name])> This method sets the setup profile. When there is no name specified, it uses the current setup profile name. It returns the list of modules associated with that profile. =cut sub set_profile { my ($slf, $nam) = @_; my ($agt, $cur, @prf, %mod); # Load the profile definition when not yet done $slf->load unless exists($slf->{'_prf'}); # Set the setting level and extract profile name components $agt = $slf->{'_agt'}; if ($nam) { foreach my $itm (split(/\-/, $nam)) { if (exists($slf->{'_lvl'}->{$itm})) { $agt->set_setting('RDA_LEVEL', $slf->{'_lvl'}->{$itm}); } else { push(@prf, $itm); } } } # Apply the profile @prf = split(/\-/, $agt->get_setting('RDA_PROFILE','')) unless scalar(@prf); foreach my $itm (@prf) { die "RDA-00122: Invalid profile name '$itm'\n" unless exists($slf->{'_prf'}->{$itm}); $cur = $slf->{'_prf'}->{$itm}; foreach my $key (keys(%$cur)) { if ($key eq '*') { foreach my $mod (split(/\,/, $cur->{$key})) { $mod{$mod} = 1; } } elsif ($key =~ /^\w/) { $agt->set_temp_setting($key, $cur->{$key}); } } } $agt->set_setting('RDA_PROFILE',join('-', @prf)) if scalar(@prf); # Return the module list sort keys(%mod); } =head2 S<$h-Exref> This method produces a cross-reference of the profile definitions and the related modules. =cut sub xref { my $slf = shift; my $agt = $slf->{'_agt'}; if (-t STDOUT ) { my $pag = exists($ENV{'PAGER'}) ? $ENV{'PAGER'} : $agt->is_vms ? 'TYPE/PAGE=SAVE SYS$INPUT' : 'more'; open(OUT, "| $pag") || die "RDA-00120: Cannot open pager:\n $!\n"; _xref($slf, $agt, *OUT); close(OUT); } else { _xref($slf, $agt, *STDOUT); } wait(); } sub _xref { my ($slf, $agt, $out) = @_; my (%tb_bad, %tb_mod, %tb_prf); # Load the profile definition when not yet done $slf->load unless exists($slf->{'_prf'}); # Get the module list foreach my $mod ($agt->get_modules) { $mod =~ s/\.(ctl|def)$//i; $tb_mod{$mod} = []; } # Analyze the profiles foreach my $prf (sort keys(%{$slf->{'_prf'}})) { $tb_prf{$prf} = []; if (exists($slf->{'_prf'}->{$prf}->{'*'})) { foreach my $mod (split(',', $slf->{'_prf'}->{$prf}->{'*'})) { if (exists($tb_mod{$mod})) { push(@{$tb_mod{$mod}}, $prf); } else { push(@{$tb_bad{$mod}}, $prf); } push(@{$tb_prf{$prf}}, $mod); } } } # Produce the cross-reference $agt->dsp_string($out, '', "Profile Cross Reference\n"); _xref_dsp($slf, $agt, $out, \%tb_prf, '-', 'Defined Profiles'); _xref_dsp($slf, $agt, $out, \%tb_mod, '', 'Referenced Modules'); _xref_dsp($slf, $agt, $out, \%tb_bad, '', 'Unknown Modules'); } # Display a result set sub _xref_dsp { my ($slf, $agt, $out, $tbl, $dft, $ttl) = @_; my ($lgt, $max, @tbl); return unless ref($tbl) eq 'HASH'; @tbl = sort keys(%$tbl); if (scalar(@tbl)) { # Determine the name length $max = 0; foreach my $nam (@tbl) { $max = $lgt if ($lgt = length($nam)) > $max; } # Display the table $agt->dsp_string($out, '', "\n$ttl:\n"); foreach my $nam (@tbl) { $agt->dsp_string($out, sprintf(" %-*s ", $max, $nam), join(', ', @{$tbl->{$nam}}) || $dft); } } } 1; __END__ =head1 SEE ALSO L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L =head1 COPYRIGHT NOTICE Copyright (c) 2002-2009, Oracle. All rights reserved. =head1 TRADEMARK NOTICE Oracle, JD Edwards, PeopleSoft, and Siebel are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. =cut