asciidoctor-diagram 1.3.0.preview.1 → 1.3.0.preview.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,6 +9,7 @@ Enhancements::
9
9
  * Add support for Seqdiag diagrams (requires Seqdiag to be installed separately)
10
10
  * Add support for Nwdiag diagrams (requires Nwdiag to be installed separately)
11
11
  * Updated PlantUML to revision 8004 (23/08/2014)
12
+ * Remove dependency on RJB to simplify installation
12
13
 
13
14
  == 1.2.0
14
15
 
data/Rakefile CHANGED
@@ -5,9 +5,5 @@ RSpec::Core::RakeTask.new(:test)
5
5
 
6
6
  task :default => :test
7
7
 
8
- ['ruby', 'java'].map do |platform|
9
- $platform = platform
10
- Gem::Specification.reset
11
- spec = Gem::Specification.load('asciidoctor-diagram.gemspec')
12
- Gem::PackageTask.new(spec) { |task| }
13
- end
8
+ spec = Gem::Specification.load('asciidoctor-diagram.gemspec')
9
+ Gem::PackageTask.new(spec) { |task| }
@@ -5,26 +5,22 @@ module Asciidoctor
5
5
  module Diagram
6
6
  # @private
7
7
  module Ditaa
8
- DITAA_JAR_PATH = File.expand_path File.join('../..', 'ditaamini0_9.jar'), File.dirname(__FILE__)
9
- Java.classpath << DITAA_JAR_PATH
8
+ JARS = ['ditaamini0_9.jar'].map do |jar|
9
+ File.expand_path File.join('../..', jar), File.dirname(__FILE__)
10
+ end
11
+ Java.classpath.concat JARS
10
12
 
11
13
  def ditaa(code)
12
14
  Java.load
13
15
 
14
- args = ['-e', 'UTF-8']
15
-
16
- bytes = code.encode(Encoding::UTF_8).bytes.to_a
17
- bis = Java.new_object(Java.java.io.ByteArrayInputStream, '[B', Java.array_to_java_array(bytes, :byte))
18
- bos = Java.new_object(Java.java.io.ByteArrayOutputStream)
19
- result_code = Java.org.stathissideris.ascii2image.core.CommandLineConverter.convert(Java.array_to_java_array(args, :string), bis, bos)
20
- bis.close
21
- bos.close
22
-
23
- result = Java.string_from_java_bytes(bos.toByteArray)
16
+ response = Java.send_request(
17
+ :url => '/ditaa',
18
+ :body => code
19
+ )
24
20
 
25
- raise "Ditaa image generation failed: #{result}" unless result_code == 0
21
+ raise "Ditaa image generation failed: #{response[:reason]}" unless response[:code] == 200
26
22
 
27
- result
23
+ response[:body]
28
24
  end
29
25
 
30
26
  def self.included(mod)
@@ -123,8 +123,7 @@ module Asciidoctor
123
123
  private
124
124
  def create_image_block(parent, source, attributes, format, generator_info)
125
125
  image_name = "#{source.image_name}.#{format}"
126
- outdir = parent.document.attr('imagesoutdir') || parent.document.attr('outdir')
127
- image_dir = parent.normalize_system_path parent.document.attr 'imagesdir', outdir
126
+ image_dir = image_output_dir(parent)
128
127
  image_file = parent.normalize_system_path image_name, image_dir
129
128
  metadata_file = parent.normalize_system_path "#{image_name}.cache", image_dir
130
129
 
@@ -158,7 +157,7 @@ module Asciidoctor
158
157
  image_attributes['alt'] ||= if title_text = attributes['title']
159
158
  title_text
160
159
  elsif target = attributes['target']
161
- (File.basename target, (File.extname target) || '').tr '_-', ' '
160
+ (File.basename(target, File.extname(target)) || '').tr '_-', ' '
162
161
  else
163
162
  'Diagram'
164
163
  end
@@ -166,6 +165,21 @@ module Asciidoctor
166
165
  Asciidoctor::Block.new parent, :image, :content_model => :empty, :attributes => image_attributes
167
166
  end
168
167
 
168
+ def image_output_dir(parent)
169
+ document = parent.document
170
+
171
+ images_dir = document.attr('imagesoutdir')
172
+
173
+ if images_dir
174
+ base_dir = nil
175
+ else
176
+ base_dir = document.attr('outdir') || (document.respond_to?(:options) && document.options[:to_dir])
177
+ images_dir = document.attr('imagesdir')
178
+ end
179
+
180
+ parent.normalize_system_path(images_dir, base_dir)
181
+ end
182
+
169
183
  def create_literal_block(parent, source, attributes, generator_info)
170
184
  literal_attributes = attributes.dup
171
185
  literal_attributes.delete('target')
@@ -6,61 +6,45 @@ module Asciidoctor
6
6
  module PlantUml
7
7
  private
8
8
 
9
- PLANTUML_JAR_PATH = File.expand_path File.join('../..', 'plantuml.jar'), File.dirname(__FILE__)
10
- Java.classpath << PLANTUML_JAR_PATH
11
-
12
- def plantuml(parent, code, tag, *flags)
13
- unless @graphvizdot
14
- @graphvizdot = parent.document.attributes['dot']
15
- @graphvizdot = ::Asciidoctor::Diagram.which('dot') unless @graphvizdot && File.executable?(@graphvizdot)
16
- raise "Could not find the Graphviz 'dot' executable in PATH; add it to the PATH or specify its location using the 'dot' document attribute" unless @graphvizdot
17
- end
9
+ JARS = ['plantuml.jar'].map do |jar|
10
+ File.expand_path File.join('../..', jar), File.dirname(__FILE__)
11
+ end
12
+ Java.classpath.concat JARS
18
13
 
14
+ def plantuml(parent, code, tag, mime_type)
19
15
  Java.load
20
16
 
21
17
  code = "@start#{tag}\n#{code}\n@end#{tag}" unless code.index "@start#{tag}"
22
18
 
23
- flags += ['-charset', 'UTF-8', '-failonerror', '-graphvizdot', @graphvizdot]
19
+ headers = {
20
+ 'Accept' => mime_type
21
+ }
24
22
 
25
23
  config_file = parent.document.attributes['plantumlconfig']
26
24
  if config_file
27
- flags += ['-config', File.expand_path(config_file, parent.document.attributes['docdir'])]
25
+ headers['X-PlantUML-Config'] = File.expand_path(config_file, parent.document.attributes['docdir'])
28
26
  end
29
27
 
30
- option = Java.new_object( Java.net.sourceforge.plantuml.Option, '[Ljava.lang.String;', Java.array_to_java_array(flags, :string) )
31
- source_reader = Java.new_object( Java.net.sourceforge.plantuml.SourceStringReader,
32
- 'Lnet.sourceforge.plantuml.preproc.Defines;Ljava.lang.String;Ljava.util.List;',
33
- Java.new_object( Java.net.sourceforge.plantuml.preproc.Defines ),
34
- code,
35
- option.getConfig()
28
+ response = Java.send_request(
29
+ :url => '/plantuml',
30
+ :body => code,
31
+ :headers => headers
36
32
  )
37
33
 
38
- bos = Java.new_object( Java.java.io.ByteArrayOutputStream )
39
- ps = Java.new_object( Java.java.io.PrintStream, 'Ljava.io.OutputStream;', bos )
40
- source_reader.generateImage(ps, 0, option.getFileFormatOption())
41
- ps.close
42
- Java.string_from_java_bytes(bos.toByteArray)
43
- end
44
-
45
- def config_args(parent)
46
- config_args = []
47
- config = parent.document.attributes['plantumlconfig']
48
- if config
49
- config_args += ['-config', File.expand_path(config, parent.document.attributes['docdir'])]
50
- end
34
+ raise "PlantUML image generation failed: #{response[:reason]}" unless response[:code] == 200
51
35
 
52
- config_args
36
+ response[:body]
53
37
  end
54
38
 
55
39
  def self.included(mod)
56
40
  mod.register_format(:png, :image) do |c, p|
57
- plantuml(p, c.to_s, 'uml', *config_args(p))
41
+ plantuml(p, c.to_s, 'uml', 'image/png')
58
42
  end
59
43
  mod.register_format(:svg, :image) do |c, p|
60
- plantuml(p, c.to_s, 'uml', '-tsvg', *config_args(p))
44
+ plantuml(p, c.to_s, 'uml', 'image/svg+xml')
61
45
  end
62
46
  mod.register_format(:txt, :literal) do |c, p|
63
- plantuml(p, c.to_s, 'uml', '-tutxt', *config_args(p))
47
+ plantuml(p, c.to_s, 'uml', 'text/plain;charset=utf-8')
64
48
  end
65
49
  end
66
50
  end
@@ -1,5 +1,81 @@
1
+ module Asciidoctor
2
+ module Diagram
3
+ # @private
4
+ module Java
5
+ def self.classpath
6
+ @classpath ||= [
7
+ File.expand_path(File.join('../..', 'asciidoctor-diagram-java-1.3.1.jar'), File.dirname(__FILE__))
8
+ ]
9
+ end
10
+
11
+ CRLF = "\r\n".encode(Encoding::US_ASCII)
12
+
13
+ def self.format_request(req, io)
14
+ io.set_encoding Encoding::US_ASCII
15
+ io.write "POST #{req[:url]} HTTP/1.1"
16
+ io.write CRLF
17
+
18
+ headers = req[:headers]
19
+ if headers
20
+ headers.each_pair do |key, value|
21
+ io.write "#{key}: #{value}"
22
+ io.write CRLF
23
+ end
24
+ end
25
+
26
+ if req[:body]
27
+ unless headers && headers['Content-Length']
28
+ io.write 'Content-Length: '
29
+ io.write req[:body].bytesize.to_s
30
+ io.write CRLF
31
+ end
32
+
33
+ unless headers && headers['Content-Type']
34
+ io.write 'Content-Type: text/plain; charset='
35
+ io.write req[:body].encoding.name
36
+ io.write CRLF
37
+ end
38
+ end
39
+
40
+ io.write CRLF
41
+
42
+ io.set_encoding Encoding::BINARY
43
+ io.write req[:body]
44
+ end
45
+
46
+ STATUS_LINE = Regexp.new("HTTP/1.1 (\\d+) (.*)\r\n".encode(Encoding::US_ASCII))
47
+
48
+ def self.parse_response(io)
49
+ resp = {}
50
+
51
+ io.set_encoding Encoding::US_ASCII
52
+ status_line = io.readline(CRLF)
53
+ status_line_parts = STATUS_LINE.match status_line
54
+ resp[:code] = status_line_parts[1].to_i
55
+ resp[:reason] = status_line_parts[2]
56
+
57
+ headers = {}
58
+ until (header = io.readline(CRLF).strip).empty?
59
+ key, value = header.split ':', 2
60
+ headers[key] = value.strip
61
+ end
62
+
63
+ resp[:headers] = headers
64
+
65
+ content_length = headers['Content-Length']
66
+ if content_length
67
+ io.set_encoding Encoding::BINARY
68
+ resp[:body] = io.read(content_length.to_i)
69
+ end
70
+
71
+ resp
72
+ end
73
+ end
74
+ end
75
+ end
76
+
1
77
  if RUBY_PLATFORM == "java"
2
78
  require_relative 'java_jruby'
3
79
  else
4
- require_relative 'java_rjb'
80
+ require_relative 'java_socket'
5
81
  end
@@ -1,40 +1,33 @@
1
1
  require 'java'
2
+ require 'stringio'
2
3
 
3
4
  module Asciidoctor
4
5
  module Diagram
5
6
  # @private
6
7
  module Java
7
- def self.classpath
8
- @classpath ||= []
9
- end
10
-
11
8
  def self.load
12
9
  if @loaded
13
10
  return
14
11
  end
15
12
 
16
- classpath.each { |j| require j }
13
+ classpath.flatten.each { |j| require j }
17
14
  @loaded = true
18
15
  end
19
16
 
20
- def self.array_to_java_array(array, type)
21
- array.to_java(type)
22
- end
17
+ def self.send_request(req)
18
+ cp = ::Java.org.asciidoctor.diagram.CommandProcessor.new()
23
19
 
24
- def self.string_from_java_bytes(bytes)
25
- String.from_java_bytes(bytes)
26
- end
20
+ req_io = StringIO.new
21
+ format_request(req, req_io)
22
+ req_io.close
27
23
 
28
- def self.method_missing(meth, *args, &block)
29
- raise "No arguments expected" unless args.empty?
30
- raise "No block expected" if block
24
+ response = cp.processRequest(req_io.string.to_java_bytes)
31
25
 
32
- load
33
- ::Java.send(meth)
34
- end
26
+ resp_io = StringIO.new(String.from_java_bytes(response))
27
+ resp = parse_response(resp_io)
28
+ resp_io.close
35
29
 
36
- def self.new_object(java_class, signature = nil, *args)
37
- java_class.new(*args)
30
+ resp
38
31
  end
39
32
  end
40
33
  end
@@ -0,0 +1,93 @@
1
+ require 'socket'
2
+
3
+ require_relative 'which'
4
+
5
+ module Asciidoctor
6
+ module Diagram
7
+ # @private
8
+ module Java
9
+ class CommandServer
10
+ attr_reader :port
11
+
12
+ def initialize(java, classpath)
13
+ args = []
14
+ args << '-Djava.awt.headless=true'
15
+ args << '-cp'
16
+ args << classpath.flatten.join(File::PATH_SEPARATOR)
17
+ args << 'org.asciidoctor.diagram.CommandServer'
18
+
19
+ @server = IO.popen([java, *args])
20
+ @port = @server.readline.strip.to_i
21
+ @client = TCPSocket.new 'localhost', port
22
+ end
23
+
24
+ def io
25
+ @client
26
+ end
27
+ end
28
+
29
+ def self.load
30
+ if @loaded
31
+ return
32
+ end
33
+
34
+ command_server
35
+ @loaded = true
36
+ end
37
+
38
+ def self.command_server
39
+ @java_exe ||= find_java
40
+ raise "Could not find Java executable" unless @java_exe
41
+ @command_server ||= CommandServer.new(@java_exe, classpath)
42
+ end
43
+
44
+ def self.send_request(req)
45
+ svr = command_server
46
+ headers = req[:headers] ||= {}
47
+ headers['Host'] = "localhost:#{svr.port}"
48
+ format_request(req, svr.io)
49
+ parse_response(svr.io)
50
+ end
51
+
52
+ private
53
+ def self.find_java
54
+ if /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM
55
+ # Windows
56
+ path_to(ENV['JAVA_HOME'], 'bin/java.exe') || registry_lookup || ::Asciidoctor::Diagram.which('java')
57
+ elsif /darwin/ =~ RUBY_PLATFORM
58
+ # Mac
59
+ path_to(ENV['JAVA_HOME'], 'bin/java') || path_to(`/usr/libexec/java_home`.strip, 'bin/java') || ::Asciidoctor::Diagram.which('java')
60
+ else
61
+ # Other unix-like system
62
+ path_to(ENV['JAVA_HOME'], 'bin/java') || ::Asciidoctor::Diagram.which('java')
63
+ end
64
+ end
65
+
66
+ def self.path_to(java_home, java_binary)
67
+ exe_path = File.expand_path(java_binary, java_home)
68
+ if File.executable?(exe_path)
69
+ exe_path
70
+ else
71
+ nil
72
+ end
73
+ end
74
+
75
+ def self.registry_lookup
76
+ key_re = /^HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\.*\\([0-9\.]+)/
77
+ value_re = /\s*JavaHome\s*REG_SZ\s*(.*)/
78
+ result = `reg query "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft" /s /v JavaHome`.lines.map { |l| l.strip }
79
+ vms = result.each_slice(3).map do |_, key, value|
80
+ key_match = key_re.match(key)
81
+ value_match = value_re.match(value)
82
+ if key_match && value_match
83
+ [key_match[1].split('.').map { |v| v.to_i }, value_match[1]]
84
+ else
85
+ nil
86
+ end
87
+ end.reject { |v| v.nil? }.sort_by { |v| v[0] }
88
+ java_exes = vms.map { |version, path| File.expand_path('bin/java.exe', path) }.select { |exe| File.executable?(exe) }
89
+ java_exes && java_exes[0]
90
+ end
91
+ end
92
+ end
93
+ end
@@ -1,5 +1,5 @@
1
1
  module Asciidoctor
2
2
  module Diagram
3
- VERSION = "1.3.0.preview.1"
3
+ VERSION = "1.3.0.preview.2"
4
4
  end
5
5
  end
@@ -297,6 +297,31 @@ Foo1 -> Foo2 : To boundary
297
297
  expect(File.exists?(File.expand_path(target, 'foo'))).to be true
298
298
  end
299
299
 
300
+ it "should write files to imagesoutdir if set" do
301
+ doc = <<-eos
302
+ = Hello, PlantUML!
303
+ Doc Writer <doc@example.com>
304
+
305
+ == First Section
306
+
307
+ [plantuml, format="svg"]
308
+ ----
309
+ actor Foo1
310
+ boundary Foo2
311
+ Foo1 -> Foo2 : To boundary
312
+ ----
313
+ eos
314
+
315
+ d = Asciidoctor.load StringIO.new(doc), {:attributes => {'imagesoutdir' => 'bar', 'outdir' => 'foo'}}
316
+ b = d.find { |b| b.context == :image }
317
+
318
+ target = b.attributes['target']
319
+ expect(target).to_not be_nil
320
+ expect(File.exists?(target)).to be false
321
+ expect(File.exists?(File.expand_path(target, 'bar'))).to be true
322
+ expect(File.exists?(File.expand_path(target, 'foo'))).to be false
323
+ end
324
+
300
325
  it "should omit width/height attributes when generating docbook" do
301
326
  doc = <<-eos
302
327
  = Hello, PlantUML!
metadata CHANGED
@@ -1,18 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciidoctor-diagram
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0.preview.1
4
+ version: 1.3.0.preview.2
5
+ prerelease: 6
5
6
  platform: ruby
6
7
  authors:
7
8
  - Pepijn Van Eeckhoudt
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-09-02 00:00:00.000000000 Z
12
+ date: 2014-11-30 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ~>
18
20
  - !ruby/object:Gem::Version
@@ -20,6 +22,7 @@ dependencies:
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - ~>
25
28
  - !ruby/object:Gem::Version
@@ -27,34 +30,39 @@ dependencies:
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rake
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - '>='
35
+ - - ! '>='
32
36
  - !ruby/object:Gem::Version
33
37
  version: '0'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - '>='
43
+ - - ! '>='
39
44
  - !ruby/object:Gem::Version
40
45
  version: '0'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rspec
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - '>='
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - '>='
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: asciidoctor
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
67
  - - ~>
60
68
  - !ruby/object:Gem::Version
@@ -62,24 +70,11 @@ dependencies:
62
70
  type: :runtime
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
75
  - - ~>
67
76
  - !ruby/object:Gem::Version
68
77
  version: 1.5.0
69
- - !ruby/object:Gem::Dependency
70
- name: rjb
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ~>
74
- - !ruby/object:Gem::Version
75
- version: 1.4.8
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ~>
81
- - !ruby/object:Gem::Version
82
- version: 1.4.8
83
78
  description: Asciidoctor diagramming extension
84
79
  email:
85
80
  - pepijn@vaneeckhoudt.net
@@ -95,6 +90,7 @@ files:
95
90
  - examples/README.adoc
96
91
  - examples/design.adoc
97
92
  - examples/features.adoc
93
+ - lib/asciidoctor-diagram-java-1.3.1.jar
98
94
  - lib/asciidoctor-diagram.rb
99
95
  - lib/asciidoctor-diagram/blockdiag.rb
100
96
  - lib/asciidoctor-diagram/blockdiag/extension.rb
@@ -111,7 +107,7 @@ files:
111
107
  - lib/asciidoctor-diagram/util/cli_generator.rb
112
108
  - lib/asciidoctor-diagram/util/java.rb
113
109
  - lib/asciidoctor-diagram/util/java_jruby.rb
114
- - lib/asciidoctor-diagram/util/java_rjb.rb
110
+ - lib/asciidoctor-diagram/util/java_socket.rb
115
111
  - lib/asciidoctor-diagram/util/png.rb
116
112
  - lib/asciidoctor-diagram/util/svg.rb
117
113
  - lib/asciidoctor-diagram/util/which.rb
@@ -127,26 +123,27 @@ files:
127
123
  homepage: https://github.com/asciidoctor/asciidoctor-diagram
128
124
  licenses:
129
125
  - MIT
130
- metadata: {}
131
126
  post_install_message:
132
127
  rdoc_options: []
133
128
  require_paths:
134
129
  - lib
135
130
  required_ruby_version: !ruby/object:Gem::Requirement
131
+ none: false
136
132
  requirements:
137
- - - '>='
133
+ - - ! '>='
138
134
  - !ruby/object:Gem::Version
139
135
  version: '0'
140
136
  required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
141
138
  requirements:
142
- - - '>'
139
+ - - ! '>'
143
140
  - !ruby/object:Gem::Version
144
141
  version: 1.3.1
145
142
  requirements: []
146
143
  rubyforge_project:
147
- rubygems_version: 2.0.14
144
+ rubygems_version: 1.8.23.2
148
145
  signing_key:
149
- specification_version: 4
146
+ specification_version: 3
150
147
  summary: An extension for asciidoctor that adds support for UML diagram generation
151
148
  using PlantUML
152
149
  test_files:
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: ec30717e348b864e01a962749e47514a55c1106c
4
- data.tar.gz: 3d85e72167829121d471faa6f20586cc2eb1eab4
5
- SHA512:
6
- metadata.gz: daedbb3ba7ef2bc7517ba7c67052a6ee9e468adf5a5ca710bbc3ca80f3dbdf078748b679696d95f791121e651f920d25bdf039e88ace183fde759f32c18b75ba
7
- data.tar.gz: ff54af102f2a7d21eefa591f4db10985826c97f2eff4044bb305a91f2c0cc888f5d965fddea4bba4f75ad96ed03baa2f5bcca8edf6e952d664c5cd8663183cd7
@@ -1,91 +0,0 @@
1
- require 'rjb'
2
-
3
- module Asciidoctor
4
- module Diagram
5
- # @private
6
- module Java
7
- INITAWT_JAR_PATH = File.expand_path File.join('..', 'initawt.jar'), File.dirname(__FILE__)
8
-
9
- module Package
10
- def method_missing(meth, *args, &block)
11
- raise "No arguments expected" unless args.empty?
12
- raise "No block expected" if block
13
-
14
- name = meth.to_s
15
- @proxies ||= {}
16
- @proxies[name] ||= create_java_proxy(name)
17
- end
18
-
19
- private
20
-
21
- def create_java_proxy(name)
22
- qualified_name = @name ? "#{@name}.#{name}" : name
23
- if name =~ /^[[:upper:]]/
24
- Package.create_class(qualified_name)
25
- else
26
- Package.create_package(qualified_name)
27
- end
28
- end
29
-
30
- def self.create_class(name)
31
- ::Rjb.import(name)
32
- end
33
-
34
- def self.create_package(name)
35
- package = Module.new
36
- package.extend Package
37
- package.instance_variable_set :@name, name
38
- package.instance_variable_set :@parent, name
39
- package
40
- end
41
- end
42
-
43
- def self.classpath
44
- @classpath ||= []
45
- end
46
-
47
- def self.load
48
- if @loaded
49
- return
50
- end
51
-
52
- Rjb::load(classpath.join(File::PATH_SEPARATOR))
53
-
54
- # On OS X using AWT from JNI is extremely deadlock prone. Enabling AWT headless mode resolves this issue. We're
55
- # never actually going to display an AWT GUI, so this should be fairly safe.
56
- Rjb::import('java.lang.System').setProperty 'java.awt.headless', 'true'
57
-
58
- @loaded = true
59
- end
60
-
61
- def self.array_to_java_array(array, type)
62
- # Rjb does not require an explicit conversion of a Ruby Array containing Ruby Strings to a Ruby Array containing
63
- # Java Strings. It handles this implicitly when calling Java methods.
64
- array
65
- end
66
-
67
- def self.string_from_java_bytes(bytes)
68
- # Rjb implictly converts Java byte arrays to Ruby Strings so nothing needs to be done here
69
- bytes
70
- end
71
-
72
- def self.method_missing(meth, *args, &block)
73
- raise "No arguments expected" unless args.empty?
74
- raise "No block expected" if block
75
-
76
- load
77
-
78
- @root_package ||= Package.send(:create_package, nil)
79
- @root_package.send(meth, *args)
80
- end
81
-
82
- def self.new_object(java_class, signature = nil, *args)
83
- if signature
84
- java_class.new_with_sig(signature, *args)
85
- else
86
- java_class.new(*args)
87
- end
88
- end
89
- end
90
- end
91
- end