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