Author: glandais Date: 2008-02-12 18:17:27 +0000 (Tue, 12 Feb 2008) New Revision: 871 Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/SimExplorerServiceStream.java Log: Magic stream clean up with transient trick for detecting remote mode Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/SimExplorerServiceStream.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/SimExplorerServiceStream.java 2008-02-12 18:16:40 UTC (rev 870) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/SimExplorerServiceStream.java 2008-02-12 18:17:27 UTC (rev 871) @@ -21,55 +21,70 @@ import java.io.IOException; import java.io.InputStream; import java.io.Serializable; +import java.rmi.RemoteException; import com.healthmarketscience.rmiio.RemoteInputStream; import com.healthmarketscience.rmiio.RemoteInputStreamClient; +import com.healthmarketscience.rmiio.SimpleRemoteInputStream; /** * The Class SimExplorerServiceStream. + * + * Simplify usage of remote streams. + * + * To allow magic remoting, your + * stream has to encapsulated in a SimExplorerServiceStream with + * + * <code> + * SimExplorerServiceStream serviceStream = new SimExplorerServiceStream(myStream); + * service.method(serviceStream); + * </code> + * + * Stream will work, even through a RMI call. + * */ public class SimExplorerServiceStream extends InputStream implements Serializable { private static final long serialVersionUID = 1976003275577858320L; - /** The ris. */ + /** The remote output stream from RMIIO, used for serializing stream. */ private RemoteInputStream ris; - /** The bis. */ - private BufferedInputStream bis; + /** The local stream, set to null if stream is serialized. */ + transient private BufferedInputStream bis; /** - * Instantiates a new sim explorer service stream. + * Instantiates a new stream. * - * @param ris - * the ris + * @param bis the bis */ - public SimExplorerServiceStream(RemoteInputStream ris) { - super(); - this.ris = ris; - } - - /** - * Instantiates a new sim explorer service stream. - * - * @param bis - * the bis - */ public SimExplorerServiceStream(InputStream bis) { super(); + // Set local stream (null if this is serialized) this.bis = new BufferedInputStream(bis); + try { + // Create a stub if possible, used in init + this.ris = new SimpleRemoteInputStream(bis).export(); + } catch (RemoteException e) { + this.ris = null; + } } /** - * Inits the. + * Inits the local stream. + * + * Called before every delegated method */ private void init() { + // Check is local stream is ok if (bis == null) { try { + // Otherwise use RMIIO to retrieve remote stream this.bis = new BufferedInputStream(RemoteInputStreamClient .wrap(ris)); } catch (IOException e) { + // Something went wrong during transformation throw new RuntimeException(e); } }
participants (1)
-
glandais@users.labs.libre-entreprise.org