| 1 | package Javonet::Core::Handler::CommandHandler::LoadLibraryHandler; | ||
| 2 | use strict; | ||
| 3 | use warnings FATAL => 'all'; | ||
| 4 | use lib 'lib'; | ||
| 5 | use Moose; | ||
| 6 | use File::Basename; | ||
| 7 | use Cwd qw(abs_path); | ||
| 8 | use Nice::Try; | ||
| 9 | use aliased 'Javonet::Core::Exception::Exception' => 'Exception'; | ||
| 10 | extends 'Javonet::Core::Handler::CommandHandler::AbstractCommandHandler'; | ||
| 11 | |||
| 12 | # Track loaded libraries by absolute path | ||
| 13 | our %LOADED_LIBRARIES; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift; | ||
| 17 | my $self = { | ||
| 18 | required_parameters_count => 1, | ||
| 19 | @_, # optional extras | ||
| 20 | }; | ||
| 21 | return bless $self, $class; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub process { | ||
| 25 | my ( $self, $command ) = @_; | ||
| 26 | try { | ||
| 27 | my $current_payload_ref = $command->{payload}; | ||
| 28 | my @cur_payload = @$current_payload_ref; | ||
| 29 | my $parameters_length = @cur_payload; | ||
| 30 | |||
| 31 | if ( $parameters_length != $self->{required_parameters_count} ) { | ||
| 32 | die Exception->new("Exception: LoadLibrary parameters mismatch"); | ||
| 33 | } | ||
| 34 | |||
| 35 | my $path_to_file = $command->{payload}[0]; | ||
| 36 | |||
| 37 | if ( !defined $path_to_file || $path_to_file eq '' ) { | ||
| 38 | die Exception->new( | ||
| 39 | "Cannot load module: Library path is required but was not provided" | ||
| 40 | ); | ||
| 41 | } | ||
| 42 | |||
| 43 | # --------------------------------------------------------------------- | ||
| 44 | # Normalize file extension | ||
| 45 | # If no extension is given, try "<file>.pm" | ||
| 46 | # --------------------------------------------------------------------- | ||
| 47 | my $normalized_path = $path_to_file; | ||
| 48 | |||
| 49 | if ( $normalized_path !~ /\.[^\/\\]+$/ ) { # no extension | ||
| 50 | my $with_pm = $normalized_path . '.pm'; | ||
| 51 | if ( -e $with_pm ) { | ||
| 52 | $normalized_path = $with_pm; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | # Resolve to absolute path if possible | ||
| 57 | my $absolute_path = | ||
| 58 | abs_path($normalized_path) | ||
| 59 | // abs_path($path_to_file) | ||
| 60 | // $normalized_path; | ||
| 61 | |||
| 62 | # Check that the file exists | ||
| 63 | if ( !-e $absolute_path ) { | ||
| 64 | die Exception->new("Cannot load module: Library not found: $path_to_file"); | ||
| 65 | } | ||
| 66 | |||
| 67 | # --------------------------------------------------------------------- | ||
| 68 | # Load only once by absolute path | ||
| 69 | # --------------------------------------------------------------------- | ||
| 70 | if ( $LOADED_LIBRARIES{$absolute_path} ) { | ||
| 71 | return 0; | ||
| 72 | } | ||
| 73 | |||
| 74 | my $path_to_file_dir = dirname($absolute_path); | ||
| 75 | my $file_name = basename($absolute_path); | ||
| 76 | |||
| 77 | # Ensure directory is in @INC | ||
| 78 | if ( !grep { $_ eq $path_to_file_dir } @INC ) { | ||
| 79 | push @INC, $path_to_file_dir; | ||
| 80 | } | ||
| 81 | |||
| 82 | # --------------------------------------------------------------------- | ||
| 83 | # Validate Perl module syntax before requiring | ||
| 84 | # --------------------------------------------------------------------- | ||
| 85 | #_validate_perl_syntax($absolute_path); #causes issues | ||
| 86 | |||
| 87 | # --------------------------------------------------------------------- | ||
| 88 | # Load the file | ||
| 89 | # --------------------------------------------------------------------- | ||
| 90 | eval { | ||
| 91 | require $file_name; | ||
| 92 | 1; | ||
| 93 | } or do { | ||
| 94 | my $err = $@ || 'Unknown error'; | ||
| 95 | die Exception->new("Cannot load module: $absolute_path\n$err"); | ||
| 96 | }; | ||
| 97 | |||
| 98 | # Mark library as loaded | ||
| 99 | $LOADED_LIBRARIES{$absolute_path} = 1; | ||
| 100 | |||
| 101 | return 0; | ||
| 102 | } | ||
| 103 | catch ($e) { | ||
| 104 | return Exception->new($e); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | sub get_loaded_libraries { | ||
| 109 | my ($self) = @_; | ||
| 110 | return [ keys %LOADED_LIBRARIES ]; | ||
| 111 | } | ||
| 112 | |||
| 113 | # ------------------------------------------------------------------------- | ||
| 114 | # Validate syntax using "perl -c <file>" | ||
| 115 | # ------------------------------------------------------------------------- | ||
| 116 | sub _validate_perl_syntax { | ||
| 117 | my ($absolute_path) = @_; | ||
| 118 | |||
| 119 | my $perl = $^X || 'perl'; | ||
| 120 | |||
| 121 | my $pid = open my $fh, '-|', $perl, '-c', $absolute_path; | ||
| 122 | if ( !defined $pid ) { | ||
| 123 | # If perl cannot be spawned, we silently skip validation. | ||
| 124 | return; | ||
| 125 | } | ||
| 126 | |||
| 127 | my $output = do { local $/; <$fh> // '' }; | ||
| 128 | close $fh; | ||
| 129 | my $exit_code = $? >> 8; | ||
| 130 | |||
| 131 | if ( $exit_code != 0 ) { | ||
| 132 | die Exception->new( | ||
| 133 | "Cannot load module: syntax check failed for $absolute_path\n$output" | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | no Moose; | ||
| 139 | 1; |