-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomcat.rb
162 lines (141 loc) · 3.99 KB
/
tomcat.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
45
46
47
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
=begin
--------------------------------------------------------------------------------
Info about this Tomcat
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
=end
require 'pathname'
require 'rexml/document'
class Tomcat
attr_reader :props
attr_reader :path
attr_reader :pid
attr_reader :port
attr_reader :jpda_port
attr_reader :max_heap
attr_reader :version
def file(filename)
return File.expand_path(filename, @path)
end
def figure_pid()
RunningTomcats.new().get_pid(self) || "0"
end
def figure_port()
begin
doc = REXML::Document.new(File.open(file('conf/server.xml')))
doc.get_elements("Server/Service/Connector[@protocol='HTTP/1.1']").each do |e|
return e.attribute('port').value()
end
rescue
puts $!
end
'unknown'
end
def figure_jpda_port()
setenv_file = File.expand_path('bin/setenv.sh', @path)
return 'none' unless File.exist?(setenv_file)
return 'none' unless /JPDA_ADDRESS=(\S+)/ =~ File.read(setenv_file)
return $1
end
def figure_max_heap()
setenv_file = File.expand_path('bin/setenv.sh', @path)
return 'default' unless File.exist?(setenv_file)
return 'default' unless /-Xmx(\S+)/ =~ File.read(setenv_file)
return $1
end
def figure_version()
release_notes = File.expand_path('RELEASE-NOTES', @path)
return '(unknown)' unless File.exist?(release_notes)
return '(unknown)' unless /Tomcat Version ([0-9.]+)/ =~ File.read(release_notes)
return $1
end
def set_props()
@props = {"tomcat_path" => @path,
"tomcat_port" => @port,
"tomcat_pid" => @pid,
"tomcat_jpda_port" => @jpda_port,
"tomcat_max_heap" => @max_heap,
"tomcat_version" => @version}
end
def running?
state != :stopped
end
def status_line()
case state()
when :stopping
state1 = "running (shutting down)"
process_id = "pid #{@pid}, "
when :starting
state1 = "running (starting up)"
process_id = "pid #{@pid}, "
when :running
state1 = "running"
process_id = "pid #{@pid}, "
else
state1 = "not running"
process_id = ""
end
"Tomcat #{@version} is #{state1}\n port #{@port}, #{process_id}jpda #{@jpda_port}, -Xmx #{@max_heap}"
end
def matches(other)
if other.path == @path
true
else
begin
other.path == Pathname.new(@path).realpath.to_s
rescue
false
end
end
end
def state()
return :stopped unless RunningTomcats.new().is_running(self)
log_file = File.expand_path('logs/catalina.out', @path)
return :running unless File.exist?(log_file)
File.readlines(log_file).reverse_each do |line|
if line.include?("A valid shutdown command was received")
return :stopping
elsif line.include?("Initializing ProtocolHandler") ||
# if we don't recognize anything since the last shutdown, we are starting.
line.include?("Destroying ProtocolHandler")
return :starting
elsif line.include?("Server startup in ")
return :running
end
end
return :running
end
def confirm()
raise SettingsError.new("Tomcat directory '#{@path}' does not exist.") unless File.exist?(@path)
raise SettingsError.new("Tomcat is not valid: unknown port") if $instance.tomcat.port == "unknown"
end
def initialize(path, pid = nil)
@path = path
@pid = pid || figure_pid()
@port = figure_port()
@jpda_port = figure_jpda_port()
@max_heap = figure_max_heap()
@version = figure_version()
@props = set_props()
end
def self.create(path)
if path
Tomcat.new(path)
else
EmptyTomcat.new()
end
end
end
class EmptyTomcat < Tomcat
def status_line()
"Tomcat is not defined."
end
def initialize()
@path = 'no current instance'
@pid = "0"
@port = 'unknown'
@jpda_port = 'unknown'
@max_heap = 'unknown'
@props = set_props()
end
end