import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Servidor extends UnicastRemoteObject implements interfaceServidor{
public Servidor() throws RemoteException{}
public static void main(String[] args){
try{
Servidor servidor = new Servidor();
servidor.conectar();
}catch(RemoteException ex){
ex.getMessage();
}
}
public void conectar() throws RemoteException{
try{
Registry registry;
Servidor ms = new Servidor();
int porta = 1122;
String host = "127.0.0.1";
//verifica se há políticas de segurança
if(System.getSecurityManager()==null){
System.out.println("Não foi encontrado arquivo de segurança do servidor.");
System.setProperty("java.security.policy",
"\\\\home\\\\lionel\\\\Área de Trabalho\\\\ServidorRMI\\\\servidor.policy");
System.setSecurityManager(new RMISecurityManager());
//verifica novamente se o arquivo foi incluso no sistema operacional
if(System.getSecurityManager()==null) System.out.println("Falha na autenticação do arquivo de segurança");
else System.out.println("Arquivo de segurança autenticado. " + System.getSecurityManager());
}else{
System.out.println("Servidor seguro.");
}
//verifica se o servidor já foi registrado
if(LocateRegistry.getRegistry(porta).equals(null)){
System.out.println("Servidor não registrado");
registry = LocateRegistry.createRegistry(porta);
try {
registry.bind("RMIJavaAbertoServidor",ms);
} catch (AlreadyBoundException e) {
e.getMessage();
}
}else{
System.out.println("Servidor já registrado");
registry = LocateRegistry.getRegistry(porta);
try {
registry.rebind("RMIJavaAbertoServidor",ms);
} catch (Exception e) {
e.getMessage();
}
}
System.out.println("Servidor registrado e conectado");
System.out.println("Configurações do servidor: " + registry);
}catch(RemoteException e){
e.getMessage();
}
}
public void desconectar() throws RemoteException{
Registry registry = LocateRegistry.getRegistry(1234);
try {
registry.unbind("RMIJavaAbertoServidor");
} catch (NotBoundException e) {
e.getMessage();
}
}
}