001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.management.j2ee;
023:
024: import org.jboss.system.ServiceMBean;
025:
026: import javax.management.AttributeChangeNotification;
027: import javax.management.Notification;
028: import javax.management.NotificationListener;
029: import java.security.InvalidParameterException;
030:
031: /**
032: * Root class of the JBoss JSR-77 implementation of StateManagement
033: *
034: * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer</a>
035: * @version $Revision: 57197 $
036: */
037: public class StateManagement implements NotificationListener {
038: // Constants -----------------------------------------------------
039: // These are not defined by JSR-77 as valid states
040: public static final int CREATED = 5;
041: public static final int DESTROYED = 6;
042: public static final int REGISTERED = 7;
043: public static final int UNREGISTERED = 8;
044:
045: /**
046: * The int state to state name mappings
047: */
048: public static final String[] stateTypes = new String[] {
049: NotificationConstants.STATE_STARTING,
050: NotificationConstants.STATE_RUNNING,
051: NotificationConstants.STATE_STOPPING,
052: NotificationConstants.STATE_STOPPED,
053: NotificationConstants.STATE_FAILED,
054: NotificationConstants.OBJECT_CREATED,
055: NotificationConstants.OBJECT_DELETED,
056: NotificationConstants.OBJECT_REGISTERED,
057: NotificationConstants.OBJECT_DELETED };
058:
059: // Attributes ----------------------------------------------------
060:
061: private long startTime = -1;
062: private int state = StateManageable.UNREGISTERED;
063: private J2EEManagedObject managedObject;
064:
065: // Static --------------------------------------------------------
066:
067: /**
068: * Converts a state from JBoss ServiceMBean to the JSR-77 state
069: *
070: * @param theState the JBoss ServiceMBean state.
071: * @return Converted state or -1 if unknown.
072: */
073: public static int convertJBossState(int theState) {
074: int jsr77State = -1;
075: switch (theState) {
076: case ServiceMBean.STARTING:
077: jsr77State = StateManageable.STARTING;
078: break;
079: case ServiceMBean.STARTED:
080: jsr77State = StateManageable.RUNNING;
081: break;
082: case ServiceMBean.STOPPING:
083: jsr77State = StateManageable.STOPPING;
084: break;
085: case ServiceMBean.STOPPED:
086: jsr77State = StateManageable.STOPPED;
087: break;
088: case ServiceMBean.FAILED:
089: jsr77State = StateManageable.FAILED;
090: break;
091: case ServiceMBean.CREATED:
092: jsr77State = CREATED;
093: break;
094: case ServiceMBean.DESTROYED:
095: jsr77State = DESTROYED;
096: break;
097: case ServiceMBean.REGISTERED:
098: jsr77State = REGISTERED;
099: break;
100: case ServiceMBean.UNREGISTERED:
101: jsr77State = UNREGISTERED;
102: break;
103: default:
104: jsr77State = -1;
105: break;
106: }
107: return jsr77State;
108: }
109:
110: /**
111: * Converts a JSR-77 state to the JBoss ServiceMBean state
112: *
113: * @param theState the JSR-77 state.
114: * @return Converted state or -1 if unknown.
115: */
116: public static int convertJSR77State(int theState) {
117: int jbossState = -1;
118: switch (theState) {
119: case StateManageable.STARTING:
120: jbossState = ServiceMBean.STARTING;
121: break;
122: case StateManageable.RUNNING:
123: jbossState = ServiceMBean.STARTED;
124: break;
125: case StateManageable.STOPPING:
126: jbossState = ServiceMBean.STOPPING;
127: break;
128: case StateManageable.STOPPED:
129: jbossState = ServiceMBean.STOPPED;
130: break;
131: case StateManageable.FAILED:
132: jbossState = ServiceMBean.FAILED;
133: break;
134: case CREATED:
135: jbossState = ServiceMBean.CREATED;
136: break;
137: case DESTROYED:
138: jbossState = ServiceMBean.DESTROYED;
139: break;
140: case REGISTERED:
141: jbossState = ServiceMBean.REGISTERED;
142: break;
143: case UNREGISTERED:
144: jbossState = ServiceMBean.UNREGISTERED;
145: break;
146: }
147: return jbossState;
148: }
149:
150: // Constructors --------------------------------------------------
151: /**
152: * @param managedObject
153: * @throws InvalidParameterException If the given Name is null
154: */
155: public StateManagement(J2EEManagedObject managedObject) {
156: if (managedObject == null) {
157: throw new InvalidParameterException(
158: "managedObject must not be null");
159: }
160: this .managedObject = managedObject;
161: this .startTime = System.currentTimeMillis();
162: }
163:
164: // Public --------------------------------------------------------
165:
166: public long getStartTime() {
167: return startTime;
168: }
169:
170: public void setStartTime(long pTime) {
171: startTime = pTime;
172: }
173:
174: public int getState() {
175: return state;
176: }
177:
178: public String getStateString() {
179: String stateName = stateTypes[state];
180: return stateName;
181: }
182:
183: /**
184: * Sets a new state and if it changed the appropriate state change event
185: * is sent.
186: *
187: * @param newState Integer indicating the new state according to
188: * {@link org.jboss.management.j2ee.StateManageable StateManageable}
189: * constants
190: */
191: public void setState(int newState) {
192: // Only send a notification if the state really changes
193: if (0 <= newState && newState < stateTypes.length) {
194: if (newState != state) {
195: state = newState;
196: // Now send the event to the JSR-77 listeners
197: String type = stateTypes[state];
198: managedObject.sendNotification(type, "State changed");
199: }
200: }
201: }
202:
203: // NotificationListener overrides ---------------------------------
204:
205: /**
206: * A notification from the underlying JBoss service.
207: *
208: * @param msg The notification msg, AttributeChangeNotification is what we
209: * care about
210: * @param handback not used
211: */
212: public void handleNotification(Notification msg, Object handback) {
213: if (msg instanceof AttributeChangeNotification) {
214: AttributeChangeNotification change = (AttributeChangeNotification) msg;
215: String attrName = change.getAttributeName();
216: Object newValue = change.getNewValue();
217: if ("State".equals(attrName) && newValue != null
218: && newValue instanceof Integer) {
219: int newState = ((Integer) newValue).intValue();
220: long eventTime = -1;
221: if (newState == ServiceMBean.STARTED) {
222: eventTime = change.getTimeStamp();
223: }
224: if (newState == ServiceMBean.STARTED)
225: setStartTime(eventTime);
226: int jsr77State = convertJBossState(newState);
227: setState(jsr77State);
228: }
229: }
230: }
231:
232: // Object overrides ---------------------------------------------------
233:
234: public String toString() {
235: return "StateManagement [ " + "State: " + state
236: + ", Start Time: " + startTime + " ]";
237: }
238: }
|