1 16 package org.apache.cocoon.components.flow; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.excalibur.source.Source; 25 import org.apache.excalibur.source.SourceResolver; 26 import org.mozilla.javascript.Context; 27 import org.mozilla.javascript.Script; 28 import org.mozilla.javascript.Scriptable; 29 30 38 public abstract class CompilingInterpreter 39 extends AbstractInterpreter { 40 41 44 protected SourceResolver sourceresolver; 45 46 49 protected Map compiledScripts = new HashMap (); 50 51 54 public void service(ServiceManager manager) throws ServiceException { 55 super.service(manager); 56 this.sourceresolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); 57 } 58 59 62 public void dispose() { 63 if (this.compiledScripts != null) { 64 Iterator i = this.compiledScripts.values().iterator(); 65 while (i.hasNext()) { 66 ScriptSourceEntry current = (ScriptSourceEntry)i.next(); 67 this.sourceresolver.release(current.getSource()); 68 } 69 this.compiledScripts = null; 70 } 71 if (this.manager != null) { 72 this.manager.release(this.sourceresolver); 73 this.sourceresolver = null; 74 } 75 super.dispose(); 76 } 77 78 84 protected abstract Script compileScript(Context context, 85 Scriptable scope, 86 Source source) throws Exception ; 87 protected class ScriptSourceEntry { 89 final private Source source; 90 private Script script; 91 private long compileTime; 92 93 public ScriptSourceEntry(Source source) { 94 this.source = source; 95 } 96 97 public ScriptSourceEntry(Source source, Script script, long t) { 98 this.source = source; 99 this.script = script; 100 this.compileTime = t; 101 } 102 103 public Source getSource() { 104 return source; 105 } 106 107 public Script getScript(Context context, Scriptable scope, 108 boolean refresh, CompilingInterpreter interpreter) 109 throws Exception { 110 if (refresh) { 111 source.refresh(); 112 } 113 if (script == null || compileTime < source.getLastModified()) { 114 script = interpreter.compileScript(context, scope, source); 115 compileTime = source.getLastModified(); 116 } 117 return script; 118 } 119 } 120 } 121 | Popular Tags |