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.lifecycle.Lifecycle;
033: import com.caucho.util.*;
034: import com.caucho.vfs.Path;
035:
036: import java.lang.reflect.*;
037: import java.net.*;
038: import java.util.Date;
039: import java.util.logging.*;
040:
041: /**
042: * Thread responsible for watching a backend server.
043: */
044: class WatchdogTask implements Runnable {
045: private static final L10N L = new L10N(WatchdogTask.class);
046: private static final Logger log = Logger
047: .getLogger(WatchdogTask.class.getName());
048:
049: private final Watchdog _watchdog;
050:
051: private final Lifecycle _lifecycle = new Lifecycle();
052:
053: private WatchdogProcess _process;
054:
055: WatchdogTask(Watchdog watchdog) {
056: _watchdog = watchdog;
057: }
058:
059: boolean isActive() {
060: return _lifecycle.isActive();
061: }
062:
063: /**
064: * Returns the state name.
065: */
066: String getState() {
067: return _lifecycle.getStateName();
068: }
069:
070: public void start() {
071: if (!_lifecycle.toActive())
072: return;
073:
074: Thread thread = new Thread(this , "watchdog-"
075: + _watchdog.getId());
076: thread.setDaemon(false);
077:
078: thread.start();
079: }
080:
081: public void stop() {
082: if (!_lifecycle.toDestroy())
083: return;
084:
085: WatchdogProcess process = _process;
086: if (process != null)
087: process.stop();
088: }
089:
090: public void run() {
091: try {
092: int i = 0;
093: long retry = Long.MAX_VALUE;
094:
095: while (_lifecycle.isActive() && i++ < retry) {
096: String id = String.valueOf(i);
097:
098: _watchdog.notifyTaskStarted();
099:
100: _process = new WatchdogProcess(id, _watchdog);
101: try {
102: _process.run();
103: } catch (Exception e) {
104: log.log(Level.WARNING, e.toString(), e);
105: } finally {
106: _process.destroy();
107: }
108: }
109: } finally {
110: _lifecycle.toDestroy();
111:
112: _watchdog.completeTask(this );
113: }
114: }
115:
116: /**
117: * kills the task
118: */
119: void kill() {
120: WatchdogProcess process = _process;
121: _process = null;
122:
123: if (process != null)
124: process.destroy();
125: }
126:
127: @Override
128: public String toString() {
129: return getClass().getSimpleName() + "[" + _watchdog + "]";
130: }
131: }
|