KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > CompilingInterpreter


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.flow;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
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 /**
31  * This is a base class for all interpreters compiling the scripts
32  *
33  * @author <a HREF="mailto:ovidiu@apache.org">Ovidiu Predescu</a>
34  * @author <a HREF="mailto:crafterm@apache.org">Marcus Crafter</a>
35  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
36  * @version CVS $Id: CompilingInterpreter.java 124685 2005-01-08 22:20:56Z antonio $
37  */

38 public abstract class CompilingInterpreter
39         extends AbstractInterpreter {
40
41     /**
42      * A source resolver
43      */

44     protected SourceResolver sourceresolver;
45
46     /**
47      * Mapping of String objects (source uri's) to ScriptSourceEntry's
48      */

49     protected Map JavaDoc compiledScripts = new HashMap JavaDoc();
50
51     /**
52      * Composable
53      */

54     public void service(ServiceManager manager) throws ServiceException {
55         super.service(manager);
56         this.sourceresolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
57     }
58
59     /* (non-Javadoc)
60      * @see org.apache.avalon.framework.activity.Disposable#dispose()
61      */

62     public void dispose() {
63         if (this.compiledScripts != null) {
64             Iterator JavaDoc 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     /**
79      * TODO - This is a little bit strange, the interpreter calls
80      * get Script on the ScriptSourceEntry which in turn
81      * calls compileScript on the interpreter. I think we
82      * need more refactoring here.
83      */

84     protected abstract Script compileScript(Context context,
85                                             Scriptable scope,
86                                             Source source) throws Exception JavaDoc;
87     // This class cannot be static
88
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 JavaDoc {
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