001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.boot;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.hessian.server.HessianServlet;
034: import com.caucho.util.*;
035:
036: import java.util.logging.Logger;
037:
038: /**
039: * Process responsible for watching a backend server.
040: */
041: public class WatchdogServlet extends HessianServlet implements
042: WatchdogAPI {
043: private final static L10N L = new L10N(WatchdogServlet.class);
044: private static final Logger log = Logger
045: .getLogger(WatchdogServlet.class.getName());
046:
047: private WatchdogManager _watchdogManager;
048:
049: @Override
050: public void init() {
051: _watchdogManager = WatchdogManager.getWatchdog();
052: }
053:
054: public String status(String password) throws ConfigException,
055: IllegalStateException {
056: if (!_watchdogManager.authenticate(password)) {
057: log.warning("watchdog status authentication failure");
058: throw new ConfigException(
059: L
060: .l("watchdog start forbidden - authentication failed."));
061: }
062:
063: return _watchdogManager.status();
064: }
065:
066: public void start(String password, String[] argv)
067: throws ConfigException, IllegalStateException {
068: if (!_watchdogManager.authenticate(password)) {
069: log.warning("watchdog start authentication failure");
070: throw new ConfigException(
071: L
072: .l("watchdog start forbidden - authentication failed."));
073: }
074:
075: _watchdogManager.startServer(argv);
076: }
077:
078: public void restart(String password, String serverId, String[] argv) {
079: if (!_watchdogManager.authenticate(password)) {
080: log.warning("watchdog restart authentication failure");
081: throw new ConfigException(
082: L
083: .l("watchdog restart forbidden - authentication failed"));
084: }
085:
086: _watchdogManager.restartServer(serverId, argv);
087: }
088:
089: public void stop(String password, String serverId) {
090: if (!_watchdogManager.authenticate(password)) {
091: log.warning("watchdog stop authentication failure");
092: throw new ConfigException(
093: L
094: .l("watchdog stop forbidden - authentication failed"));
095: }
096:
097: log.info("Watchdog stop: " + serverId);
098:
099: _watchdogManager.stopServer(serverId);
100: }
101:
102: public void kill(String password, String serverId) {
103: if (!_watchdogManager.authenticate(password)) {
104: log.warning("watchdog kill authentication failure");
105: throw new ConfigException(
106: L
107: .l("watchdog kill forbidden - authentication failed"));
108: }
109:
110: log.info("Watchdog kill: " + serverId);
111:
112: _watchdogManager.killServer(serverId);
113: }
114:
115: public boolean shutdown(String password) {
116: if (!_watchdogManager.authenticate(password)) {
117: log.warning("watchdog stop authentication failure");
118: throw new ConfigException(
119: L
120: .l("watchdog shutdown forbidden - authentication failed"));
121: }
122:
123: log.info("Watchdog shutdown");
124:
125: new Thread(new Shutdown()).start();
126:
127: return true;
128: }
129:
130: static class Shutdown implements Runnable {
131: public void run() {
132: try {
133: Thread.sleep(1000);
134: } catch (Exception e) {
135: }
136:
137: System.exit(0);
138: }
139: }
140: }
|