Closed Bug 213418 Opened 21 years ago Closed 11 years ago

Java methods called from JavaScript - not working in Netscape

Categories

(Core Graveyard :: Java: OJI, defect)

x86
Windows XP
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED INCOMPLETE

People

(Reporter: ganor, Assigned: alfred.peng)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02

Hi Mozilla gurus,

My HTML page contains JavaScripts that call methods of a signed Java applet jar.
It works OK in Microsoft Internet Explorer (IE). The methods do privileged
operations. Due to a bug in Netscape/Mozilla, a JavaScript cannot call a
privileged Java method (even if you sign both the HTML and the jar with
signtool), I made a workaround: 

The applet starts a worker thread upon init. The JavaScript calls a Java method
calledfromJS(), which notifies the worker thread and waits for the result. The
thread calls the privileged method and notifies the caller when the method finishes.

This runs OK in IE, but not in Netscape and Mozilla. The privileged method (in
the example below, sendPostRequest()) halts and freezes when it arrives to this
line:
DataOutputStream outSock = new DataOutputStream (m_urlConn.getOutputStream()); 
// <===

After experiments, it seems as if this statement halts if another thread is
doing wait() or sleep(). E.g., if I replace the wait() with Thread.sleep(10000),
than the statement will execute after 10 seconds(!!!).

Below is a sample Java applet that runs in IE but freezes in Netscape (using
Java plugin 1.4 in both cases):

Please help!
Sruli Ganor

-----------------------------------------------------------------------------------------------------------------
<SCRIPT LANGUAGE="JavaScript"><!--
function Test() {
  obj = document.applets["memtest"];
  alert(obj.calledFromJS());
}
//--></SCRIPT>

-----------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.net.*;

public class MemTest extends java.applet.Applet {
Thread m_worker;
String m_return;

public void init() {
   try {
       m_worker = new WorkerThread();
       m_worker.start();
   } catch (Exception ex) {ex.printStackTrace();}
}

public String calledFromJS() {
   try {
       synchronized(m_worker) {
           m_worker.notify();  // Tell worker thread to call sendPostRequest()
           m_worker.wait();    // Wait until sendPostRequest() finishes and puts
response in m_return
       }
   } catch (Exception exp) {exp.printStackTrace();}
   return m_return;
}

 // Internal privileged method: post a request to a server and returns the
server's response
public String sendPostRequest() {
   URL                 m_url;
   HttpURLConnection   m_urlConn;
   try {
       /* Post request */
       m_url       = new URL("http://www.census.gov/cgi-bin/ipc/idbsprd");
       m_urlConn = (HttpURLConnection)m_url.openConnection();
       m_urlConn.setDoOutput(true);
       m_urlConn.setRequestProperty("Content-type", "text/plain");
       m_urlConn.setUseCaches(false);
       System.out.println("Open sock... (The next statement will stall in
Netscape)"); 
        DataOutputStream outSock = new DataOutputStream
(m_urlConn.getOutputStream());  // <===
       System.out.println("sock opened");
       outSock.writeBytes("cty=ch");
       
       /* Read response */
       BufferedInputStream inSock = new
BufferedInputStream(m_urlConn.getInputStream());
       byte[] m_byteResponse = new byte[2048];
       int offset = 0, nRead = 0;
       while (nRead != -1) {
           nRead = inSock.read(m_byteResponse, offset, 1024);
           offset += nRead;
       }
       return new String(m_byteResponse);  // return the server's response
   } catch (Exception exp) {
       exp.printStackTrace();
       return null;
   }
}
//----------------------------------------------------------------------------
class WorkerThread extends Thread {
   public void run() {
       try {
           synchronized(this) {
               wait();                         // Wait for notify from
calledFromJS()
               m_return = sendPostRequest();   // Call internal method and store
response 
                notify();                       // Notify calledFromJS() that
the response is ready
           }
       } catch (Exception exp) {exp.printStackTrace();}
   }
}
}


Reproducible: Always

Steps to Reproduce:
1. Build a jar from the above Java source.
2. Sign the jar.
3. Call it from a JavaScript.
4. Look at the Java Console. If it runs OK, You will see a valid response string. 
Otherwise the last line in the Console will be "Open sock... (The next statement
will stall in Netscape)".
5. If you want to access the a signed jar on our Web site, tell me at
ganor@softlink.com.

Actual Results:  
Look at the Java Console. If it runs OK, You will see a valid server response
string. For the server in the example code, the response might be "This script
can only be used to decode form results".

If the bug occurs, the last line in the Console will be "Open sock... (The next
statement will stall in Netscape)". Then the browser will hand.


Expected Results:  
Open a socket, POST the server and return its response.
Severity: blocker → normal
Summary: Java methods called from JavaScript - not working in Netscape → Java methods called from JavaScript - not working in Netscape
-> Oji
Assignee: idk → joshua.xia
Component: Java-Implemented Plugins → OJI
QA Contact: avm → dsirnapalli
This is a known issue that mozilla has some limitations when calling a signed
applet from signed or unsigned Javascript.
Assignee: joshua.xia → kyle.yuan
Status: UNCONFIRMED → NEW
Ever confirmed: true
(In reply to comment #2)
> This is a known issue that mozilla has some limitations when calling a signed
> applet from signed or unsigned Javascript.


May I waht are these limitations?
Is calling signed applet from Javascript generally supported?
Please note that this problem as well as lack of adequate support in SSL led us 
to minimize our usage of Netscape/Java in favor of IE/ActiveX.

Sruli Ganor 
Assignee: yuanyi21 → pete.zha
mass reassign to Alfred
Assignee: zhayupeng → alfred.peng
Product: Core → Core Graveyard
QA Contact: dsirnapalli → java.oji
Mass-closing bugs in the "OJI" component: OJI plugin integration was replaced with npruntime long ago, and these bugs appear to be irrelevant now. If there is in fact a real bug that remains, please file it new in the "Core" product, component "Plug-ins".
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → INCOMPLETE
You need to log in before you can comment on or make changes to this bug.