001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/EmbeddedManager.java,v 1.4 2001/09/26 18:55:40 remm Exp $
003: * $Revision: 1.4 $
004: * $Date: 2001/09/26 18:55:40 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.startup;
065:
066: import java.net.InetAddress;
067: import org.apache.catalina.Connector;
068: import org.apache.catalina.Container;
069: import org.apache.catalina.Context;
070: import org.apache.catalina.Engine;
071: import org.apache.catalina.Host;
072: import org.apache.catalina.Lifecycle;
073: import org.apache.catalina.LifecycleEvent;
074: import org.apache.catalina.LifecycleException;
075: import org.apache.catalina.LifecycleListener;
076: import org.apache.catalina.Logger;
077: import org.apache.catalina.Realm;
078: import org.apache.catalina.connector.http.HttpConnector;
079: import javax.management.NotificationBroadcasterSupport;
080: import javax.management.ObjectName;
081: import javax.management.MBeanServer;
082: import javax.management.MBeanRegistration;
083: import javax.management.AttributeChangeNotification;
084: import javax.management.Notification;
085:
086: /**
087: * Implementation of the Catalina JMX MBean as a wrapper of the Catalina class.
088: *
089: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
090: * @version $Revision: 1.4 $
091: */
092:
093: public final class EmbeddedManager extends
094: NotificationBroadcasterSupport implements EmbeddedManagerMBean,
095: MBeanRegistration {
096:
097: // ----------------------------------------------------- Instance Variables
098:
099: /**
100: * Status of the Slide domain.
101: */
102: private int state = STOPPED;
103:
104: /**
105: * Notification sequence number.
106: */
107: private long sequenceNumber = 0;
108:
109: /**
110: * Embedded Catalina.
111: */
112: private Embedded embedded = new Embedded();
113:
114: // ---------------------------------------------- MBeanRegistration Methods
115:
116: public ObjectName preRegister(MBeanServer server, ObjectName name)
117: throws Exception {
118: return new ObjectName(OBJECT_NAME);
119: }
120:
121: public void postRegister(Boolean registrationDone) {
122: if (!registrationDone.booleanValue())
123: destroy();
124: }
125:
126: public void preDeregister() throws Exception {
127: }
128:
129: public void postDeregister() {
130: destroy();
131: }
132:
133: // ----------------------------------------------------- SlideMBean Methods
134:
135: /**
136: * Retruns the Catalina component name.
137: */
138: public String getName() {
139: return NAME;
140: }
141:
142: /**
143: * Returns the state.
144: */
145: public int getState() {
146: return state;
147: }
148:
149: /**
150: * Returns a String representation of the state.
151: */
152: public String getStateString() {
153: return states[state];
154: }
155:
156: /**
157: * Start the servlet container.
158: */
159: public void start() {
160:
161: Notification notification = null;
162:
163: if (state != STOPPED)
164: return;
165:
166: state = STARTING;
167:
168: // Notifying the MBEan server that we're starting
169:
170: notification = new AttributeChangeNotification(this ,
171: sequenceNumber++, System.currentTimeMillis(),
172: "Starting " + NAME, "State", "java.lang.Integer",
173: new Integer(STOPPED), new Integer(STARTING));
174: sendNotification(notification);
175:
176: try {
177:
178: embedded.start();
179:
180: state = STARTED;
181: notification = new AttributeChangeNotification(this ,
182: sequenceNumber++, System.currentTimeMillis(),
183: "Started " + NAME, "State", "java.lang.Integer",
184: new Integer(STARTING), new Integer(STARTED));
185: sendNotification(notification);
186:
187: } catch (Throwable t) {
188: state = STOPPED;
189: notification = new AttributeChangeNotification(this ,
190: sequenceNumber++, System.currentTimeMillis(),
191: "Stopped " + NAME, "State", "java.lang.Integer",
192: new Integer(STARTING), new Integer(STOPPED));
193: sendNotification(notification);
194: }
195:
196: }
197:
198: /**
199: * Stop the servlet container.
200: */
201: public void stop() {
202:
203: Notification notification = null;
204:
205: if (state != STARTED)
206: return;
207:
208: state = STOPPING;
209:
210: notification = new AttributeChangeNotification(this ,
211: sequenceNumber++, System.currentTimeMillis(),
212: "Stopping " + NAME, "State", "java.lang.Integer",
213: new Integer(STARTED), new Integer(STOPPING));
214: sendNotification(notification);
215:
216: try {
217:
218: embedded.stop();
219:
220: } catch (Throwable t) {
221:
222: // FIXME
223: t.printStackTrace();
224:
225: }
226:
227: state = STOPPED;
228:
229: notification = new AttributeChangeNotification(this ,
230: sequenceNumber++, System.currentTimeMillis(),
231: "Stopped " + NAME, "State", "java.lang.Integer",
232: new Integer(STOPPING), new Integer(STOPPED));
233: sendNotification(notification);
234:
235: }
236:
237: /**
238: * Destroy servlet container (if any is running).
239: */
240: public void destroy() {
241:
242: if (getState() != STOPPED)
243: stop();
244:
245: }
246:
247: /**
248: * Return the debugging detail level for this component.
249: */
250: public int getDebug() {
251: return embedded.getDebug();
252: }
253:
254: /**
255: * Set the debugging detail level for this component.
256: *
257: * @param debug The new debugging detail level
258: */
259: public void setDebug(int debug) {
260: embedded.setDebug(debug);
261: }
262:
263: /**
264: * Return true if naming is enabled.
265: */
266: public boolean isUseNaming() {
267: return embedded.isUseNaming();
268: }
269:
270: /**
271: * Enables or disables naming support.
272: *
273: * @param useNaming The new use naming value
274: */
275: public void setUseNaming(boolean useNaming) {
276: embedded.setUseNaming(useNaming);
277: }
278:
279: /**
280: * Return the Logger for this component.
281: */
282: public Logger getLogger() {
283: return embedded.getLogger();
284: }
285:
286: /**
287: * Set the Logger for this component.
288: *
289: * @param logger The new logger
290: */
291: public void setLogger(Logger logger) {
292: embedded.setLogger(logger);
293: }
294:
295: /**
296: * Return the default Realm for our Containers.
297: */
298: public Realm getRealm() {
299: return embedded.getRealm();
300: }
301:
302: /**
303: * Set the default Realm for our Containers.
304: *
305: * @param realm The new default realm
306: */
307: public void setRealm(Realm realm) {
308: embedded.setRealm(realm);
309: }
310:
311: /**
312: * Return the secure socket factory class name.
313: */
314: public String getSocketFactory() {
315: return embedded.getSocketFactory();
316: }
317:
318: /**
319: * Set the secure socket factory class name.
320: *
321: * @param socketFactory The new secure socket factory class name
322: */
323: public void setSocketFactory(String socketFactory) {
324: embedded.setSocketFactory(socketFactory);
325: }
326:
327: /**
328: * Add a new Connector to the set of defined Connectors. The newly
329: * added Connector will be associated with the most recently added Engine.
330: *
331: * @param connector The connector to be added
332: *
333: * @exception IllegalStateException if no engines have been added yet
334: */
335: public void addConnector(Connector connector) {
336: embedded.addConnector(connector);
337: }
338:
339: /**
340: * Add a new Engine to the set of defined Engines.
341: *
342: * @param engine The engine to be added
343: */
344: public void addEngine(Engine engine) {
345: embedded.addEngine(engine);
346: }
347:
348: /**
349: * Create, configure, and return a new TCP/IP socket connector
350: * based on the specified properties.
351: *
352: * @param address InetAddress to listen to, or <code>null</code>
353: * to listen on all address on this server
354: * @param port Port number to listen to
355: * @param secure Should this port be SSL-enabled?
356: */
357: public Connector createConnector(InetAddress address, int port,
358: boolean secure) {
359: return embedded.createConnector(address, port, secure);
360: }
361:
362: /**
363: * Create, configure, and return a Context that will process all
364: * HTTP requests received from one of the associated Connectors,
365: * and directed to the specified context path on the virtual host
366: * to which this Context is connected.
367: * <p>
368: * After you have customized the properties, listeners, and Valves
369: * for this Context, you must attach it to the corresponding Host
370: * by calling:
371: * <pre>
372: * host.addChild(context);
373: * </pre>
374: * which will also cause the Context to be started if the Host has
375: * already been started.
376: *
377: * @param path Context path of this application ("" for the default
378: * application for this host, must start with a slash otherwise)
379: * @param docBase Absolute pathname to the document base directory
380: * for this web application
381: *
382: * @exception IllegalArgumentException if an invalid parameter
383: * is specified
384: */
385: public Context createContext(String path, String docBase) {
386: return embedded.createContext(path, docBase);
387: }
388:
389: /**
390: * Create, configure, and return an Engine that will process all
391: * HTTP requests received from one of the associated Connectors,
392: * based on the specified properties.
393: */
394: public Engine createEngine() {
395: return embedded.createEngine();
396: }
397:
398: /**
399: * Create, configure, and return a Host that will process all
400: * HTTP requests received from one of the associated Connectors,
401: * and directed to the specified virtual host.
402: * <p>
403: * After you have customized the properties, listeners, and Valves
404: * for this Host, you must attach it to the corresponding Engine
405: * by calling:
406: * <pre>
407: * engine.addChild(host);
408: * </pre>
409: * which will also cause the Host to be started if the Engine has
410: * already been started. If this is the default (or only) Host you
411: * will be defining, you may also tell the Engine to pass all requests
412: * not assigned to another virtual host to this one:
413: * <pre>
414: * engine.setDefaultHost(host.getName());
415: * </pre>
416: *
417: * @param name Canonical name of this virtual host
418: * @param appBase Absolute pathname to the application base directory
419: * for this virtual host
420: *
421: * @exception IllegalArgumentException if an invalid parameter
422: * is specified
423: */
424: public Host createHost(String name, String appBase) {
425: return embedded.createHost(name, appBase);
426: }
427:
428: /**
429: * Return descriptive information about this Server implementation and
430: * the corresponding version number, in the format
431: * <code><description>/<version></code>.
432: */
433: public String getInfo() {
434: return embedded.getInfo();
435: }
436:
437: /**
438: * Remove the specified Connector from the set of defined Connectors.
439: *
440: * @param connector The Connector to be removed
441: */
442: public void removeConnector(Connector connector) {
443: embedded.removeConnector(connector);
444: }
445:
446: /**
447: * Remove the specified Context from the set of defined Contexts for its
448: * associated Host. If this is the last Context for this Host, the Host
449: * will also be removed.
450: *
451: * @param context The Context to be removed
452: */
453: public void removeContext(Context context) {
454: embedded.removeContext(context);
455: }
456:
457: /**
458: * Remove the specified Engine from the set of defined Engines, along with
459: * all of its related Hosts and Contexts. All associated Connectors are
460: * also removed.
461: *
462: * @param engine The Engine to be removed
463: */
464: public void removeEngine(Engine engine) {
465: embedded.removeEngine(engine);
466: }
467:
468: /**
469: * Remove the specified Host, along with all of its related Contexts,
470: * from the set of defined Hosts for its associated Engine. If this is
471: * the last Host for this Engine, the Engine will also be removed.
472: *
473: * @param host The Host to be removed
474: */
475: public void removeHost(Host host) {
476: embedded.removeHost(host);
477: }
478:
479: }
|