|
| 1 | +/** |
| 2 | + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This software is licensed under the GNU General Public License v3 or later. |
| 5 | + * |
| 6 | + * It is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or any later version. |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + */ |
| 18 | +package com.cloud.consoleproxy; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.URISyntaxException; |
| 23 | +import java.net.URL; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.apache.log4j.xml.DOMConfigurator; |
| 28 | + |
| 29 | +import com.cloud.console.Logger; |
| 30 | + |
| 31 | +public class ConsoleProxyMonitor { |
| 32 | + private static final Logger s_logger = Logger.getLogger(ConsoleProxyMonitor.class); |
| 33 | + |
| 34 | + private String[] _argv; |
| 35 | + private Map<String, String> _argMap = new HashMap<String, String>(); |
| 36 | + |
| 37 | + private volatile Process _process; |
| 38 | + private boolean _quit = false; |
| 39 | + |
| 40 | + public ConsoleProxyMonitor(String[] argv) { |
| 41 | + _argv = argv; |
| 42 | + |
| 43 | + for(String arg : _argv) { |
| 44 | + String[] tokens = arg.split("="); |
| 45 | + if(tokens.length == 2) { |
| 46 | + s_logger.info("Add argument " + tokens[0] + "=" + tokens[1] + " to the argument map"); |
| 47 | + |
| 48 | + _argMap.put(tokens[0].trim(), tokens[1].trim()); |
| 49 | + } else { |
| 50 | + s_logger.warn("unrecognized argument, skip adding it to argument map"); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private void run() { |
| 56 | + Runtime.getRuntime().addShutdownHook(new Thread() { |
| 57 | + @Override |
| 58 | + public void run() { |
| 59 | + onShutdown(); |
| 60 | + _quit = true; |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + while(!_quit) { |
| 65 | + String cmdLine = getLaunchCommandLine(); |
| 66 | + |
| 67 | + s_logger.info("Launch console proxy process with command line: " + cmdLine); |
| 68 | + |
| 69 | + try { |
| 70 | + _process = Runtime.getRuntime().exec(cmdLine); |
| 71 | + } catch (IOException e) { |
| 72 | + s_logger.error("Unexpected exception ", e); |
| 73 | + System.exit(1); |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + _process.waitFor(); |
| 78 | + } catch (InterruptedException e) { |
| 79 | + // TODO |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private String getLaunchCommandLine() { |
| 85 | + StringBuffer sb = new StringBuffer("java "); |
| 86 | + String jvmOptions = _argMap.get("jvmoptions"); |
| 87 | + |
| 88 | + if(jvmOptions != null) |
| 89 | + sb.append(jvmOptions); |
| 90 | + |
| 91 | + for(Map.Entry<String, String> entry : _argMap.entrySet()) { |
| 92 | + if(!"jvmoptions".equalsIgnoreCase(entry.getKey())) |
| 93 | + sb.append(" ").append(entry.getKey()).append("=").append(entry.getValue()); |
| 94 | + } |
| 95 | + |
| 96 | + return sb.toString(); |
| 97 | + } |
| 98 | + |
| 99 | + private void onShutdown() { |
| 100 | + } |
| 101 | + |
| 102 | + private static void configLog4j() { |
| 103 | + URL configUrl = System.class.getResource("/conf/log4j-cloud.xml"); |
| 104 | + if(configUrl == null) |
| 105 | + configUrl = System.class.getClassLoader().getSystemResource("log4j-cloud.xml"); |
| 106 | + |
| 107 | + if(configUrl == null) |
| 108 | + configUrl = System.class.getClassLoader().getSystemResource("conf/log4j-cloud.xml"); |
| 109 | + |
| 110 | + if(configUrl != null) { |
| 111 | + try { |
| 112 | + System.out.println("Configure log4j using " + configUrl.toURI().toString()); |
| 113 | + } catch (URISyntaxException e1) { |
| 114 | + e1.printStackTrace(); |
| 115 | + } |
| 116 | + |
| 117 | + try { |
| 118 | + File file = new File(configUrl.toURI()); |
| 119 | + |
| 120 | + System.out.println("Log4j configuration from : " + file.getAbsolutePath()); |
| 121 | + DOMConfigurator.configureAndWatch(file.getAbsolutePath(), 10000); |
| 122 | + } catch (URISyntaxException e) { |
| 123 | + System.out.println("Unable to convert log4j configuration Url to URI"); |
| 124 | + } |
| 125 | + } else { |
| 126 | + System.out.println("Configure log4j with default properties"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public static void main(String[] argv) { |
| 131 | + configLog4j(); |
| 132 | + |
| 133 | + (new ConsoleProxyMonitor(argv)).run(); |
| 134 | + } |
| 135 | +} |
0 commit comments