bind-it 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/bind-it.rb +1 -1
- data/lib/bind-it/binding.rb +27 -8
- data/lib/bind-it/jar_loader.rb +43 -21
- data/lib/bind-it/{proxy_decorator.rb → rjb_proxy.rb} +7 -3
- metadata +4 -4
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
**About**
|
2
2
|
|
3
|
-
BindIt is a very simple tool to
|
3
|
+
BindIt is a very simple tool that allows to make Ruby bindings to Java packages that work both on JRuby and MRI platforms. It provides a simplified interface to load JARs and Java classes, and allows logging of JVM output to a text file.
|
4
4
|
|
5
5
|
BindIt uses the Ruby-Java-Bridge (Rjb) to access Java objects. It runs on Ruby 1.9.2 and 1.9.3.
|
6
6
|
|
data/lib/bind-it.rb
CHANGED
data/lib/bind-it/binding.rb
CHANGED
@@ -5,11 +5,18 @@ module BindIt
|
|
5
5
|
|
6
6
|
VERSION = '0.0.1'
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
if RUBY_PLATFORM =~ /java/
|
9
|
+
require 'java'
|
10
|
+
# require 'bind-it/jruby_proxy'
|
11
|
+
else
|
12
|
+
require 'rjb'
|
13
|
+
require 'bind-it/rjb_proxy'
|
14
|
+
end
|
11
15
|
|
12
|
-
|
16
|
+
require 'bind-it/jar_loader'
|
17
|
+
|
18
|
+
|
19
|
+
def self.extended(base)
|
13
20
|
super(base)
|
14
21
|
# Create configuration options.
|
15
22
|
base.module_eval do
|
@@ -40,6 +47,8 @@ module BindIt
|
|
40
47
|
end
|
41
48
|
end
|
42
49
|
|
50
|
+
######
|
51
|
+
|
43
52
|
# Load the default JARs and classes.
|
44
53
|
def bind
|
45
54
|
unless self.bound
|
@@ -83,13 +92,23 @@ module BindIt
|
|
83
92
|
end
|
84
93
|
end
|
85
94
|
|
95
|
+
protected
|
96
|
+
|
86
97
|
# Private function to load classes.
|
87
98
|
# Doesn't check if initialized.
|
88
|
-
def load_klass(klass, base, name
|
99
|
+
def load_klass(klass, base, name=nil)
|
89
100
|
base += '.' unless base == ''
|
101
|
+
fqcn = "#{base}#{klass}"
|
90
102
|
name ||= klass
|
91
|
-
|
92
|
-
|
103
|
+
if RUBY_PLATFORM =~ /java/
|
104
|
+
rb_class = java_import(fqcn)
|
105
|
+
if name != klass
|
106
|
+
const_set(name.intern, rb_class)
|
107
|
+
end
|
108
|
+
else
|
109
|
+
rb_class = Rjb::import(fqcn)
|
110
|
+
const_set(name.intern, rb_class)
|
111
|
+
end
|
93
112
|
end
|
94
113
|
|
95
114
|
# Utility function to CamelCase names.
|
@@ -101,4 +120,4 @@ module BindIt
|
|
101
120
|
|
102
121
|
end
|
103
122
|
|
104
|
-
end
|
123
|
+
end
|
data/lib/bind-it/jar_loader.rb
CHANGED
@@ -13,34 +13,56 @@ module BindIt
|
|
13
13
|
# Default configuration options.
|
14
14
|
self.jvm_args = []
|
15
15
|
self.log_file = nil
|
16
|
-
|
17
|
-
# Load
|
18
|
-
def self.
|
19
|
-
|
20
|
-
|
16
|
+
|
17
|
+
# Load a JAR through Jruby/Rjb.
|
18
|
+
def self.load(jar, path)
|
19
|
+
if !::File.readable?(path + jar)
|
20
|
+
raise "Could not find JAR file (looking in #{jar})."
|
21
|
+
end
|
22
|
+
if RUBY_PLATFORM =~ /java/
|
23
|
+
set_java_logging if self.log_file
|
24
|
+
load_jar_jruby(jar,path)
|
25
|
+
else
|
26
|
+
load_jar_rjb(jar,path)
|
27
|
+
end
|
21
28
|
end
|
22
29
|
|
23
|
-
#
|
24
|
-
def self.
|
25
|
-
|
26
|
-
const_set(:PrintStream, Rjb::import('java.io.PrintStream'))
|
27
|
-
const_set(:File2, Rjb::import('java.io.File'))
|
28
|
-
ps = PrintStream.new(File2.new(self.log_file))
|
29
|
-
ps.write(::Time.now.strftime("[%m/%d/%Y at %I:%M%p]\n\n"))
|
30
|
-
System.setOut(ps)
|
31
|
-
System.setErr(ps)
|
30
|
+
# Load a Jruby jar.
|
31
|
+
def self.load_jar_jruby(jar, path)
|
32
|
+
require path + jar
|
32
33
|
end
|
33
34
|
|
34
|
-
#
|
35
|
-
def self.
|
36
|
-
self.
|
35
|
+
# Laad an Rjb jar.
|
36
|
+
def self.load_jar_rjb(jar,path)
|
37
|
+
self.init_rjb unless ::Rjb::loaded?
|
37
38
|
jar = path + jar
|
38
|
-
if !::File.readable?(jar)
|
39
|
-
raise "Could not find JAR file (looking in #{jar})."
|
40
|
-
end
|
41
39
|
::Rjb::add_jar(jar)
|
42
40
|
end
|
43
41
|
|
42
|
+
# Load Rjb and create Java VM.
|
43
|
+
def self.init_rjb
|
44
|
+
::Rjb::load(nil, self.jvm_args)
|
45
|
+
set_java_logging if self.log_file
|
46
|
+
end
|
47
|
+
|
48
|
+
# Redirect the output of the JVM to log.
|
49
|
+
def self.set_java_logging
|
50
|
+
system, print_stream, file = nil, nil, nil
|
51
|
+
if RUBY_PLATFORM =~ /java/
|
52
|
+
system = java.lang.System
|
53
|
+
print_stream = java.io.PrintStream
|
54
|
+
file = java.io.File
|
55
|
+
else
|
56
|
+
system = Rjb::import('java.lang.System')
|
57
|
+
print_stream = Rjb::import('java.io.PrintStream')
|
58
|
+
file = Rjb::import('java.io.File')
|
59
|
+
end
|
60
|
+
ps = print_stream.new(file.new(self.log_file))
|
61
|
+
ps.write(::Time.now.strftime("[%m/%d/%Y at %I:%M%p]\n\n"))
|
62
|
+
system.setOut(ps)
|
63
|
+
system.setErr(ps)
|
64
|
+
end
|
65
|
+
|
44
66
|
end
|
45
67
|
|
46
|
-
end
|
68
|
+
end
|
@@ -1,13 +1,17 @@
|
|
1
|
-
module BindIt
|
1
|
+
module BindIt::RjbProxy
|
2
2
|
|
3
3
|
# Modify the Rjb JavaProxy class to add our own
|
4
4
|
# methods to every proxied Java object.
|
5
5
|
Rjb::Rjb_JavaProxy.class_eval do
|
6
6
|
|
7
7
|
# Dynamically defined on all proxied Java objects.
|
8
|
-
# Shorthand for
|
8
|
+
# Shorthand for toString() defined by Java classes.
|
9
9
|
def to_s; to_string; end
|
10
|
-
|
10
|
+
|
11
|
+
# Dynamically defined on all proxied Java objects.
|
12
|
+
# Shorthand for toArray() defined by Java classes.
|
13
|
+
def to_a; to_array; end
|
14
|
+
|
11
15
|
# Dynamically defined on all proxied Java iterators.
|
12
16
|
# Provide Ruby-style iterators to wrap Java iterators.
|
13
17
|
def each
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bind-it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rjb
|
@@ -37,7 +37,7 @@ extra_rdoc_files: []
|
|
37
37
|
files:
|
38
38
|
- lib/bind-it/binding.rb
|
39
39
|
- lib/bind-it/jar_loader.rb
|
40
|
-
- lib/bind-it/
|
40
|
+
- lib/bind-it/rjb_proxy.rb
|
41
41
|
- lib/bind-it.rb
|
42
42
|
- README.md
|
43
43
|
- LICENSE
|
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
61
|
version: '0'
|
62
62
|
requirements: []
|
63
63
|
rubyforge_project:
|
64
|
-
rubygems_version: 1.8.
|
64
|
+
rubygems_version: 1.8.24
|
65
65
|
signing_key:
|
66
66
|
specification_version: 3
|
67
67
|
summary: BindIt is a tool to facilitate the creation of Java bindings in Ruby.
|