Saturday, April 28, 2012

Create an RMI-JRMP-SSL Server Using the New JDK1.5.0 Classes


Usually, the RMI client is an RMI client only in that it specifies the true store zone for certificates. For example, you can use any certificate available to System.setProperty: 

("javax.net.ssl.trustStore","SSLcert"); System.setProperty
("javax.net.ssl.trustStorePassword","e1002qa2"); 

In this example, the remote interface is SumaInterface and there's only one remote method called Suma: 

import java.rmi.*;
import java.rmi.server.*;
import javax.rmi.ssl.SslRMIServerSocketFactory;
import javax.rmi.ssl.SslRMIClientSocketFactory;

public class SumaImpl extends UnicastRemoteObject implements SumaInterface{

//constructor
public SumaImpl(int port,RMIClientSocketFactory rmiC, RMIServerSocketFactory rmiS)
throws RemoteException
       {
       //optional
       super(port,rmiC,rmiS);
       }
         
//implementing the Suma method
public int Suma(int a,int b)throws RemoteException{
  return (a+b);  
  }
  
  public static void main(String[] args)
      {      
      SumaImpl SI=null;
      
      System.setProperty("javax.net.ssl.keyStore","SSLcert");
      System.setProperty("javax.net.ssl.keyStorePassword","e1002qa2");

      RMIClientSocketFactory rmiClientSocketFactory=new SslRMIClientSocketFactory();
     RMIServerSocketFactory rmiServerSockeyFactory=new SslRMIServerSocketFactory();

      //create the remote object
      try{
         SI=new SumaImpl(0,rmiClientSocketFactory,rmiServerSockeyFactory);       
         }catch(java.rmi.RemoteException e)
            {System.out.println(e.getMessage());}
      
 //registration of SI object
      try{        
         Naming.bind("rmi://localhost:1099/Adunare",SI);
         }catch(java.rmi.AlreadyBoundException e)
            {System.out.println(e.getMessage());
         }catch(java.net.MalformedURLException e)     
           {System.out.println(e.getMessage());
         }catch(java.rmi.RemoteException e)
            {System.out.println(e.getMessage());}            
      }
}

No comments:

Post a Comment