Class: Asciidoctor::Diagram::Java::CommandServer

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor-diagram/util/java_socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(java, classpath) ⇒ CommandServer

Returns a new instance of CommandServer.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/asciidoctor-diagram/util/java_socket.rb', line 13

def initialize(java, classpath)
  args = []
  args << '-Djava.awt.headless=true'
  args << '-cp'
  # special case for cygwin, it requires path translation for java to work
  if RbConfig::CONFIG['host_os'] =~ /cygwin/i
    cygpath = ::Asciidoctor::Diagram.which('cygpath')
    if(cygpath != nil) 
      args << classpath.flatten.map { |jar| `cygpath -w "#{jar}"`.strip }.join(";")
    else
      puts 'cygwin warning: cygpath not found'
      args << classpath.flatten.join(File::PATH_SEPARATOR)
    end
  else
    args << classpath.flatten.join(File::PATH_SEPARATOR)
  end
  args << 'org.asciidoctor.diagram.CommandServer'

  @server = IO.popen([java, *args])
  @port = @server.readline.strip.to_i
  @client = TCPSocket.new 'localhost', port
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



11
12
13
# File 'lib/asciidoctor-diagram/util/java_socket.rb', line 11

def port
  @port
end

Instance Method Details

#ioObject



36
37
38
# File 'lib/asciidoctor-diagram/util/java_socket.rb', line 36

def io
  @client
end

#shutdownObject



40
41
42
43
# File 'lib/asciidoctor-diagram/util/java_socket.rb', line 40

def shutdown
  # KILL is a bit heavy handed, but TERM does not seem to shut down the JVM on Windows.
  Process.kill('KILL', @server.pid)
end