[libinline-java-perl] 209/398: basic code done for 0.34.

Jonas Smedegaard dr at jones.dk
Thu Feb 26 11:43:04 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.55
in repository libinline-java-perl.

commit 8a386a30ee1f2bcce9c4c8773903fc1465441d5a
Author: patrick_leb <>
Date:   Mon Jan 20 19:28:14 2003 +0000

    basic code done for 0.34.
---
 Java.pm          | 14 +++++++++++++-
 Java.pod         |  7 +++++--
 Java/Callback.pm |  4 ++++
 Java/JNI.xs      | 39 +++++++++++++++++++++++++++++--------
 Java/JVM.pm      |  6 ++++++
 Java/Makefile.PL |  2 +-
 Java/Portable.pm | 12 ------------
 Makefile.PL      |  2 +-
 README           | 20 +++++++++----------
 TODO             | 36 +----------------------------------
 t/01_init.t      |  9 +++++++--
 t/t1.pl          | 58 ++++++++++++++++++++++++++++++++++----------------------
 12 files changed, 114 insertions(+), 95 deletions(-)

diff --git a/Java.pm b/Java.pm
index 81dca78..1f951fd 100644
--- a/Java.pm
+++ b/Java.pm
@@ -7,7 +7,7 @@ package Inline::Java ;
 
 use strict ;
 
-$Inline::Java::VERSION = '0.33' ;
+$Inline::Java::VERSION = '0.34' ;
 
 
 # DEBUG is set via the DEBUG config
@@ -135,6 +135,9 @@ sub _validate {
 	if (! exists($o->{ILSM}->{JNI})){
 		$o->{ILSM}->{JNI} = 0 ;
 	}
+	if (! exists($o->{ILSM}->{EMBEDDED_JNI})){
+		$o->{ILSM}->{EMBEDDED_JNI} = 0 ;
+	}
 	if (! exists($o->{ILSM}->{CLASSPATH})){
 		$o->{ILSM}->{CLASSPATH} = '' ;
 	}
@@ -201,6 +204,15 @@ sub _validate {
 	if (defined($ENV{PERL_INLINE_JAVA_JNI})){
 		$o->{ILSM}->{JNI} = $ENV{PERL_INLINE_JAVA_JNI} ;
 	}
+	
+	if (defined($ENV{PERL_INLINE_JAVA_EMBEDDED_JNI})){
+		$o->{ILSM}->{EMBEDDED_JNI} = $ENV{PERL_INLINE_JAVA_EMBEDDED_JNI} ;
+	}
+
+	# Embedded JNI turns on regular JNI
+	if ($o->{ILSM}->{EMBEDDED_JNI}){
+		$o->{ILSM}->{JNI} = $o->{ILSM}->{EMBEDDED_JNI} ;
+	}
 
 	if (defined($ENV{PERL_INLINE_JAVA_SHARED_JVM})){
 		$o->{ILSM}->{SHARED_JVM} = $ENV{PERL_INLINE_JAVA_SHARED_JVM} ;
diff --git a/Java.pod b/Java.pod
index 6ed2151..24b9b49 100644
--- a/Java.pod
+++ b/Java.pod
@@ -626,7 +626,8 @@ dealt with and should be thrown back all the way up to the function that was
 initially called by Perl. The latter indicates that the Perl callback threw
 an exception (die() or croak()). The value of $@ (this can be a scalar or 
 any valid "Inline::Java" object) can be retreived using the GetObject method
-of the PerlException object.
+of the PerlException object (if you are sure that $@ was a Perl scalar, you
+can use the GetString method).
    Z<>
 
 
@@ -799,6 +800,8 @@ static members in the Java code.
 If processes not forked off the parent are connecting to the shared JVM, the 
 parent's CLASSPATH must be set properly or else the parent will not see these
 classes. See USING MULTIPLE SECTIONS for more details.
+
+Note: The Java System.out stream is closed int SHARED_JVM mode.
    Z<>
 
 
@@ -976,7 +979,7 @@ rebuilt to match the Perl code.
 
 =head1 AUTHOR
 
-Patrick LeBoutillier <patl at cpan.org>.
+Patrick LeBoutillier <patl at cpan.org> is the author of Inline::Java.
 
 Brendan W. McAdams <bwmcadams at cpan.org> is a contributor.
 
diff --git a/Java/Callback.pm b/Java/Callback.pm
index ca227f8..97a5f6a 100644
--- a/Java/Callback.pm
+++ b/Java/Callback.pm
@@ -120,6 +120,10 @@ public class InlineJavaPerlCaller {
 		public Object GetObject(){
 			return obj ;
 		}
+
+		public String GetString(){
+			return (String)obj ;
+		}
 	}
 
 
diff --git a/Java/JNI.xs b/Java/JNI.xs
index be4fb7d..84b6372 100644
--- a/Java/JNI.xs
+++ b/Java/JNI.xs
@@ -9,13 +9,14 @@
 
 /* JNI structure */
 typedef struct {
-	JavaVM 	*jvm ;
-	jclass	ijs_class ;
-	jclass	string_class ;
+	JavaVM *jvm ;
+	jclass ijs_class ;
+	jclass string_class ;
 	jobject	ijs ;
 	jmethodID jni_main_mid ;
 	jmethodID process_command_mid ;
 	jint debug ;
+	int embedded ;
 	int destroyed ;
 } InlineJavaJNIVM ;
 
@@ -116,9 +117,10 @@ PROTOTYPES: DISABLE
 
 
 InlineJavaJNIVM * 
-new(CLASS, classpath, debug)
+new(CLASS, classpath, embedded, debug)
 	char * CLASS
 	char * classpath
+	int	embedded
 	int	debug
 
 	PREINIT:
@@ -135,6 +137,7 @@ new(CLASS, classpath, debug)
 		croak("Can't create InlineJavaJNIVM") ;
 	}
 	RETVAL->ijs = NULL ;
+	RETVAL->embedded = embedded ;
 	RETVAL->debug = debug ;
 	RETVAL->destroyed = 0 ;
 
@@ -148,11 +151,31 @@ new(CLASS, classpath, debug)
 	vm_args.nOptions = 2 ;
 	vm_args.ignoreUnrecognized = JNI_FALSE ;
 
-	/* Create the Java VM */
-	res = JNI_CreateJavaVM(&(RETVAL->jvm), (void **)&(env), &vm_args) ;
-	if (res < 0) {
-		croak("Can't create Java interpreter using JNI") ;
+	/* Embedded patch and idea by Doug MacEachern */
+	if (RETVAL->embedded) {
+		/* We are already inside a JVM */
+		jint n = 0 ;
+
+		res = JNI_GetCreatedJavaVMs(&(RETVAL->jvm), 1, &n) ;
+		if (n <= 0) {
+			/* res == 0 even if no JVMs are alive */
+			res = -1;
+		}
+		if (res < 0) {
+			croak("Can't find any created Java JVMs") ;
+		}
+
+		env = get_env(RETVAL) ;
+		RETVAL->destroyed = 1 ; /* do not shutdown, we did not create it */
 	}
+	else {
+		/* Create the Java VM */
+		res = JNI_CreateJavaVM(&(RETVAL->jvm), (void **)&(env), &vm_args) ;
+		if (res < 0) {
+			croak("Can't create Java JVM using JNI") ;
+		}
+	}
+
 	free(cp) ;
 
 
diff --git a/Java/JVM.pm b/Java/JVM.pm
index 2ac123f..44ee883 100644
--- a/Java/JVM.pm
+++ b/Java/JVM.pm
@@ -42,6 +42,7 @@ sub new {
 
 		my $jni = new Inline::Java::JNI(
 			$ENV{CLASSPATH} || "",
+			$o->get_java_config('EMBEDDED_JNI'),
 			Inline::Java::get_DEBUG(),
 		) ;
 		$jni->create_ijs() ;
@@ -322,10 +323,15 @@ sub process_command {
 	my $data = shift ;
 
 	my $resp = undef ;
+	# Patch by Simon Cozens for perl -wle 'use Our::Module; do_stuff()'
+	local $/ = "\n" ;
+	local $\ = "" ;
+	# End Patch
 	while (1){
 		Inline::Java::debug(3, "packet sent is $data") ;
 
 		if ($this->{socket}){
+
 			my $sock = $this->{socket} ;
 			print $sock $data . "\n" or
 				croak "Can't send packet to JVM: $!" ;
diff --git a/Java/Makefile.PL b/Java/Makefile.PL
index 111fddb..d335fd4 100644
--- a/Java/Makefile.PL
+++ b/Java/Makefile.PL
@@ -49,7 +49,7 @@ print
 	"(JVM) to be dynamically linked with Perl instead of running as a separate\n" .
 	"process. The use of this extension is optional, and building it still\n" .
 	"allows Inline::Java to run the JVM in the default (separate process)\n" .
-	"fashion.\n\n" ;
+	"fashion. Note: You need a C compiler to build the extension.\n\n" ;
 
 if (($build_jni || AskYN("Do you wish to build the JNI extension?"))){
 	print "\nBuilding JNI extension.\n\n" ;
diff --git a/Java/Portable.pm b/Java/Portable.pm
index 671f96f..8403b7c 100644
--- a/Java/Portable.pm
+++ b/Java/Portable.pm
@@ -45,12 +45,6 @@ sub mkpath {
 	my $o = shift ;
 	my $path = shift ;
 
-	if ($Inline::VERSION <= 0.43){
-		my $sep = File::Spec->catdir('', '') ;
-		$sep = quotemeta($sep) ;
-		$path =~ s/$sep/\//g ;
-	}
-	
 	return $o->Inline::mkpath($path) ;
 } ;
 
@@ -63,12 +57,6 @@ sub rmpath {
 	my $prefix = shift ;
 	my $path = shift ;
 	
-	if ($Inline::VERSION <= 0.43){
-		my $sep = File::Spec->catdir('', '') ;
-		$sep = quotemeta($sep) ;
-		$path =~ s/$sep/\//g ;
-	}
-	
 	return $o->Inline::rmpath($prefix, $path) ;
 } ;
 
diff --git a/Makefile.PL b/Makefile.PL
index efbba43..3d80d06 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -15,7 +15,7 @@ WriteMakefile(
 	NAME => 'Inline::Java',
 	VERSION_FROM => 'Java.pm',
 	PREREQ_PM => {
-		Inline	=> 0.43
+		Inline	=> 0.44
 	},
 	clean => {FILES => '_Inline_test/'},
 ) ;
diff --git a/README b/README
index fc43f4c..85d7c91 100644
--- a/README
+++ b/README
@@ -7,12 +7,12 @@ Inline::Java lets you write Perl classes in Java.
 Example:
 
     use Inline Java => <<'END';
-	    class JAxH {
-			public JAxH(String x){
-				System.out.println("Just Another " + x + " Hacker") ;
-			}
-	    }
-	END
+        class JAxH {
+            public JAxH(String x){
+                System.out.println("Just Another " + x + " Hacker") ;
+            }
+        }
+    END
     
     new JAxH('Inline') ;
 
@@ -131,13 +131,13 @@ INFORMATION:
 
 = For more information on Inline::Java, see 'perldoc Inline::Java'.
 = For information about Inline, see 'perldoc Inline'.
-= For information on using Java, visit http://java.sun.org
+= For information on using Java, visit http://java.sun.org.
 
 Inline::Java's mailing list is inline at perl.org. 
-To subscribe, send an email to inline-subscribe at perl.org
+To subscribe, send an email to inline-subscribe at perl.org.
 
-Inline::Java's home page is http://inline.perl.org/java/
+Inline::Java's home page is http://inline.perl.org/java/.
 
-Please send questions and comments to Patrick LeBoutillier <patl at cpan.org>
+Please send questions and comments to Patrick LeBoutillier <patl at cpan.org>.
 
 Copyright (c) 2001-2002, Patrick LeBoutillier. All Rights Reserved.  
diff --git a/TODO b/TODO
index c504bf9..0390c62 100644
--- a/TODO
+++ b/TODO
@@ -1,40 +1,6 @@
 CODE:
 - Check INFO flag vs. PRINT_INFO (shortcuts not working?)
-- Fix for Java 1.4 (reflection inverting method/member order)
-- Improve documentation of issues:
-- Include patch by Doug MacEachern
-
-
-These tips should get you going:
-
- - Take note that in SHARED_JVM mode, System.out is closed(). This is
- necessary
-   in order for the process to detach itself properly on certain platforms.
- Anyway only the
-   processs that actually started the JVM could see the output. Maybe a log
- file would be a good
-   idea... I'll think about that.
-
- - Try running it without SHARED_JVM first. It's a lot easier to debug. First
- you get System.out, second
-   JVM restarts each time, so when you change your Java code you don't need
- to kill the JVM manually.
-   Anyways I don't think in your case your need SHARED_JVM. SHARED_JVM is
- really meant to be used
-   when your Perl script is a CGI/mod_perl module where you get many requests
- and you can't afford
-   the JVM startup delay every time. It's also useful if you need to share
- data betwen each of your script
-   invocations (if your use 'static' data in your Java class, all the script
- invocations can access the data and it's
-   persistent between accesses).
-
-
- I'm taking note of these details and I'll beef up the documentation of these
- issues.
-
-
-
+- MAkefile.PL: ExtUtils::MakeMaker::prompt
 
 TEST:
 - Alpha
diff --git a/t/01_init.t b/t/01_init.t
index cd82299..4cdc2ae 100644
--- a/t/01_init.t
+++ b/t/01_init.t
@@ -15,9 +15,14 @@ use Inline (
 
 
 my $ver = types1->version() ;
-print STDERR "\nJ2SDK version is $ver\n" ;
+print STDERR "\nInline version is $Inline::VERSION\n" ;
+print STDERR "Inline::Java version is $Inline::Java::VERSION\n" ;
+print STDERR "J2SDK version is $ver\n" ;
 
-if ($ENV{PERL_INLINE_JAVA_JNI}){
+if ($ENV{PERL_INLINE_JAVA_EMBEDDED_JNI}){
+	print STDERR "Using JNI extension (embedded).\n" ;
+}
+elsif ($ENV{PERL_INLINE_JAVA_JNI}){
 	print STDERR "Using JNI extension.\n" ;
 }
 
diff --git a/t/t1.pl b/t/t1.pl
index ca11d9b..c1667e0 100755
--- a/t/t1.pl
+++ b/t/t1.pl
@@ -3,31 +3,43 @@ use strict ;
 use blib ;
 
 
-BEGIN {
-	mkdir('./_Inline_test', 0777) unless -e './_Inline_test';
+use Inline Java => <<'END_OF_JAVA_CODE' ;
+   class Pod_alu extends InlineJavaPerlCaller {
+      public Pod_alu(){
+      }
+
+      public int add(int i, int j) throws InlineJavaException {
+         try {
+            CallPerl("main", "tt", null) ;
+            CallPerl("main", "tt", new Object [] {"hello"}) ;
+            CallPerl("main", "tt", new Object [] {"die"}) ;
+         }
+         catch (PerlException pe){
+			System.out.println("perl died : " + (String)pe.GetObject()) ;
+         }
+		
+         return i + j ;
+      }
+
+      public int subtract(int i, int j){
+         return i - j ;
+      }
+   }   
+END_OF_JAVA_CODE
+
+
+sub tt {
+	my $arg = shift ;
+
+	print "$arg: it works!\n" ;
+	if ($arg eq "die"){
+		die("ouch!") ;
+	}
 }
 
-use Inline Config => 
-           DIRECTORY => './_Inline_test' ;
 
-use Inline (
-	Java => qq|
-		class t  {
-			public java.util.ArrayList al [] = new java.util.ArrayList[5] ;
-
-			public t(){
-				al[0] = new java.util.ArrayList() ;
-			}
-		}
-	|, 
-	# PRINT_INFO => 1,
-	STUDY => ['java.util.ArrayList'],
-) ;
-
-Inline::Java::release_JVM() ;
-
-my $t = new t() ;
-$t->{al}->[0]->add("allo") ;
-print $t->{al}->[0]->get(0) . "\n" ;
+my $alu = new Pod_alu() ;
+print($alu->add(9, 16) . "\n") ; # prints 25
+print($alu->subtract(9, 16) . "\n") ; # prints -7
 
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libinline-java-perl.git



More information about the Pkg-perl-cvs-commits mailing list