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.Config;
033: import com.caucho.config.ConfigException;
034: import com.caucho.loader.*;
035: import com.caucho.server.resin.ResinELContext;
036: import com.caucho.util.L10N;
037: import com.caucho.vfs.Path;
038: import com.caucho.vfs.Vfs;
039: import com.caucho.Version;
040:
041: import com.caucho.webbeans.manager.WebBeansContainer;
042: import java.io.*;
043: import java.lang.management.*;
044: import java.util.*;
045: import java.util.logging.Level;
046: import java.util.logging.Logger;
047:
048: import javax.management.*;
049:
050: /**
051: * ResinBoot is the main bootstrap class for Resin. It parses the
052: * resin.conf and looks for the <server> block matching the -server
053: * argument.
054: *
055: * <h3>Start Modes:</h3>
056: *
057: * The start modes are STATUS, DIRECT, START, STOP, KILL, RESTART, SHUTDOWN.
058: *
059: * <ul>
060: * <li>DIRECT starts a <server> from the command line
061: * <li>START starts a <server> with a Watchdog in the background
062: * <li>STOP stop the <server> Resin in the background
063: * </ul>
064: */
065: public class ResinBoot {
066: private static L10N _L;
067: private static Logger _log;
068:
069: private WatchdogArgs _args;
070:
071: private WatchdogClient _client;
072:
073: ResinBoot(String[] argv) throws Exception {
074: _args = new WatchdogArgs(argv);
075:
076: Path resinHome = _args.getResinHome();
077:
078: ClassLoader loader = ProLoader.create(resinHome);
079: if (loader != null) {
080: System.setProperty("resin.home", resinHome.getNativePath());
081:
082: Thread.currentThread().setContextClassLoader(loader);
083:
084: Vfs.initJNI();
085:
086: resinHome = Vfs.lookup(resinHome.getFullPath());
087: _args.setResinHome(resinHome);
088: }
089:
090: Environment.init();
091:
092: // required for license check
093: System.setProperty("resin.home", resinHome.getNativePath());
094:
095: // watchdog/0210
096: // Vfs.setPwd(_rootDirectory);
097:
098: if (!_args.getResinConf().canRead()) {
099: throw new ConfigException(L().l(
100: "Resin/{0} can't open configuration file '{1}'",
101: Version.VERSION,
102: _args.getResinConf().getNativePath()));
103: }
104:
105: // XXX: set _isResinProfessional
106:
107: Config config = new Config();
108: BootManager bootManager = new BootManager(_args);
109:
110: ResinELContext elContext = _args.getELContext();
111:
112: /**
113: * XXX: the following setVar calls should not be necessary, but the
114: * EL.setEnviornment() call above is not effective:
115: */
116: WebBeansContainer webBeans = WebBeansContainer.create();
117: webBeans.addSingletonByName(elContext.getResinHome(),
118: "resinHome");
119: webBeans.addSingletonByName(elContext.getJavaVar(), "java");
120: webBeans.addSingletonByName(elContext.getResinVar(), "resin");
121: webBeans.addSingletonByName(elContext.getServerVar(), "server");
122:
123: config.configure(bootManager, _args.getResinConf(),
124: "com/caucho/server/resin/resin.rnc");
125:
126: _client = bootManager.findClient(_args.getServerId());
127:
128: if (_client == null)
129: throw new ConfigException(
130: L()
131: .l(
132: "Resin/{0}: -server '{1}' does not match any defined <server>\nin {2}.",
133: Version.VERSION,
134: _args.getServerId(),
135: _args.getResinConf()));
136:
137: Path logDirectory = bootManager.getLogDirectory();
138: if (!logDirectory.exists()) {
139: logDirectory.mkdirs();
140:
141: if (_client.getUserName() != null)
142: logDirectory.changeOwner(_client.getUserName());
143:
144: if (_client.getGroupName() != null)
145: logDirectory.changeOwner(_client.getGroupName());
146: }
147: }
148:
149: boolean start() throws Exception {
150: if (_args.isStatus()) {
151: try {
152: String status = _client.statusWatchdog();
153:
154: System.out
155: .println(L()
156: .l(
157: "Resin/{0} status for watchdog at 127.0.0.1:{2}",
158: Version.VERSION,
159: _client.getId(),
160: _client.getWatchdogPort()));
161: System.out.println(status);
162: } catch (Exception e) {
163: System.out
164: .println(L()
165: .l(
166: "Resin/{0} can't start -server '{1}' for watchdog at 127.0.0.1:{2}.\n{3}",
167: Version.VERSION,
168: _client.getId(),
169: _client.getWatchdogPort(),
170: e.toString()));
171:
172: log().log(Level.FINE, e.toString(), e);
173:
174: System.exit(1);
175: }
176:
177: return false;
178: } else if (_args.isStart()) {
179: try {
180: _client.startWatchdog(_args.getArgv());
181:
182: System.out
183: .println(L()
184: .l(
185: "Resin/{0} started -server '{1}' for watchdog at 127.0.0.1:{2}",
186: Version.VERSION,
187: _client.getId(),
188: _client.getWatchdogPort()));
189: } catch (Exception e) {
190: System.out
191: .println(L()
192: .l(
193: "Resin/{0} can't start -server '{1}' for watchdog at 127.0.0.1:{2}.\n{3}",
194: Version.VERSION,
195: _client.getId(),
196: _client.getWatchdogPort(),
197: e.toString()));
198:
199: log().log(Level.FINE, e.toString(), e);
200:
201: System.exit(1);
202: }
203:
204: return false;
205: } else if (_args.isStop()) {
206: try {
207: _client.stopWatchdog();
208:
209: System.out
210: .println(L()
211: .l(
212: "Resin/{0} stopped -server '{1}' for watchdog at 127.0.0.1:{2}",
213: Version.VERSION,
214: _client.getId(),
215: _client.getWatchdogPort()));
216: } catch (Exception e) {
217: System.out
218: .println(L()
219: .l(
220: "Resin/{0} can't stop -server '{1}' for watchdog at 127.0.0.1:{2}.\n{3}",
221: Version.VERSION,
222: _client.getId(),
223: _client.getWatchdogPort(),
224: e.toString()));
225:
226: log().log(Level.FINE, e.toString(), e);
227:
228: System.exit(1);
229: }
230:
231: return false;
232: } else if (_args.isKill()) {
233: try {
234: _client.killWatchdog();
235:
236: System.out
237: .println(L()
238: .l(
239: "Resin/{0} killed -server '{1}' for watchdog at 127.0.0.1:{2}",
240: Version.VERSION,
241: _client.getId(),
242: _client.getWatchdogPort()));
243: } catch (Exception e) {
244: System.out
245: .println(L()
246: .l(
247: "Resin/{0} can't kill -server '{1}' for watchdog at 127.0.0.1:{2}.\n{3}",
248: Version.VERSION,
249: _client.getId(),
250: _client.getWatchdogPort(),
251: e.toString()));
252:
253: log().log(Level.FINE, e.toString(), e);
254:
255: System.exit(1);
256: }
257:
258: return false;
259: } else if (_args.isRestart()) {
260: try {
261: _client.restartWatchdog(_args.getArgv());
262:
263: System.out
264: .println(L()
265: .l(
266: "Resin/{0} stopped -server '{1}' for watchdog at 127.0.0.1:{2}",
267: Version.VERSION,
268: _client.getId(),
269: _client.getWatchdogPort()));
270: } catch (Exception e) {
271: System.out
272: .println(L()
273: .l(
274: "Resin/{0} can't restart -server '{1}'.\n{2}",
275: Version.VERSION,
276: _client.getId(), e.toString()));
277:
278: log().log(Level.FINE, e.toString(), e);
279:
280: System.exit(1);
281: }
282:
283: return false;
284: } else if (_args.isShutdown()) {
285: try {
286: _client.shutdown();
287:
288: System.err.println(L().l(
289: "Resin/{0} shutdown ResinWatchdogManager",
290: Version.VERSION));
291: } catch (Exception e) {
292: System.err
293: .println(L()
294: .l(
295: "Resin/{0} can't shutdown ResinWatchdogManager.\n{1}",
296: Version.VERSION, e.toString()));
297:
298: log().log(Level.FINE, e.toString(), e);
299:
300: System.exit(1);
301: }
302:
303: return false;
304: } else if (_args.isSingle()) {
305: return _client.startSingle() != 0;
306: } else {
307: throw new IllegalStateException(L().l("Unknown start mode"));
308: }
309: }
310:
311: /**
312: * The main start of the web server.
313: *
314: * <pre>
315: * -conf resin.conf : alternate configuration file
316: * -server web-a : <server> to start
317: * <pre>
318: */
319:
320: public static void main(String[] argv) {
321: try {
322: ResinBoot boot = new ResinBoot(argv);
323:
324: while (boot.start()) {
325: try {
326: synchronized (boot) {
327: boot.wait(5000);
328: }
329: } catch (Exception e) {
330: }
331: }
332: } catch (Exception e) {
333: if (e instanceof ConfigException) {
334: System.out.println(e.getMessage());
335:
336: System.exit(2);
337: } else {
338: e.printStackTrace();
339:
340: System.exit(3);
341: }
342: }
343: }
344:
345: private static L10N L() {
346: if (_L == null)
347: _L = new L10N(ResinBoot.class);
348:
349: return _L;
350: }
351:
352: private static Logger log() {
353: if (_log == null)
354: _log = Logger.getLogger(ResinBoot.class.getName());
355:
356: return _log;
357: }
358: }
|