001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/NamingService.java,v 1.2 2001/11/22 16:48:32 remm Exp $
003: * $Revision: 1.2 $
004: * $Date: 2001/11/22 16:48:32 $
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.naming;
065:
066: import javax.naming.Context;
067: import javax.management.NotificationBroadcasterSupport;
068: import javax.management.ObjectName;
069: import javax.management.MBeanServer;
070: import javax.management.MBeanRegistration;
071: import javax.management.AttributeChangeNotification;
072: import javax.management.Notification;
073:
074: /**
075: * Implementation of the NamingService JMX MBean.
076: *
077: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
078: * @version $Revision: 1.2 $
079: */
080:
081: public final class NamingService extends NotificationBroadcasterSupport
082: implements NamingServiceMBean, MBeanRegistration {
083:
084: // ----------------------------------------------------- Instance Variables
085:
086: /**
087: * Status of the Slide domain.
088: */
089: private int state = STOPPED;
090:
091: /**
092: * Notification sequence number.
093: */
094: private long sequenceNumber = 0;
095:
096: /**
097: * Old URL packages value.
098: */
099: private String oldUrlValue = "";
100:
101: /**
102: * Old initial context value.
103: */
104: private String oldIcValue = "";
105:
106: // ---------------------------------------------- MBeanRegistration Methods
107:
108: public ObjectName preRegister(MBeanServer server, ObjectName name)
109: throws Exception {
110: return new ObjectName(OBJECT_NAME);
111: }
112:
113: public void postRegister(Boolean registrationDone) {
114: if (!registrationDone.booleanValue())
115: destroy();
116: }
117:
118: public void preDeregister() throws Exception {
119: }
120:
121: public void postDeregister() {
122: destroy();
123: }
124:
125: // ----------------------------------------------------- SlideMBean Methods
126:
127: /**
128: * Retruns the Catalina component name.
129: */
130: public String getName() {
131: return NAME;
132: }
133:
134: /**
135: * Returns the state.
136: */
137: public int getState() {
138: return state;
139: }
140:
141: /**
142: * Returns a String representation of the state.
143: */
144: public String getStateString() {
145: return states[state];
146: }
147:
148: /**
149: * Start the servlet container.
150: */
151: public void start() throws Exception {
152:
153: Notification notification = null;
154:
155: if (state != STOPPED)
156: return;
157:
158: state = STARTING;
159:
160: // Notifying the MBEan server that we're starting
161:
162: notification = new AttributeChangeNotification(this ,
163: sequenceNumber++, System.currentTimeMillis(),
164: "Starting " + NAME, "State", "java.lang.Integer",
165: new Integer(STOPPED), new Integer(STARTING));
166: sendNotification(notification);
167:
168: try {
169:
170: String value = "org.apache.naming";
171: String oldValue = System
172: .getProperty(Context.URL_PKG_PREFIXES);
173: if (oldValue != null) {
174: oldUrlValue = oldValue;
175: value = oldValue + ":" + value;
176: }
177: System.setProperty(Context.URL_PKG_PREFIXES, value);
178:
179: oldValue = System
180: .getProperty(Context.INITIAL_CONTEXT_FACTORY);
181: if (oldValue != null) {
182: oldIcValue = oldValue;
183: } else {
184: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
185: Constants.Package
186: + ".java.javaURLContextFactory");
187: }
188:
189: } catch (Throwable t) {
190: state = STOPPED;
191: notification = new AttributeChangeNotification(this ,
192: sequenceNumber++, System.currentTimeMillis(),
193: "Stopped " + NAME, "State", "java.lang.Integer",
194: new Integer(STARTING), new Integer(STOPPED));
195: sendNotification(notification);
196: }
197:
198: state = STARTED;
199: notification = new AttributeChangeNotification(this ,
200: sequenceNumber++, System.currentTimeMillis(),
201: "Started " + NAME, "State", "java.lang.Integer",
202: new Integer(STARTING), new Integer(STARTED));
203: sendNotification(notification);
204:
205: }
206:
207: /**
208: * Stop the servlet container.
209: */
210: public void stop() {
211:
212: Notification notification = null;
213:
214: if (state != STARTED)
215: return;
216:
217: state = STOPPING;
218:
219: notification = new AttributeChangeNotification(this ,
220: sequenceNumber++, System.currentTimeMillis(),
221: "Stopping " + NAME, "State", "java.lang.Integer",
222: new Integer(STARTED), new Integer(STOPPING));
223: sendNotification(notification);
224:
225: try {
226:
227: System.setProperty(Context.URL_PKG_PREFIXES, oldUrlValue);
228: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
229: oldIcValue);
230:
231: } catch (Throwable t) {
232:
233: // FIXME
234: t.printStackTrace();
235:
236: }
237:
238: state = STOPPED;
239:
240: notification = new AttributeChangeNotification(this ,
241: sequenceNumber++, System.currentTimeMillis(),
242: "Stopped " + NAME, "State", "java.lang.Integer",
243: new Integer(STOPPING), new Integer(STOPPED));
244: sendNotification(notification);
245:
246: }
247:
248: /**
249: * Destroy servlet container (if any is running).
250: */
251: public void destroy() {
252:
253: if (getState() != STOPPED)
254: stop();
255:
256: }
257:
258: }
|