document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. import java.rmi.AlreadyBoundException;
  3. import java.rmi.NotBoundException;
  4. import java.rmi.RMISecurityManager;
  5. import java.rmi.RemoteException;
  6. import java.rmi.registry.LocateRegistry;
  7. import java.rmi.registry.Registry;
  8. import java.rmi.server.UnicastRemoteObject;
  9.  
  10. public class Servidor extends UnicastRemoteObject implements interfaceServidor{
  11.  
  12.     public Servidor() throws RemoteException{}
  13.    
  14.     public static void main(String[] args){    
  15.         try{
  16.             Servidor servidor = new Servidor();
  17.             servidor.conectar();
  18.         }catch(RemoteException ex){
  19.             ex.getMessage();
  20.         }
  21.     }
  22.  
  23.     public void conectar() throws RemoteException{
  24.         try{
  25.            
  26.             Registry registry;
  27.             Servidor ms = new Servidor();
  28.            
  29.             int porta = 1122;
  30.             String host = "127.0.0.1";
  31.            
  32.             //verifica se há políticas de segurança
  33.             if(System.getSecurityManager()==null){
  34.                
  35.                 System.out.println("Não foi encontrado arquivo de segurança do servidor.");
  36.                
  37.                 System.setProperty("java.security.policy",
  38.                                     "\\\\home\\\\lionel\\\\Área de Trabalho\\\\ServidorRMI\\\\servidor.policy");
  39.                                    
  40.                 System.setSecurityManager(new RMISecurityManager());
  41.                
  42.                 //verifica novamente se o arquivo foi incluso no sistema operacional
  43.                 if(System.getSecurityManager()==null) System.out.println("Falha na autenticação do arquivo de segurança");
  44.                 else System.out.println("Arquivo de segurança autenticado. " + System.getSecurityManager());              
  45.                
  46.             }else{
  47.                 System.out.println("Servidor seguro.");
  48.             }
  49.            
  50.            
  51.             //verifica se o servidor já foi registrado
  52.             if(LocateRegistry.getRegistry(porta).equals(null)){
  53.                
  54.                 System.out.println("Servidor não registrado");
  55.                
  56.                 registry = LocateRegistry.createRegistry(porta);
  57.                
  58.                 try {
  59.                     registry.bind("RMIJavaAbertoServidor",ms);
  60.                 } catch (AlreadyBoundException e) {                
  61.                     e.getMessage();
  62.                 }              
  63.                
  64.             }else{
  65.                
  66.                 System.out.println("Servidor já registrado");
  67.                
  68.                 registry = LocateRegistry.getRegistry(porta);
  69.                
  70.                 try {              
  71.                     registry.rebind("RMIJavaAbertoServidor",ms);
  72.                 } catch (Exception e) {                
  73.                     e.getMessage();
  74.                 }              
  75.                
  76.             }          
  77.            
  78.             System.out.println("Servidor registrado e conectado");
  79.            
  80.             System.out.println("Configurações do servidor: " + registry);
  81.            
  82.         }catch(RemoteException e){
  83.             e.getMessage();
  84.         }
  85.     }
  86.    
  87.     public void desconectar() throws RemoteException{
  88.        
  89.         Registry registry = LocateRegistry.getRegistry(1234);
  90.            
  91.         try {
  92.             registry.unbind("RMIJavaAbertoServidor");
  93.         } catch (NotBoundException e) {        
  94.             e.getMessage();
  95.         }
  96.     }
  97.  
  98. }
');