No Description

7ef660942d5fb53428523f65f9f49a858fe9a8d2.svn-base 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.rdlze.radializebase.utils;
  2. import java.rmi.Remote;
  3. import java.rmi.RemoteException;
  4. import java.rmi.registry.LocateRegistry;
  5. import java.rmi.registry.Registry;
  6. import java.rmi.server.UnicastRemoteObject;
  7. import java.sql.Timestamp;
  8. import com.idealizeframework.irf.base.exceptions.IdealizeUnavailableResourceException;
  9. public class Utils {
  10. public static Timestamp getNowDate() {
  11. return new Timestamp(System.currentTimeMillis());
  12. }
  13. /**
  14. * Makes the object remotely available.
  15. *
  16. * @param remoteObject
  17. * @param nameObject
  18. * @throws IdealizeUnavailableResourceException
  19. */
  20. public static Remote registerRemoteObject(Remote remoteObject,
  21. String nameObject, int port) {
  22. try {
  23. try {
  24. LocateRegistry.createRegistry(port);
  25. // System.out.println("Criou o RMI............................................................................................");
  26. } catch (Exception e) {
  27. System.out.println("A porta do RMI ja esta sendo utilizada?!");
  28. }
  29. Remote stub;
  30. Registry registry = LocateRegistry.getRegistry(port);
  31. stub = UnicastRemoteObject.exportObject(remoteObject, port);
  32. System.out.println("Name Obj " + nameObject);
  33. registry.rebind(nameObject, stub);
  34. System.out.println("REGISTROU OBJETO " + nameObject
  35. + " REMOTAMENTE NA PORTA "+port);
  36. return stub;
  37. } catch (RemoteException e) {
  38. e.printStackTrace();
  39. }
  40. return remoteObject;
  41. }
  42. /**
  43. * Makes the object remotely available.
  44. *
  45. * @param remoteObject
  46. * @param nameObject
  47. * @throws IdealizeUnavailableResourceException
  48. */
  49. public static Remote registerRemoteObjectUseRandom(Remote remoteObject,
  50. String nameObject, int port) {
  51. try {
  52. try {
  53. LocateRegistry.createRegistry(port);
  54. // System.out.println("Criou o RMI............................................................................................");
  55. } catch (Exception e) {
  56. System.out.println("A porta do RMI ja esta sendo utilizada?!");
  57. }
  58. Remote stub;
  59. Registry registry = LocateRegistry.getRegistry(port);
  60. stub = UnicastRemoteObject.exportObject(remoteObject, 0);
  61. System.out.println("Name Obj " + nameObject);
  62. registry.rebind(nameObject, stub);
  63. System.out.println("REGISTROU OBJETO " + nameObject
  64. + " REMOTAMENTE.");
  65. return stub;
  66. } catch (RemoteException e) {
  67. e.printStackTrace();
  68. }
  69. return remoteObject;
  70. }
  71. public static Remote getRemoteObject(String ipObject, String nameObject,
  72. int port) {
  73. Remote remote = null;
  74. try {
  75. Registry registry = LocateRegistry.getRegistry(ipObject, port);
  76. remote = registry.lookup(nameObject);
  77. if (remote == null) {
  78. System.out.println("NÃO PEGOU O OBJETO " + nameObject
  79. + " REMOTAMENTE. O OBJETO ESTÁ NULO. IP: " + ipObject
  80. + " NAME: " + nameObject + " PORTA: " + port);
  81. } else {
  82. System.out.println("PEGOU O OBJETO " + nameObject
  83. + " REMOTAMENTE.");
  84. }
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. System.err
  88. .println("Could not possible get remote object: nameObject: "
  89. + nameObject + " IP Object: " + ipObject);
  90. remote = null;
  91. }
  92. return remote;
  93. }
  94. /**
  95. * Makes the object remotely available.
  96. *
  97. * @param remoteObject
  98. * @param nameObject
  99. * @param host
  100. * @throws IdealizeUnavailableResourceException
  101. */
  102. public static Remote registerRemoteObject(Remote remoteObject,
  103. String nameObject, String host, int port) {
  104. try {
  105. try {
  106. LocateRegistry.createRegistry(port);
  107. // System.out.println("Criou o RMI............................................................................................");
  108. } catch (Exception e) {
  109. System.out.println("A porta do RMI ja esta sendo utilizada?!");
  110. }
  111. Remote stub;
  112. Registry registry = LocateRegistry.getRegistry(host, port);
  113. stub = UnicastRemoteObject.exportObject(remoteObject, port);
  114. System.out.println("Name Obj " + nameObject);
  115. registry.rebind(nameObject, stub);
  116. System.out.println("REGISTROU OBJETO " + nameObject
  117. + " REMOTAMENTE.");
  118. return stub;
  119. } catch (RemoteException e) {
  120. e.printStackTrace();
  121. }
  122. return remoteObject;
  123. }
  124. public String escapeUnicode(String name) {
  125. name = name.replaceAll("'", "''");
  126. name = name.replaceAll("\\", "'\\");
  127. return name;
  128. }
  129. }