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.program.ConfigProgram;
033: import com.caucho.config.ConfigException;
034: import com.caucho.lifecycle.Lifecycle;
035: import com.caucho.management.server.AbstractManagedObject;
036: import com.caucho.management.server.WatchdogMXBean;
037: import com.caucho.server.port.Port;
038: import com.caucho.util.*;
039: import com.caucho.vfs.Path;
040: import com.caucho.vfs.Vfs;
041:
042: import java.lang.reflect.*;
043: import java.net.*;
044: import java.util.ArrayList;
045: import java.util.Date;
046: import java.util.logging.Logger;
047:
048: /**
049: * Thread responsible for watching a backend server.
050: */
051: public class Watchdog {
052: private static final L10N L = new L10N(Watchdog.class);
053: private static final Logger log = Logger.getLogger(Watchdog.class
054: .getName());
055:
056: private final String _id;
057:
058: private WatchdogConfig _config;
059:
060: private boolean _isSingle;
061: private WatchdogTask _task;
062:
063: // statistics
064: private Date _initialStartTime;
065: private Date _lastStartTime;
066: private int _startCount;
067: private WatchdogAdmin _admin;
068:
069: Watchdog(String id, WatchdogArgs args) {
070: _id = id;
071: _config = new WatchdogConfig(args);
072:
073: _admin = new WatchdogAdmin();
074: }
075:
076: Watchdog(WatchdogConfig config) {
077: _id = config.getId();
078: _config = config;
079:
080: _admin = new WatchdogAdmin();
081: }
082:
083: /**
084: * Returns the server id of the watchdog.
085: */
086: public String getId() {
087: return _id;
088: }
089:
090: /**
091: * Returns the watchdog arguments.
092: */
093: WatchdogArgs getArgs() {
094: return _config.getArgs();
095: }
096:
097: /**
098: * Returns the java startup args
099: */
100: String[] getArgv() {
101: return _config.getArgv();
102: }
103:
104: /**
105: * Returns the config state of the watchdog
106: */
107: public WatchdogConfig getConfig() {
108: return _config;
109: }
110:
111: /**
112: * Sets the config state of the watchdog
113: */
114: public void setConfig(WatchdogConfig config) {
115: _config = config;
116: }
117:
118: /**
119: * Returns the JAVA_HOME for the Resin instance
120: */
121: public Path getJavaHome() {
122: return _config.getJavaHome();
123: }
124:
125: /**
126: * Returns the location of the java executable
127: */
128: public String getJavaExe() {
129: return _config.getJavaExe();
130: }
131:
132: /**
133: * Returns the JVM arguments for the instance
134: */
135: public ArrayList<String> getJvmArgs() {
136: return _config.getJvmArgs();
137: }
138:
139: /**
140: * Returns the setuid user name.
141: */
142: public String getUserName() {
143: return _config.getUserName();
144: }
145:
146: /**
147: * Returns the setgid group name.
148: */
149: public String getGroupName() {
150: return _config.getGroupName();
151: }
152:
153: /**
154: * Returns true for a standalone start.
155: */
156: public boolean isSingle() {
157: return _isSingle;
158: }
159:
160: /**
161: * Returns the jvm-foo-log.log file path
162: */
163: public Path getLogPath() {
164: return _config.getLogPath();
165: }
166:
167: /**
168: * Returns the maximum time to wait for a shutdown
169: */
170: public long getShutdownWaitTime() {
171: return _config.getShutdownWaitTime();
172: }
173:
174: /**
175: * Returns the watchdog-port for this watchdog instance
176: */
177: public int getWatchdogPort() {
178: return _config.getWatchdogPort();
179: }
180:
181: Iterable<Port> getPorts() {
182: return _config.getPorts();
183: }
184:
185: Path getPwd() {
186: return _config.getPwd();
187: }
188:
189: Path getResinHome() {
190: return _config.getResinHome();
191: }
192:
193: Path getResinRoot() {
194: return _config.getResinRoot();
195: }
196:
197: Path getResinConf() {
198: return _config.getResinConf();
199: }
200:
201: boolean hasXmx() {
202: return _config.hasXmx();
203: }
204:
205: boolean hasXss() {
206: return _config.hasXss();
207: }
208:
209: boolean is64bit() {
210: return _config.is64bit();
211: }
212:
213: public String getState() {
214: WatchdogTask task = _task;
215:
216: if (task == null)
217: return "inactive";
218: else
219: return task.getState();
220: }
221:
222: boolean isVerbose() {
223: return _config.isVerbose();
224: }
225:
226: public int startSingle() {
227: if (_task != null)
228: return -1;
229:
230: _isSingle = true;
231: _task = new WatchdogTask(this );
232:
233: _task.start();
234:
235: return 1;
236: }
237:
238: /**
239: * Starts the watchdog instance.
240: */
241: public void start() {
242: WatchdogTask task = null;
243:
244: synchronized (this ) {
245: if (_task != null)
246: throw new IllegalStateException(
247: L
248: .l(
249: "Can't start new task because of old task '{0}'",
250: _task));
251:
252: task = new WatchdogTask(this );
253: _task = task;
254: }
255:
256: task.start();
257: }
258:
259: /**
260: * Stops the watchdog instance
261: */
262: public void stop() {
263: WatchdogTask task = _task;
264:
265: if (task != null)
266: task.stop();
267: }
268:
269: /**
270: * Kills the watchdog instance
271: */
272: public void kill() {
273: WatchdogTask task = _task;
274: _task = null;
275:
276: if (task != null)
277: task.kill();
278: }
279:
280: void notifyTaskStarted() {
281: _startCount++;
282: _lastStartTime = new Date(Alarm.getExactTime());
283:
284: if (_initialStartTime == null)
285: _initialStartTime = _lastStartTime;
286: }
287:
288: void completeTask(WatchdogTask task) {
289: synchronized (this ) {
290: if (_task == task)
291: _task = null;
292: }
293: }
294:
295: @Override
296: public String toString() {
297: return getClass().getSimpleName() + "[" + getId() + "]";
298: }
299:
300: class WatchdogAdmin extends AbstractManagedObject implements
301: WatchdogMXBean {
302: WatchdogAdmin() {
303: registerSelf();
304: }
305:
306: public String getId() {
307: return Watchdog.this .getId();
308: }
309:
310: public String getName() {
311: return getId();
312: }
313:
314: @Override
315: public String getType() {
316: return "Watchdog";
317: }
318:
319: public String getResinHome() {
320: return Watchdog.this .getResinHome().getNativePath();
321: }
322:
323: public String getResinRoot() {
324: return Watchdog.this .getResinRoot().getNativePath();
325: }
326:
327: public String getResinConf() {
328: return Watchdog.this .getResinConf().getNativePath();
329: }
330:
331: public String getUserName() {
332: String userName = Watchdog.this .getUserName();
333:
334: if (userName != null)
335: return userName;
336: else
337: return System.getProperty("user.name");
338: }
339:
340: public String getState() {
341: WatchdogTask task = _task;
342:
343: if (task == null)
344: return "inactive";
345: else
346: return task.getState();
347: }
348:
349: //
350: // statistics
351: //
352:
353: public Date getInitialStartTime() {
354: return _initialStartTime;
355: }
356:
357: public Date getStartTime() {
358: return _lastStartTime;
359: }
360:
361: public int getStartCount() {
362: return _startCount;
363: }
364:
365: //
366: // operations
367: //
368:
369: public void start() {
370: Watchdog.this .start();
371: }
372:
373: public void stop() {
374: Watchdog.this .stop();
375: }
376:
377: public void kill() {
378: Watchdog.this.kill();
379: }
380: }
381: }
|