001 package org.shiftone.jrat.util.jmx.dynamic;
002
003
004 import org.shiftone.jrat.util.log.Logger;
005
006 import javax.management.*;
007 import java.util.HashMap;
008 import java.util.Iterator;
009 import java.util.Map;
010
011
012 /**
013 * @author jeff@shiftone.org (Jeff Drost)
014 */
015 public class ConfigurableMBean implements DynamicMBean {
016
017 private static final Logger LOG = Logger.getLogger(ConfigurableMBean.class);
018 private String className = ConfigurableMBean.class.getName();
019 private String description;
020 private Map attributeValues = new HashMap();
021 private Map operations = new HashMap();
022
023 // --- info cache ---
024 private MBeanAttributeInfo[] attributeInfos;
025 private MBeanOperationInfo[] operationInfos;
026 private MBeanConstructorInfo[] constructors = null;
027 private MBeanNotificationInfo[] notifications = null;
028
029 public ConfigurableMBean(String description) {
030 this.description = description;
031 }
032
033
034 public String getDescription() {
035 return description;
036 }
037
038
039 public void setDescription(String description) {
040 this.description = description;
041 }
042
043
044 public void add(String name, AttributeValue attributeValue) {
045
046 if (attributeValues.put(name, attributeValue) != null) {
047 LOG.warn("replacing attribute '" + name + "' with new value");
048 }
049
050 attributeInfos = null;
051 }
052
053
054 public void add(String name, Operation operation) {
055 add(name, null, operation);
056 }
057
058
059 public void add(String name, String signature[], Operation operation) {
060
061 if (signature == null) {
062 signature = new String[0];
063 }
064
065 if (operations.put(new OperationKey(name, signature), operation) != null) {
066 LOG.warn("replacing operation '" + name + "' with new operation");
067 }
068
069 operationInfos = null;
070 }
071
072
073 private AttributeValue getAttributeValue(String attributeName) throws AttributeNotFoundException {
074
075 AttributeValue attribute = (AttributeValue) attributeValues.get(attributeName);
076
077 if (attribute == null) {
078 throw new AttributeNotFoundException(attributeName);
079 }
080
081 return attribute;
082 }
083
084
085 public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
086 return getAttributeValue(name).getValue();
087 }
088
089
090 public void setAttribute(Attribute newValue)
091 throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
092
093 AttributeValue attributeValue = getAttributeValue(newValue.getName());
094
095 attributeValue.setValue(newValue.getValue());
096 }
097
098
099 public AttributeList getAttributes(String[] attributeNames) {
100
101 AttributeList attributeList = new AttributeList();
102
103 for (int i = 0; i < attributeNames.length; i++) {
104 try {
105 attributeList.add(getAttribute(attributeNames[i]));
106 }
107 catch (Exception ignore) {
108 }
109 }
110
111 return null;
112 }
113
114
115 public AttributeList setAttributes(AttributeList attributes) {
116
117 AttributeList setList = new AttributeList();
118
119 for (int i = 0; i < attributes.size(); i++) {
120 try {
121 Attribute attribute = (Attribute) attributes.get(i);
122
123 setAttribute(attribute);
124 setList.add(attribute);
125 }
126 catch (Exception ignore) {
127 }
128 }
129
130 return setList;
131 }
132
133
134 public Object invoke(String actionName, Object params[], String signature[])
135 throws MBeanException, ReflectionException {
136
137 OperationKey key = new OperationKey(actionName, signature);
138 Operation operation = (Operation) operations.get(key);
139
140 if (operation == null) {
141 throw new MBeanException(new Exception("operation not found : " + key));
142 }
143
144 return operation.invoke(params);
145 }
146
147
148 private MBeanOperationInfo[] buildMBeanOperationInfo() {
149
150 MBeanOperationInfo[] operationInfos = new MBeanOperationInfo[operations.size()];
151 Iterator keys = operations.keySet().iterator();
152 int i = 0;
153
154 while (keys.hasNext()) {
155 OperationKey key = (OperationKey) keys.next();
156 Operation operation = (Operation) operations.get(key);
157 MBeanParameterInfo[] parameterInfos = new MBeanParameterInfo[key.getSignature().length];
158
159 for (int p = 0; i < parameterInfos.length; p++) {
160 parameterInfos[p] = new MBeanParameterInfo(operation.getParameterName(p), key.getSignature()[p],
161 operation.getParameterDescription(p));
162 }
163
164 String returnType = operation.getReturnType();
165
166 if (returnType == null) {
167 returnType = Void.TYPE.getName();
168 }
169
170 operationInfos[i++] = new MBeanOperationInfo(key.getName(), // name
171 operation.getDescription(), // description
172 parameterInfos, // parameterInfos
173 returnType, // type
174 MBeanOperationInfo.UNKNOWN // impact
175 );
176 }
177
178 return operationInfos;
179 }
180
181
182 private MBeanAttributeInfo[] buildMBeanAttributeInfo() {
183
184 MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[attributeValues.size()];
185 Iterator keys = attributeValues.keySet().iterator();
186 int i = 0;
187
188 while (keys.hasNext()) {
189 String key = (String) keys.next();
190 AttributeValue value = (AttributeValue) attributeValues.get(key);
191
192 attributeInfos[i++] = new MBeanAttributeInfo(key, // name
193 value.getType(), // type
194 value.getDescription(), // description
195 value.isReadable(), // isReadable,
196 value.isWritable(), // isWritable,
197 false // isIs)
198 );
199 }
200
201 return attributeInfos;
202 }
203
204
205 public MBeanAttributeInfo[] getMBeanAttributeInfo() {
206
207 if (attributeInfos == null) {
208 attributeInfos = buildMBeanAttributeInfo();
209 }
210
211 return attributeInfos;
212 }
213
214
215 public MBeanOperationInfo[] getMBeanOperationInfo() {
216
217 if (operationInfos == null) {
218 operationInfos = buildMBeanOperationInfo();
219 }
220
221 return operationInfos;
222 }
223
224
225 public MBeanInfo getMBeanInfo() {
226 return new MBeanInfo(className, description, getMBeanAttributeInfo(), constructors, getMBeanOperationInfo(),
227 notifications);
228 }
229
230
231 public String toString() {
232 return "DynamicAttributeMBean";
233 }
234 }