coliver     2003/12/27 23:19:23

  Modified:    src/java/org/apache/cocoon/components/flow/javascript/fom
                        CompilingClassLoader.java
                        FOM_JavaScriptInterpreter.java
  Log:
  Only recompile classes whose source files aren't up to date. Otherwise the 
cached byte code will be used. Also added experimental fix to provide direct 
access to javax, org, and com packages
  
  Revision  Changes    Path
  1.3       +26 -8     
cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/CompilingClassLoader.java
  
  Index: CompilingClassLoader.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/CompilingClassLoader.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CompilingClassLoader.java 27 Dec 2003 09:06:26 -0000      1.2
  +++ CompilingClassLoader.java 28 Dec 2003 07:19:23 -0000      1.3
  @@ -76,15 +76,22 @@
   
       SourceResolver sourceResolver;
       JavaCompiler compiler;
  -    Map output = new HashMap();
       List sourcePath = new LinkedList();
       HashSet sourceListeners = new HashSet();
  +    ClassRepository classRepository;
   
       public interface SourceListener {
           public void sourceCompiled(Source src);
           public void sourceCompilationError(Source src, String error);
       }
   
  +    public interface ClassRepository {
  +        public byte[] getCompiledClass(String className);
  +        public void addCompiledClass(String className, 
  +                                     Source source,
  +                                     byte[] contents);
  +    }
  +
       protected Class findClass(String className) 
           throws ClassNotFoundException {
           byte[] bytes = compile(className);
  @@ -92,9 +99,11 @@
       }
   
       public CompilingClassLoader(ClassLoader parent,
  -                                SourceResolver sourceResolver) {
  +                                SourceResolver sourceResolver,
  +                                ClassRepository classRepository) {
           super(parent);
           this.sourceResolver = sourceResolver;
  +        this.classRepository = classRepository;
           this.compiler = new JavaCompilerImpl();
           this.sourcePath.add("");
       }
  @@ -136,6 +145,14 @@
           }
       }
   
  +    public void setClassRepository(ClassRepository rep) {
  +        classRepository = rep;
  +    }
  +
  +    public ClassRepository getClassRepository() {
  +        return classRepository;
  +    }
  +
       public void setSourcePath(String[] path) {
           synchronized (sourcePath) {
               sourcePath.clear();
  @@ -146,7 +163,6 @@
           }
       }
   
  -    
       private Source getSource(String className) {
           synchronized (sourcePath) {
               Iterator iter = sourcePath.iterator();
  @@ -228,7 +244,7 @@
               getClassReader(final String className) 
                throws IOException {
                final byte[] bytes = 
  -                 (byte[])output.get(className);
  +                 (byte[])classRepository.getCompiledClass(className);
                if (bytes != null) {
                    return new JavaClassReader() {
                            public String getClassName() {
  @@ -281,9 +297,10 @@
                            }
                            s.flush();
                            System.out.println("Compiled: " + className);
  -                         output.put(className,
  -                                 s.toByteArray());
                               Source src = getSource(className);
  +                         classRepository.addCompiledClass(className,
  +                                                             src,
  +                                                             
s.toByteArray());
                               notifyListeners(src, null);
                               releaseSource(src);
                        }
  @@ -326,7 +343,8 @@
   
        private byte[] compile(String className) 
               throws ClassNotFoundException {
  -         byte[] result = (byte[])output.get(className);
  +         byte[] result = (byte[])
  +                classRepository.getCompiledClass(className);
            if (result != null) {
                return result;
            }
  @@ -354,7 +372,7 @@
                       throw new ClassCompilationException(msg);
                       
                   }
  -                return (byte[])output.get(className);
  +                return (byte[])classRepository.getCompiledClass(className);
               } finally {
                   releaseSource(src);
               }
  
  
  
  1.15      +79 -31    
cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java
  
  Index: FOM_JavaScriptInterpreter.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- FOM_JavaScriptInterpreter.java    27 Dec 2003 07:30:42 -0000      1.14
  +++ FOM_JavaScriptInterpreter.java    28 Dec 2003 07:19:23 -0000      1.15
  @@ -56,6 +56,7 @@
   import java.io.OutputStream;
   import java.io.Reader;
   import java.util.ArrayList;
  +import java.util.LinkedList;
   import java.util.Iterator;
   import java.util.StringTokenizer;
   import java.util.HashMap;
  @@ -134,7 +135,7 @@
       Global scope;
   
       CompilingClassLoader classLoader;
  -    Map javaSource = new HashMap();
  +    MyClassRepository javaClassRepository = new MyClassRepository();
       String[] javaSourcePath;
   
       /**
  @@ -146,6 +147,66 @@
       JSErrorReporter errorReporter;
       boolean enableDebugger = false;
   
  +    class MyClassRepository implements CompilingClassLoader.ClassRepository {
  +        
  +        Map javaSource = new HashMap();
  +        Map javaClass = new HashMap();
  +        Map sourceToClass = new HashMap();
  +        Map classToSource = new HashMap();
  +
  +        public synchronized void addCompiledClass(String className,
  +                                                  Source src,
  +                                                  byte[] contents) {
  +            javaSource.put(src.getURI(), src.getValidity());
  +            javaClass.put(className, contents);
  +            sourceToClass.put(src.getURI(), className);
  +            classToSource.put(className, src.getURI());
  +        }
  +
  +        public synchronized byte[] getCompiledClass(String className) {
  +            return (byte[])javaClass.get(className);
  +        }
  +
  +        public synchronized boolean upToDateCheck() throws Exception {
  +            SourceResolver sourceResolver = (SourceResolver)
  +                manager.lookup(SourceResolver.ROLE);
  +            Iterator iter = javaSource.entrySet().iterator();
  +            List invalid = new LinkedList();
  +            while (iter.hasNext()) {
  +                Map.Entry e = (Map.Entry)iter.next();
  +                String uri = (String)e.getKey();
  +                SourceValidity validity = 
  +                    (SourceValidity)e.getValue();
  +                int valid = validity.isValid();
  +                if (valid == SourceValidity.UNKNOWN) {
  +                    Source newSrc = null;
  +                    try {
  +                        newSrc = sourceResolver.resolveURI(uri);
  +                        valid = newSrc.getValidity().isValid(validity);
  +                    } catch (Exception ignored) {
  +                    } finally {
  +                        if (newSrc != null) {
  +                            sourceResolver.release(newSrc);
  +                        }
  +                    }
  +                }
  +                if (valid != SourceValidity.VALID) {
  +                    invalid.add(uri);
  +                }
  +            }
  +            iter = invalid.iterator();
  +            while (iter.hasNext()) {
  +                String uri = (String)iter.next();
  +                String className = (String)sourceToClass.get(uri);
  +                sourceToClass.remove(className);
  +                javaClass.remove(className);
  +                javaSource.remove(uri);
  +                classToSource.remove(className);
  +            }
  +            return invalid.size() == 0;
  +        }
  +    }
  +
       /**
        * JavaScript debugger: there's only one of these: it can debug multiple
        * threads executing JS code.
  @@ -195,7 +256,7 @@
           if (reloadScripts) {
               String classPath 
                   = config.getChild("classpath").getValue(null);
  -            synchronized (javaSource) {
  +            synchronized (javaClassRepository) {
                   if (classPath == null) {
                       javaSourcePath = new String[]{""};
                   } else {
  @@ -248,42 +309,19 @@
           if (!reloadScripts) {
               return Thread.currentThread().getContextClassLoader();
           }
  -        synchronized (javaSource) {
  -            SourceResolver sourceResolver = (SourceResolver)
  -                manager.lookup(SourceResolver.ROLE);
  +        synchronized (javaClassRepository) {
               boolean reload = needsRefresh || classLoader == null;
               if (needsRefresh && classLoader != null) {
  -                reload = false;
  -                Iterator iter = javaSource.entrySet().iterator();
  -                while (iter.hasNext()) {
  -                    Map.Entry e = (Map.Entry)iter.next();
  -                    String uri = (String)e.getKey();
  -                    SourceValidity validity = 
  -                        (SourceValidity)e.getValue();
  -                    int valid = validity.isValid();
  -                    if (valid == SourceValidity.UNKNOWN) {
  -                        Source newSrc = 
  -                            sourceResolver.resolveURI(uri);
  -                        valid = newSrc.getValidity().isValid(validity);
  -                        sourceResolver.release(newSrc);
  -                    }
  -                    if (valid != SourceValidity.VALID) {
  -                        reload = true;
  -                        javaSource.clear();
  -                        break;
  -                    }
  -                }
  +                reload = !javaClassRepository.upToDateCheck();
               }
               if (reload) {
                   classLoader = 
                       new 
CompilingClassLoader(Thread.currentThread().getContextClassLoader(), 
  -                                             sourceResolver);
  +                                             
(SourceResolver)manager.lookup(SourceResolver.ROLE),
  +                                             javaClassRepository);
                   classLoader.addSourceListener(new 
CompilingClassLoader.SourceListener() {
                           public void sourceCompiled(Source src) {
  -                            synchronized (javaSource) {
  -                                javaSource.put(src.getURI(), 
  -                                               src.getValidity());
  -                            }
  +                            // no action
                           }
   
                           public void sourceCompilationError(Source src,
  @@ -366,7 +404,10 @@
   
       public static class ThreadScope extends ScriptableObject {
   
  +        static final String[] builtinPackages = {"javax", "org", "com"};
  +
           ClassLoader classLoader;
  +
           /* true if this scope has assigned any global vars */
           boolean useSession = false;
   
  @@ -423,6 +464,13 @@
                   newPackages.setParentScope(this);
                   
newPackages.setPrototype(ScriptableObject.getClassPrototype(this, 
"JavaPackage"));
                   super.put("Packages", this, newPackages);
  +                for (int i = 0; i < builtinPackages.length; i++) {
  +                    String pkgName = builtinPackages[i];
  +                    Scriptable pkg = new NativeJavaPackage(pkgName, cl);
  +                    pkg.setParentScope(this);
  +                    
pkg.setPrototype(ScriptableObject.getClassPrototype(this, "JavaPackage"));
  +                    super.put(pkgName, this, pkg);
  +                }
               }
           }
           
  
  
  

Reply via email to