Module: Asciidoctor::Diagram::Java

Defined in:
lib/asciidoctor-diagram/util/java.rb,
lib/asciidoctor-diagram/util/java_jruby.rb,
lib/asciidoctor-diagram/util/java_socket.rb

Defined Under Namespace

Classes: CommandServer

Constant Summary collapse

CRLF =
"\r\n".encode(Encoding::US_ASCII)
STATUS_LINE =
Regexp.new("HTTP/1.1 (\\d+) (.*)\r\n".encode(Encoding::US_ASCII))

Class Method Summary collapse

Class Method Details

.classpathObject



7
8
9
# File 'lib/asciidoctor-diagram/util/java.rb', line 7

def self.classpath
  @classpath ||= Dir[File.join(File.dirname(__FILE__), '*.jar')]
end

.create_error(prefix_msg, response) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/asciidoctor-diagram/util/java.rb', line 78

def self.create_error(prefix_msg, response)
  content_type = response[:headers]['Content-Type'] || 'text/plain'
  if content_type.start_with? 'application/json'
    json = JSON.parse(response[:body].force_encoding(Encoding::UTF_8))
    ruby_bt = Kernel.caller(2)
    java_bt = json['stk'].map { |java_line| "#{java_line[0]}:#{java_line[3]}: in '#{java_line[2]}'" }
    error = RuntimeError.new("#{prefix_msg}: #{json['msg']}")
    error.set_backtrace java_bt + ruby_bt
    raise error
  elsif content_type.start_with? 'text/plain'
    raise "#{prefix_msg}: #{response[:reason]} #{response[:body].force_encoding(Encoding::UTF_8)}"
  else
    raise "#{prefix_msg}: #{response[:reason]}"
  end
end

.environment_variable(key) ⇒ Object



8
9
10
# File 'lib/asciidoctor-diagram/util/java_jruby.rb', line 8

def self.environment_variable(key)
  ENV[key] || ENV_JAVA[key.downcase.gsub('_', '.')]
end

.format_request(req, io) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/asciidoctor-diagram/util/java.rb', line 13

def self.format_request(req, io)
  io.set_encoding Encoding::US_ASCII
  io.write "POST #{req[:url]} HTTP/1.1"
  io.write CRLF

  headers = req[:headers]
  if headers
    headers.each_pair do |key, value|
      io.write "#{key}: #{value}"
      io.write CRLF
    end
  end

  if req[:body]
    unless headers && headers['Content-Length']
      io.write 'Content-Length: '
      io.write req[:body].bytesize.to_s
      io.write CRLF
    end

    unless headers && headers['Content-Type']
      io.write 'Content-Type: text/plain; charset='
      io.write req[:body].encoding.name
      io.write CRLF
    end
  end

  io.write CRLF

  io.set_encoding Encoding::BINARY
  io.write req[:body]
end

.instanceObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/asciidoctor-diagram/util/java_socket.rb', line 52

def self.instance
  unless defined?(@command_server) && @command_server
    server = CommandServer.new(java, classpath)
    @command_server = server
    at_exit do
      server.shutdown
    end
  end

  @command_server
end

.javaObject



94
95
96
97
98
# File 'lib/asciidoctor-diagram/util/java.rb', line 94

def self.java
  @java_exe ||= find_java
  raise "Could not find Java executable" unless @java_exe
  @java_exe
end

.loadObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/asciidoctor-diagram/util/java_jruby.rb', line 12

def self.load
  if defined?(@loaded) && @loaded
    return
  end

  classpath.flatten.each do |j|
    raise "Classpath item #{j} does not exist" unless File.exist?(j)
    require j
  end
  @loaded = true
end

.parse_response(io) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/asciidoctor-diagram/util/java.rb', line 48

def self.parse_response(io)
  resp = {}

  io.set_encoding Encoding::US_ASCII
  status_line = io.readline(CRLF)
  status_line_parts = STATUS_LINE.match status_line
  unless status_line_parts
    raise "Unexpected HTTP status line: #{status_line}"
  end

  resp[:code] = status_line_parts[1].to_i
  resp[:reason] = status_line_parts[2]

  headers = {}
  until (header = io.readline(CRLF).strip).empty?
    key, value = header.split ':', 2
    headers[key] = value.strip
  end

  resp[:headers] = headers

  content_length = headers['Content-Length']
  if content_length
    io.set_encoding Encoding::BINARY
    resp[:body] = io.read(content_length.to_i)
  end

  resp
end

.send_request(req) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/asciidoctor-diagram/util/java_jruby.rb', line 24

def self.send_request(req)
  cp = ::Java.org.asciidoctor.diagram.CommandProcessor.new()

  req_io = StringIO.new
  format_request(req, req_io)
  req_io.close

  response = cp.processRequest(req_io.string.to_java_bytes)

  resp_io = StringIO.new(String.from_java_bytes(response))
  resp = parse_response(resp_io)
  resp_io.close

  resp
end