KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > jmx > JalistoAbstractDynamicMBean


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.jmx;
25
26 import javax.management.*;
27 import java.util.Iterator JavaDoc;
28
29 public abstract class JalistoAbstractDynamicMBean implements DynamicMBean {
30
31     public Object JavaDoc getAttribute(String JavaDoc attributeName)
32             throws AttributeNotFoundException, MBeanException, ReflectionException {
33
34         if (attributeName == null) {
35             throw new RuntimeOperationsException(
36                     new IllegalArgumentException JavaDoc("Attribute name cannot be null"),
37                     "Cannot invoke a getter of " + this.getClass().getName() +
38                     " with null attribute name");
39         }
40
41         return internalGetAttribute(attributeName);
42     }
43
44     public AttributeList getAttributes(String JavaDoc[] attributeNames) {
45         // Check attributeNames is not null to avoid NullPointerException later on
46
if (attributeNames == null) {
47             throw new RuntimeOperationsException(
48                     new IllegalArgumentException JavaDoc("attributeNames[] cannot be null"),
49                                                  "Cannot invoke a getter of " + this.getClass().getName());
50         }
51         AttributeList resultList = new AttributeList();
52
53         // if attributeNames is empty, return an empty result list
54
if (attributeNames.length == 0)
55             return resultList;
56
57         // build the result attribute list
58
for (int i = 0; i < attributeNames.length; i++) {
59             Object JavaDoc value = null;
60             try {
61                 value = getAttribute(attributeNames[i]);
62             } catch (AttributeNotFoundException e) {
63                 e.printStackTrace();
64             } catch (MBeanException e) {
65                 e.printStackTrace();
66             } catch (ReflectionException e) {
67                 e.printStackTrace();
68             }
69             resultList.add(new Attribute(attributeNames[i], value));
70         }
71         return (resultList);
72     }
73
74     public Object JavaDoc invoke(String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature)
75             throws MBeanException, ReflectionException {
76
77         if (operationName == null) {
78             throw new RuntimeOperationsException(
79                     new IllegalArgumentException JavaDoc("Operation name cannot be null"),
80                                                  "Cannot invoke a null operation in " + this.getClass().getName());
81         }
82
83         return internalInvoke(operationName, params, signature);
84     }
85
86     public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
87         if (attribute == null) {
88             throw new RuntimeOperationsException(
89                     new IllegalArgumentException JavaDoc("Attribute cannot be null"),
90                     "Cannot invoke a setter of " + this.getClass().getName() +
91                     " with null attribute");
92         }
93
94         String JavaDoc name = attribute.getName();
95         Object JavaDoc value = attribute.getValue();
96
97         if (name == null) {
98             throw new RuntimeOperationsException(
99                     new IllegalArgumentException JavaDoc(
100                             "Attribute name cannot be null"),
101                             "Cannot invoke the setter of " + this.getClass().getName() +
102                             " with null attribute name");
103         }
104
105         if (!internalSetAttribute(name, value)) {
106             throw(new AttributeNotFoundException("Attribute " + name +
107                                                  " not found in " + this.getClass().getName()));
108         }
109     }
110
111     public AttributeList setAttributes(AttributeList attributeList) {
112          if (attributeList == null) {
113             throw new RuntimeOperationsException(
114                     new IllegalArgumentException JavaDoc("AttributeList attributes cannot be null"),
115                                                  "Cannot invoke a setter of " + this.getClass().getName());
116         }
117
118         AttributeList resultList = new AttributeList();
119
120         if (attributeList.isEmpty()) {
121             return resultList;
122         }
123
124         for (Iterator JavaDoc i = attributeList.iterator(); i.hasNext();) {
125             Attribute attr = (Attribute) i.next();
126             try {
127                 setAttribute(attr);
128                 String JavaDoc name = attr.getName();
129                 Object JavaDoc value = getAttribute(name);
130                 resultList.add(new Attribute(name, value));
131             } catch (Exception JavaDoc e) {
132                 e.printStackTrace();
133             }
134         }
135         return resultList;
136     }
137
138     abstract public MBeanInfo getMBeanInfo();
139
140     abstract protected Object JavaDoc internalGetAttribute(String JavaDoc attributeName)
141             throws AttributeNotFoundException, MBeanException, ReflectionException;
142
143     abstract protected boolean internalSetAttribute(String JavaDoc name, Object JavaDoc value)
144             throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException;
145
146     abstract protected Object JavaDoc internalInvoke(String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature)
147             throws MBeanException, ReflectionException;
148 }
149
Popular Tags