001: package com.calipso.reportgenerator.reportcalculator;
002:
003: import com.calipso.reportgenerator.common.LanguageTraslator;
004:
005: import java.io.*;
006: import java.nio.channels.FileChannel;
007: import java.nio.channels.FileLock;
008: import java.sql.ResultSet;
009:
010: /**
011: *
012: * User: jbassino
013: * Date: 16-sep-2005
014: * Time: 14:07:46
015: */
016: public class BlockSerializerThread extends Thread {
017:
018: private String blockFileName;
019: private boolean serialized;
020: private Matrix matrix;
021: private Exception savedException;
022:
023: public BlockSerializerThread(String blockFileName) {
024: super ();
025: this .blockFileName = blockFileName;
026: }
027:
028: public BlockSerializerThread(String fileName, Matrix usedPart) {
029: super ();
030: this .blockFileName = fileName;
031: this .matrix = usedPart;
032: }
033:
034: public void run() {
035: try {
036: if (matrix == null) {
037: //Deserializa
038: loadBlock();
039: } else {
040: //Sino, significa que le pasaron la matrix, y la serializa
041: serializeBlock();
042: }
043: } catch (Exception e) {
044: //La excepcion se imprime, pero se guarda para que la vea el proceso padre
045: e.printStackTrace();
046: savedException = e;
047: } catch (Throwable t) {
048: savedException = new Exception(t);
049: }
050: System.out.println(LanguageTraslator.traslate("487"));
051: System.out.println(LanguageTraslator.traslate("488")
052: + blockFileName);
053: System.gc();
054: System.gc();
055: System.gc();
056: System.gc();
057: System.gc();
058: System.out.println("5 gc()" + blockFileName);
059: }
060:
061: private void serializeBlock() throws Exception, Throwable {
062: System.out.println(LanguageTraslator.traslate("489")
063: + blockFileName);
064: File file = new File(blockFileName);
065: if (file.exists()) {
066: file.delete();
067: }
068: FileOutputStream fileOutputStream = new FileOutputStream(
069: blockFileName);
070: FileChannel channel = fileOutputStream.getChannel();
071: FileLock lock = channel.tryLock();
072: ObjectOutputStream objectOutputStream = new ObjectOutputStream(
073: fileOutputStream);
074: objectOutputStream.writeObject(matrix);
075: matrix = null;
076: lock.release();
077: objectOutputStream.flush();
078: fileOutputStream.flush();
079: objectOutputStream.close();
080: fileOutputStream.close();
081: }
082:
083: private synchronized void loadBlock() throws Exception {
084: System.out.println(LanguageTraslator.traslate("490")
085: + blockFileName);
086: FileInputStream fileInputStream = new FileInputStream(
087: blockFileName);
088: //fileInputStream.getFD().sync();
089: while (matrix == null) {
090: try {
091: ObjectInputStream objectInputStream = new ObjectInputStream(
092: fileInputStream);
093: matrix = (Matrix) objectInputStream.readObject();
094: objectInputStream.close();
095: } catch (IOException e) {
096: //e.printStackTrace();
097: sleep(10000);
098: }
099: }
100: fileInputStream.close();
101: notify();
102: }
103:
104: public synchronized Matrix getPreparedPart() throws Exception {
105: while (matrix == null && savedException == null) {
106: wait();
107: }
108: if (savedException != null) {
109: System.out.println(LanguageTraslator.traslate("491"));
110: savedException.printStackTrace();
111: throw savedException;
112: }
113: return matrix;
114: }
115:
116: }
|