[libinline-java-perl] 245/398: ok

Jonas Smedegaard dr at jones.dk
Thu Feb 26 11:43:10 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 89c921478f006ced6c59a743ca325d97963303d8
Author: patrick_leb <>
Date:   Thu Nov 20 03:42:05 2003 +0000

    ok
---
 Java/sources/InlineJavaPerlCaller.java |  16 +++-
 Java/sources/InlineJavaServer.java     | 150 ++++++++++++++++++---------------
 t/swing_callback.pl                    |  55 ++++++++++++
 3 files changed, 151 insertions(+), 70 deletions(-)

diff --git a/Java/sources/InlineJavaPerlCaller.java b/Java/sources/InlineJavaPerlCaller.java
index 2a14780..928c4da 100644
--- a/Java/sources/InlineJavaPerlCaller.java
+++ b/Java/sources/InlineJavaPerlCaller.java
@@ -4,7 +4,16 @@ package org.perl.inline.java ;
 	Callback to Perl...
 */
 public class InlineJavaPerlCaller {
-	public InlineJavaPerlCaller(){
+	private Thread creator ;
+
+	public InlineJavaPerlCaller() throws InlineJavaException {
+		Thread t = Thread.currentThread() ;
+		if (InlineJavaServer.GetInstance().IsThreadPerlContact(t)){
+			creator = t ;
+		}
+		else{
+			throw new InlineJavaException("InlineJavaPerlCaller objects can only be created by threads that communicate directly with Perl") ;
+		}
 	}
 
 
@@ -16,4 +25,9 @@ public class InlineJavaPerlCaller {
 	public Object CallPerl(String pkg, String method, Object args[], String cast) throws InlineJavaException, InlineJavaPerlException {
 		return InlineJavaServer.GetInstance().Callback(pkg, method, args, cast) ;
 	}
+
+	
+	public void wait_for_callbacks(){
+		// Not sure how this will work just yet...
+	}
 }
diff --git a/Java/sources/InlineJavaServer.java b/Java/sources/InlineJavaServer.java
index aa118bc..92a6dba 100644
--- a/Java/sources/InlineJavaServer.java
+++ b/Java/sources/InlineJavaServer.java
@@ -18,6 +18,7 @@ public class InlineJavaServer {
 	private HashMap thread_objects = new HashMap() ;
 	private int objid = 1 ;
 	private boolean jni = false ;
+	private Thread creator = null ;
 
 
 	// This constructor is used in JNI mode
@@ -25,7 +26,7 @@ public class InlineJavaServer {
 		init(d) ;
 
 		jni = true ; 
-		thread_objects.put(Thread.currentThread(), new HashMap()) ;
+		thread_objects.put(creator, new HashMap()) ;
 	}
 
 
@@ -73,6 +74,7 @@ public class InlineJavaServer {
 
 	private void init(int debug){
 		instance = this ;
+		creator = Thread.currentThread() ;
 		InlineJavaUtils.debug = debug ;
 
 		ijucl = new InlineJavaUserClassLoader() ;
@@ -150,75 +152,11 @@ public class InlineJavaServer {
 		return resp ;
 	}
 
-	
-	Object GetObject(int id) throws InlineJavaException {
-		Object o = null ;
-		HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
-
-		if (h == null){
-			throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
-		}
-		else{
-			o = h.get(new Integer(id)) ;
-			if (o == null){
-				throw new InlineJavaException("Can't find object " + id + " for thread " +Thread.currentThread().getName()) ;
-			}
-		}
-
-		return o ;
-	}
-
-
-	synchronized int PutObject(Object o) throws InlineJavaException {
-		HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
-
-		int id = objid ;
-		if (h == null){
-			throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
-		}
-		else{
-			h.put(new Integer(objid), o) ;
-			objid++ ;
-		}
-
-		return id ;
-	}
-
-
-	Object DeleteObject(int id) throws InlineJavaException {
-		Object o = null ;
-		HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
-
-		if (h == null){
-			throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
-		}
-		else{
-			o = h.remove(new Integer(id)) ;
-			if (o == null){
-				throw new InlineJavaException("Can't find object " + id + " for thread " + Thread.currentThread().getName()) ;
-			}
-		}
-
-		return o ;
-	}
-
 
-	int ObjectCount() throws InlineJavaException {
-		int i = -1 ;
-		HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
-
-		if (h == null){
-			throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
-		}
-		else{
-			i = h.values().size() ;
-		}
-
-		return i ;
-	}
+	native private String jni_callback(String cmd) ;
 
 
-	// So far this is the only method that can be called (indirectly) by the 
+	// So far this is the only method that can be called (indirectly) by the
 	// user code to get back in to Perl. This means that this method really
 	// can be called by other threads the are not InlineJavaServerThreads...
 	//
@@ -269,7 +207,7 @@ public class InlineJavaServer {
 					}
 
 					break ;
-				}	
+				}
 				else{
 					// Pass it on through the regular channel...
 					InlineJavaUtils.debug(3, "packet is not callback response: " + resp) ;
@@ -287,7 +225,81 @@ public class InlineJavaServer {
 	}
 
 
-	native private String jni_callback(String cmd) ;
+	boolean IsThreadPerlContact(Thread t){
+		if (((jni)&&(t == creator))||
+			((! jni)&&(t instanceof InlineJavaServerThread))){
+			return true ;
+		}
+
+		return false ;
+	}
+
+
+	Object GetObject(int id) throws InlineJavaException {
+		Object o = null ;
+		HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
+
+		if (h == null){
+			throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
+		}
+		else{
+			o = h.get(new Integer(id)) ;
+			if (o == null){
+				throw new InlineJavaException("Can't find object " + id + " for thread " +Thread.currentThread().getName()) ;
+			}
+		}
+
+		return o ;
+	}
+
+
+	int PutObject(Object o) throws InlineJavaException {
+		HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
+
+		int id = objid ;
+		if (h == null){
+			throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
+		}
+		else{
+			h.put(new Integer(objid), o) ;
+			objid++ ;
+		}
+
+		return id ;
+	}
+
+
+	Object DeleteObject(int id) throws InlineJavaException {
+		Object o = null ;
+		HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
+
+		if (h == null){
+			throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
+		}
+		else{
+			o = h.remove(new Integer(id)) ;
+			if (o == null){
+				throw new InlineJavaException("Can't find object " + id + " for thread " + Thread.currentThread().getName()) ;
+			}
+		}
+
+		return o ;
+	}
+
+
+	int ObjectCount() throws InlineJavaException {
+		int i = -1 ;
+		HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
+
+		if (h == null){
+			throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
+		}
+		else{
+			i = h.values().size() ;
+		}
+
+		return i ;
+	}
 
 
 	void AddThread(InlineJavaServerThread t){
diff --git a/t/swing_callback.pl b/t/swing_callback.pl
new file mode 100644
index 0000000..7ae68f8
--- /dev/null
+++ b/t/swing_callback.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+use Inline Java => "DATA";
+
+my $greeter = MyButton->new();
+while (1) { sleep(100000) };
+
+###########################################
+
+sub button_pressed
+{
+  print "Button Pressed (from perl)\n"
+}
+
+__DATA__
+__Java__
+
+import java.util.*;
+import org.perl.inline.java.*;
+import javax.swing.*;
+import java.awt.event.*;
+
+public class MyButton extends    InlineJavaPerlCaller
+                      implements ActionListener
+{
+  public MyButton() throws InlineJavaException
+  {
+    // create frame
+    JFrame frame = new JFrame("MyButton");
+    frame.setSize(200,200);
+
+    // create button
+    JButton button = new JButton("Click Me!");
+    frame.getContentPane().add(button);
+
+    // tell the button that when it's clicked, report it to
+    // this class.
+    button.addActionListener(this);
+
+    // all done, everything added, just show it
+    frame.show();
+  }
+
+  public void actionPerformed(ActionEvent e)
+  {
+    try
+    {
+      CallPerl("main", "button_pressed", new Object [] {});
+    }
+    catch (InlineJavaPerlException pe)  { }
+    catch (InlineJavaException pe) { }
+  }
+}

-- 
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