001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/CatalinaManager.java,v 1.3 2001/07/22 20:25:13 pier Exp $
003: * $Revision: 1.3 $
004: * $Date: 2001/07/22 20:25:13 $
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 javax.management.NotificationBroadcasterSupport;
067: import javax.management.ObjectName;
068: import javax.management.MBeanServer;
069: import javax.management.MBeanRegistration;
070: import javax.management.AttributeChangeNotification;
071: import javax.management.Notification;
072:
073: /**
074: * Implementation of the Catalina JMX MBean as a wrapper of the Catalina class.
075: * To be used, the JAR containing this MBean should contain all the classes
076: * which are present in bootstrap.jar. The setPath(String path) method should
077: * be used to set the correct path where the Tomcat distribution is.
078: *
079: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
080: * @version $Revision: 1.3 $
081: */
082:
083: public final class CatalinaManager extends
084: NotificationBroadcasterSupport implements CatalinaManagerMBean,
085: MBeanRegistration {
086:
087: // ----------------------------------------------------- Instance Variables
088:
089: /**
090: * Status of the Slide domain.
091: */
092: private int state = STOPPED;
093:
094: /**
095: * Notification sequence number.
096: */
097: private long sequenceNumber = 0;
098:
099: // ---------------------------------------------- MBeanRegistration Methods
100:
101: public ObjectName preRegister(MBeanServer server, ObjectName name)
102: throws Exception {
103: return new ObjectName(OBJECT_NAME);
104: }
105:
106: public void postRegister(Boolean registrationDone) {
107: if (!registrationDone.booleanValue())
108: destroy();
109: }
110:
111: public void preDeregister() throws Exception {
112: }
113:
114: public void postDeregister() {
115: destroy();
116: }
117:
118: // ----------------------------------------------------- SlideMBean Methods
119:
120: /**
121: * Retruns the Catalina component name.
122: */
123: public String getName() {
124: return NAME;
125: }
126:
127: /**
128: * Returns the state.
129: */
130: public int getState() {
131: return state;
132: }
133:
134: /**
135: * Returns a String representation of the state.
136: */
137: public String getStateString() {
138: return states[state];
139: }
140:
141: /**
142: * Path accessor.
143: */
144: public String getPath() {
145: return System.getProperty("catalina.home");
146: }
147:
148: /**
149: * Config file path mutator.
150: */
151: public void setPath(String path) {
152: System.setProperty("catalina.home", path);
153: }
154:
155: /**
156: * Start the servlet container.
157: */
158: public void start() throws Exception {
159:
160: Notification notification = null;
161:
162: if (state != STOPPED)
163: return;
164:
165: state = STARTING;
166:
167: // Notifying the MBEan server that we're starting
168:
169: notification = new AttributeChangeNotification(this ,
170: sequenceNumber++, System.currentTimeMillis(),
171: "Starting " + NAME, "State", "java.lang.Integer",
172: new Integer(STOPPED), new Integer(STARTING));
173: sendNotification(notification);
174:
175: try {
176:
177: String[] args = { "start" };
178: Bootstrap.main(args);
179:
180: } catch (Throwable t) {
181: state = STOPPED;
182: notification = new AttributeChangeNotification(this ,
183: sequenceNumber++, System.currentTimeMillis(),
184: "Stopped " + NAME, "State", "java.lang.Integer",
185: new Integer(STARTING), new Integer(STOPPED));
186: sendNotification(notification);
187: }
188:
189: state = STARTED;
190: notification = new AttributeChangeNotification(this ,
191: sequenceNumber++, System.currentTimeMillis(),
192: "Started " + NAME, "State", "java.lang.Integer",
193: new Integer(STARTING), new Integer(STARTED));
194: sendNotification(notification);
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: String[] args = { "stop" };
219: Bootstrap.main(args);
220:
221: } catch (Throwable t) {
222:
223: // FIXME
224: t.printStackTrace();
225:
226: }
227:
228: state = STOPPED;
229:
230: notification = new AttributeChangeNotification(this ,
231: sequenceNumber++, System.currentTimeMillis(),
232: "Stopped " + NAME, "State", "java.lang.Integer",
233: new Integer(STOPPING), new Integer(STOPPED));
234: sendNotification(notification);
235:
236: }
237:
238: /**
239: * Destroy servlet container (if any is running).
240: */
241: public void destroy() {
242:
243: if (getState() != STOPPED)
244: stop();
245:
246: }
247:
248: }
|