Inline-Java
view release on metacpan or search on metacpan
lib/Inline/Java/JVM.pm view on Meta::CPAN
package Inline::Java::JVM ;
use strict ;
use Carp ;
use IO::File ;
use IPC::Open3 ;
use IO::Socket ;
use Text::ParseWords ;
use Inline::Java::Portable ;
$Inline::Java::JVM::VERSION = '0.53_90' ;
my %SIGS = () ;
my @SIG_LIST = ('HUP', 'INT', 'PIPE', 'TERM') ;
sub new {
my $class = shift ;
my $o = shift ;
my $this = {} ;
bless($this, $class) ;
foreach my $sig (@SIG_LIST){
local $SIG{__WARN__} = sub {} ;
if (exists($SIG{$sig})){
$SIGS{$sig} = $SIG{$sig} ;
}
}
$this->{socket} = undef ;
$this->{JNI} = undef ;
$this->{embedded} = $o->get_java_config('EMBEDDED_JNI') ;
$this->{owner} = 1 ;
$this->{destroyed} = 0 ;
$this->{private} = $o->get_java_config('PRIVATE') ;
$this->{debugger} = $o->get_java_config('DEBUGGER') ;
if ($this->{embedded}){
Inline::Java::debug(1, "using embedded JVM...") ;
}
else{
Inline::Java::debug(1, "starting JVM...") ;
}
my $args = $o->get_java_config('EXTRA_JAVA_ARGS') ;
if ($o->get_java_config('JNI')){
Inline::Java::debug(1, "JNI mode") ;
# Split args and remove quotes
my @args = map {s/(['"])(.*)\1/$2/ ; $_}
parse_line('\s+', 1, $args) ;
my $jni = new Inline::Java::JNI(
$ENV{CLASSPATH} || '',
\@args,
$this->{embedded},
Inline::Java::get_DEBUG(),
$o->get_java_config('NATIVE_DOUBLES'),
) ;
$jni->create_ijs() ;
$this->{JNI} = $jni ;
}
else {
Inline::Java::debug(1, "client/server mode") ;
my $debug = Inline::Java::get_DEBUG() ;
$this->{shared} = $o->get_java_config('SHARED_JVM') ;
$this->{start_jvm} = $o->get_java_config('START_JVM') ;
$this->{port} = $o->get_java_config('PORT') ;
$this->{host} = $o->get_java_config('HOST') ;
# Used to limit the bind of the JVM server
$this->{'bind'} = $o->get_java_config('BIND') ;
# Grab the next free port number and release it.
if ((! $this->{shared})&&($this->{port} < 0)){
if (Inline::Java::Portable::portable("GOT_NEXT_FREE_PORT")){
my $sock = IO::Socket::INET->new(
Listen => 0, Proto => 'tcp',
LocalAddr => 'localhost', LocalPort => 0) ;
if ($sock){
$this->{port} = $sock->sockport() ;
Inline::Java::debug(2, "next available port number is $this->{port}") ;
close($sock) ;
}
else{
# Revert to the default.
$this->{port} = - $this->{port} ;
carp(
"Could not get next available port number, using port " .
"$this->{port} instead. Use the PORT configuration " .
( run in 1.050 second using v1.01-cache-2.11-cpan-71847e10f99 )