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.system;
23
24 import java.util.List;
25 import javax.management.Attribute;
26 import javax.management.AttributeList;
27 import javax.management.AttributeNotFoundException;
28 import javax.management.DynamicMBean;
29 import javax.management.InvalidAttributeValueException;
30 import javax.management.MBeanAttributeInfo;
31 import javax.management.MBeanConstructorInfo;
32 import javax.management.MBeanException;
33 import javax.management.MBeanInfo;
34 import javax.management.MBeanNotificationInfo;
35 import javax.management.MBeanOperationInfo;
36 import javax.management.MBeanParameterInfo;
37 import javax.management.ReflectionException;
38
39 import org.jboss.logging.Logger;
40
41 /**
42 * <description>
43 *
44 * @see <related>
45 *
46 * @author <a href="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
47 * @version $Revision: 57108 $
48 *
49 * <p><b>Revisions:</b>
50 *
51 * <p><b>6 janv. 2003 Sacha Labourey:</b>
52 * <ul>
53 * <li> First implementation </li>
54 * </ul>
55 */
56 public class ServiceDynamicMBeanSupport
57 extends ServiceMBeanSupport
58 implements DynamicMBean
59 {
60
61 // Constants -----------------------------------------------------
62
63 // Attributes ----------------------------------------------------
64
65 // Static --------------------------------------------------------
66
67 // Constructors --------------------------------------------------
68
69 public ServiceDynamicMBeanSupport()
70 {
71 super();
72 }
73
74 public ServiceDynamicMBeanSupport(Class type)
75 {
76 super(type);
77 }
78
79 public ServiceDynamicMBeanSupport(String category)
80 {
81 super(category);
82 }
83
84 public ServiceDynamicMBeanSupport(Logger log)
85 {
86 super(log);
87 }
88
89 // Public --------------------------------------------------------
90
91 // DynamicMBean implementation -----------------------------------
92
93 public Object getAttribute(String attribute)
94 throws AttributeNotFoundException, MBeanException, ReflectionException
95 {
96 // locally managed attributes!
97 //
98 if("State".equals(attribute))
99 {
100 return new Integer(getState());
101 }
102 if("StateString".equals(attribute))
103 {
104 return getStateString();
105 }
106 if("Name".equals(attribute))
107 {
108 return getName();
109 }
110
111 // Wrapped attributes?
112 //
113 return getInternalAttribute (attribute);
114
115 }
116
117 public Object invoke(String actionName, Object[] params, String[] signature)
118 throws MBeanException, ReflectionException
119 {
120 try
121 {
122 if (ServiceController.JBOSS_INTERNAL_LIFECYCLE.equals(actionName))
123 {
124 jbossInternalLifecycle((String) params[0]);
125 return null;
126 }
127 if (params == null || params.length == 0)
128 {
129 if ("create".equals(actionName))
130 {
131 create(); return null;
132 }
133 else if ("start".equals(actionName))
134 {
135 start(); return null;
136 }
137 else if ("stop".equals(actionName))
138 {
139 stop(); return null;
140 }
141 else if ("destroy".equals(actionName))
142 {
143 destroy(); return null;
144 }
145 }
146 }
147 catch (Exception e)
148 {
149 throw new MBeanException(e, "Exception in service lifecyle operation: " + actionName);
150 }
151
152 // If I am here, it means that the invocation has not been handled locally
153 //
154 try
155 {
156 return internalInvoke (actionName, params, signature);
157 }
158 catch (Exception e)
159 {
160 throw new MBeanException(e,
161 "Exception invoking: " + actionName);
162 }
163 }
164
165 public void setAttribute(Attribute attribute)
166 throws
167 AttributeNotFoundException,
168 InvalidAttributeValueException,
169 MBeanException,
170 ReflectionException
171 {
172 setInternalAttribute (attribute);
173 }
174
175 public AttributeList setAttributes(AttributeList attributes)
176 {
177 AttributeList list = new AttributeList();
178 if (attributes == null)
179 return list;
180 for (int i = 0; i < attributes.size(); ++i)
181 {
182 Attribute attribute = (Attribute) attributes.get(i);
183 try
184 {
185 setAttribute(attribute);
186 list.add(attribute);
187 }
188 catch (Throwable t)
189 {
190 log.debug("Error setting attribute " + attribute.getName(), t);
191 }
192 }
193 return list;
194 }
195
196 public AttributeList getAttributes(String[] attributes)
197 {
198 AttributeList list = new AttributeList();
199 if (attributes == null)
200 return list;
201 for (int i = 0; i < attributes.length; ++i)
202 {
203 try
204 {
205 Object value = getAttribute(attributes[i]);
206 list.add(new Attribute(attributes[i], value));
207 }
208 catch (Throwable t)
209 {
210 log.debug("Error getting attribute " + attributes[i], t);
211 }
212 }
213 return list;
214 }
215
216 public MBeanInfo getMBeanInfo()
217 {
218 MBeanParameterInfo[] noParams = new MBeanParameterInfo[] {};
219
220 MBeanConstructorInfo[] ctorInfo = getInternalConstructorInfo();
221
222 MBeanAttributeInfo[] attrs = getInternalAttributeInfo();
223 MBeanAttributeInfo[] attrInfo = new MBeanAttributeInfo[3 + attrs.length];
224 attrInfo[0] = new MBeanAttributeInfo("Name",
225 "java.lang.String",
226 "Return the service name",
227 true,
228 false,
229 false);
230 attrInfo[1] = new MBeanAttributeInfo("State",
231 "int",
232 "Return the service state",
233 true,
234 false,
235 false);
236 attrInfo[2] = new MBeanAttributeInfo("StateString",
237 "java.lang.String",
238 "Return the service's state as a String",
239 true,
240 false,
241 false);
242 System.arraycopy(attrs, 0, attrInfo, 3, attrs.length);
243
244 MBeanParameterInfo[] jbossInternalLifecycleParms = new MBeanParameterInfo[1];
245 jbossInternalLifecycleParms[0] = new MBeanParameterInfo("method", String.class.getName(), "The lifecycle method");
246
247 MBeanOperationInfo[] ops = getInternalOperationInfo();
248 MBeanOperationInfo[] opInfo = new MBeanOperationInfo[5 + ops.length];
249 opInfo[0] = new MBeanOperationInfo("create",
250 "create service lifecycle operation",
251 noParams,
252 "void",
253 MBeanOperationInfo.ACTION);
254
255 opInfo[1] = new MBeanOperationInfo("start",
256 "start service lifecycle operation",
257 noParams,
258 "void",
259 MBeanOperationInfo.ACTION);
260
261 opInfo[2] = new MBeanOperationInfo("stop",
262 "stop service lifecycle operation",
263 noParams,
264 "void",
265 MBeanOperationInfo.ACTION);
266
267 opInfo[3] = new MBeanOperationInfo("destroy",
268 "destroy service lifecycle operation",
269 noParams,
270 "void",
271 MBeanOperationInfo.ACTION);
272
273 opInfo[4] = new MBeanOperationInfo(ServiceController.JBOSS_INTERNAL_LIFECYCLE,
274 "Internal lifecycle (for internal use)",
275 jbossInternalLifecycleParms,
276 "void",
277 MBeanOperationInfo.ACTION);
278
279 System.arraycopy(ops, 0, opInfo, 5, ops.length);
280
281 MBeanNotificationInfo[] notifyInfo = getInternalNotificationInfo();
282 return new MBeanInfo(getClass().getName(),
283 getInternalDescription(),
284 attrInfo,
285 ctorInfo,
286 opInfo,
287 notifyInfo);
288 }
289
290 // Y overrides ---------------------------------------------------
291
292 // Package protected ---------------------------------------------
293
294 // Protected -----------------------------------------------------
295
296 protected String getInternalDescription()
297 {
298 return "DynamicMBean Service";
299 }
300
301 protected MBeanConstructorInfo[] getInternalConstructorInfo()
302 {
303 return new MBeanConstructorInfo[0];
304 }
305
306 protected MBeanAttributeInfo[] getInternalAttributeInfo()
307 {
308 return new MBeanAttributeInfo[0];
309 }
310
311 protected MBeanOperationInfo[] getInternalOperationInfo()
312 {
313 return new MBeanOperationInfo[0];
314 }
315
316 protected MBeanNotificationInfo[] getInternalNotificationInfo()
317 {
318 return new MBeanNotificationInfo[0];
319 }
320
321 protected Object getInternalAttribute(String attribute)
322 throws AttributeNotFoundException, MBeanException, ReflectionException
323 {
324 throw new AttributeNotFoundException ("Attribute not found " + attribute);
325 }
326
327 protected void setInternalAttribute(Attribute attribute)
328 throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException
329 {
330 throw new AttributeNotFoundException ("Attribute not found " + attribute);
331 }
332
333 protected Object internalInvoke(String actionName, Object[] params, String[] signature)
334 throws MBeanException, ReflectionException
335 {
336 StringBuffer buffer = new StringBuffer();
337 buffer.append(actionName);
338 buffer.append('(');
339 for (int i = 0; i < signature.length; ++i)
340 {
341 buffer.append(signature[i]);
342 if (i < signature.length - 1)
343 buffer.append(", ");
344 }
345 buffer.append(')');
346 throw new MBeanException(new Exception("Operation not found " + buffer.toString()), "Operation not found " + actionName);
347 }
348
349
350 // Private -------------------------------------------------------
351
352 // Inner classes -------------------------------------------------
353
354 }