KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportcalculator > BlockSerializerThread


1 package com.calipso.reportgenerator.reportcalculator;
2
3 import com.calipso.reportgenerator.common.LanguageTraslator;
4
5 import java.io.*;
6 import java.nio.channels.FileChannel JavaDoc;
7 import java.nio.channels.FileLock JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9
10 /**
11  *
12  * User: jbassino
13  * Date: 16-sep-2005
14  * Time: 14:07:46
15  */

16 public class BlockSerializerThread extends Thread JavaDoc {
17
18   private String JavaDoc blockFileName;
19   private boolean serialized;
20   private Matrix matrix;
21   private Exception JavaDoc savedException;
22
23   public BlockSerializerThread(String JavaDoc blockFileName){
24     super();
25     this.blockFileName = blockFileName;
26   }
27
28   public BlockSerializerThread(String JavaDoc fileName, Matrix usedPart) {
29     super();
30     this.blockFileName = fileName;
31     this.matrix = usedPart;
32   }
33
34   public void run() {
35     try{
36       if(matrix==null){
37         //Deserializa
38
loadBlock();
39       }else{
40         //Sino, significa que le pasaron la matrix, y la serializa
41
serializeBlock();
42       }
43     }catch (Exception JavaDoc e){
44       //La excepcion se imprime, pero se guarda para que la vea el proceso padre
45
e.printStackTrace();
46       savedException = e;
47     }catch (Throwable JavaDoc t){
48       savedException = new Exception JavaDoc(t);
49     }
50     System.out.println(LanguageTraslator.traslate("487"));
51     System.out.println(LanguageTraslator.traslate("488") + blockFileName);
52     System.gc();
53     System.gc();
54     System.gc();
55     System.gc();
56     System.gc();
57     System.out.println("5 gc()" + blockFileName);
58   }
59
60   private void serializeBlock() throws Exception JavaDoc, Throwable JavaDoc{
61     System.out.println(LanguageTraslator.traslate("489") + blockFileName);
62     File file = new File(blockFileName);
63     if(file.exists()){
64       file.delete();
65     }
66     FileOutputStream fileOutputStream = new FileOutputStream(blockFileName);
67     FileChannel JavaDoc channel = fileOutputStream.getChannel();
68     FileLock JavaDoc lock = channel.tryLock();
69     ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
70     objectOutputStream.writeObject(matrix);
71     matrix=null;
72     lock.release();
73     objectOutputStream.flush();
74     fileOutputStream.flush();
75     objectOutputStream.close();
76     fileOutputStream.close();
77   }
78
79   private synchronized void loadBlock() throws Exception JavaDoc{
80     System.out.println(LanguageTraslator.traslate("490") + blockFileName);
81     FileInputStream fileInputStream = new FileInputStream(blockFileName);
82     //fileInputStream.getFD().sync();
83
while(matrix==null){
84       try{
85         ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
86         matrix = (Matrix)objectInputStream.readObject();
87         objectInputStream.close();
88       }catch(IOException e){
89         //e.printStackTrace();
90
sleep(10000);
91       }
92     }
93     fileInputStream.close();
94     notify();
95   }
96
97   public synchronized Matrix getPreparedPart() throws Exception JavaDoc {
98     while(matrix==null && savedException==null){
99       wait();
100     }
101     if(savedException!=null){
102       System.out.println(LanguageTraslator.traslate("491"));
103       savedException.printStackTrace();
104       throw savedException;
105     }
106     return matrix;
107   }
108
109 }
110
111
Popular Tags