001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming;
018:
019: import javax.naming.Context;
020: import javax.management.NotificationBroadcasterSupport;
021: import javax.management.ObjectName;
022: import javax.management.MBeanServer;
023: import javax.management.MBeanRegistration;
024: import javax.management.AttributeChangeNotification;
025: import javax.management.Notification;
026:
027: /**
028: * Implementation of the NamingService JMX MBean.
029: *
030: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
031: * @version $Revision: 1.2.2.1 $
032: */
033:
034: public final class NamingService extends NotificationBroadcasterSupport
035: implements NamingServiceMBean, MBeanRegistration {
036:
037: // ----------------------------------------------------- Instance Variables
038:
039: /**
040: * Status of the Slide domain.
041: */
042: private int state = STOPPED;
043:
044: /**
045: * Notification sequence number.
046: */
047: private long sequenceNumber = 0;
048:
049: /**
050: * Old URL packages value.
051: */
052: private String oldUrlValue = "";
053:
054: /**
055: * Old initial context value.
056: */
057: private String oldIcValue = "";
058:
059: // ---------------------------------------------- MBeanRegistration Methods
060:
061: public ObjectName preRegister(MBeanServer server, ObjectName name)
062: throws Exception {
063: return new ObjectName(OBJECT_NAME);
064: }
065:
066: public void postRegister(Boolean registrationDone) {
067: if (!registrationDone.booleanValue())
068: destroy();
069: }
070:
071: public void preDeregister() throws Exception {
072: }
073:
074: public void postDeregister() {
075: destroy();
076: }
077:
078: // ----------------------------------------------------- SlideMBean Methods
079:
080: /**
081: * Retruns the Catalina component name.
082: */
083: public String getName() {
084: return NAME;
085: }
086:
087: /**
088: * Returns the state.
089: */
090: public int getState() {
091: return state;
092: }
093:
094: /**
095: * Returns a String representation of the state.
096: */
097: public String getStateString() {
098: return states[state];
099: }
100:
101: /**
102: * Start the servlet container.
103: */
104: public void start() throws Exception {
105:
106: Notification notification = null;
107:
108: if (state != STOPPED)
109: return;
110:
111: state = STARTING;
112:
113: // Notifying the MBEan server that we're starting
114:
115: notification = new AttributeChangeNotification(this ,
116: sequenceNumber++, System.currentTimeMillis(),
117: "Starting " + NAME, "State", "java.lang.Integer",
118: new Integer(STOPPED), new Integer(STARTING));
119: sendNotification(notification);
120:
121: try {
122:
123: String value = "org.apache.naming";
124: String oldValue = System
125: .getProperty(Context.URL_PKG_PREFIXES);
126: if (oldValue != null) {
127: oldUrlValue = oldValue;
128: value = oldValue + ":" + value;
129: }
130: System.setProperty(Context.URL_PKG_PREFIXES, value);
131:
132: oldValue = System
133: .getProperty(Context.INITIAL_CONTEXT_FACTORY);
134: if ((oldValue != null) && (oldValue.length() > 0)) {
135: oldIcValue = oldValue;
136: } else {
137: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
138: Constants.Package
139: + ".java.javaURLContextFactory");
140: }
141:
142: } catch (Throwable t) {
143: state = STOPPED;
144: notification = new AttributeChangeNotification(this ,
145: sequenceNumber++, System.currentTimeMillis(),
146: "Stopped " + NAME, "State", "java.lang.Integer",
147: new Integer(STARTING), new Integer(STOPPED));
148: sendNotification(notification);
149: }
150:
151: state = STARTED;
152: notification = new AttributeChangeNotification(this ,
153: sequenceNumber++, System.currentTimeMillis(),
154: "Started " + NAME, "State", "java.lang.Integer",
155: new Integer(STARTING), new Integer(STARTED));
156: sendNotification(notification);
157:
158: }
159:
160: /**
161: * Stop the servlet container.
162: */
163: public void stop() {
164:
165: Notification notification = null;
166:
167: if (state != STARTED)
168: return;
169:
170: state = STOPPING;
171:
172: notification = new AttributeChangeNotification(this ,
173: sequenceNumber++, System.currentTimeMillis(),
174: "Stopping " + NAME, "State", "java.lang.Integer",
175: new Integer(STARTED), new Integer(STOPPING));
176: sendNotification(notification);
177:
178: try {
179:
180: System.setProperty(Context.URL_PKG_PREFIXES, oldUrlValue);
181: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
182: oldIcValue);
183:
184: } catch (Throwable t) {
185:
186: // FIXME
187: t.printStackTrace();
188:
189: }
190:
191: state = STOPPED;
192:
193: notification = new AttributeChangeNotification(this ,
194: sequenceNumber++, System.currentTimeMillis(),
195: "Stopped " + NAME, "State", "java.lang.Integer",
196: new Integer(STOPPING), new Integer(STOPPED));
197: sendNotification(notification);
198:
199: }
200:
201: /**
202: * Destroy servlet container (if any is running).
203: */
204: public void destroy() {
205:
206: if (getState() != STOPPED)
207: stop();
208:
209: }
210:
211: }
|