bind-it 0.2.1-java
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.md +72 -0
- data/lib/bind-it.rb +7 -0
- data/lib/bind-it/binding.rb +123 -0
- data/lib/bind-it/jar_loader.rb +68 -0
- data/lib/bind-it/rjb_proxy.rb +28 -0
- metadata +60 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
== BindIt Ruby Gem
|
2
|
+
BindIt is a tool to facilitate the creation of Java bindings in Ruby.
|
3
|
+
|
4
|
+
This program is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
This license also applies to the included Stanford CoreNLP files.
|
15
|
+
|
16
|
+
You should have received a copy of the GNU General Public License
|
17
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
|
19
|
+
Author: Louis-Antoine Mullie (louis.mullie@gmail.com). Copyright 2012.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
**About**
|
2
|
+
|
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
|
+
|
5
|
+
BindIt uses the Ruby-Java-Bridge (Rjb) to access Java objects. It runs on Ruby 1.9.2 and 1.9.3.
|
6
|
+
|
7
|
+
**Installing**
|
8
|
+
|
9
|
+
`gem install bind-it`
|
10
|
+
|
11
|
+
**Usage**
|
12
|
+
|
13
|
+
1. Create a module into which Java classes will be loaded.
|
14
|
+
2. Make sure that module extends `BindIt::Binding`.
|
15
|
+
3. Set your configuration options (see examples below).
|
16
|
+
4. Run YourModule.bind to load the default JARs and classes.
|
17
|
+
5. Access loaded Java classes using YourModule::YourClass.
|
18
|
+
|
19
|
+
**Example**
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
|
23
|
+
require 'bind-it'
|
24
|
+
|
25
|
+
module MyBindings
|
26
|
+
|
27
|
+
extend BindIt::Binding
|
28
|
+
|
29
|
+
# Arguments to use when initializing the JVM.
|
30
|
+
self.jvm_args = ['-option1X', '-option2X']
|
31
|
+
|
32
|
+
# If set, JVM output is redirected to file.
|
33
|
+
self.log_file = 'log_file.txt'
|
34
|
+
|
35
|
+
# The path in which to look for JAR files.
|
36
|
+
self.jar_path = '/path/to/jars/'
|
37
|
+
|
38
|
+
# Default JAR files to load.
|
39
|
+
self.default_jars = [
|
40
|
+
'jar1.jar',
|
41
|
+
'jar2.jar'
|
42
|
+
]
|
43
|
+
|
44
|
+
# Default namespace to use when loading classes.
|
45
|
+
self.default_namespace = 'path.to.default'
|
46
|
+
|
47
|
+
# Default classes to load under MyBindings.
|
48
|
+
# Shown are the three different ways of adding
|
49
|
+
# a class to the default list.
|
50
|
+
self.default_classes = [
|
51
|
+
# Will search for class in default_namespace.
|
52
|
+
['MyClass'],
|
53
|
+
# Supply a non-default namespace.
|
54
|
+
['MyClass', 'path.to.namespace'],
|
55
|
+
# Rename the Java class in Ruby.
|
56
|
+
['MyClass', 'path.to.namespace', 'MyClass2']
|
57
|
+
]
|
58
|
+
|
59
|
+
# Load the default JARs and classes.
|
60
|
+
self.bind
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
obj = MyBindings::MyClass.new
|
67
|
+
|
68
|
+
```
|
69
|
+
|
70
|
+
**Contributing**
|
71
|
+
|
72
|
+
Feel free to fork the project and send me a pull request!
|
data/lib/bind-it.rb
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
module BindIt
|
2
|
+
|
3
|
+
# This is the module your namespace should extend.
|
4
|
+
module Binding
|
5
|
+
|
6
|
+
VERSION = '0.0.1'
|
7
|
+
|
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
|
15
|
+
|
16
|
+
require 'bind-it/jar_loader'
|
17
|
+
|
18
|
+
|
19
|
+
def self.extended(base)
|
20
|
+
super(base)
|
21
|
+
# Create configuration options.
|
22
|
+
base.module_eval do
|
23
|
+
class << self
|
24
|
+
# Are the default JARs/classes loaded?
|
25
|
+
attr_accessor :bound
|
26
|
+
# The default path to look in for JARs.
|
27
|
+
attr_accessor :jar_path
|
28
|
+
# The flags for starting the JVM machine.
|
29
|
+
attr_accessor :jvm_args
|
30
|
+
# A file to redirect JVM output to.
|
31
|
+
attr_accessor :log_file
|
32
|
+
# The default JARs to load.
|
33
|
+
attr_accessor :default_jars
|
34
|
+
# The default Java namespace to
|
35
|
+
# search in for classes.
|
36
|
+
attr_accessor :default_namespace
|
37
|
+
# The default classes to load.
|
38
|
+
attr_accessor :default_classes
|
39
|
+
end
|
40
|
+
# Set default values.
|
41
|
+
self.jar_path = ''
|
42
|
+
self.jvm_args = []
|
43
|
+
self.log_file = nil
|
44
|
+
self.default_namespace = 'java'
|
45
|
+
self.default_classes = []
|
46
|
+
self.bound = false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
######
|
51
|
+
|
52
|
+
# Load the default JARs and classes.
|
53
|
+
def bind
|
54
|
+
unless self.bound
|
55
|
+
BindIt::JarLoader.log_file = self.log_file
|
56
|
+
BindIt::JarLoader.jvm_args = self.jvm_args
|
57
|
+
self.load_default_jars
|
58
|
+
self.load_default_classes
|
59
|
+
end
|
60
|
+
self.bound = true
|
61
|
+
end
|
62
|
+
|
63
|
+
# Load a JAR file.
|
64
|
+
def load_jar(jar, path = nil)
|
65
|
+
BindIt::JarLoader.load(jar, path)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Load the JARs, looking in default path.
|
69
|
+
def load_default_jars
|
70
|
+
self.default_jars.each do |jar|
|
71
|
+
file, path = *jar
|
72
|
+
path ||= self.jar_path
|
73
|
+
load_jar(jar, path)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Public function to load classes inside
|
78
|
+
# the namespace. Makes sure the default
|
79
|
+
# JARs are loaded before.
|
80
|
+
def load_class(klass, base = nil, rename = nil)
|
81
|
+
base ||= self.default_namespace
|
82
|
+
self.load unless self.bound
|
83
|
+
self.load_klass(klass, base, rename)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Create the Ruby classes corresponding to
|
87
|
+
# the StanfordNLP core classes.
|
88
|
+
def load_default_classes
|
89
|
+
self.default_classes.each do |info|
|
90
|
+
klass, base, rename = *info
|
91
|
+
self.load_klass(klass, base, rename)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
protected
|
96
|
+
|
97
|
+
# Private function to load classes.
|
98
|
+
# Doesn't check if initialized.
|
99
|
+
def load_klass(klass, base, name=nil)
|
100
|
+
base += '.' unless base == ''
|
101
|
+
fqcn = "#{base}#{klass}"
|
102
|
+
name ||= klass
|
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
|
112
|
+
end
|
113
|
+
|
114
|
+
# Utility function to CamelCase names.
|
115
|
+
def camel_case(text)
|
116
|
+
text.to_s.gsub(/^[a-z]|_[a-z]/) do |a|
|
117
|
+
a.upcase
|
118
|
+
end.gsub('_', '')
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module BindIt
|
2
|
+
|
3
|
+
class JarLoader
|
4
|
+
|
5
|
+
# Configuration options.
|
6
|
+
class << self
|
7
|
+
# An array of flags to pass to the JVM machine.
|
8
|
+
attr_accessor :jvm_args
|
9
|
+
# A log file to send JVM output to.
|
10
|
+
attr_accessor :log_file
|
11
|
+
end
|
12
|
+
|
13
|
+
# Default configuration options.
|
14
|
+
self.jvm_args = []
|
15
|
+
self.log_file = nil
|
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
|
28
|
+
end
|
29
|
+
|
30
|
+
# Load a Jruby jar.
|
31
|
+
def self.load_jar_jruby(jar, path)
|
32
|
+
require path + jar
|
33
|
+
end
|
34
|
+
|
35
|
+
# Laad an Rjb jar.
|
36
|
+
def self.load_jar_rjb(jar,path)
|
37
|
+
self.init_rjb unless ::Rjb::loaded?
|
38
|
+
jar = path + jar
|
39
|
+
::Rjb::add_jar(jar)
|
40
|
+
end
|
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
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module BindIt::RjbProxy
|
2
|
+
|
3
|
+
# Modify the Rjb JavaProxy class to add our own
|
4
|
+
# methods to every proxied Java object.
|
5
|
+
Rjb::Rjb_JavaProxy.class_eval do
|
6
|
+
|
7
|
+
# Dynamically defined on all proxied Java objects.
|
8
|
+
# Shorthand for toString() defined by Java classes.
|
9
|
+
def to_s; to_string; end
|
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
|
+
|
15
|
+
# Dynamically defined on all proxied Java iterators.
|
16
|
+
# Provide Ruby-style iterators to wrap Java iterators.
|
17
|
+
def each
|
18
|
+
if !java_methods.include?('iterator()')
|
19
|
+
raise 'This object cannot be iterated.'
|
20
|
+
else
|
21
|
+
i = self.iterator
|
22
|
+
while i.has_next; yield i.next; end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bind-it
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.1
|
6
|
+
platform: java
|
7
|
+
authors:
|
8
|
+
- Louis Mullie
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-12-18 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: " BindIt is a thin wrapper over the Ruby-Java-Bridge (Rjb) to facilitate the creation of Java bindings in Ruby. "
|
17
|
+
email:
|
18
|
+
- louis.mullie@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- lib/bind-it.rb
|
27
|
+
- lib/bind-it/binding.rb
|
28
|
+
- lib/bind-it/jar_loader.rb
|
29
|
+
- lib/bind-it/rjb_proxy.rb
|
30
|
+
- README.md
|
31
|
+
- LICENSE
|
32
|
+
homepage: https://github.com/louismullie/bind-it
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.24
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: BindIt is a tool to facilitate the creation of Java bindings in Ruby.
|
59
|
+
test_files: []
|
60
|
+
|