forked from AnythingLinux/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleProxyMonitor.java
More file actions
153 lines (126 loc) · 5.31 KB
/
ConsoleProxyMonitor.java
File metadata and controls
153 lines (126 loc) · 5.31 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.consoleproxy;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.xml.DOMConfigurator;
import com.cloud.consoleproxy.util.Logger;
//
//
// I switched to a simpler solution to monitor only unrecoverable exceptions, under these cases, console proxy process will exit
// itself and the shell script will re-launch console proxy
//
public class ConsoleProxyMonitor {
private static final Logger s_logger = Logger.getLogger(ConsoleProxyMonitor.class);
private String[] _argv;
private Map<String, String> _argMap = new HashMap<String, String>();
private volatile Process _process;
private boolean _quit = false;
public ConsoleProxyMonitor(String[] argv) {
_argv = argv;
for(String arg : _argv) {
String[] tokens = arg.split("=");
if(tokens.length == 2) {
s_logger.info("Add argument " + tokens[0] + "=" + tokens[1] + " to the argument map");
_argMap.put(tokens[0].trim(), tokens[1].trim());
} else {
s_logger.warn("unrecognized argument, skip adding it to argument map");
}
}
}
private void run() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
_quit = true;
onShutdown();
}
});
while(!_quit) {
String cmdLine = getLaunchCommandLine();
s_logger.info("Launch console proxy process with command line: " + cmdLine);
try {
_process = Runtime.getRuntime().exec(cmdLine);
} catch (IOException e) {
s_logger.error("Unexpected exception ", e);
System.exit(1);
}
boolean waitSucceeded = false;
int exitCode = 0;
while(!waitSucceeded) {
try {
exitCode = _process.waitFor();
waitSucceeded = true;
if(s_logger.isInfoEnabled())
s_logger.info("Console proxy process exits with code: " + exitCode);
} catch (InterruptedException e) {
if(s_logger.isInfoEnabled())
s_logger.info("InterruptedException while waiting for termination of console proxy, will retry");
}
}
}
}
private String getLaunchCommandLine() {
StringBuffer sb = new StringBuffer("java ");
String jvmOptions = _argMap.get("jvmoptions");
if(jvmOptions != null)
sb.append(jvmOptions);
for(Map.Entry<String, String> entry : _argMap.entrySet()) {
if(!"jvmoptions".equalsIgnoreCase(entry.getKey()))
sb.append(" ").append(entry.getKey()).append("=").append(entry.getValue());
}
return sb.toString();
}
private void onShutdown() {
if(_process != null) {
if(s_logger.isInfoEnabled())
s_logger.info("Console proxy monitor shuts dwon, terminate console proxy process");
_process.destroy();
}
}
private static void configLog4j() {
URL configUrl = System.class.getResource("/conf/log4j-cloud.xml");
if(configUrl == null)
configUrl = ClassLoader.getSystemResource("log4j-cloud.xml");
if(configUrl == null)
configUrl = ClassLoader.getSystemResource("conf/log4j-cloud.xml");
if(configUrl != null) {
try {
System.out.println("Configure log4j using " + configUrl.toURI().toString());
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
try {
File file = new File(configUrl.toURI());
System.out.println("Log4j configuration from : " + file.getAbsolutePath());
DOMConfigurator.configureAndWatch(file.getAbsolutePath(), 10000);
} catch (URISyntaxException e) {
System.out.println("Unable to convert log4j configuration Url to URI");
}
} else {
System.out.println("Configure log4j with default properties");
}
}
public static void main(String[] argv) {
configLog4j();
(new ConsoleProxyMonitor(argv)).run();
}
}