package com.rdlze.radializebase.utils; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; import java.sql.Timestamp; import com.idealizeframework.irf.base.exceptions.IdealizeUnavailableResourceException; public class Utils { public static Timestamp getNowDate() { return new Timestamp(System.currentTimeMillis()); } /** * Makes the object remotely available. * * @param remoteObject * @param nameObject * @throws IdealizeUnavailableResourceException */ public static Remote registerRemoteObject(Remote remoteObject, String nameObject, int port) { try { try { LocateRegistry.createRegistry(port); // System.out.println("Criou o RMI............................................................................................"); } catch (Exception e) { System.out.println("A porta do RMI ja esta sendo utilizada?!"); } Remote stub; Registry registry = LocateRegistry.getRegistry(port); stub = UnicastRemoteObject.exportObject(remoteObject, port); System.out.println("Name Obj " + nameObject); registry.rebind(nameObject, stub); System.out.println("REGISTROU OBJETO " + nameObject + " REMOTAMENTE NA PORTA "+port); return stub; } catch (RemoteException e) { e.printStackTrace(); } return remoteObject; } /** * Makes the object remotely available. * * @param remoteObject * @param nameObject * @throws IdealizeUnavailableResourceException */ public static Remote registerRemoteObjectUseRandom(Remote remoteObject, String nameObject, int port) { try { try { LocateRegistry.createRegistry(port); // System.out.println("Criou o RMI............................................................................................"); } catch (Exception e) { System.out.println("A porta do RMI ja esta sendo utilizada?!"); } Remote stub; Registry registry = LocateRegistry.getRegistry(port); stub = UnicastRemoteObject.exportObject(remoteObject, 0); System.out.println("Name Obj " + nameObject); registry.rebind(nameObject, stub); System.out.println("REGISTROU OBJETO " + nameObject + " REMOTAMENTE."); return stub; } catch (RemoteException e) { e.printStackTrace(); } return remoteObject; } public static Remote getRemoteObject(String ipObject, String nameObject, int port) { Remote remote = null; try { Registry registry = LocateRegistry.getRegistry(ipObject, port); remote = registry.lookup(nameObject); if (remote == null) { System.out.println("NÃO PEGOU O OBJETO " + nameObject + " REMOTAMENTE. O OBJETO ESTÁ NULO. IP: " + ipObject + " NAME: " + nameObject + " PORTA: " + port); } else { System.out.println("PEGOU O OBJETO " + nameObject + " REMOTAMENTE."); } } catch (Exception e) { e.printStackTrace(); System.err .println("Could not possible get remote object: nameObject: " + nameObject + " IP Object: " + ipObject); remote = null; } return remote; } /** * Makes the object remotely available. * * @param remoteObject * @param nameObject * @param host * @throws IdealizeUnavailableResourceException */ public static Remote registerRemoteObject(Remote remoteObject, String nameObject, String host, int port) { try { try { LocateRegistry.createRegistry(port); // System.out.println("Criou o RMI............................................................................................"); } catch (Exception e) { System.out.println("A porta do RMI ja esta sendo utilizada?!"); } Remote stub; Registry registry = LocateRegistry.getRegistry(host, port); stub = UnicastRemoteObject.exportObject(remoteObject, port); System.out.println("Name Obj " + nameObject); registry.rebind(nameObject, stub); System.out.println("REGISTROU OBJETO " + nameObject + " REMOTAMENTE."); return stub; } catch (RemoteException e) { e.printStackTrace(); } return remoteObject; } public String escapeUnicode(String name) { name = name.replaceAll("'", "''"); name = name.replaceAll("\\", "'\\"); return name; } }