Module: Jerbil::JavaHelper

Description

The JavaHelper module provides common helper functionality needed across different classes.

Public Class methods


load_jvm (classpath, build_dir = nil, options = {} )

Loads the java virtual machine. This method should only be invoked once, typically before task definitions in a Rakefile. If the environment variable JAVA_OPTS is set, it will be treated as extra parameter for the initial VM load.

classpath:a Rake::FileList containing the initial classpath.
build_dir:an optional directory (or list of directories) which will be used to resolve classes at runtime.

Available options:

java_home:JDK path (defaults to ENV[‘JAVA_HOME’])
java_opts:additional JVM arguments (defaults to ENV[‘JAVA_OPTS’])
loggingprops:the location of a java.util.logging configuration file.
enableassert:wheter to enable assertions (default: enabled)

Example

            load_jvm(FileList.new("lib/*.jar"), "build", :loggingprops => "logging.properties")
     # File lib/jerbil/java_helper.rb, line 85
 85:     def JavaHelper.load_jvm(classpath, build_dir = nil, options = {} )
 86:                         
 87:             defaultopts = { :enableassert => true }
 88:       options = defaultopts.merge(options.dup)
 89:           
 90:       #need verbose java exceptions
 91:       $VERBOSE = true
 92:     
 93:       guess_java_home  
 94:       java_home = options[:java_home] || ENV['JAVA_HOME']
 95:       
 96:       puts "using JDK in #{java_home}" if Rake.application.options.trace
 97:       
 98:       #include tools.jar from JDK (needed for javac etc.)
 99:       classpath.include(File.join(java_home, "lib", "tools.jar")) if java_home    
100:       #include custom classloader
101:       classpath.include(File.join(File.dirname(__FILE__), "../../classloader")) if build_dir
102:       
103:       jvmargs = []    
104:       jvmargs << "-ea" if options[:enableassert]
105:       jvmargs << "-Djava.util.logging.config.file=#{options[:loggingprops].to_s}" if options[:loggingprops] 
106:        
107:       if ENV['JAVA_DEBUG']
108:         suspend = ENV['JAVA_DEBUG'].to_s.index('suspend') ? 'y' : 'n'
109:         port = 8000
110:         if ENV['JAVA_DEBUG'] =~ /port=([0-9]+)/
111:           port = $1
112:         end
113:        
114:         jvmargs += [
115:         "-Xdebug",
116:         "-Xnoagent",
117:         "-Xrunjdwp:transport=dt_socket,address=#{port},server=y,suspend=#{suspend}" ]
118:       end
119:          
120:       if build_dir
121:         jerbil_debug = ENV['JERBIL_DEBUG'] ? 'true' : 'false'
122:         
123:         jvmargs += [ "-Djava.system.class.loader=JerbilClassLoader", 
124:           "-Djerbil.build.root=#{build_dir.to_a.join(':')}", "-Djerbil.debug=#{jerbil_debug}" ] 
125:       else      
126:         $stderr << "jerbil: build_dir not set: dynamic classloading is disabled\n" if Rake.application.options.trace
127:       end
128:            
129:       java_opts = ENV['JAVA_OPTS'] || options[:java_opts]
130:       jvmargs.unshift(java_opts) if java_opts
131:       
132:       puts jvmargs if ENV['JERBIL_DEBUG']
133:            
134:       begin
135:         Rjb::load(classpath.to_cp, jvmargs.flatten)
136:       rescue  
137:         $stderr << "could not load java vm: make sure JAVA_HOME is set correctly!\n"
138:         raise
139:       end
140:     end

Public Instance methods


empty_list ()

Returns an empty instance of java.util.List.

    # File lib/jerbil/java_helper.rb, line 43
43:     def empty_list
44:       Rjb::import('java.util.ArrayList').new
45:     end

printStream_to_s () {|printstream| ...}

Provides the block with an instance of java.io.PrintStream and returns all text printed to it as ruby-typed string.

    # File lib/jerbil/java_helper.rb, line 22
22:     def printStream_to_s(&block) # :yields: printstream
23:       yieldIO('java.io.PrintStream', block)
24:     end

printWriter_to_s () {|printwriter| ...}

Provides the block with an instance of java.io.PrintWriter and returns all text printed to it as ruby-typed string.

    # File lib/jerbil/java_helper.rb, line 29
29:     def printWriter_to_s(&block) # :yields: printwriter
30:       yieldIO('java.io.PrintWriter', block)
31:     end

serialize (obj)

Serializes obj using standard Java serialization. The result is returned as bytearray.

    # File lib/jerbil/java_helper.rb, line 57
57:     def serialize(obj)
58:       byteoos = Rjb::import('java.io.ByteArrayOutputStream').new
59:       oosKlass = Rjb::import('java.io.ObjectOutputStream')
60:       oos = oosKlass.new_with_sig 'Ljava.io.OutputStream;', byteoos
61:       begin
62:         oos.writeObject(obj)
63:       ensure
64:         oos.close
65:       end 
66:       byteoos.toByteArray
67:     end

str_list (strings)

Returns an instance of java.util.List, populated with all string found in strings.

    # File lib/jerbil/java_helper.rb, line 49
49:     def str_list(strings)
50:       l = empty_list
51:       strings.each { |s| l.add s.to_s }
52:       l
53:     end