1 Reply Latest reply on Jun 15, 2004 10:40 AM by starksm64

    Using an EJB acting as a client of a RMI Server

    adtau

      Hallo,

      I think my problem is very simple. I like to access a RMI Server via an EJB (the bean acts as a client). The reason for doing this is that I need to load a native library within a Java API. I know that this is not allowed within an EJB. So I created a RMI Server which handles this task. Since I'm an EJB newbee I first created a small helloWorld example for testing the connection.
      Here is the RMI Server:



      package helloServer;
      import java.rmi.server.*;
      import java.rmi.*;
      import rinterface.HelloServer;

      public class HelloImpl extends UnicastRemoteObject implements HelloServer {

      public HelloImpl() throws RemoteException {
      }

      public String sayHello() {
      return "Hello World";
      }

      public static void main(String args[]) {

      try {
      Naming.rebind("rmi://localhost:2001/hello-server", new HelloImpl());

      } catch(Exception ex) {
      ex.printStackTrace();
      }
      }

      }



      The stub class is transmitted dynamically via a small http server. There is no problem in accessing the RMI server with a "normal" RMI java client.
      But when I try to access the HelloImpl RMI Server with an EJB an exception is generated. The ejb code is the following:



      try {
      System.setSecurityManager(new RMISecurityManager());
      System.out.println("Security Manager initialisiert");
      InitialContext serverContext = new InitialContext();
      Object obj = serverContext.lookup("java:comp/env/NamingFactory");//NamingFactory is an env variable with
      //the value com.sun.jndi.rmi.registry.RegistryContextFactory

      if (obj != null) {
      this.namingFactory = obj.toString();
      }
      obj = serverContext.lookup("java:comp/env/NamingURL"); //NamingURL has the value rmi://localhost:2001

      if (obj != null) {
      this.namingUrl = obj.toString();
      }

      Hashtable lTable = new Hashtable();
      lTable.put(Context.INITIAL_CONTEXT_FACTORY, this.namingFactory);
      lTable.put(Context.PROVIDER_URL, this.namingUrl);
      Context remoteContext = new InitialContext(lTable);
      System.out.println("InitialContext initialisiert");

      obj = remoteContext.lookup("hello-server");
      rinterface.HelloServer helloServer = (rinterface.HelloServer) PortableRemoteObject.narrow(obj, rinterface.HelloServer.class);

      String result = helloServer.sayHello();
      System.out.println(result);
      }

      catch(Exception ex) {
      ex.printStackTrace();
      }



      The following exception is generated with the statement "obj = remoteContext.lookup("hello-server");":



      16:40:21,843 INFO [STDOUT] InitialContext initialisiert
      16:40:21,843 ERROR [STDERR] java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:2001 connect,resolve)
      16:40:21,843 ERROR [STDERR] at java.security.AccessControlContext.checkPermission(AccessControlContext.java:269)
      16:40:21,843 ERROR [STDERR] at java.security.AccessController.checkPermission(AccessController.java:401)
      16:40:21,843 ERROR [STDERR] at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
      16:40:21,859 ERROR [STDERR] at java.lang.SecurityManager.checkConnect(SecurityManager.java:1026)
      16:40:21,859 ERROR [STDERR] at java.net.Socket.connect(Socket.java:446)
      16:40:21,859 ERROR [STDERR] at java.net.Socket.connect(Socket.java:402)
      16:40:21,859 ERROR [STDERR] ...



      I can produce the same exception with a normal Java client, if I don't refer to a java.policy file with the argument -Djava.security.policy when I start the client.

      So here is my question: Where and how do I have to specify the security policy for this case in JBOSS 3.2.3. I wasn't able to find anything about this special topic. Perhaps I missed something, because I'm an EJB newbee.
      I would be very pleased, if somebody knows an answer. I'm searching for a solution for quite a while.










      Featured Articles

      Building Dynamic Web sites with WebSphere Studio

      Building database applications with WebSphere and DB2

      Easy, breezy EJB tooling in WebSphere Studio

      Building a J2EE application with Lotus Domino and WebSphere


      Related Links

      EJB FAQ

      EJB Forum

      Sun's EJB FAQ

      EJBNow.com

      O'Reilly Book by guru Richard Monson-Haefel

      Wish List
      Features
      About jGuru
      Contact Us

      jGuru Privacy Policy Copyright/Legal Notices