From c92a922678a45db0723f40105108f4ffbc29d03f Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 10 Dec 2004 01:23:44 +0000 Subject: [PATCH] The rabbit hole goes deeper. Now we can configure the client with a custom classloader to use when unserializing objects off the network. Also fixed the way custom classloaders were used as Class.forName(class, true, loader) seems to be the proper way to go to have caching work and whatnot. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3268 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../crowd/server/PlaceRegistry.java | 3 ++- .../com/threerings/io/ObjectInputStream.java | 16 ++++++++++++++-- .../threerings/presents/client/Client.java | 19 ++++++++++++++++++- .../presents/client/Communicator.java | 16 +++++++++++++++- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/java/com/threerings/crowd/server/PlaceRegistry.java b/src/java/com/threerings/crowd/server/PlaceRegistry.java index 7505070e0..a57b62cc3 100644 --- a/src/java/com/threerings/crowd/server/PlaceRegistry.java +++ b/src/java/com/threerings/crowd/server/PlaceRegistry.java @@ -114,7 +114,8 @@ public class PlaceRegistry try { // load up the manager class - Class pmgrClass = loader.loadClass(config.getManagerClassName()); + Class pmgrClass = Class.forName( + config.getManagerClassName(), true, loader); // create a place manager for this place pmgr = (PlaceManager)pmgrClass.newInstance(); // let the pmgr know about us and its configuration diff --git a/src/java/com/threerings/io/ObjectInputStream.java b/src/java/com/threerings/io/ObjectInputStream.java index 050a911aa..e42e68d3d 100644 --- a/src/java/com/threerings/io/ObjectInputStream.java +++ b/src/java/com/threerings/io/ObjectInputStream.java @@ -1,5 +1,5 @@ // -// $Id: ObjectInputStream.java,v 1.4 2004/08/27 02:12:36 mdb Exp $ +// $Id$ // // Narya library - tools for developing networked games // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved @@ -48,6 +48,15 @@ public class ObjectInputStream extends DataInputStream super(source); } + /** + * Customizes the class loader used to instnatiate objects read from + * the input stream. + */ + public void setClassLoader (ClassLoader loader) + { + _loader = loader; + } + /** * Reads a {@link Streamable} instance or one of the supported object * types from the input stream. @@ -82,7 +91,7 @@ public class ObjectInputStream extends DataInputStream // read in the class metadata, create a class mapping record // for the class, and cache it String cname = readUTF(); - Class sclass = Class.forName(cname); + Class sclass = Class.forName(cname, true, _loader); Streamer streamer = Streamer.getStreamer(sclass); if (STREAM_DEBUG) { Log.info(hashCode() + ": New class '" + cname + "'."); @@ -209,6 +218,9 @@ public class ObjectInputStream extends DataInputStream /** The streamer being used currently. */ protected Streamer _streamer; + /** The class loader we use to instantiate objects. */ + protected ClassLoader _loader = getClass().getClassLoader(); + /** Used to activate verbose debug logging. */ protected static final boolean STREAM_DEBUG = false; } diff --git a/src/java/com/threerings/presents/client/Client.java b/src/java/com/threerings/presents/client/Client.java index 6e5d225b6..ab876ecc5 100644 --- a/src/java/com/threerings/presents/client/Client.java +++ b/src/java/com/threerings/presents/client/Client.java @@ -1,5 +1,5 @@ // -// $Id: Client.java,v 1.50 2004/10/18 21:40:24 mdb Exp $ +// $Id$ // // Narya library - tools for developing networked games // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved @@ -188,6 +188,18 @@ public class Client _version = version; } + /** + * Configures the client with a custom class loader which will be used + * when reading objects off of the network. + */ + public void setClassLoader (ClassLoader loader) + { + _loader = loader; + if (_comm != null) { + _comm.setClassLoader(loader); + } + } + /** * Returns the data associated with our authentication response. Users * of the Presents system may wish to communicate authentication @@ -352,6 +364,7 @@ public class Client // otherwise create a new communicator instance and start it up. // this will initiate the logon process _comm = new Communicator(this); + _comm.setClassLoader(_loader); _comm.logon(); // register an interval that we'll use to keep the clock synced @@ -688,6 +701,10 @@ public class Client /** The entity that manages our network communications. */ protected Communicator _comm; + /** A custom class loader used to load objects that come in over the + * network. */ + protected ClassLoader _loader = getClass().getClassLoader(); + /** General startup information provided by the server. */ protected BootstrapData _bstrap; diff --git a/src/java/com/threerings/presents/client/Communicator.java b/src/java/com/threerings/presents/client/Communicator.java index 2b1fb4ccb..2572fd00f 100644 --- a/src/java/com/threerings/presents/client/Communicator.java +++ b/src/java/com/threerings/presents/client/Communicator.java @@ -1,5 +1,5 @@ // -// $Id: Communicator.java,v 1.36 2004/10/21 23:36:27 mdb Exp $ +// $Id$ // // Narya library - tools for developing networked games // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved @@ -162,6 +162,18 @@ public class Communicator _msgq.append(msg); } + /** + * Configures this communicator with a custom class loader to be used + * when reading and writing objects over the network. + */ + public void setClassLoader (ClassLoader loader) + { + _loader = loader; + if (_oin != null) { + _oin.setClassLoader(loader); + } + } + /** * Callback called by the reader when the authentication process * completes successfully. Here we extract the bootstrap information @@ -439,6 +451,7 @@ public class Communicator // create our object input and output streams _oin = new ObjectInputStream(_fin); + _oin.setClassLoader(_loader); _oout = new ObjectOutputStream(_fout); } @@ -600,6 +613,7 @@ public class Communicator protected ObjectInputStream _oin; protected ClientDObjectMgr _omgr; + protected ClassLoader _loader; /** Used to control low-level message logging. */ protected static RuntimeAdjust.BooleanAdjust _logMessages =