~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/org/jcsp/net/dynamic/DynamicClassLoaderMessage.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////
 
2
//                                                                  //
 
3
//  JCSP ("CSP for Java") Libraries                                 //
 
4
//  Copyright (C) 1996-2008 Peter Welch and Paul Austin.            //
 
5
//                2001-2004 Quickstone Technologies Limited.        //
 
6
//                                                                  //
 
7
//  This library is free software; you can redistribute it and/or   //
 
8
//  modify it under the terms of the GNU Lesser General Public      //
 
9
//  License as published by the Free Software Foundation; either    //
 
10
//  version 2.1 of the License, or (at your option) any later       //
 
11
//  version.                                                        //
 
12
//                                                                  //
 
13
//  This library is distributed in the hope that it will be         //
 
14
//  useful, but WITHOUT ANY WARRANTY; without even the implied      //
 
15
//  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR         //
 
16
//  PURPOSE. See the GNU Lesser General Public License for more     //
 
17
//  details.                                                        //
 
18
//                                                                  //
 
19
//  You should have received a copy of the GNU Lesser General       //
 
20
//  Public License along with this library; if not, write to the    //
 
21
//  Free Software Foundation, Inc., 59 Temple Place, Suite 330,     //
 
22
//  Boston, MA 02111-1307, USA.                                     //
 
23
//                                                                  //
 
24
//  Author contact: P.H.Welch@kent.ac.uk                             //
 
25
//                                                                  //
 
26
//                                                                  //
 
27
//////////////////////////////////////////////////////////////////////
 
28
 
 
29
package org.jcsp.net.dynamic;
 
30
 
 
31
import java.io.*;
 
32
import org.jcsp.lang.*;
 
33
import org.jcsp.net.*;
 
34
 
 
35
/**
 
36
 * Wraps an object when it is being sent over a channel by the <code>DataSerializationFilter</code> so
 
37
 * that it includes a <code>NetChannelLocation</code> referring to the JFTP request channel to service
 
38
 * requests for the class's binary image if it is not held at the receiving end.
 
39
 *
 
40
 * @author Quickstone Technologies Limited
 
41
 */
 
42
class DynamicClassLoaderMessage implements Serializable
 
43
{
 
44
   /**
 
45
    * Creates a new <code>DynamicClassLoaderMessage</code> encapsulating the given object.
 
46
    *
 
47
    * @param data the actual user object being sent.
 
48
    * @param classSourceChannelLoc the request channel of the JFTP process.
 
49
    */
 
50
   public DynamicClassLoaderMessage(Object data, NetChannelLocation classSourceChannelLoc)
 
51
   throws NotSerializableException, IOException
 
52
   {
 
53
      this.classSourceChannelLoc = classSourceChannelLoc;
 
54
      this.createTime = System.currentTimeMillis();
 
55
      this.serializedData = new SerializedData(data,false);
 
56
   }
 
57
   
 
58
   /**
 
59
    * Creation timestamp.
 
60
    */
 
61
   private long createTime;
 
62
   
 
63
   /**
 
64
    * Diagnostic string identifying the message by timestamp.
 
65
    */
 
66
   public String toString()
 
67
   {
 
68
      return "DynamicClassLoaderMessage " + createTime;
 
69
   }
 
70
   
 
71
   /**
 
72
    * Public accessor to deserialize and retrieve the object using the specified
 
73
    * <code>ClassManager</code>.
 
74
    *
 
75
    * @param cm the class manager to use.
 
76
    * @return the user object passed in this message.
 
77
    */
 
78
   public Object get(ClassManager cm) throws ClassNotFoundException, IOException
 
79
   {
 
80
      //JA - probably don't want to create a new factory each time
 
81
      if (cm != null)
 
82
      {
 
83
         Object g =  serializedData.get(new AdvancedInputStreamFactory(cm));
 
84
         return g;
 
85
      }
 
86
      return serializedData.get();
 
87
   }
 
88
   
 
89
   /**
 
90
    * Sets the class loader to use when deserializing the object.
 
91
    *
 
92
    * @param classLoader the new class loader.
 
93
    */
 
94
   public void setClassLoader(ClassLoader classLoader)
 
95
   {
 
96
      this.classLoaderToUse = classLoader;
 
97
   }
 
98
   
 
99
   /**
 
100
    * The class loader to use when deserializing the object. This is not sent as part of the message -
 
101
    * it is for use by methods invoked by the receiver.
 
102
    */
 
103
   private transient ClassLoader classLoaderToUse = null;
 
104
   
 
105
   /**
 
106
    * The location of the request channel of the sender's JFTP process.
 
107
    */
 
108
   private final NetChannelLocation classSourceChannelLoc;
 
109
   
 
110
   /**
 
111
    * The serialized form of the user object being sent.
 
112
    */
 
113
   private SerializedData serializedData;
 
114
   
 
115
   /**
 
116
    * This allows a custom ClassLoader to be used to resolve the object
 
117
    * being deserialized.
 
118
    *
 
119
    * @author Jo Aldous
 
120
    */
 
121
   private class AdvancedObjectInputStream extends ObjectInputStream
 
122
   {
 
123
      /**
 
124
       * Constructs a new <code>AdvancedObjectInputStream</code>.
 
125
       *
 
126
       * @param cm the class manager process at this node.
 
127
       * @param in the underlying input stream to read data from.
 
128
       */
 
129
      public AdvancedObjectInputStream(ClassManager cm, InputStream in) throws IOException
 
130
      {
 
131
         super(in);
 
132
         this.cm = cm;
 
133
      }
 
134
      
 
135
      /**
 
136
       * Attempts to resolve the class requested using the class manager. If this fails an exception
 
137
       * is raised.
 
138
       *
 
139
       * @param v indicates the class to resolve.
 
140
       * @throws IOException if there is a problem with the underlying stream.
 
141
       * @throws ClassNotFoundException if the class could not be resolved.
 
142
       * @return the resolved class.
 
143
       */
 
144
      protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException
 
145
      {
 
146
         One2OneChannel in = Channel.one2one();
 
147
         cm.getClass(v.getName(), classSourceChannelLoc, in.out());
 
148
         Object reply = in.in().read();
 
149
         if(reply instanceof Class)
 
150
            return (Class) reply;
 
151
         throw new ClassNotFoundException(v.getName());
 
152
      }
 
153
      
 
154
      /**
 
155
       * The class manager to use when resolving a class.
 
156
       */
 
157
      private ClassManager cm;
 
158
   }
 
159
   
 
160
   /**
 
161
    * Factory for creating instances of <code>AdvancedObjectInputStream</code> bound to a given
 
162
    * class manager.
 
163
    */
 
164
   private class AdvancedInputStreamFactory implements SerializedData.InputStreamFactory
 
165
   {
 
166
      /**
 
167
       * Constructs a new factory for the given class manager. All streams created by this factory
 
168
       * will be associated with this class manager.
 
169
       *
 
170
       * @param cm class manager to associate will all created streams.
 
171
       */
 
172
      AdvancedInputStreamFactory(ClassManager cm)
 
173
      {
 
174
         this.cm = cm;
 
175
      }
 
176
      
 
177
      /**
 
178
       * Creates a new input stream.
 
179
       *
 
180
       * @param in the underlying input stream.
 
181
       */
 
182
      public ObjectInput create(InputStream in) throws IOException
 
183
      {
 
184
         return new AdvancedObjectInputStream(cm, in);
 
185
      }
 
186
      
 
187
      /**
 
188
       * The class manager to associate with all created input streams.
 
189
       */
 
190
      private ClassManager cm;
 
191
   }
 
192
}
 
 
b'\\ No newline at end of file'