commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Art Gramlich <...@gramlich-net.com>
Subject [jci] Waiting for classes to be compiled before continuing
Date Mon, 21 Feb 2005 04:23:28 GMT
I found that on a slower system that the compilation sometimes
takes some time and the classloader does not have the classes
available until it is done.  This happens quite a bit in the simple
tests I was using.  I did a patch to wait for the first compilation in
the constructor.  I'm not completely happy with this change, but it
does make my tests run.

Art Gramlich



Index: CompilingClassLoader.java
===================================================================
--- src/java/org/apache/commons/jci/CompilingClassLoader.java	(revision 
154619)
+++ src/java/org/apache/commons/jci/CompilingClassLoader.java	(working 
copy)
@@ -49,6 +49,114 @@
      private final ResourceReader reader;
      private final JavaCompiler compiler;
      private final AlterationMonitor fam;
+
+    private class MonitorAlterationListener implements 
AlterationListener {
+
+        private Collection created = new ArrayList();
+        private Collection changed = new ArrayList();
+        private Collection deleted = new ArrayList();
+        private boolean hasCompletedFirstRun = false;
+
+        public void onStart() {
+            created.clear();
+            changed.clear();
+            deleted.clear();
+            store.onStart();
+        }
+        public void onStop() {
+            /*
+            log.debug("resources " +
+                    created.size() + " created, " +
+                    changed.size() + " changed, " +
+                    deleted.size() + " deleted");
+                    */
+
+            boolean reload = false;
+
+            if (deleted.size() > 0) {
+                for (Iterator it = deleted.iterator(); it.hasNext();) {
+                    final File file = (File) it.next();
+                    store.remove(clazzName(fam.getRoot(), file));
+                }
+                reload = true;
+            }
+
+            final Collection compileables = new ArrayList();
+            compileables.addAll(created);
+            compileables.addAll(changed);
+
+            final String[] clazzes = new String[compileables.size()];
+
+            if (compileables.size() > 0) {
+
+                int i = 0;
+                for (Iterator it = compileables.iterator(); 
it.hasNext();) {
+                    final File file = (File) it.next();
+                    clazzes[i] = clazzName(fam.getRoot(),file);
+                    //log.debug(clazzes[i]);
+                    i++;
+                }
+
+                final ConsoleCompilationProblemHandler problemHandler 
= new ConsoleCompilationProblemHandler();
+
+                compiler.compile(
+                        clazzes,
+                        reader,
+                        store,
+                        problemHandler
+                        );
+
+
+                log.debug(
+                        problemHandler.getErrorCount() + " errors, " +
+                        problemHandler.getWarningCount() + " warnings"
+                        );
+
+                if (problemHandler.getErrorCount() > 0) {
+                    for (int j = 0; j < clazzes.length; j++) {
+                        store.remove(clazzes[j]);
+                    }
+                }
+
+                reload = true;
+
+            }
+
+            store.onStop();
+
+            //log.debug(store);
+
+            if (reload) {
+                reload();
+            }
+            if (!hasCompletedFirstRun) {
+            		hasCompletedFirstRun = true;
+            }
+        }
+
+        public void onCreateFile( final File file ) {
+            if (file.getName().endsWith(".java")) {
+                created.add(file);
+            }
+        }
+        public void onChangeFile( final File file ) {
+            if (file.getName().endsWith(".java")) {
+                changed.add(file);
+            }
+        }
+        public void onDeleteFile( final File file ) {
+            if (file.getName().endsWith(".java")) {
+                deleted.add(file);
+            }
+        }
+
+        public void onCreateDirectory( final File file ) {
+        }
+        public void onChangeDirectory( final File file ) {
+        }
+        public void onDeleteDirectory( final File file ) {
+        }    	
+    }

      public CompilingClassLoader(final ClassLoader pParent, final File 
pRepository) {
          this(pParent, pRepository,
@@ -69,112 +177,12 @@
          reader = new FileResourceReader(repository);
          store = pStore;
          compiler = new EclipseJavaCompiler();
+
+        MonitorAlterationListener listener = new 
MonitorAlterationListener();

          fam = new AlterationMonitor(repository);

-        fam.addListener(new AlterationListener() {
-
-            private Collection created = new ArrayList();
-            private Collection changed = new ArrayList();
-            private Collection deleted = new ArrayList();
-
-            public void onStart() {
-                created.clear();
-                changed.clear();
-                deleted.clear();
-                store.onStart();
-            }
-            public void onStop() {
-                /*
-                log.debug("resources " +
-                        created.size() + " created, " +
-                        changed.size() + " changed, " +
-                        deleted.size() + " deleted");
-                        */
-
-                boolean reload = false;
-
-                if (deleted.size() > 0) {
-                    for (Iterator it = deleted.iterator(); 
it.hasNext();) {
-                        final File file = (File) it.next();
-                        store.remove(clazzName(fam.getRoot(), file));
-                    }
-                    reload = true;
-                }
-
-                final Collection compileables = new ArrayList();
-                compileables.addAll(created);
-                compileables.addAll(changed);
-
-                final String[] clazzes = new 
String[compileables.size()];
-
-                if (compileables.size() > 0) {
-
-                    int i = 0;
-                    for (Iterator it = compileables.iterator(); 
it.hasNext();) {
-                        final File file = (File) it.next();
-                        clazzes[i] = clazzName(fam.getRoot(),file);
-                        //log.debug(clazzes[i]);
-                        i++;
-                    }
-
-                    final ConsoleCompilationProblemHandler 
problemHandler = new ConsoleCompilationProblemHandler();
-
-                    compiler.compile(
-                            clazzes,
-                            reader,
-                            store,
-                            problemHandler
-                            );
-
-
-                    log.debug(
-                            problemHandler.getErrorCount() + " errors, 
" +
-                            problemHandler.getWarningCount() + " 
warnings"
-                            );
-
-                    if (problemHandler.getErrorCount() > 0) {
-                        for (int j = 0; j < clazzes.length; j++) {
-                            store.remove(clazzes[j]);
-                        }
-                    }
-
-                    reload = true;
-
-                }
-
-                store.onStop();
-
-                //log.debug(store);
-
-                if (reload) {
-                    reload();
-                }
-            }
-
-            public void onCreateFile( final File file ) {
-                if (file.getName().endsWith(".java")) {
-                    created.add(file);
-                }
-            }
-            public void onChangeFile( final File file ) {
-                if (file.getName().endsWith(".java")) {
-                    changed.add(file);
-                }
-            }
-            public void onDeleteFile( final File file ) {
-                if (file.getName().endsWith(".java")) {
-                    deleted.add(file);
-                }
-            }
-
-            public void onCreateDirectory( final File file ) {
-            }
-            public void onChangeDirectory( final File file ) {
-            }
-            public void onDeleteDirectory( final File file ) {
-            }
-            });
+        fam.addListener(listener);

          delegate = new ResourceStoreClassLoader(parent, store);

@@ -179,7 +187,19 @@
          delegate = new ResourceStoreClassLoader(parent, store);

          Thread myThread = new Thread(fam);
+        myThread.setDaemon(true);
          myThread.start();
+
+        // Wait for the first run to complete so that
+        // classes are compiled.
+        while (!listener.hasCompletedFirstRun) {
+        		log.debug("Waiting for first compile to complete");
+        		try {
+                  Thread.sleep(1000);
+             } catch (InterruptedException e) {
+                  log.debug("Error while waiting for first compile to 
complete",e);
+             }
+        }
      }

      private void reload() {


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message