Author: vborja
Date: Mon Sep  1 11:09:35 2008
New Revision: 691025

URL: http://svn.apache.org/viewvc?rev=691025&view=rev
Log:
Javadoc tools seems to require tools.jar in system classLoader

Added Java.java_home
When running on jruby tries to guess the current java_home from
system properties.

Added Java.tools_jar

Modified:
    incubator/buildr/trunk/lib/buildr/java.rb
    incubator/buildr/trunk/lib/buildr/java/compilers.rb
    incubator/buildr/trunk/lib/buildr/java/jruby.rb
    incubator/buildr/trunk/lib/buildr/java/rjb.rb

Modified: incubator/buildr/trunk/lib/buildr/java.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java.rb?rev=691025&r1=691024&r2=691025&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java.rb (original)
+++ incubator/buildr/trunk/lib/buildr/java.rb Mon Sep  1 11:09:35 2008
@@ -17,6 +17,19 @@
 ENV['JAVA_HOME'] ||= '/System/Library/Frameworks/JavaVM.framework/Home' if 
Config::CONFIG['host_os'] =~ /darwin/i
 require RUBY_PLATFORM == 'java' ? 'buildr/java/jruby' : 'buildr/java/rjb'
 
+module Java
+  class << self
+    # Returns the path to the tools.jar in JAVA_HOME
+    def tools_jar
+      home = java_home
+      tools_jar = File.expand_path('lib/tools.jar', home)
+      # if java_home is a jre inside jdk...
+      tools_jar = File.expand_path('../lib/tools.jar', home) unless 
File.exist?(tools_jar)
+      raise "I need tools.jar, can't find it in #{home}/lib" unless 
File.exist?(tools_jar)
+      tools_jar
+    end
+  end
+end
 
 require 'buildr/java/compilers'
 require 'buildr/java/test_frameworks'

Modified: incubator/buildr/trunk/lib/buildr/java/compilers.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java/compilers.rb?rev=691025&r1=691024&r2=691025&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java/compilers.rb (original)
+++ incubator/buildr/trunk/lib/buildr/java/compilers.rb Mon Sep  1 11:09:35 2008
@@ -54,8 +54,7 @@
       def compile(sources, target, dependencies) #:nodoc:
         check_options options, OPTIONS
         cmd_args = []
-        tools = File.expand_path('lib/tools.jar', ENV['JAVA_HOME']) if 
ENV['JAVA_HOME']
-        dependencies << tools if tools && File.exist?(tools)
+        dependencies << Java.tools_jar
         cmd_args << '-classpath' << dependencies.join(File::PATH_SEPARATOR) 
unless dependencies.empty?
         source_paths = sources.select { |source| File.directory?(source) }
         cmd_args << '-sourcepath' << source_paths.join(File::PATH_SEPARATOR) 
unless source_paths.empty?

Modified: incubator/buildr/trunk/lib/buildr/java/jruby.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java/jruby.rb?rev=691025&r1=691024&r2=691025&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java/jruby.rb (original)
+++ incubator/buildr/trunk/lib/buildr/java/jruby.rb Mon Sep  1 11:09:35 2008
@@ -58,6 +58,11 @@
 
   class << self
 
+    # Returns the path to JAVA_HOME
+    def java_home
+      ENV['JAVA_HOME'] || java.lang.System.getProperty("java.home")
+    end
+
     # Returns the classpath, an array listing directories, JAR files and
     # artifacts.  Use when loading the extension to add any additional
     # libraries used by that extension.
@@ -75,20 +80,25 @@
     def load
       return self if @loaded
       cp = Buildr.artifacts(classpath).map(&:to_s).each { |path| 
file(path).invoke }
-      cp.each { |j| $CLASSPATH << j }
-      java_home = ENV['JAVA_HOME'] || java.lang.System.getProperty("java.home")
-      tools_jar = File.expand_path('lib/tools.jar', java_home)
-      tools_jar = File.expand_path('../lib/tools.jar', java_home) unless 
File.file?(tools_jar)
-      unless File.file?(tools_jar)
-        home = ENV['JAVA_HOME'] or fail 'Are we forgetting something? 
JAVA_HOME not set.'
-        tools = File.expand_path('lib/tools.jar', home)
-        raise "Missing #{tools}, perhaps your JAVA_HOME is not correclty set" 
unless File.file?(tools)
-      end
-      $CLASSPATH << tools_jar
+
+      # Adding jars to the jruby's $CLASSPATH should be the correct thing, 
however it
+      # seems like some tools require their jars on system class loader 
(javadoc, junit, etc)
+      # cp.each { |path| $CLASSPATH << path }
+
+      # Use system ClassLoader to add classpath
+      sysloader = java.lang.ClassLoader.getSystemClassLoader
+      add_url_method = java.lang.Class.forName('java.net.URLClassLoader').
+        getDeclaredMethod('addURL', 
[java.net.URL.java_class].to_java(java.lang.Class))
+      add_url_method.setAccessible(true)
+      add_path = lambda { |path| add_url_method.invoke(sysloader, 
[java.io.File.new(path).toURI.toURL].to_java(java.net.URL)) }
+
+      add_path[tools_jar]
+      cp.each(&add_path)
+      
       @loaded = true
       self
     end
-    
+
   end
 
 end

Modified: incubator/buildr/trunk/lib/buildr/java/rjb.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java/rjb.rb?rev=691025&r1=691024&r2=691025&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java/rjb.rb (original)
+++ incubator/buildr/trunk/lib/buildr/java/rjb.rb Mon Sep  1 11:09:35 2008
@@ -73,6 +73,12 @@
   end
 
   class << self
+    
+    # Returns the path to JAVA_HOME on environment. 
+    # Raises an exception if no JAVA_HOME is set.
+    def java_home
+      ENV['JAVA_HOME'] or fail 'Are we forgetting something? JAVA_HOME not 
set.'
+    end
 
     # Returns the classpath, an array listing directories, JAR files and
     # artifacts.  Use when loading the extension to add any additional
@@ -90,13 +96,8 @@
     # that append to the classpath and specify which remote repositories to 
use.
     def load
       return self if @loaded
-      unless RUBY_PLATFORM =~ /darwin/i
-        home = ENV['JAVA_HOME'] or fail 'Are we forgetting something? 
JAVA_HOME not set.'
-        tools = File.expand_path('lib/tools.jar', home)
-        raise "I need tools.jar to compile, can't find it in #{home}/lib" 
unless File.exist?(tools)
-        classpath << tools
-      end
       cp = Buildr.artifacts(classpath).map(&:to_s).each { |path| 
file(path).invoke }
+      cp << tools_jar
       java_opts = (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split
       ::Rjb.load cp.join(File::PATH_SEPARATOR), java_opts
 


Reply via email to