Changeset 6019
- Timestamp:
- 11/08/06 23:52:12
- Files:
-
- trunk/Rakefile (modified) (3 diffs)
- trunk/Rakefile.jerbil (deleted)
- trunk/buildsupport/tools.jar (deleted)
- trunk/classloader/JerbilClassLoader.class (modified) (previous)
- trunk/classloader/JerbilClassLoader.java (modified) (4 diffs)
- trunk/lib/jerbil/java_helper.rb (modified) (3 diffs)
- trunk/sample/Rakefile (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Rakefile
r6010 r6019 4 4 require 'rake/rdoctask' 5 5 6 7 6 task :default => :test 8 9 Rake::TestTask.new do |t|10 t.libs << "test"11 t.test_files = FileList['test/test*.rb']12 t.verbose = false13 end14 15 16 desc "install sample Rakefile. Specify your SRC=x and TESTSRC=y for your source file locations."17 task :install do |t|18 19 dstfile = File.join(Rake.original_dir, "../Rakefile")20 21 raise "\n#{dstfile} already exists, delete it first to proceed\n" if File.exists?(dstfile)22 23 src = ENV['SRC'] || "src"24 testsrc = ENV['TESTSRC'] || "testsrc"25 26 rfile = File.read('Rakefile.jerbil')27 28 rfile.gsub!(/##SRC##/, src)29 rfile.gsub!(/##TESTSRC##/, testsrc)30 31 File.open( dstfile, 'w' ) do |f|32 f << rfile33 end34 35 puts "created sample Rakefile in #{dstfile}"36 end37 7 38 8 def read_version … … 53 23 s.requirements << 'rjb' 54 24 s.requirements << 'JDK 5.0' 55 files = FileList['lib/*.rb', 'lib/jerbil/*.rb', 'samples/**/*', 56 'test/*.rb', 'classloader/*' 'COPYING', 'ChangeLog', 'README'] 57 25 files = FileList['lib/**/*', 'test/*.rb', 'buildsupport/**/*', 'classloader/*', 'COPYING', 'ChangeLog', 'README'] 26 s.has_rdoc = false 58 27 s.files = files 59 28 s.test_files = FileList['test/*.rb'] … … 74 43 end 75 44 45 Rake::TestTask.new do |t| 46 t.libs << "test" 47 t.test_files = FileList['test/test*.rb'] 48 t.verbose = false 49 end 50 51 desc "compile the classloader" 76 52 task :compile_classloader do |t| 77 53 javac = "javac" trunk/classloader/JerbilClassLoader.java
r5865 r6019 3 3 import java.io.IOException; 4 4 import java.io.ByteArrayOutputStream; 5 import java.io.ByteArrayInputStream; 5 6 import java.io.InputStream; 7 import java.util.Arrays; 6 8 7 9 /** 8 * TODO: comment 10 * <p>A classloader which is used by the jerbil build system to dynamically 11 * add classes to the running VM, after they've been built.</p> 12 * 13 * <p>The initial build paths must be set using the system property 14 * <code>jerbil.build.root</code> (classpath-like).</p> 9 15 */ 10 16 public class JerbilClassLoader extends ClassLoader { 11 17 12 private static final boolean debug = Boolean.getBoolean( "jerbil.debug" ); 13 18 private final boolean debug = Boolean.getBoolean( "jerbil.debug" ); 19 private final String[] roots; 20 14 21 public JerbilClassLoader(ClassLoader parent) { 15 22 super( parent ); 23 String root = System.getProperty( "jerbil.build.root" ); 24 if ( root == null ) { 25 throw new IllegalArgumentException("Need to set system property build.root!"); 26 } 27 roots = root.split(":"); 28 debug("initialised with root paths " + Arrays.asList(roots)); 16 29 } 17 30 18 31 public Class<?> findClass(String name) throws 19 32 ClassNotFoundException { 20 21 if (debug) { 22 System.err.println( "findClass(" + name + ")" ); 23 } 24 33 debug( "findClass(" + name + ")" ); 25 34 byte[] classBytes = findClassBytes( name ); 26 35 return defineClass( name, classBytes, 27 36 0, classBytes.length ); 37 } 38 39 public InputStream getResourceAsStream(String name) { 40 debug("getResourceAsStream(" + name + ")"); 41 42 File f = null; 43 for (String root : roots) { 44 File aFile = new File(root, name); 45 if ( aFile.exists()) { 46 f = aFile; 47 debug("resolved to " + aFile.toString()); 48 break; 49 } 50 } 51 52 if (f != null) { 53 try { 54 return new ByteArrayInputStream(readBytes(new FileInputStream(f))); 55 } catch (Exception e) {/*falltrough*/} 56 } 57 58 return super.getResourceAsStream(name); 28 59 } 29 60 … … 32 63 try 33 64 { 34 String root = System.getProperty( "build.root" ); 35 if ( root == null ) 36 { 37 throw new ClassNotFoundException( "Class " + className + " not found: set build.root" ); 65 File f = null; 66 for (String root : roots) { 67 String pathName = root + 68 File.separatorChar + className. 69 replace( '.', File.separatorChar ) 70 + ".class"; 71 72 File aFile = new File(pathName); 73 if (aFile.exists()) { 74 f = aFile; 75 debug("resolved to " + aFile.toString()); 76 break; 77 } 78 } 79 80 if (f == null) { 81 throw new ClassNotFoundException("Class " + className + " not found"); 38 82 } 39 String pathName = root + 40 File.separatorChar + className. 41 replace( '.', File.separatorChar ) 42 + ".class"; 83 return readBytes(new FileInputStream(f)); 84 } 85 catch ( IOException e ) 86 { 87 throw new ClassNotFoundException( "Class " + className + " not found: I/O", e ); 88 } 89 } 43 90 44 in = new FileInputStream( pathName ); 45 46 ByteArrayOutputStream out = new ByteArrayOutputStream(); 47 byte[] buffer = new byte[16384]; 48 49 int c; 91 private byte[] readBytes(InputStream in) throws IOException { 92 ByteArrayOutputStream out = new ByteArrayOutputStream(); 93 byte[] buffer = new byte[16384]; 94 int c; 95 try { 50 96 while ((c = in.read( buffer )) >= 0) 51 97 { … … 53 99 } 54 100 return out.toByteArray(); 55 } 56 catch ( IOException e ) 57 { 58 throw new ClassNotFoundException( "Class " + className + " not found: I/O", e ); 59 } 60 finally 61 { 62 if ( in != null ) 101 } finally { 102 if ( in != null ) 63 103 { 64 104 try … … 71 111 } 72 112 } 113 114 private void debug(String s) { 115 if (debug) { 116 System.err.println(getClass().getSimpleName() + ": " + s); 117 } 118 } 73 119 } trunk/lib/jerbil/java_helper.rb
r6010 r6019 63 63 end 64 64 65 def load_vm(classpath, loggingprops = nil, build_dir= nil )65 def load_vm(classpath, build_dir = nil, loggingprops = nil ) 66 66 #need verbose java exceptions 67 67 $VERBOSE = true 68 69 #include tools.jar from JDK (needed for javac etc.) 70 java_home = ENV['JAVA_HOME'] 71 classpath.include(File.join(java_home, "lib", "tools.jar")) if java_home 68 72 #include build jars and custom classloader 69 70 java_home = ENV['JAVA_HOME']71 classpath.include(File.join(java_home, "lib", "tools.jar")) if java_home72 73 73 classpath.include(File.join(File.dirname(__FILE__), "../../buildsupport/*.jar")) 74 74 classpath.include(File.join(File.dirname(__FILE__), "../../classloader")) unless build_dir.nil? … … 91 91 92 92 if build_dir 93 jvmargs += [ "-Djava.system.class.loader=JerbilClassLoader", 94 "-Dbuild.root=#{build_dir}", "-Djerbil.debug=false" ] 95 end 96 93 jvmargs += [ "-Djava.system.class.loader=JerbilClassLoader", 94 "-Djerbil.build.root=#{build_dir.to_a.join(':')}", "-Djerbil.debug=false" ] 95 else 96 $stderr << "jerbil: build_dir not set: dynamic classloading is disabled\n" if Rake.application.options.trace 97 end 98 97 99 begin 98 100 Rjb::load(classpath.to_cp, jvmargs) … … 101 103 raise 102 104 end 103 105 104 106 #TODO: test javac main and raise if not found 105 107 end trunk/sample/Rakefile
r5865 r6019 1 $:.unshift File.join( File.dirname(__FILE__), "..", "lib" ) 2 1 3 require 'rake' 2 Dir[File.join(Rake.original_dir, "..", "lib", "*.rb")].each {|f| require f} 4 require 'jerbil' 3 5 require 'buildconfig' 4 6
