Class: Luruju::JudeApiLoader

Inherits:
Object
  • Object
show all
Includes:
MethodWrappable
Defined in:
lib/luruju/jude_api_loader.rb,
lib/luruju/jude_api_loader_in_cruby.rb,
lib/luruju/jude_api_loader_in_jruby.rb,
lib/luruju/jude_api_loader_for_cache.rb,
lib/luruju/jude_api_loader_for_locked_project.rb

Constant Summary collapse

@@jude_cache =
{}
@@tmpdir_no =
0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodWrappable

included

Constructor Details

#initializeJudeApiLoader

Returns a new instance of JudeApiLoader.



22
# File 'lib/luruju/jude_api_loader_in_jruby.rb', line 22

def initialize() @already_builded, @members = {}, [] end

Class Method Details

.access_jruby_using_command(jude_file, options) ⇒ Object



19
20
21
22
# File 'lib/luruju/jude_api_loader_in_cruby.rb', line 19

def self.access_jruby_using_command jude_file, options
  encoded_options = Base64.encode64(Marshal.dump(options)).gsub(/\n/,' ')
  %x{jruby #{luruju_bin 'access_jruby_with_command.rb'} #{jude_file} #{encoded_options}}
end

.access_jruby_using_drb(jude_file, options) ⇒ Object



15
16
17
# File 'lib/luruju/jude_api_loader_in_cruby.rb', line 15

def self.access_jruby_using_drb jude_file, options
  LurujuClient.new.marshal jude_file, options
end

.define_dummy_jude_class(const_name) ⇒ Object



31
32
33
34
# File 'lib/luruju/jude_api_loader_in_cruby.rb', line 31

def self.define_dummy_jude_class const_name      
  return if /Methods$/ =~ const_name
  JudeApi.module_eval "class #{const_name} < JudeBase ; end"
end

.load(jude_file, options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/luruju/jude_api_loader_in_cruby.rb', line 7

def self.load jude_file, options={}
  dto = unmarshal send("access_jruby_using_#{Luruju.config.access_jruby_with.to_s}", jude_file, options)
  raise dto.exception if dto.exception
  JudeMeta.instance= dto.meta
  dto.meta.define_all
  dto.model
end

.marshal(jude_file, options = {}) ⇒ Object



24
25
26
27
28
# File 'lib/luruju/jude_api_loader_in_jruby.rb', line 24

def self.marshal jude_file, options={}
  Base64.encode64(Marshal.dump(DtoBetweenCrubyAndJruby.new(self.load(jude_file, options))))
rescue
  return Base64.encode64(Marshal.dump(DtoBetweenCrubyAndJruby.new(nil, Luruju.wrap_native_exception($!))))
end

.unmarshal(marshaled) ⇒ Object



24
25
26
27
28
29
# File 'lib/luruju/jude_api_loader_in_cruby.rb', line 24

def self.unmarshal marshaled
  Marshal.load(Base64.decode64(marshaled))
rescue ArgumentError => e
  define_dummy_jude_class(e.message.split(' ').last.split('::').last)
  retry
end

.wrap_load_default_option(jude_file, options = {}) {|jude_file, default.merge(options)| ... } ⇒ Object

Yields:

  • (jude_file, default.merge(options))


7
8
9
10
# File 'lib/luruju/jude_api_loader_for_locked_project.rb', line 7

def self.wrap_load_default_option jude_file, options={}
  default = {:copy => :if_locked}
  yield jude_file, default.merge(options)
end

.wrap_load_for_cache(jude_file, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/luruju/jude_api_loader_for_cache.rb', line 7

def self.wrap_load_for_cache jude_file, options={}
  raise ProjectNotFoundException unless FileTest.file?(jude_file)
  return yield(jude_file, options) unless Luruju.config.use_jude_cache
  mtime = File.mtime(jude_file)
  if previous = @@jude_cache[jude_file]
	return previous if previous.mtime == mtime
  end
  loaded = yield jude_file, options
  loaded.mtime = mtime
  @@jude_cache[jude_file] = loaded
  loaded
end

.wrap_load_for_tmpdir(jude_file, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/luruju/jude_api_loader_for_locked_project.rb', line 12

def self.wrap_load_for_tmpdir jude_file, options={}
  do_jude_file = options[:copy] == :first ? copy_jude_file(jude_file, options) : jude_file
  yield do_jude_file, options
rescue ProjectLockedException
  raise $! if do_jude_file != jude_file or options[:copy] != :if_locked or options[:retried]
  do_jude_file = copy_jude_file jude_file, options
  load do_jude_file, options.update(:retried => true)
ensure
  FileUtils.remove_entry(File.dirname(do_jude_file), :force => true) if do_jude_file != jude_file
end

Instance Method Details

#build(java_parent, java_getter) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/luruju/jude_api_loader_in_jruby.rb', line 41

def build java_parent, java_getter
  
  return nil if java_getter.getGenericParameterTypes.length > 0
  
  begin
    java_obj = java_parent.send java_getter.getName
  rescue NativeException
#         p "Warning(#{__FILE__},#{__LINE__}) " + java_getter.getName
  end
  return nil if java_obj == nil
  
  java_return_class = java_getter.getReturnType      
  return build_judeapi(java_obj) if is_jude_class? java_return_class
  return build_array(java_obj, java_getter) if java_return_class.isArray
  return java_obj
end

#build_array(java_obj, java_getter) ⇒ Object



58
59
60
61
62
# File 'lib/luruju/jude_api_loader_in_jruby.rb', line 58

def build_array java_obj, java_getter
  comp_type =  java_getter.getReturnType.getComponentType
  return java_obj.to_a unless is_jude_class? comp_type      
  java_obj.inject([]) {|jude_objects,i| jude_objects << build_judeapi(i) }
end

#build_judeapi(java_obj) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/luruju/jude_api_loader_in_jruby.rb', line 64

def build_judeapi java_obj
     
  if is_jude_named_element?(java_obj) and @already_builded.key?(java_obj.getId)
    return @already_builded[java_obj.getId] 
  end

  java_if = java_obj.getClass.getInterfaces[0]
  jude_object = define_jude_class(java_if).new(@members)

  @members << jude_object
  @already_builded[java_obj.getId] = jude_object if jude_object.kind_of_named_element?

  java_if.getMethods.each do |java_method|
    next if java_method.getGenericParameterTypes.length > 0
    setter_name = java_method.getName.sub(/^(get)/,'').underscore << "="
    jude_object.send setter_name, build(java_obj, java_method)
  end
  
  return jude_object
end

#define_jude_class(java_if) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/luruju/jude_api_loader_in_jruby.rb', line 87

def define_jude_class java_if
  jude_meta = JudeMeta.instance
  jude_if_name = java_if.getSimpleName.sub(/^I/,'').sub(/ER/, "Er")
  return jude_meta.jude_class(jude_if_name) if JudeMeta.is_defined_for_luruju?(jude_if_name)

  jude_if = JudeMeta.instance.create_jude_if jude_if_name      
  java_if.getGenericInterfaces().each do |generic_if|
    super_if_name = generic_if.getSimpleName.sub(/^I/,'').sub(/ER/, "Er")
    jude_if.super_if_names << super_if_name
    define_jude_class generic_if unless JudeMeta.is_defined_for_luruju?(super_if_name)
  end
  java_if.getDeclaredMethods.each do |method|
    next if method.getGenericParameterTypes.length > 0
    jude_if.attributes << method.getName.sub(/^(get)/,'').underscore
  end
  java_if.getDeclaredFields.each{|f| jude_if.constants[f.getName] = f.get(nil)}
  jude_if.define_for_luruju
end

#is_jude_class?(java_class) ⇒ Boolean

Returns:

  • (Boolean)


85
# File 'lib/luruju/jude_api_loader_in_jruby.rb', line 85

def is_jude_class?(java_class) java_class.getName.index(JUDEAPI_PHEAD) == 0 end

#is_jude_named_element?(jude_obj) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'lib/luruju/jude_api_loader_in_jruby.rb', line 106

def is_jude_named_element? jude_obj
  jude_obj.getClass.getMethod "getId", nil
  return true
rescue java.lang.NoSuchMethodException
  return false
end