Source code: org/mobicents/slee/container/management/jmx/ServiceManagementMBeanImpl.java
1 /***************************************************
2 * *
3 * Mobicents: The Open Source VoIP Platform *
4 * *
5 * Distributable under LGPL license. *
6 * See terms of license at gnu.org. *
7 * *
8 ***************************************************/
9
10 package org.mobicents.slee.container.management.jmx;
11
12 import javax.management.NotCompliantMBeanException;
13 import javax.management.ObjectName;
14 import javax.management.StandardMBean;
15 import javax.slee.InvalidArgumentException;
16 import javax.slee.InvalidStateException;
17 import javax.slee.ServiceID;
18 import javax.slee.UnrecognizedServiceException;
19 import javax.slee.management.ManagementException;
20 import javax.slee.management.ServiceManagementMBean;
21 import javax.slee.management.ServiceState;
22 import javax.transaction.SystemException;
23
24 import org.jboss.logging.Logger;
25 import org.mobicents.slee.container.SleeContainer;
26 import org.mobicents.slee.container.service.Service;
27 import org.mobicents.slee.container.service.ServiceComponent;
28 import org.mobicents.slee.runtime.transaction.SleeTransactionManager;
29
30 /**
31 * Implementation of the ServiceManagementMBean
32 *
33 * @author M. Ranganathan
34 */
35 public class ServiceManagementMBeanImpl extends StandardMBean implements
36 ServiceManagementMBean {
37
38 private static Logger logger;
39
40 static {
41 logger = Logger.getLogger(ServiceManagementMBeanImpl.class);
42 }
43
44 public ServiceManagementMBeanImpl() throws NotCompliantMBeanException {
45 super(ServiceManagementMBean.class);
46
47 }
48
49 /*
50 * (non-Javadoc)
51 *
52 * @see javax.slee.management.ServiceManagementMBean#getState(javax.slee.ServiceID)
53 */
54 public ServiceState getState(ServiceID serviceID)
55 throws NullPointerException, UnrecognizedServiceException,
56 ManagementException {
57
58
59 if ( logger.isDebugEnabled()) {
60 logger.debug("Service.getState " + serviceID );
61 }
62
63 if ( serviceID == null ) throw new NullPointerException ("Null service ID!");
64 SleeTransactionManager transactionManager = SleeContainer.getTransactionManager();
65 boolean b = false;
66 try {
67 SleeContainer serviceContainer = SleeContainer.lookupFromJndi();
68
69 ServiceComponent service = null;
70
71 b = transactionManager.requireTransaction();
72
73 service = serviceContainer.getServiceComponent(serviceID);
74
75 if (service == null)
76 throw new UnrecognizedServiceException(" unrecognized service "
77 + serviceID);
78 if ( logger.isDebugEnabled()) {
79 logger.debug("returning state " + service.getState());
80 }
81 return service.getState();
82
83 } catch (Exception ex) {
84
85 try {
86 transactionManager.setRollbackOnly();
87 } catch (SystemException e) {
88 logger.error("Failed getState for serviceID " + serviceID);
89 }
90 throw new ManagementException(
91 "Unexpected system exception while getting state of service: " + serviceID, ex);
92 } finally {
93 if (b)
94 try {
95 transactionManager.commit();
96 } catch (SystemException e) {
97 logger.error("Failed getState for serviceID " + serviceID);
98 throw new ManagementException(
99 "Unexpected system exception while committing transaction after getState for serviceID: " + serviceID, e);
100 }
101 }
102
103
104 }
105
106 /*
107 * (non-Javadoc)
108 *
109 * @see javax.slee.management.ServiceManagementMBean#getServices(javax.slee.management.ServiceState)
110 */
111 public ServiceID[] getServices(ServiceState serviceState)
112 throws NullPointerException, ManagementException {
113 if (serviceState == null)
114 throw new NullPointerException("Passed a null state");
115 try {
116 SleeContainer serviceContainer = SleeContainer.lookupFromJndi();
117 ServiceID[] retval = serviceContainer
118 .getServicesByState(serviceState);
119
120 return retval;
121 } catch (Exception ex) {
122 throw new ManagementException("Error getting services by state!");
123 }
124 }
125
126 /*
127 * (non-Javadoc)
128 *
129 * @see javax.slee.management.ServiceManagementMBean#activate(javax.slee.ServiceID)
130 */
131 public void activate(ServiceID serviceID) throws NullPointerException,
132 UnrecognizedServiceException, InvalidStateException,
133 ManagementException {
134 if (serviceID == null)
135 throw new NullPointerException("NullPointerException");
136 try {
137 SleeContainer serviceContainer = SleeContainer.lookupFromJndi();
138 serviceContainer.startService(serviceID);
139 } catch (InvalidStateException ise) {
140 throw ise;
141 } catch (Exception ex) {
142 throw new ManagementException("system exception starting service",
143 ex);
144 }
145 }
146
147 /*
148 * (non-Javadoc)
149 *
150 * @see javax.slee.management.ServiceManagementMBean#activate(javax.slee.ServiceID[])
151 */
152 public void activate(ServiceID[] serviceIDs) throws NullPointerException,
153 InvalidArgumentException, UnrecognizedServiceException,
154 InvalidStateException, ManagementException {
155 if (serviceIDs.length == 0)
156 {
157 throw new InvalidArgumentException("InvalidArgumentException");
158 }
159
160 for (int i = 0; i < serviceIDs.length; i++)
161 {
162 if (serviceIDs[i] == null)
163 {
164 throw new InvalidArgumentException("InvalidArgumentException");
165 }
166 }
167 for (int i = 0; i < serviceIDs.length - 1; i++)
168 for (int j = i + 1; j < serviceIDs.length; j++)
169 if (serviceIDs[i] == (serviceIDs[j]))
170 {
171 throw new InvalidArgumentException("InvalidArgumentException");
172 }
173 try {
174 for (int i = 0; i < serviceIDs.length; i++) {
175 activate(serviceIDs[i]);
176 }
177 } catch (InvalidStateException ise) {
178 throw ise;
179 } catch (Exception ex) {
180 throw new ManagementException("system exception starting service",
181 ex);
182 }
183 }
184
185 /*
186 * (non-Javadoc)
187 *
188 * @see javax.slee.management.ServiceManagementMBean#deactivate(javax.slee.ServiceID)
189 */
190 public void deactivate(ServiceID serviceID) throws NullPointerException,
191 UnrecognizedServiceException, InvalidStateException,
192 ManagementException {
193
194 // FIXME -- remove this - this is just for testing.
195 // TODO -- add mechansm to make sure service cannot
196 // be uninstalled when busy.
197
198 if (serviceID == null)
199 throw new NullPointerException("NullPointerException");
200 SleeContainer serviceContainer = SleeContainer.lookupFromJndi();
201 try {
202 serviceContainer.stopService(serviceID);
203 } catch (InvalidStateException ise) {
204 throw ise;
205 }catch (Exception ex) {
206 throw new ManagementException(
207 "exception whild deactivating service ! ");
208 }
209
210 }
211
212 /*
213 * (non-Javadoc)
214 *
215 * @see javax.slee.management.ServiceManagementMBean#deactivate(javax.slee.ServiceID[])
216 */
217 public void deactivate(ServiceID[] arg0) throws NullPointerException,
218 InvalidArgumentException, UnrecognizedServiceException,
219 InvalidStateException, ManagementException {
220 // TODO Auto-generated method stub
221 if (arg0.length == 0)
222 {
223 throw new InvalidArgumentException("InvalidArgumentException");
224 }
225
226 for (int i = 0; i < arg0.length; i++)
227 {
228 if (arg0[i] == null)
229 {
230 throw new InvalidArgumentException("InvalidArgumentException");
231 }
232 }
233 for (int i = 0; i < arg0.length - 1; i++)
234 for (int j = i + 1; j < arg0.length; j++)
235 if (arg0[i] == (arg0[j]))
236 {
237 throw new InvalidArgumentException("InvalidArgumentException");
238 }
239 try {
240 for (int i = 0; i < arg0.length; i++) {
241 deactivate(arg0[i]);
242 }
243 } catch (InvalidStateException ise) {
244 throw ise;
245 } catch (Exception ex) {
246 throw new ManagementException("system exception starting service",
247 ex);
248 }
249 }
250
251 /*
252 * (non-Javadoc)
253 *
254 * @see javax.slee.management.ServiceManagementMBean#deactivateAndActivate(javax.slee.ServiceID,
255 * javax.slee.ServiceID)
256 */
257 public void deactivateAndActivate(ServiceID arg0, ServiceID arg1)
258 throws NullPointerException, InvalidArgumentException,
259 UnrecognizedServiceException, InvalidStateException,
260 ManagementException {
261
262 if (arg0 == arg1)
263 throw new InvalidArgumentException("Activating and deactivating the same service!");
264 if ((arg0 == null) || (arg1 == null))
265 throw new InvalidArgumentException("The service(s) are null!");
266 try {
267 deactivate(arg0);
268 activate(arg1);
269 } catch (InvalidStateException ise) {
270 throw ise;
271 }catch (Exception ex) {
272 throw new ManagementException(
273 "exception in deactivating/activating service ! ");
274 }
275 }
276
277 /*
278 * (non-Javadoc)
279 *
280 * @see javax.slee.management.ServiceManagementMBean#deactivateAndActivate(javax.slee.ServiceID[],
281 * javax.slee.ServiceID[])
282 */
283 public void deactivateAndActivate(ServiceID[] arg0, ServiceID[] arg1)
284 throws NullPointerException, InvalidArgumentException,
285 UnrecognizedServiceException, InvalidStateException,
286 ManagementException {
287
288 if(arg0.length == 0 || arg1.length == 0)
289 throw new InvalidArgumentException("The service array is empty!");
290 for (int i = 0; i < arg0.length; i++)
291 if (arg0[i] == null)
292 throw new InvalidArgumentException("InvalidArgumentException");
293
294 for (int i = 0; i < arg1.length; i++)
295 if (arg1[i] == null)
296 throw new InvalidArgumentException("InvalidArgumentException");
297 for (int i = 0; i < arg0.length - 1; i++)
298 for (int j = i + 1; j < arg0.length; j++)
299 if (arg0[i] == (arg0[j]))
300 throw new InvalidArgumentException("InvalidArgumentException");
301 for (int i = 0; i < arg1.length - 1; i++)
302 for (int j = i + 1; j < arg1.length; j++)
303 if (arg1[i] == (arg1[j]))
304 throw new InvalidArgumentException("InvalidArgumentException");
305
306 for (int i = 0; i < arg0.length; i++)
307 for (int j = 0; j < arg1.length; j++)
308 if (arg0[i] == (arg1[j]))
309 throw new InvalidArgumentException(
310 "InvalidArgumentException");
311 try {
312 for (int i = 0; i < arg0.length; i++) {
313 deactivate(arg0[i]);
314 }
315 for (int i = 0; i < arg1.length; i++) {
316 activate(arg1[i]);
317 }
318 }catch (InvalidStateException ise) {
319 throw ise;
320 }
321 catch (ManagementException me) {
322 throw me;
323 }
324 }
325
326 /*
327 * (non-Javadoc)
328 *
329 * @see javax.slee.management.ServiceManagementMBean#getServiceUsageMBean(javax.slee.ServiceID)
330 */
331 public ObjectName getServiceUsageMBean(ServiceID serviceID)
332 throws NullPointerException, UnrecognizedServiceException,
333 ManagementException {
334 try {
335 if (serviceID == null)
336 throw new NullPointerException("Null service ID ");
337 logger.debug("getServiceUsageMBean " + serviceID);
338 SleeContainer serviceContainer = SleeContainer.lookupFromJndi();
339
340 if ( ! serviceContainer.checkServiceExists(serviceID))
341 throw new UnrecognizedServiceException("bad service id "
342 + serviceID);
343
344 ObjectName objName = new ObjectName("slee:ServiceUsageMBean="
345 + serviceID.toString());
346 return objName;
347 } catch (Exception ex) {
348 throw new ManagementException(ex.getMessage(), ex);
349 }
350 }
351
352 }
353