KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > page > PageManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.page;
31
32 import com.caucho.quercus.Quercus;
33 import com.caucho.quercus.parser.QuercusParser;
34 import com.caucho.quercus.program.QuercusProgram;
35 import com.caucho.util.LruCache;
36 import com.caucho.vfs.IOExceptionWrapper;
37 import com.caucho.vfs.Path;
38
39 import java.io.IOException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Each "page" refers to a quercus file.
45  */

46 public class PageManager
47 {
48   private static final Logger JavaDoc log
49     = Logger.getLogger(PageManager.class.getName());
50
51   private final Quercus _quercus;
52   
53   private Path _pwd;
54   private boolean _isLazyCompile;
55   private boolean _isCompile;
56
57   protected LruCache<Path,QuercusProgram> _programCache
58     = new LruCache<Path,QuercusProgram>(1024);
59
60   private boolean _isClosed;
61   
62   /**
63    * Constructor.
64    */

65   public PageManager(Quercus quercus)
66   {
67     _quercus = quercus;
68   }
69
70   public Quercus getQuercus()
71   {
72     return _quercus;
73   }
74
75   /**
76    * Gets the owning directory.
77    */

78   public Path getPwd()
79   {
80     return _quercus.getPwd();
81   }
82
83   /**
84    * true if the pages should be compiled.
85    */

86   public boolean isCompile()
87   {
88     return _isCompile;
89   }
90
91   /**
92    * true if the pages should be compiled.
93    */

94   public void setCompile(boolean isCompile)
95   {
96     _isCompile = isCompile;
97   }
98
99   /**
100    * true if the pages should be compiled lazily.
101    */

102   public boolean isLazyCompile()
103   {
104     return _isLazyCompile;
105   }
106
107   /**
108    * true if the pages should be compiled.
109    */

110   public void setLazyCompile(boolean isCompile)
111   {
112     _isLazyCompile = isCompile;
113   }
114
115   /**
116    * true if the manager is active.
117    */

118   public boolean isActive()
119   {
120     return ! _isClosed;
121   }
122
123   /**
124    * Returns the relative path.
125    */

126   /*
127   public String getClassName(Path path)
128   {
129     if (path == null)
130       return "tmp.eval";
131     
132     String pathName = path.getFullPath();
133     String pwdName = getPwd().getFullPath();
134
135     String relPath;
136
137     if (pathName.startsWith(pwdName))
138       relPath = pathName.substring(pwdName.length());
139     else
140       relPath = pathName;
141
142     return "_quercus." + JavaCompiler.mangleName(relPath);
143   }
144   */

145   
146   /**
147    * Returns a parsed or compiled quercus program.
148    *
149    * @param path the source file path
150    *
151    * @return the parsed program
152    *
153    * @throws IOException
154    */

155   public QuercusPage parse(Path path)
156     throws IOException JavaDoc
157   {
158     return parse(path, null, -1);
159   }
160   
161   /**
162    * Returns a parsed or compiled quercus program.
163    *
164    * @param path the source file path
165    *
166    * @return the parsed program
167    *
168    * @throws IOException
169    */

170   public QuercusPage parse(Path path, String JavaDoc fileName, int line)
171     throws IOException JavaDoc
172   {
173     try {
174       QuercusProgram program;
175
176       program = _programCache.get(path);
177
178       if (program == null || program.isModified()) {
179     program = QuercusParser.parse(_quercus,
180                       path,
181                       _quercus.getScriptEncoding(),
182                       fileName,
183                       line);
184     _programCache.put(path, program);
185       }
186
187       if (program.getCompiledPage() != null) {
188     return program.getCompiledPage();
189       }
190
191       return compilePage(program, path);
192     } catch (IOException JavaDoc e) {
193       throw e;
194     } catch (RuntimeException JavaDoc e) {
195       throw e;
196     } catch (Throwable JavaDoc e) {
197       throw new IOExceptionWrapper(e);
198     }
199   }
200
201   protected QuercusPage compilePage(QuercusProgram program, Path path)
202   {
203     return new InterpretedPage(program);
204   }
205
206   public void close()
207   {
208     _isClosed = true;
209   }
210 }
211
212
Popular Tags