KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > ServiceDynamicMBeanSupport


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 JavaDoc;
25 import javax.management.Attribute JavaDoc;
26 import javax.management.AttributeList JavaDoc;
27 import javax.management.AttributeNotFoundException JavaDoc;
28 import javax.management.DynamicMBean JavaDoc;
29 import javax.management.InvalidAttributeValueException JavaDoc;
30 import javax.management.MBeanAttributeInfo JavaDoc;
31 import javax.management.MBeanConstructorInfo JavaDoc;
32 import javax.management.MBeanException JavaDoc;
33 import javax.management.MBeanInfo JavaDoc;
34 import javax.management.MBeanNotificationInfo JavaDoc;
35 import javax.management.MBeanOperationInfo JavaDoc;
36 import javax.management.MBeanParameterInfo JavaDoc;
37 import javax.management.ReflectionException JavaDoc;
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 JavaDoc
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 JavaDoc type)
75    {
76       super(type);
77    }
78
79    public ServiceDynamicMBeanSupport(String JavaDoc 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 JavaDoc getAttribute(String JavaDoc attribute)
94       throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
95    {
96       // locally managed attributes!
97
//
98
if("State".equals(attribute))
99       {
100          return new Integer JavaDoc(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 JavaDoc invoke(String JavaDoc actionName, Object JavaDoc[] params, String JavaDoc[] signature)
118       throws MBeanException JavaDoc, ReflectionException JavaDoc
119    {
120       try
121       {
122          if (ServiceController.JBOSS_INTERNAL_LIFECYCLE.equals(actionName))
123          {
124             jbossInternalLifecycle((String JavaDoc) 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 JavaDoc e)
148       {
149          throw new MBeanException JavaDoc(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 JavaDoc e)
159       {
160          throw new MBeanException JavaDoc(e,
161                "Exception invoking: " + actionName);
162       }
163    }
164
165    public void setAttribute(Attribute JavaDoc attribute)
166       throws
167          AttributeNotFoundException JavaDoc,
168          InvalidAttributeValueException JavaDoc,
169          MBeanException JavaDoc,
170          ReflectionException JavaDoc
171    {
172       setInternalAttribute (attribute);
173    }
174
175    public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes)
176    {
177       AttributeList JavaDoc list = new AttributeList JavaDoc();
178       if (attributes == null)
179          return list;
180       for (int i = 0; i < attributes.size(); ++i)
181       {
182          Attribute JavaDoc attribute = (Attribute JavaDoc) attributes.get(i);
183          try
184          {
185             setAttribute(attribute);
186             list.add(attribute);
187          }
188          catch (Throwable JavaDoc t)
189          {
190             log.debug("Error setting attribute " + attribute.getName(), t);
191          }
192       }
193       return list;
194    }
195
196    public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes)
197    {
198       AttributeList JavaDoc list = new AttributeList JavaDoc();
199       if (attributes == null)
200          return list;
201       for (int i = 0; i < attributes.length; ++i)
202       {
203          try
204          {
205             Object JavaDoc value = getAttribute(attributes[i]);
206             list.add(new Attribute JavaDoc(attributes[i], value));
207          }
208          catch (Throwable JavaDoc t)
209          {
210             log.debug("Error getting attribute " + attributes[i], t);
211          }
212       }
213       return list;
214    }
215
216    public MBeanInfo JavaDoc getMBeanInfo()
217    {
218       MBeanParameterInfo JavaDoc[] noParams = new MBeanParameterInfo JavaDoc[] {};
219       
220       MBeanConstructorInfo JavaDoc[] ctorInfo = getInternalConstructorInfo();
221       
222       MBeanAttributeInfo JavaDoc[] attrs = getInternalAttributeInfo();
223       MBeanAttributeInfo JavaDoc[] attrInfo = new MBeanAttributeInfo JavaDoc[3 + attrs.length];
224       attrInfo[0] = new MBeanAttributeInfo JavaDoc("Name",
225             "java.lang.String",
226             "Return the service name",
227             true,
228             false,
229             false);
230       attrInfo[1] = new MBeanAttributeInfo JavaDoc("State",
231             "int",
232             "Return the service state",
233             true,
234             false,
235             false);
236       attrInfo[2] = new MBeanAttributeInfo JavaDoc("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 JavaDoc[] jbossInternalLifecycleParms = new MBeanParameterInfo JavaDoc[1];
245       jbossInternalLifecycleParms[0] = new MBeanParameterInfo JavaDoc("method", String JavaDoc.class.getName(), "The lifecycle method");
246       
247       MBeanOperationInfo JavaDoc[] ops = getInternalOperationInfo();
248       MBeanOperationInfo JavaDoc[] opInfo = new MBeanOperationInfo JavaDoc[5 + ops.length];
249       opInfo[0] = new MBeanOperationInfo JavaDoc("create",
250                                 "create service lifecycle operation",
251                                 noParams,
252                                 "void",
253                                 MBeanOperationInfo.ACTION);
254          
255       opInfo[1] = new MBeanOperationInfo JavaDoc("start",
256                                 "start service lifecycle operation",
257                                 noParams,
258                                 "void",
259                                 MBeanOperationInfo.ACTION);
260          
261       opInfo[2] = new MBeanOperationInfo JavaDoc("stop",
262                                 "stop service lifecycle operation",
263                                 noParams,
264                                 "void",
265                                 MBeanOperationInfo.ACTION);
266          
267       opInfo[3] = new MBeanOperationInfo JavaDoc("destroy",
268                                 "destroy service lifecycle operation",
269                                 noParams,
270                                 "void",
271                                 MBeanOperationInfo.ACTION);
272                                 
273       opInfo[4] = new MBeanOperationInfo JavaDoc(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 JavaDoc[] notifyInfo = getInternalNotificationInfo();
282       return new MBeanInfo JavaDoc(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 JavaDoc getInternalDescription()
297    {
298       return "DynamicMBean Service";
299    }
300    
301    protected MBeanConstructorInfo JavaDoc[] getInternalConstructorInfo()
302    {
303       return new MBeanConstructorInfo JavaDoc[0];
304    }
305    
306    protected MBeanAttributeInfo JavaDoc[] getInternalAttributeInfo()
307    {
308       return new MBeanAttributeInfo JavaDoc[0];
309    }
310    
311    protected MBeanOperationInfo JavaDoc[] getInternalOperationInfo()
312    {
313       return new MBeanOperationInfo JavaDoc[0];
314    }
315    
316    protected MBeanNotificationInfo JavaDoc[] getInternalNotificationInfo()
317    {
318       return new MBeanNotificationInfo JavaDoc[0];
319    }
320    
321    protected Object JavaDoc getInternalAttribute(String JavaDoc attribute)
322       throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
323    {
324       throw new AttributeNotFoundException JavaDoc ("Attribute not found " + attribute);
325    }
326    
327    protected void setInternalAttribute(Attribute JavaDoc attribute)
328        throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
329    {
330       throw new AttributeNotFoundException JavaDoc ("Attribute not found " + attribute);
331    }
332
333    protected Object JavaDoc internalInvoke(String JavaDoc actionName, Object JavaDoc[] params, String JavaDoc[] signature)
334       throws MBeanException JavaDoc, ReflectionException JavaDoc
335    {
336       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc(new Exception JavaDoc("Operation not found " + buffer.toString()), "Operation not found " + actionName);
347    }
348    
349    
350    // Private -------------------------------------------------------
351

352    // Inner classes -------------------------------------------------
353

354 }
355
Popular Tags