| 1 | |
package org.webslinger.ext.quercus.template; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.OutputStreamWriter; |
| 5 | |
import java.io.StringWriter; |
| 6 | |
import java.io.Writer; |
| 7 | |
import java.util.Arrays; |
| 8 | |
import java.util.List; |
| 9 | |
import java.util.Map; |
| 10 | |
import java.util.Vector; |
| 11 | |
|
| 12 | |
import org.apache.bsf.BSFException; |
| 13 | |
import org.apache.bsf.BSFManager; |
| 14 | |
|
| 15 | |
import com.caucho.quercus.Quercus; |
| 16 | |
import com.caucho.quercus.env.Env; |
| 17 | |
import com.caucho.quercus.env.MethodMap; |
| 18 | |
import com.caucho.quercus.env.Value; |
| 19 | |
import com.caucho.quercus.page.QuercusPage; |
| 20 | |
import com.caucho.vfs.WriterStreamImpl; |
| 21 | |
import com.caucho.vfs.WriteStream; |
| 22 | |
|
| 23 | |
import org.webslinger.bsf.CompilingLanguageEngineImpl; |
| 24 | |
import org.webslinger.bsf.LanguageEngineInfo; |
| 25 | |
import org.webslinger.ext.quercus.LocalEnv; |
| 26 | |
|
| 27 | 3 | public class QuercusTemplateEngine extends CompilingLanguageEngineImpl<QuercusPage, QuercusPage> { |
| 28 | 3 | protected final Quercus php = new Quercus(); |
| 29 | |
protected final LocalEnv globalEnv; |
| 30 | 3 | protected final ThreadLocal<LocalEnv> tl = new ThreadLocal<LocalEnv>(); |
| 31 | 3 | protected final int _methodHash = MethodMap.hash("method"); |
| 32 | 3 | protected final char[] _methodArray = "method".toCharArray(); |
| 33 | |
protected Value managerValue; |
| 34 | |
|
| 35 | |
public QuercusTemplateEngine() { |
| 36 | 3 | super(); |
| 37 | 3 | compiler = new QuercusTemplateCompiler(this); |
| 38 | 3 | php.setCompile(true); |
| 39 | 3 | php.setLazyCompile(false); |
| 40 | 3 | globalEnv = newLocalEnv(); |
| 41 | 3 | } |
| 42 | |
|
| 43 | |
public LanguageEngineInfo getLanguageEngineInfo() { |
| 44 | 0 | return QuercusTemplateInfo.INSTANCE; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public void initialize(BSFManager manager, String lang, Vector declaredBeans) throws BSFException { |
| 48 | 3 | super.initialize(manager, lang, declaredBeans); |
| 49 | 3 | managerValue = (Value) compiler.wrap(manager, BSFManager.class); |
| 50 | 3 | getQuercus().addModule(new WebslingerTemplateModule()); |
| 51 | 3 | } |
| 52 | |
|
| 53 | |
public void terminate() { |
| 54 | 0 | globalEnv.close(); |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
public Quercus getQuercus() { |
| 58 | 12 | return php; |
| 59 | |
} |
| 60 | |
|
| 61 | |
public Object apply(Object body, QuercusPage page, String[] names, Object[] args) throws Throwable { |
| 62 | 3 | WriterStreamImpl writerImpl = new WriterStreamImpl(); |
| 63 | 3 | writerImpl.setWriter((Writer) args[0]); |
| 64 | 3 | LocalEnv env = new LocalEnv(getQuercus(), page, writerImpl, null, null); |
| 65 | 12 | for (int i = 2; i < args.length; i++) { |
| 66 | 9 | env.setValue(names[i], (Value) args[i]); |
| 67 | |
} |
| 68 | 3 | env.setValue("bsf", managerValue); |
| 69 | 3 | env.setValue("BSFParent", (Value) compiler.wrap(body, body.getClass())); |
| 70 | 3 | Map<String, Object> context = (Map<String, Object>) args[1]; |
| 71 | 3 | for (Map.Entry<String, Object> entry: context.entrySet()) { |
| 72 | 6 | Object value = entry.getValue(); |
| 73 | 6 | env.setValue(entry.getKey(), value != null ? (Value) compiler.wrap(value, value.getClass()) : null); |
| 74 | 6 | } |
| 75 | 3 | env.start(); |
| 76 | |
try { |
| 77 | 3 | return page.execute(env); |
| 78 | |
} finally { |
| 79 | 3 | env.close(); |
| 80 | 3 | env.getOriginalOut().flush(); |
| 81 | |
} |
| 82 | |
} |
| 83 | |
|
| 84 | |
public Object apply(Object body, QuercusPage page, Object[] args) throws Throwable { |
| 85 | 0 | throw new InternalError("Should not be called."); |
| 86 | |
} |
| 87 | |
|
| 88 | |
public Object eval(Object body, QuercusPage page) throws Throwable { |
| 89 | 0 | throw new InternalError(); |
| 90 | |
} |
| 91 | |
|
| 92 | |
public void exec(Object body, QuercusPage page) throws Throwable { |
| 93 | 0 | throw new InternalError(); |
| 94 | |
} |
| 95 | |
|
| 96 | |
public LocalEnv getLocalEnv() { |
| 97 | 0 | LocalEnv env = tl.get(); |
| 98 | 0 | if (env == null) { |
| 99 | 0 | env = newLocalEnv(); |
| 100 | 0 | tl.set(env); |
| 101 | |
} |
| 102 | 0 | return env; |
| 103 | |
} |
| 104 | |
|
| 105 | |
public LocalEnv getGlobalEnv() { |
| 106 | 21 | return globalEnv; |
| 107 | |
} |
| 108 | |
|
| 109 | |
public LocalEnv newLocalEnv() { |
| 110 | 3 | return newLocalEnv(new OutputStreamWriter(System.err)); |
| 111 | |
} |
| 112 | |
|
| 113 | |
public LocalEnv newLocalEnv(Writer writer) { |
| 114 | 3 | LocalEnv env = new LocalEnv(getQuercus(), null, null, null); |
| 115 | 3 | WriteStream writeStream = env.getOriginalOut(); |
| 116 | 3 | env.getWriterStreamImpl().setWriter(new OutputStreamWriter(System.err)); |
| 117 | 3 | return env; |
| 118 | |
} |
| 119 | |
} |