KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > StateManagement


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.management.j2ee;
23
24 import org.jboss.system.ServiceMBean;
25
26 import javax.management.AttributeChangeNotification JavaDoc;
27 import javax.management.Notification JavaDoc;
28 import javax.management.NotificationListener JavaDoc;
29 import java.security.InvalidParameterException JavaDoc;
30
31 /**
32  * Root class of the JBoss JSR-77 implementation of StateManagement
33  *
34  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
35  * @version $Revision: 40550 $
36  */

37 public class StateManagement implements NotificationListener JavaDoc
38 {
39    // Constants -----------------------------------------------------
40
// These are not defined by JSR-77 as valid states
41
public static final int CREATED = 5;
42    public static final int DESTROYED = 6;
43    public static final int REGISTERED = 7;
44    public static final int UNREGISTERED = 8;
45
46    /**
47     * The int state to state name mappings
48     */

49    public static final String JavaDoc[] stateTypes = new String JavaDoc[]
50    {
51       NotificationConstants.STATE_STARTING,
52       NotificationConstants.STATE_RUNNING,
53       NotificationConstants.STATE_STOPPING,
54       NotificationConstants.STATE_STOPPED,
55       NotificationConstants.STATE_FAILED,
56       NotificationConstants.OBJECT_CREATED,
57       NotificationConstants.OBJECT_DELETED,
58       NotificationConstants.OBJECT_REGISTERED,
59       NotificationConstants.OBJECT_DELETED
60    };
61
62    // Attributes ----------------------------------------------------
63

64    private long startTime = -1;
65    private int state = StateManageable.UNREGISTERED;
66    private J2EEManagedObject managedObject;
67
68    // Static --------------------------------------------------------
69

70    /**
71     * Converts a state from JBoss ServiceMBean to the JSR-77 state
72     *
73     * @param theState the JBoss ServiceMBean state.
74     * @return Converted state or -1 if unknown.
75     */

76    public static int convertJBossState(int theState)
77    {
78       int jsr77State = -1;
79       switch (theState)
80       {
81          case ServiceMBean.STARTING:
82             jsr77State = StateManageable.STARTING;
83             break;
84          case ServiceMBean.STARTED:
85             jsr77State = StateManageable.RUNNING;
86             break;
87          case ServiceMBean.STOPPING:
88             jsr77State = StateManageable.STOPPING;
89             break;
90          case ServiceMBean.STOPPED:
91             jsr77State = StateManageable.STOPPED;
92             break;
93          case ServiceMBean.FAILED:
94             jsr77State = StateManageable.FAILED;
95             break;
96          case ServiceMBean.CREATED:
97             jsr77State = CREATED;
98             break;
99          case ServiceMBean.DESTROYED:
100             jsr77State = DESTROYED;
101             break;
102          case ServiceMBean.REGISTERED:
103             jsr77State = REGISTERED;
104             break;
105          case ServiceMBean.UNREGISTERED:
106             jsr77State = UNREGISTERED;
107             break;
108          default:
109             jsr77State = -1;
110             break;
111       }
112       return jsr77State;
113    }
114
115    /**
116     * Converts a JSR-77 state to the JBoss ServiceMBean state
117     *
118     * @param theState the JSR-77 state.
119     * @return Converted state or -1 if unknown.
120     */

121    public static int convertJSR77State(int theState)
122    {
123       int jbossState = -1;
124       switch (theState)
125       {
126          case StateManageable.STARTING:
127             jbossState = ServiceMBean.STARTING;
128             break;
129          case StateManageable.RUNNING:
130             jbossState = ServiceMBean.STARTED;
131             break;
132          case StateManageable.STOPPING:
133             jbossState = ServiceMBean.STOPPING;
134             break;
135          case StateManageable.STOPPED:
136             jbossState = ServiceMBean.STOPPED;
137             break;
138          case StateManageable.FAILED:
139             jbossState = ServiceMBean.FAILED;
140             break;
141          case CREATED:
142             jbossState = ServiceMBean.CREATED;
143             break;
144          case DESTROYED:
145             jbossState = ServiceMBean.DESTROYED;
146             break;
147          case REGISTERED:
148             jbossState = ServiceMBean.REGISTERED;
149             break;
150          case UNREGISTERED:
151             jbossState = ServiceMBean.UNREGISTERED;
152             break;
153       }
154       return jbossState;
155    }
156
157    // Constructors --------------------------------------------------
158
/**
159     * @param managedObject
160     * @throws InvalidParameterException If the given Name is null
161     */

162    public StateManagement(J2EEManagedObject managedObject)
163    {
164       if (managedObject == null)
165       {
166          throw new InvalidParameterException JavaDoc("managedObject must not be null");
167       }
168       this.managedObject = managedObject;
169       this.startTime = System.currentTimeMillis();
170    }
171
172    // Public --------------------------------------------------------
173

174    public long getStartTime()
175    {
176       return startTime;
177    }
178
179    public void setStartTime(long pTime)
180    {
181       startTime = pTime;
182    }
183
184    public int getState()
185    {
186       return state;
187    }
188
189    public String JavaDoc getStateString()
190    {
191       String JavaDoc stateName = stateTypes[state];
192       return stateName;
193    }
194
195    /**
196     * Sets a new state and if it changed the appropriate state change event
197     * is sent.
198     *
199     * @param newState Integer indicating the new state according to
200     * {@link org.jboss.management.j2ee.StateManageable StateManageable}
201     * constants
202     */

203    public void setState(int newState)
204    {
205       // Only send a notification if the state really changes
206
if (0 <= newState && newState < stateTypes.length)
207       {
208          if (newState != state)
209          {
210             state = newState;
211             // Now send the event to the JSR-77 listeners
212
String JavaDoc type = stateTypes[state];
213             managedObject.sendNotification(type, "State changed");
214          }
215       }
216    }
217
218    // NotificationListener overrides ---------------------------------
219

220    /**
221     * A notification from the underlying JBoss service.
222     *
223     * @param msg The notification msg, AttributeChangeNotification is what we
224     * care about
225     * @param handback not used
226     */

227    public void handleNotification(Notification JavaDoc msg, Object JavaDoc handback)
228    {
229       if (msg instanceof AttributeChangeNotification JavaDoc)
230       {
231          AttributeChangeNotification JavaDoc change = (AttributeChangeNotification JavaDoc) msg;
232          String JavaDoc attrName = change.getAttributeName();
233          Object JavaDoc newValue = change.getNewValue();
234          if ("State".equals(attrName) && newValue != null && newValue instanceof Integer JavaDoc)
235          {
236             int newState = ((Integer JavaDoc) newValue).intValue();
237             long eventTime = -1;
238             if (newState == ServiceMBean.STARTED)
239             {
240                eventTime = change.getTimeStamp();
241             }
242             if (newState == ServiceMBean.STARTED)
243                setStartTime(eventTime);
244             int jsr77State = convertJBossState(newState);
245             setState(jsr77State);
246          }
247       }
248    }
249
250    // Object overrides ---------------------------------------------------
251

252    public String JavaDoc toString()
253    {
254       return "StateManagement [ " +
255               "State: " + state +
256               ", Start Time: " + startTime +
257               " ]";
258    }
259 }
260
Popular Tags