bind-it 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +19 -0
- data/README.md +72 -0
- data/lib/bind-it.rb +7 -0
- data/lib/bind-it/binding.rb +104 -0
- data/lib/bind-it/jar_loader.rb +46 -0
- data/lib/bind-it/proxy_decorator.rb +24 -0
- metadata +68 -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 to facilitate the creation of Java bindings in Ruby. It provides a simplified interface to load JARs and Java classes, and allows logging of JVM output to a text file. It also enhances the Rjb Java proxy with useful functions, such as internal iterators.
|
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,104 @@
|
|
1
|
+
module BindIt
|
2
|
+
|
3
|
+
# This is the module your namespace should extend.
|
4
|
+
module Binding
|
5
|
+
|
6
|
+
VERSION = '0.0.1'
|
7
|
+
|
8
|
+
require 'rjb'
|
9
|
+
require 'bind-it/jar_loader'
|
10
|
+
require 'bind-it/proxy_decorator'
|
11
|
+
|
12
|
+
def self.extended(base)
|
13
|
+
super(base)
|
14
|
+
# Create configuration options.
|
15
|
+
base.module_eval do
|
16
|
+
class << self
|
17
|
+
# Are the default JARs/classes loaded?
|
18
|
+
attr_accessor :bound
|
19
|
+
# The default path to look in for JARs.
|
20
|
+
attr_accessor :jar_path
|
21
|
+
# The flags for starting the JVM machine.
|
22
|
+
attr_accessor :jvm_args
|
23
|
+
# A file to redirect JVM output to.
|
24
|
+
attr_accessor :log_file
|
25
|
+
# The default JARs to load.
|
26
|
+
attr_accessor :default_jars
|
27
|
+
# The default Java namespace to
|
28
|
+
# search in for classes.
|
29
|
+
attr_accessor :default_namespace
|
30
|
+
# The default classes to load.
|
31
|
+
attr_accessor :default_classes
|
32
|
+
end
|
33
|
+
# Set default values.
|
34
|
+
self.jar_path = ''
|
35
|
+
self.jvm_args = []
|
36
|
+
self.log_file = nil
|
37
|
+
self.default_namespace = 'java'
|
38
|
+
self.default_classes = []
|
39
|
+
self.bound = false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Load the default JARs and classes.
|
44
|
+
def bind
|
45
|
+
unless self.bound
|
46
|
+
BindIt::JarLoader.log_file = self.log_file
|
47
|
+
BindIt::JarLoader.jvm_args = self.jvm_args
|
48
|
+
self.load_default_jars
|
49
|
+
self.load_default_classes
|
50
|
+
end
|
51
|
+
self.bound = true
|
52
|
+
end
|
53
|
+
|
54
|
+
# Load a JAR file.
|
55
|
+
def load_jar(jar, path = nil)
|
56
|
+
BindIt::JarLoader.load(jar, path)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Load the JARs, looking in default path.
|
60
|
+
def load_default_jars
|
61
|
+
self.default_jars.each do |jar|
|
62
|
+
file, path = *jar
|
63
|
+
path ||= self.jar_path
|
64
|
+
load_jar(jar, path)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Public function to load classes inside
|
69
|
+
# the namespace. Makes sure the default
|
70
|
+
# JARs are loaded before.
|
71
|
+
def load_class(klass, base = nil, rename = nil)
|
72
|
+
base ||= self.default_namespace
|
73
|
+
self.load unless self.bound
|
74
|
+
self.load_klass(klass, base, rename)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Create the Ruby classes corresponding to
|
78
|
+
# the StanfordNLP core classes.
|
79
|
+
def load_default_classes
|
80
|
+
self.default_classes.each do |info|
|
81
|
+
klass, base, rename = *info
|
82
|
+
self.load_klass(klass, base, rename)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Private function to load classes.
|
87
|
+
# Doesn't check if initialized.
|
88
|
+
def load_klass(klass, base, name = nil)
|
89
|
+
base += '.' unless base == ''
|
90
|
+
name ||= klass
|
91
|
+
const_set(name.intern,
|
92
|
+
Rjb::import("#{base}#{klass}"))
|
93
|
+
end
|
94
|
+
|
95
|
+
# Utility function to CamelCase names.
|
96
|
+
def camel_case(text)
|
97
|
+
text.to_s.gsub(/^[a-z]|_[a-z]/) do |a|
|
98
|
+
a.upcase
|
99
|
+
end.gsub('_', '')
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
@@ -0,0 +1,46 @@
|
|
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 Rjb and create Java VM.
|
18
|
+
def self.init
|
19
|
+
::Rjb::load(nil, self.jvm_args)
|
20
|
+
set_java_logging if self.log_file
|
21
|
+
end
|
22
|
+
|
23
|
+
# Redirect the output of the JVM to log.
|
24
|
+
def self.set_java_logging
|
25
|
+
const_set(:System, Rjb::import('java.lang.System'))
|
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)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Load a JAR through Rjb.
|
35
|
+
def self.load(jar, path)
|
36
|
+
self.init unless ::Rjb::loaded?
|
37
|
+
jar = path + jar
|
38
|
+
if !::File.readable?(jar)
|
39
|
+
raise "Could not find JAR file (looking in #{jar})."
|
40
|
+
end
|
41
|
+
::Rjb::add_jar(jar)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module BindIt
|
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 to_string defined by Java classes.
|
9
|
+
def to_s; to_string; end
|
10
|
+
|
11
|
+
# Dynamically defined on all proxied Java iterators.
|
12
|
+
# Provide Ruby-style iterators to wrap Java iterators.
|
13
|
+
def each
|
14
|
+
if !java_methods.include?('iterator()')
|
15
|
+
raise 'This object cannot be iterated.'
|
16
|
+
else
|
17
|
+
i = self.iterator
|
18
|
+
while i.has_next; yield i.next; end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bind-it
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Louis Mullie
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rjb
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: ! ' BindIt is a thin wrapper over the Ruby-Java-Bridge (Rjb) to facilitate
|
31
|
+
the creation of Java bindings in Ruby. '
|
32
|
+
email:
|
33
|
+
- louis.mullie@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- lib/bind-it/binding.rb
|
39
|
+
- lib/bind-it/jar_loader.rb
|
40
|
+
- lib/bind-it/proxy_decorator.rb
|
41
|
+
- lib/bind-it.rb
|
42
|
+
- README.md
|
43
|
+
- LICENSE
|
44
|
+
homepage: https://github.com/louismullie/bind-it
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.21
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: BindIt is a tool to facilitate the creation of Java bindings in Ruby.
|
68
|
+
test_files: []
|