KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > script > QuercusCompiledScript


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.script;
31
32 import com.caucho.quercus.env.Env;
33 import com.caucho.quercus.page.InterpretedPage;
34 import com.caucho.quercus.page.QuercusPage;
35 import com.caucho.quercus.program.QuercusProgram;
36 import com.caucho.vfs.*;
37
38 import javax.script.CompiledScript;
39 import javax.script.ScriptContext;
40 import javax.script.ScriptEngine;
41 import javax.script.ScriptException;
42 import java.io.Writer JavaDoc;
43
44 /**
45  * Script engine
46  */

47 public class QuercusCompiledScript extends CompiledScript {
48   private final QuercusScriptEngine _engine;
49   private final QuercusProgram _program;
50
51   QuercusCompiledScript(QuercusScriptEngine engine, QuercusProgram program)
52   {
53     _engine = engine;
54     _program = program;
55   }
56
57   /**
58    * evaluates based on a reader.
59    */

60   public Object JavaDoc eval(ScriptContext cxt)
61     throws ScriptException
62   {
63     try {
64       Writer JavaDoc writer = cxt.getWriter();
65
66       WriteStream out;
67
68       if (writer != null) {
69     ReaderWriterStream s = new ReaderWriterStream(null, writer);
70     WriteStream os = new WriteStream(s);
71     
72     try {
73       os.setEncoding("utf-8");
74     } catch (Exception JavaDoc e) {
75     }
76
77     out = os;
78       }
79       else
80     out = new NullWriteStream();
81
82       QuercusPage page = new InterpretedPage(_program);
83
84       Env env = new Env(_engine.getQuercus(), page, out, null, null);
85
86       env.setScriptContext(cxt);
87
88       Object JavaDoc value = _program.execute(env).toJavaObject();
89
90       out.flushBuffer();
91       out.free();
92
93       return value;
94       /*
95     } catch (ScriptException e) {
96       throw e;
97       */

98     } catch (RuntimeException JavaDoc e) {
99       throw e;
100     } catch (Exception JavaDoc e) {
101       throw new ScriptException(e);
102     } catch (Throwable JavaDoc e) {
103       throw new RuntimeException JavaDoc(e);
104     }
105   }
106
107   /**
108    * Returns the script engine.
109    */

110   public ScriptEngine getEngine()
111   {
112     return _engine;
113   }
114
115   public String JavaDoc toString()
116   {
117     return "QuercusCompiledScript[]";
118   }
119 }
120
121
Popular Tags