Class: Naether::Java::Ruby

Inherits:
Object
  • Object
show all
Defined in:
lib/naether/java/ruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuby

Creates new instance by loading the Naether jar to the parent ClassLoader and creating the internal Naether ClassLoader



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/naether/java/ruby.rb', line 19

def initialize
  require 'rjb' 
  
  naether_jar = Naether::Configuration.naether_jar
        
  # Call Rjb::load with an empty classpath, incase Rjb::load has already been called
  java_opts = (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split

  begin
    Rjb::load("", java_opts)
  rescue StandardError => e
    if e.message == "can't create Java VM"
      puts "RJB was unable to find JVM, try setting JAVA_HOME env"
    end
    raise e
  end
  
  @loaded_paths = []
  load_paths( naether_jar )
  
  class_loader_class = Rjb::import( "com.tobedevoured.naether.PathClassLoader" )
  @class_loader = class_loader_class.new()
    
  internal_load_paths( naether_jar )
  
end

Instance Attribute Details

#class_loaderObject (readonly)

Returns the value of attribute class_loader.



13
14
15
# File 'lib/naether/java/ruby.rb', line 13

def class_loader
  @class_loader
end

#loaded_pathsObject (readonly)

Returns the value of attribute loaded_paths.



13
14
15
# File 'lib/naether/java/ruby.rb', line 13

def loaded_paths
  @loaded_paths
end

Instance Method Details

#convert_to_java_list(ruby_array) ⇒ java.util.ArrayList

Convert a Ruby Array to a java.util.ArrayList

Parameters:

  • ruby_array (Array)

    Array to convert to Java.util.ArrayList

Returns:

  • (java.util.ArrayList)


148
149
150
151
152
153
154
155
# File 'lib/naether/java/ruby.rb', line 148

def convert_to_java_list( ruby_array ) 
  list = Rjb::import("java.util.ArrayList").new
  ruby_array.each do |item|
    list.add( item )
  end
  
  list
end

#convert_to_ruby_array(java_array, to_string = false) ⇒ Array

Convert a java,util.List to a Ruby Array

Parameters:

  • java_array (java.util.ArrayList)
  • to_string (Boolean) (defaults to: false)

    converts each element using toString

Returns:

  • (Array)


164
165
166
167
168
169
170
171
172
# File 'lib/naether/java/ruby.rb', line 164

def convert_to_ruby_array( java_array, to_string = false )
  ruby_array = java_array.toArray()
  
  if to_string
    ruby_array = ruby_array.map { |x| x.toString()}
  end
  
  ruby_array
end

#convert_to_ruby_hash(java_hash, to_string = false) ⇒ Hash

Convert a java.util.Map to a Ruby Hash

Parameters:

  • java_hash (java.util.Map)
  • to_string (Boolean) (defaults to: false)

    converts each element using toString

Returns:

  • (Hash)


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/naether/java/ruby.rb', line 181

def convert_to_ruby_hash( java_hash, to_string = false )
  
  hash = {}
  unless java_hash.is_a? Hash
    keys = java_hash.keySet()
    iterator = keys.iterator()
    if to_string
      while iterator.hasNext()
        key = iterator.next().toString()
        hash[key] = java_hash.get( key ).toString()
      end
    else
      while iterator.hasNext()
        key = iterator.next()
        hash[key] = java_hash.get( key )              
      end
    end
  else
    if to_string
      java_hash.each do |k,v|
        hash[k.toString()] = v.toString()
      end
    else
      hash = java_hash
    end
  end
  
  hash
  
end

#create(target_class, *args) ⇒ Object

Create a Java Object from the Naether Class Loader

Parameters:

  • target_class (String)

    to create

  • args (Array)

    Array of constructor arguments



51
52
53
54
55
56
57
58
# File 'lib/naether/java/ruby.rb', line 51

def create( target_class, *args )
  #@class_loader.newInstance(target_class, *args )
  if args.size > 0
    @class_loader._invoke('newInstance', 'Ljava.lang.String;[Ljava.lang.Object;', target_class, args )
  else
    @class_loader._invoke('newInstance', 'Ljava.lang.String;', target_class )
  end
end

#exec_static_method(target_class, target_method, params, types = nil) ⇒ Object

Execute a Staic method on a Java class from the Naether Class Loader

Parameters:

  • target_class (String)
  • target_method (String)
  • params (Array)

    Array of method parameters

  • types (Array) (defaults to: nil)

    if defined, a Array of String classes of params type that lines up with params one to one.

Returns:

  • (Object)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/naether/java/ruby.rb', line 68

def exec_static_method( target_class, target_method, params, types = nil ) 
  unless params.is_a? Array
    params = [params]
  end
  
  if types
    unless types.is_a? Array
       types = [types]
    end
  end
  result = nil
  if params.nil?
    result = @class_loader._invoke('execStaticMethod','Ljava.lang.String;Ljava.lang.String;', target_class, target_method )
  elsif types.nil?
    result = @class_loader._invoke('execStaticMethod','Ljava.lang.String;Ljava.lang.String;Ljava.util.List;', target_class, target_method, convert_to_java_list(params) )
  else
    result = @class_loader._invoke('execStaticMethod','Ljava.lang.String;Ljava.lang.String;Ljava.util.List;Ljava.util.List;', target_class, target_method, convert_to_java_list(params), convert_to_java_list(types) )
  end
  
  unless result.nil?
    # Force toString on java.lang.String otherwise the result will be a Rjb::Proxy
    if result.getClass().getName() == 'java.lang.String'
      result.toString()
    else
      result
    end
  end
end

#internal_load_paths(paths) ⇒ Object

Load a path into the internal Naether ClassLoader

Parameters:

  • paths (Array)

    as an Array of String paths or a String path



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/naether/java/ruby.rb', line 102

def internal_load_paths(paths)
  loadable_paths = []
  unless paths.is_a? Array
    paths = [paths]
  end
  
  paths.each do |path|
    expanded_path = File.expand_path(path)
    if File.exists?( expanded_path )
      @class_loader.addPath( path )
      loadable_paths << expanded_path
    end
  end
  
  loadable_paths
end

#load_paths(paths) ⇒ Object

Load a path onto the parent ClassLoader

Parameters:

  • paths (Array)

    as an Array of String paths or a String path



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/naether/java/ruby.rb', line 124

def load_paths(paths)
  loadable_paths = []
  unless paths.is_a? Array
    paths = [paths]
  end
  
  paths.each do |path|
    expanded_path = File.expand_path(path)
    if !@loaded_paths.include?(expanded_path) && File.exists?( expanded_path )
      @loaded_paths << expanded_path
      Rjb::add_jar( expanded_path )
      loadable_paths << expanded_path
    end
  end
  
  loadable_paths
end