提交 04ce5187 编写于 作者: K kohsuke

implemented unexport of pipes.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@1368 71c3de6d-444a-0410-be80-ed276b4c234a
上级 3e3f5592
......@@ -143,13 +143,17 @@ public class Channel {
}
/*package*/ int export(Object instance) {
return exportedObjects.intern(instance);
return exportedObjects.export(instance);
}
/*package*/ Object getExportedObject(int oid) {
return exportedObjects.get(oid);
}
/*package*/ void unexport(int id) {
exportedObjects.unexport(id);
}
/**
* Makes a remote procedure call.
*
......
......@@ -4,7 +4,7 @@ import java.util.HashMap;
import java.util.Map;
/**
* Manages unique ID for exported objects.
* Manages unique ID for exported objects, and allows look-up from IDs.
*
* @author Kohsuke Kawaguchi
*/
......@@ -12,10 +12,24 @@ final class ExportTable<T> {
private final Map<Integer,T> table = new HashMap<Integer,T>();
private final Map<T,Integer> reverse = new HashMap<T,Integer>();
// id==0 is reserved for bootstrap classloader
/**
* Unique ID generator.
*/
private int iota = 1;
public synchronized int intern(T t) {
/**
* Exports the given object.
*
* <p>
* Until the object is {@link #unexport(Object) unexported}, it will
* not be subject to GC.
*
* @return
* The assigned 'object ID'. If the object is already exported,
* it will return the ID already assigned to it.
*/
// TODO: the 'intern' semantics requires reference counting for proper unexport op.
public synchronized int export(T t) {
if(t==null) return 0; // bootstrap classloader
Integer id = reverse.get(t);
......@@ -32,6 +46,9 @@ final class ExportTable<T> {
return table.get(id);
}
/**
* Removes the exported object from the table.
*/
public synchronized void unexport(T t) {
if(t==null) return;
Integer id = reverse.remove(t);
......
......@@ -43,8 +43,6 @@ import java.util.logging.Logger;
* to send data, instead of typed proxy object. This allows the writer to send data
* without blocking until the arrival of the data is confirmed.
*
* TODO: unexport
*
* @author Kohsuke Kawaguchi
*/
public final class Pipe implements Serializable {
......@@ -143,8 +141,7 @@ public final class Pipe implements Serializable {
*/
private ByteArrayOutputStream tmp;
/**
* Set to true if the stream is closed before it's connected
* to a remote object.
* Set to true if the stream is closed.
*/
private boolean closed;
......@@ -169,7 +166,7 @@ public final class Pipe implements Serializable {
write(tmp.toByteArray());
tmp = null;
}
if(closed)
if(closed) // already marked closed?
close();
}
......@@ -178,6 +175,8 @@ public final class Pipe implements Serializable {
}
public void write(byte b[], int off, int len) throws IOException {
if(closed)
throw new IOException("stream is already closed");
if(off==0 && len==b.length)
write(b);
else {
......@@ -188,6 +187,8 @@ public final class Pipe implements Serializable {
}
public synchronized void write(byte b[]) throws IOException {
if(closed)
throw new IOException("stream is already closed");
if(channel==null) {
if(tmp==null)
tmp = new ByteArrayOutputStream();
......@@ -245,6 +246,7 @@ public final class Pipe implements Serializable {
protected void execute(Channel channel) {
OutputStream os = (OutputStream) channel.getExportedObject(oid);
channel.unexport(oid);
try {
os.close();
} catch (IOException e) {
......@@ -274,6 +276,7 @@ public final class Pipe implements Serializable {
protected void execute(Channel channel) {
try {
RemoteOutputStream ros = (RemoteOutputStream) channel.getExportedObject(oidRos);
channel.unexport(oidRos);
ros.connect(channel, oidPos);
} catch (IOException e) {
logger.severe("Failed to connect to pipe");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册