问题
Possible Duplicate:
Java RMI server and Objective C client
Suppose you have the following in RMI:
This is the remote interface:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote
{
String sayHello() throws RemoteException;
}
this is the server implementation code: (it implements the interface)
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.net.*;
public class Server implements Hello {
public Server() {}
public String sayHello()
{
return "Hello, world!";
}
public static void main(String args[])
{
try
{
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("HelloServer", stub);
System.err.println("Server ready");
} catch (Exception e)
{
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
this is the client code:
import java.rmi.*;
import java.rmi.server.*;//////note additional imports
import java.io.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.net.*;
public class HelloClient2 {
private HelloClient2() {}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {/////note this addition
RMISocketFactory.setSocketFactory(new sun.rmi.transport.proxy.RMIHttpToCGISocketFactory());
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("HelloServer");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
Now I want to create an objective-C client connected to the rmi server. In order to be able to invoke methods on the rmi server, i created a proxy on the apache web server that embeds RMI calls in HTTP requests.
The line in the client: RMISocketFactory.setSocketFactory(new sun.rmi.transport.proxy.RMIHttpToCGISocketFactory()); is responsible for RMI tunneling (passing RMI calls in http requests). So basically this line creates an http post request of the form
http://localhost:80/cgi-bin/java-rmi?forward=portnum
where portnum is the port of the server remote object (which is anonymous)
what does this mean? it means there should be a web server listening on port 80 on the localhost. We have the apache tomcat web server ideally listening on 8080, but we changed its port to 80.
That solved, we still have to handle the rest of the url. So in web apps of tomcat, we created a new directory called cgi-bin. cgi-bin has a WEB-INF directory, which contains a classes directory and a web.xml file. In the classes directory we added the compiled classes of the server and client and the servlet handler. the servlet handler receives http post requests, fetches the rmi calls and forwards them to the rmi server.
How to make the web server know that he should call the servlet handler? in the web.xml file, we added an entry for that servlet with the name ServletHandler (name of the class) and a url mapping : /java-rmi. That way the localhost knows where to forward the request. As I said before, once the servlet handler receives the request it will deal with it and send it to the rmi server.
now my problem is with these:
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("HelloServer");
I simply don't know how to locate the registry and to look it up in objective-C to find the remote object. (stub is the local reference of the remote object)
And I don't think it can be done using the proxy, the proxy is specifically made to handle requests directed to the rmi server. So in order to test the servlet, one needs to call a certain remote method like sayHello() which is implemented in the server. Stuff related to registry are not remote method calls.
So how do I handle these in objective-C ?
回答1:
It doesn't matter what your boss wants. The answer is the same as the last time you posted this question. It can't be done. Reposting the question won't change that.
来源:https://stackoverflow.com/questions/13137207/java-rmi-server-and-objective-c-client