001: /* *****************************************************************************
002: * CachedInfo.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.cm;
011:
012: import org.openlaszlo.compiler.*;
013:
014: import java.io.File;
015: import java.io.FileInputStream;
016: import java.io.FileNotFoundException;
017: import java.io.FileOutputStream;
018: import java.io.IOException;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import java.io.Serializable;
022:
023: import org.apache.log4j.*;
024:
025: /** A <code>CachedInfo</code> contains all the information
026: * we cache from a compilation. This includes dependency information
027: * and the canvas.
028: *
029: * @author Oliver Steele
030: */
031: public class CachedInfo implements Serializable {
032:
033: private final static Logger mLogger = Logger
034: .getLogger(CachedInfo.class);
035: private final DependencyTracker mTracker;
036: private final Canvas mCanvas;
037: private final String mEncoding;
038:
039: /**
040: * Construct a CachedInfo
041: * @param tracker the DependencyTracker
042: * @param canvas the Canvas
043: */
044: public CachedInfo(DependencyTracker tracker, Canvas canvas,
045: String encoding) {
046: mTracker = tracker;
047: mCanvas = canvas;
048: mEncoding = encoding;
049: }
050:
051: /**
052: * Read a CachedInfo from an existing file.
053: */
054: public static CachedInfo readFrom(File file)
055: throws CompilationError {
056: CachedInfo info = null;
057:
058: try {
059: FileInputStream istream = new FileInputStream(file);
060: try {
061: ObjectInputStream p = new ObjectInputStream(istream);
062: return (CachedInfo) p.readObject();
063: } finally {
064: istream.close();
065: }
066: } catch (java.io.InvalidClassException ioe) {
067: } catch (FileNotFoundException ioe) {
068: } catch (IOException ioe) {
069: CompilationError e = new CompilationError(ioe);
070: e.initPathname(file.getPath());
071: mLogger.error(e.getMessage());
072: throw e;
073: } catch (ClassNotFoundException cnfe) {
074: }
075: return new CachedInfo(null, null, null);
076: }
077:
078: /** Write a CachedInfo to an existing file
079: * @param file a File to save to
080: * @throws IOException if an error occurs
081: */
082: void writeTo(File file) throws IOException {
083: mLogger.debug(
084: /* (non-Javadoc)
085: * @i18n.test
086: * @org-mes="writeTo " + p[0]
087: */
088: org.openlaszlo.i18n.LaszloMessages.getMessage(CachedInfo.class
089: .getName(), "051018-86", new Object[] { file
090: .getAbsolutePath() }));
091: File dir = file.getParentFile();
092: if (dir != null) {
093: dir.mkdirs();
094: }
095: file.createNewFile();
096: FileOutputStream ostream = new FileOutputStream(file);
097: ObjectOutputStream p = new ObjectOutputStream(ostream);
098: p.writeObject(this );
099: p.flush();
100: ostream.close();
101: }
102:
103: public Canvas getCanvas() {
104: mLogger.debug(
105: /* (non-Javadoc)
106: * @i18n.test
107: * @org-mes="Getting canvas with size " + p[0] + " by " + p[1]
108: */
109: org.openlaszlo.i18n.LaszloMessages.getMessage(CachedInfo.class
110: .getName(), "051018-107", new Object[] {
111: new Integer(mCanvas.getWidth()),
112: new Integer(mCanvas.getHeight()) }));
113: return mCanvas;
114: }
115:
116: public String getEncoding() {
117: return mEncoding;
118: }
119:
120: public DependencyTracker getDependencyTracker() {
121: return mTracker;
122: }
123: }
|