drbproxy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Jerry Luk <jerryluk@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,21 @@
1
+ DRbProxy
2
+ =========
3
+ Why?
4
+ I am using JRuby for my development because I need to access some Java libraries. That said, every time I run tests is going to take forever as I need to start a JVM and Rails environment initialization. Even with the help on Nailgun, it is still very slow. DRbProxy allows me to interact JRuby classes with Ruby.
5
+
6
+ How?
7
+ Let's say I want to use my GData JRuby client library from Ruby:
8
+ 1. I create a file called 'libs.rb' with the following content:
9
+ gem 'gdata-jruby-client'
10
+ require 'gdata_jruby_client'
11
+
12
+ 2. I start the drbproxy server
13
+ jruby drbproxy -f libs.rb
14
+
15
+ 3. Define GData class in Ruby/IRb:
16
+ require 'drbproxy'
17
+ class GData < DRbProxy::DRbProxyClass; end
18
+
19
+ 4. Now I can do things like:
20
+ puts GData::CalendarService::OWN_CALENDAR_URL
21
+ => "http://www.google.com/calendar/feeds/default/owncalendars/full"
data/bin/drbproxy ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib') unless $LOAD_PATH.include?(File.dirname(__FILE__) + '/../lib')
5
+
6
+ require 'drbproxy'
7
+ require 'drbproxy/server'
8
+
9
+ args = ARGV
10
+
11
+ raise ArgumentError, "expected array of args" unless args.is_a?(Array)
12
+ @options = {}
13
+ opt = OptionParser.new
14
+ opt.banner = "Usage: drbserver [options]\n\n"
15
+
16
+ opt.separator "Options:"
17
+ opt.on("-f", "--file [FILE]") {|file| @options[:file] = file }
18
+ opt.on("-d", "--debug") {|ignore| @options[:debug] = true }
19
+ # opt.on("-p", "--port [PORT]") {|port| @options[:port] = port }
20
+ opt.parse!(args)
21
+
22
+ if @options[:file]
23
+ require @options[:file]
24
+ end
25
+
26
+
27
+ DRb.start_service(DRbProxy::DRB_URI, DRbProxy::Server.new(@options))
28
+ puts "DRbProxy started: #{DRb.uri}"
29
+ # Wait for the drb server thread to finish before exiting.
30
+ DRb.thread.join
data/lib/drbproxy.rb ADDED
@@ -0,0 +1,5 @@
1
+ module DRbProxy
2
+ DRB_URI = "druby://localhost:8787" # TODO: make it an argument
3
+ end
4
+ require 'drb/drb'
5
+ require File.dirname(__FILE__) + '/drbproxy/drbproxy_class'
@@ -0,0 +1,25 @@
1
+ class DRbProxy::DRbProxyClass
2
+ @@proxy_server = DRbObject.new_with_uri(DRbProxy::DRB_URI)
3
+ def initialize(*args)
4
+ @object = @@proxy_server.new_object_for(self.class.to_s, *args)
5
+ end
6
+
7
+ def method_missing(sym, *args, &block)
8
+ @object.send sym, *args, &block
9
+ end
10
+
11
+ def self.method_missing(sym, *args, &block)
12
+ klass = @@proxy_server.klass_for(self.to_s)
13
+ klass.send sym, *args, &block
14
+ end
15
+
16
+ def self.const_missing(const_name)
17
+ const = @@proxy_server.constant_for(self.to_s, const_name)
18
+ if const.is_a? DRb::DRbUnknown
19
+ self.module_eval("class #{const_name} < DRbProxy::DRbProxyClass; end")
20
+ self.const_get(const_name)
21
+ else
22
+ const
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,39 @@
1
+ class DRbProxy::Server
2
+ @@klasses = []
3
+
4
+ def initialize(options={})
5
+ @debug = options[:debug]
6
+ end
7
+
8
+ def ready?
9
+ true
10
+ end
11
+
12
+ def new_object_for(klass_name, *args)
13
+ puts "Initializing new object #{klass_name}" if @debug
14
+ klass = get_klass(klass_name)
15
+ return klass.new(*args)
16
+ end
17
+
18
+ def klass_for(klass_name)
19
+ return get_klass(klass_name)
20
+ end
21
+
22
+ def constant_for(klass_name, const_name)
23
+ puts "Retrieving constant #{klass_name}::#{const_name}" if @debug
24
+ return get_klass(klass_name).const_get(const_name)
25
+ end
26
+
27
+ def get_klass(klass_name)
28
+ klass_arch = klass_name.split('::')
29
+ klass = Kernel
30
+ klass_arch.each { |k| klass = klass.const_get(k) }
31
+ unless @@klasses.include?(klass)
32
+ klass.send :include, DRb::DRbUndumped
33
+ klass.send :extend, DRb::DRbUndumped
34
+ @@klasses << klass
35
+ puts "Defined new class #{klass_name}" if @debug
36
+ end
37
+ klass
38
+ end
39
+ end
@@ -0,0 +1,67 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require File.dirname(__FILE__) + '/../lib/drbproxy'
3
+
4
+ describe DRbProxy do
5
+ before(:all) do
6
+ @server_pid = fork do
7
+ class TestProxy
8
+ attr_reader :param
9
+
10
+ CONSTANT = 'constant'
11
+
12
+ def initialize(param)
13
+ @param = param
14
+ end
15
+
16
+ def instance_method
17
+ 'instance method'
18
+ end
19
+
20
+ def self.klass_method
21
+ 'klass method'
22
+ end
23
+
24
+ class Inner
25
+ CONSTANT = 'inner constant'
26
+ def instance_method
27
+ 'inner instance method'
28
+ end
29
+
30
+ def self.klass_method
31
+ 'inner klass method'
32
+ end
33
+ end
34
+ end
35
+ require File.dirname(__FILE__) + '/../lib/drbproxy/server'
36
+ end
37
+ server = DRbObject.new_with_uri(DRbProxy::DRB_URI)
38
+ sleep(0.1) until server_ready?(server)
39
+ end
40
+
41
+ after(:all) do
42
+ Process.kill(Signal.list['TERM'], @server_pid)
43
+ end
44
+
45
+ def server_ready?(server)
46
+ begin
47
+ server.ready?
48
+ rescue Exception#DRb::DRbUnknownError
49
+ false
50
+ end
51
+ end
52
+
53
+ it "should do something" do
54
+ class TestProxy < DRbProxy::DRbProxyClass
55
+ end
56
+
57
+ test = TestProxy.new('test')
58
+ test.instance_method.should == 'instance method'
59
+ test.param.should == 'test'
60
+ TestProxy.klass_method == 'klass method'
61
+ TestProxy::CONSTANT.should == 'constant'
62
+ inner = TestProxy::Inner.new
63
+ inner.instance_method.should == 'inner instance method'
64
+ TestProxy::Inner.klass_method.should == 'inner klass method'
65
+ TestProxy::Inner::CONSTANT.should == 'inner constant'
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drbproxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jerry Luk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-30 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Invoke objects in another Ruby/JRuby VM via DRb
17
+ email: jerry@presdo.com
18
+ executables:
19
+ - drbproxy
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - MIT-LICENSE
27
+ - lib/drbproxy/drbproxy_class.rb
28
+ - lib/drbproxy/server.rb
29
+ - lib/drbproxy.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/jerryluk/drbproxy
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Invoke objects in another Ruby/JRuby VM via DRb.
58
+ test_files:
59
+ - spec/drbproxy_spec.rb