/interpreter/tags/at2dist220411/src/edu/vub/at/actors/natives/Packet.java

http://ambienttalk.googlecode.com/ · Java · 183 lines · 104 code · 19 blank · 60 comment · 1 complexity · 6151448447978890529d94f13c7976e0 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * Packet.java created on 29-dec-2006 at 19:15:00
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.actors.natives;
  29. import edu.vub.at.actors.net.SerializationException;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.exceptions.XClassNotFound;
  32. import edu.vub.at.exceptions.XIOProblem;
  33. import edu.vub.at.objects.ATObject;
  34. import java.io.ByteArrayInputStream;
  35. import java.io.ByteArrayOutputStream;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.io.ObjectInputStream;
  39. import java.io.ObjectOutputStream;
  40. import java.io.ObjectStreamClass;
  41. import java.io.Serializable;
  42. import java.lang.reflect.Proxy;
  43. /**
  44. * Packet instances are the serialized representation of AmbientTalk messages or other objects.
  45. * They are exchanged directly in between local actors and in a wrapped VMCommand object between
  46. * remote actors.
  47. *
  48. * @author tvcutsem
  49. */
  50. public class Packet implements Serializable {
  51. private final byte[] payload_;
  52. private final String description_;
  53. public Packet(String description, ATObject object) throws XIOProblem {
  54. description_ = description;
  55. try {
  56. payload_ = serialize(object);
  57. } catch (IOException e) {
  58. throw new XIOProblem(e);
  59. }
  60. }
  61. public Packet(ATObject object) throws XIOProblem {
  62. this(object.toString(), object);
  63. }
  64. public byte[] getPayload() {
  65. return payload_;
  66. }
  67. /**
  68. * Deserialize this message using the default class loader.
  69. * In 99% of all cases this is okay, but on the Dalvik VM (used by Android)
  70. * it needs a specific classloader. It also fails on java6 systems sometimes.
  71. * Therefore, we try all three flavors of classloaders.
  72. */
  73. public ATObject unpack() throws InterpreterException {
  74. try {
  75. ClassLoader c = this.getClass().getClassLoader();
  76. return unpackUsingClassLoader(c);
  77. } catch ( XClassNotFound e) {
  78. try {
  79. ClassLoader s = ClassLoader.getSystemClassLoader();
  80. return unpackUsingClassLoader(s);
  81. } catch (XClassNotFound ef) {
  82. ClassLoader t = Thread.currentThread().getContextClassLoader();
  83. return unpackUsingClassLoader(t);
  84. }
  85. }
  86. }
  87. /** deserialize this message, using a custom class loader to load the classes into the JVM */
  88. public ATObject unpackUsingClassLoader(ClassLoader cld) throws InterpreterException {
  89. try {
  90. return (ATObject) deserialize(payload_, cld);
  91. } catch (SerializationException e) {
  92. throw e.getWrappedException();
  93. } catch (IOException e) {
  94. throw new XIOProblem(e);
  95. } catch (ClassNotFoundException e) {
  96. throw new XClassNotFound(e.getMessage(), e);
  97. }
  98. }
  99. /**
  100. * Beware: the same object serialized into 2 different packets results in
  101. * 2 non-equal packets. It is NOT the case that
  102. * <code>new Packet(o).equals(new Packet(o))</code>
  103. */
  104. public boolean equals(Object other) {
  105. return ((other instanceof Packet) &&
  106. ((Packet) other).payload_.equals(payload_));
  107. }
  108. public int hashCode() { return payload_.hashCode(); }
  109. public String toString() { return "packet["+description_+"]"; }
  110. private static byte[] serialize(Object o) throws IOException {
  111. ByteArrayOutputStream out = new ByteArrayOutputStream();
  112. ObjectOutputStream stream = new ObjectOutputStream(out);
  113. stream.writeObject(o);
  114. stream.flush();
  115. stream.close();
  116. return out.toByteArray();
  117. }
  118. private static Object deserialize(byte[] b) throws IOException, ClassNotFoundException {
  119. ByteArrayInputStream in = new ByteArrayInputStream(b);
  120. ObjectInputStream instream = new ObjectInputStream(in);
  121. return instream.readObject();
  122. }
  123. /**
  124. * A dedicated subclass of {@link ObjectInputStream} that hooks into
  125. * that class's facility to define additional sources to look for classes
  126. * that are read from the input stream.
  127. */
  128. private static class HookedObjectInputStream extends ObjectInputStream {
  129. private final ClassLoader loader_;
  130. protected HookedObjectInputStream(ClassLoader cld, InputStream is) throws IOException, SecurityException {
  131. super(is);
  132. loader_ = cld;
  133. }
  134. protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
  135. /* This does not work in the Sun Java 6 VM because of http://bugs.sun.com/view_bug.do?bug_id=6434149
  136. * Using the suggested workaround.
  137. */
  138. // return loader_.loadClass(desc.getName());
  139. return Class.forName(desc.getName(), false, loader_);
  140. }
  141. /**
  142. * Creates the proxy class that implements the interfaces specified in
  143. * {@code interfaceNames}.
  144. */
  145. protected Class resolveProxyClass(String[] interfaceNames)
  146. throws IOException, ClassNotFoundException {
  147. Class[] interfaces = new Class[interfaceNames.length];
  148. for (int i = 0; i < interfaceNames.length; i++) {
  149. interfaces[i] = Class.forName(interfaceNames[i], false, loader_);
  150. }
  151. try {
  152. return Proxy.getProxyClass(loader_, interfaces);
  153. } catch (IllegalArgumentException e) {
  154. throw new ClassNotFoundException(e.toString(), e);
  155. }
  156. }
  157. }
  158. // deserialize and use the given class loader to try and load any missing classes
  159. private static Object deserialize(byte[] b, ClassLoader cld) throws IOException, ClassNotFoundException {
  160. ByteArrayInputStream in = new ByteArrayInputStream(b);
  161. ObjectInputStream instream = new HookedObjectInputStream(cld, in);
  162. return instream.readObject();
  163. }
  164. }