001: /*
002: * Created on Feb 12, 2004
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.xssl.io;
008:
009: import java.io.File;
010: import java.io.FileInputStream;
011: import java.io.OutputStream;
012: import java.io.PipedWriter;
013: import java.io.PipedReader;
014: import java.io.Writer;
015: import java.io.BufferedOutputStream;
016: import java.lang.reflect.Method;
017: import java.util.HashMap;
018:
019: import org.xdev.base.xssl.XSSLReturn;
020: import org.xdev.base.xssl.manage.ITransactionFinalizable;
021:
022: /**
023: * @author AYegorov
024: *
025: * To change the template for this generated type comment go to
026: * Window>Preferences>Java>Code Generation>Code and Comments
027: */
028: public class FileTunnel extends AbstractFile implements
029: ITransactionFinalizable {
030: private Object objStream = null;
031:
032: public final static int WRITE = 0;
033: public final static int FLUSH = 1;
034: public final static int CLOSE = 2;
035:
036: /**
037: * @param id
038: */
039: public FileTunnel(String id) {
040: super (id);
041: // XXX Auto-generated constructor stub
042: }
043:
044: /**
045: * @param id
046: * @param properties
047: */
048: public FileTunnel(String id, HashMap properties) {
049: super (id, properties);
050: // XXX Auto-generated constructor stub
051: }
052:
053: /* (non-Javadoc)
054: * @see org.xdev.base.xssl.io.AbstractFile#processFile(java.io.File)
055: */
056: protected Object processFile(File f) throws Exception {
057: byte[] buffer = new byte[this .hasProperty("buffer-size") ? this
058: .getIntegerProperty("buffer-size") : new Long(f
059: .length()).intValue()];
060:
061: FileInputStream in = new FileInputStream(f);
062:
063: XSSLReturn w = (XSSLReturn) this .getElement(0);
064:
065: if (w.execute(this )) {
066:
067: this .objStream = w.getObjectValue();
068:
069: if (this .objStream instanceof OutputStream) {
070: this .objStream = new BufferedOutputStream(
071: (OutputStream) this .objStream);
072: }
073:
074: int c;
075:
076: while ((c = in.read(buffer)) != -1) {
077:
078: //this.objStream.getClass().getMethod("write", new Class[] {byte[].class}).invoke(this.objStream, new Object[]{});
079:
080: this .doStreamAction(this .objStream, FileTunnel.WRITE,
081: new Object[] { buffer });
082: }
083: }
084:
085: return null;
086: }
087:
088: protected Object doStreamAction(Object stream, int action,
089: Object[] params) throws Exception {
090:
091: Class streamClass = stream.getClass();
092:
093: Method streamMethod = null;
094:
095: Class[] signature = null;
096:
097: String methodName = null;
098:
099: switch (action) {
100: case FileTunnel.WRITE:
101: methodName = "write";
102:
103: signature = new Class[] { byte[].class };
104: break;
105:
106: case FileTunnel.FLUSH:
107: methodName = "flush";
108: break;
109:
110: case FileTunnel.CLOSE:
111: methodName = "close";
112: break;
113: }
114:
115: return streamMethod.invoke(stream, params);
116: }
117:
118: public void doFinalize() throws Exception {
119: this.doStreamAction(this.objStream, FileTunnel.FLUSH, null);
120: this.doStreamAction(this.objStream, FileTunnel.CLOSE, null);
121: }
122: }
|