1
//////////////////////////////////////////////////////////////////////
3
// JCSP ("CSP for Java") Libraries //
4
// Copyright (C) 1996-2008 Peter Welch and Paul Austin. //
5
// 2001-2004 Quickstone Technologies Limited. //
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 //
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 //
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. //
24
// Author contact: P.H.Welch@kent.ac.uk //
27
//////////////////////////////////////////////////////////////////////
29
package org.jcsp.net.dynamic;
32
import org.jcsp.lang.*;
33
import org.jcsp.net.*;
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.
40
* @author Quickstone Technologies Limited
42
class DynamicClassLoaderMessage implements Serializable
45
* Creates a new <code>DynamicClassLoaderMessage</code> encapsulating the given object.
47
* @param data the actual user object being sent.
48
* @param classSourceChannelLoc the request channel of the JFTP process.
50
public DynamicClassLoaderMessage(Object data, NetChannelLocation classSourceChannelLoc)
51
throws NotSerializableException, IOException
53
this.classSourceChannelLoc = classSourceChannelLoc;
54
this.createTime = System.currentTimeMillis();
55
this.serializedData = new SerializedData(data,false);
61
private long createTime;
64
* Diagnostic string identifying the message by timestamp.
66
public String toString()
68
return "DynamicClassLoaderMessage " + createTime;
72
* Public accessor to deserialize and retrieve the object using the specified
73
* <code>ClassManager</code>.
75
* @param cm the class manager to use.
76
* @return the user object passed in this message.
78
public Object get(ClassManager cm) throws ClassNotFoundException, IOException
80
//JA - probably don't want to create a new factory each time
83
Object g = serializedData.get(new AdvancedInputStreamFactory(cm));
86
return serializedData.get();
90
* Sets the class loader to use when deserializing the object.
92
* @param classLoader the new class loader.
94
public void setClassLoader(ClassLoader classLoader)
96
this.classLoaderToUse = classLoader;
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.
103
private transient ClassLoader classLoaderToUse = null;
106
* The location of the request channel of the sender's JFTP process.
108
private final NetChannelLocation classSourceChannelLoc;
111
* The serialized form of the user object being sent.
113
private SerializedData serializedData;
116
* This allows a custom ClassLoader to be used to resolve the object
117
* being deserialized.
121
private class AdvancedObjectInputStream extends ObjectInputStream
124
* Constructs a new <code>AdvancedObjectInputStream</code>.
126
* @param cm the class manager process at this node.
127
* @param in the underlying input stream to read data from.
129
public AdvancedObjectInputStream(ClassManager cm, InputStream in) throws IOException
136
* Attempts to resolve the class requested using the class manager. If this fails an exception
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.
144
protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException
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());
155
* The class manager to use when resolving a class.
157
private ClassManager cm;
161
* Factory for creating instances of <code>AdvancedObjectInputStream</code> bound to a given
164
private class AdvancedInputStreamFactory implements SerializedData.InputStreamFactory
167
* Constructs a new factory for the given class manager. All streams created by this factory
168
* will be associated with this class manager.
170
* @param cm class manager to associate will all created streams.
172
AdvancedInputStreamFactory(ClassManager cm)
178
* Creates a new input stream.
180
* @param in the underlying input stream.
182
public ObjectInput create(InputStream in) throws IOException
184
return new AdvancedObjectInputStream(cm, in);
188
* The class manager to associate with all created input streams.
190
private ClassManager cm;
b'\\ No newline at end of file'