1 28 29 package com.caucho.jmx; 30 31 import com.caucho.config.BuilderProgram; 32 import com.caucho.config.ConfigException; 33 import com.caucho.util.L10N; 34 35 import javax.management.Attribute ; 36 import javax.management.MBeanAttributeInfo ; 37 import javax.management.MBeanInfo ; 38 import javax.management.MBeanServer ; 39 import javax.management.NotificationFilter ; 40 import javax.management.ObjectName ; 41 import java.lang.reflect.Constructor ; 42 import java.util.ArrayList ; 43 44 47 public class MBeanConfig { 48 private static L10N L = new L10N(MBeanConfig.class); 49 50 private Class _type; 51 52 private String _mbeanName; 53 54 private Class _interface; 55 56 private String _jndiName; 57 58 private ObjectName _name; 59 private MBeanInfo _mbeanInfo; 60 61 private ArrayList <BuilderProgram> _args = new ArrayList <BuilderProgram>(); 62 63 private boolean _isInit; 64 65 68 public void setJndiName(String name) 69 { 70 _jndiName = name; 71 } 72 73 76 public String getJndiName() 77 { 78 return _jndiName; 79 } 80 81 84 public void setName(String name) 85 { 86 _mbeanName = name; 87 } 88 89 92 public String getName() 93 { 94 return _mbeanName; 95 } 96 97 100 public void setType(Class mbeanClass) 101 { 102 _type = mbeanClass; 103 } 104 105 108 public Class getMBeanClass() 109 { 110 return _type; 111 } 112 113 116 public void setInterface(Class cl) 117 { 118 _interface = cl; 119 } 120 121 124 public void addArg(BuilderProgram builder) 125 { 126 _args.add(builder); 127 } 128 129 132 public void init() 133 throws Throwable 134 { 135 if (_isInit) 136 return; 137 138 _isInit = true; 139 140 if (_mbeanName == null) 141 throw new ConfigException(L.l("<mbean> configuration needs a 'name' attribute. The 'name' is the MBean ObjectName for the bean.")); 142 143 MBeanServer server = Jmx.getMBeanServer(); 144 145 _name = Jmx.getObjectName(_mbeanName); 146 147 if (_type != null) { 148 } 149 else if (server.getMBeanInfo(_name) != null) { 150 return; 151 } 152 else { 153 throw new ConfigException(L.l("<mbean> configuration needs a 'type' attribute. The 'class' is the class name of the resource bean.")); 154 } 155 156 Constructor constructor = getConstructor(_args.size()); 157 158 Class []params = constructor.getParameterTypes(); 159 160 Object []args = new Object [_args.size()]; 161 162 for (int i = 0; i < args.length; i++) 163 args[0] = _args.get(i).configure(params[i]); 164 165 Object obj = constructor.newInstance(args); 166 167 if (_interface != null) { 168 Object mbean = new IntrospectionMBean(obj, _interface); 169 server.registerMBean(mbean, _name); 170 } 171 else { 172 server.registerMBean(obj, _name); 173 } 174 175 _mbeanInfo = server.getMBeanInfo(_name); 176 } 177 178 ObjectName getObjectName() 179 throws Throwable 180 { 181 if (_name == null) 182 init(); 183 184 return _name; 185 } 186 187 private Constructor getConstructor(int len) 188 throws Throwable 189 { 190 Constructor []constructors = _type.getConstructors(); 191 192 for (int i = 0; i < constructors.length; i++) { 193 if (constructors[i].getParameterTypes().length == len) 194 return constructors[i]; 195 } 196 197 throw new ConfigException(L.l("`{0}' has no matching constructors.", 198 _type.getName())); 199 } 200 201 MBeanInfo getMBeanInfo() 202 throws Throwable 203 { 204 if (_mbeanInfo == null) 205 init(); 206 207 return _mbeanInfo; 208 } 209 210 public Init createInit() 211 { 212 return new Init(); 213 } 214 215 public Listener createListener() 216 { 217 return new Listener(); 218 } 219 220 public String toString() 221 { 222 return "MBean[" + _mbeanName + "]"; 223 } 224 225 public class Init { 226 public void setProperty(String attrName, BuilderProgram program) 227 throws Throwable 228 { 229 MBeanAttributeInfo attr = getAttribute(attrName); 230 if (attr == null) 231 throw new ConfigException(L.l("`{0}' is an unknown attribute for {1}", 232 attrName, _mbeanName)); 233 234 String typeName = attr.getType(); 235 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 236 Class type = Class.forName(typeName, false, loader); 237 238 Object value = program.configure(type); 239 240 MBeanServer server = Jmx.getMBeanServer(); 241 242 server.setAttribute(_name, new Attribute (attrName, value)); 243 } 244 245 private MBeanAttributeInfo getAttribute(String key) 246 throws Throwable 247 { 248 MBeanInfo info = getMBeanInfo(); 249 250 MBeanAttributeInfo []attrs = info.getAttributes(); 251 252 if (attrs == null) 253 return null; 254 255 for (int i = 0; i < attrs.length; i++) { 256 if (attrs[i].getName().equals(key)) 257 return attrs[i]; 258 } 259 260 return null; 261 } 262 } 263 264 public class Listener { 265 private String _name; 266 private Object _handback; 267 private NotificationFilter _filter; 268 269 public void setName(String name) 270 { 271 _name = name; 272 } 273 274 public String getName() 275 { 276 return _name; 277 } 278 279 public void setHandback(Object handback) 280 { 281 _handback = handback; 282 } 283 284 public Object getHandback() 285 { 286 return _handback; 287 } 288 289 public void init() 290 throws Throwable 291 { 292 ObjectName mbeanName = getObjectName(); 293 294 ObjectName listenerName = Jmx.getObjectName(_name); 295 296 MBeanServer server = Jmx.getMBeanServer(); 297 298 server.addNotificationListener(mbeanName, listenerName, 299 _filter, _handback); 300 301 } 302 } 303 } 304 | Popular Tags |