KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > cm > CachedInfo


1 /* *****************************************************************************
2  * CachedInfo.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.cm;
11 import org.openlaszlo.compiler.*;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.ObjectInputStream JavaDoc;
19 import java.io.ObjectOutputStream JavaDoc;
20 import java.io.Serializable JavaDoc;
21
22 import org.apache.log4j.*;
23
24 /** A <code>CachedInfo</code> contains all the information
25  * we cache from a compilation. This includes dependency information
26  * and the canvas.
27  *
28  * @author Oliver Steele
29  */

30 public class CachedInfo implements Serializable JavaDoc {
31
32     private final static Logger mLogger = Logger.getLogger(CachedInfo.class);
33     private final DependencyTracker mTracker;
34     private final Canvas mCanvas;
35     private final String JavaDoc mEncoding;
36
37     /**
38      * Construct a CachedInfo
39      * @param tracker the DependencyTracker
40      * @param canvas the Canvas
41      */

42     public CachedInfo(DependencyTracker tracker, Canvas canvas, String JavaDoc encoding) {
43         mTracker = tracker;
44         mCanvas = canvas;
45         mEncoding = encoding;
46     }
47
48     /**
49      * Read a CachedInfo from an existing file.
50      */

51     public static CachedInfo readFrom(File JavaDoc file)
52         throws CompilationError
53     {
54         CachedInfo info = null;
55         
56         try {
57             FileInputStream JavaDoc istream = new FileInputStream JavaDoc(file);
58             try {
59                 ObjectInputStream JavaDoc p = new ObjectInputStream JavaDoc(istream);
60                 return (CachedInfo) p.readObject();
61             } finally {
62                 istream.close();
63             }
64         } catch (java.io.InvalidClassException JavaDoc ioe) {
65         } catch (FileNotFoundException JavaDoc ioe) {
66         } catch (IOException JavaDoc ioe) {
67             CompilationError e = new CompilationError(ioe);
68             e.initPathname(file.getPath());
69             mLogger.error(e.getMessage());
70             throw e;
71         } catch (ClassNotFoundException JavaDoc cnfe) {
72         }
73         return new CachedInfo(null, null, null);
74     }
75
76     /** Write a CachedInfo to an existing file
77      * @param file a File to save to
78      * @throws IOException if an error occurs
79      */

80     void writeTo(File JavaDoc file) throws IOException JavaDoc {
81         mLogger.debug("writeTo " + file.getAbsolutePath());
82         File JavaDoc dir = file.getParentFile();
83         if (dir != null) {
84             dir.mkdirs();
85         }
86         file.createNewFile();
87         FileOutputStream JavaDoc ostream = new FileOutputStream JavaDoc(file);
88         ObjectOutputStream JavaDoc p = new ObjectOutputStream JavaDoc(ostream);
89         p.writeObject(this);
90         p.flush();
91         ostream.close();
92     }
93
94     public Canvas getCanvas() {
95         mLogger.debug("Getting canvas with size " + mCanvas.getWidth() +
96                       " by " + mCanvas.getHeight());
97         return mCanvas;
98     }
99
100     public String JavaDoc getEncoding() {
101         return mEncoding;
102     }
103
104
105     public DependencyTracker getDependencyTracker() {
106         return mTracker;
107     }
108 }
109
Popular Tags