1 17 package org.apache.geronimo.kernel.jmx; 18 19 import java.io.ObjectInputStream ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Set ; 23 import javax.management.Attribute ; 24 import javax.management.AttributeList ; 25 import javax.management.AttributeNotFoundException ; 26 import javax.management.InstanceAlreadyExistsException ; 27 import javax.management.InstanceNotFoundException ; 28 import javax.management.ListenerNotFoundException ; 29 import javax.management.MBeanException ; 30 import javax.management.MBeanInfo ; 31 import javax.management.MBeanRegistrationException ; 32 import javax.management.MBeanServer ; 33 import javax.management.NotCompliantMBeanException ; 34 import javax.management.NotificationFilter ; 35 import javax.management.NotificationListener ; 36 import javax.management.ObjectInstance ; 37 import javax.management.ObjectName ; 38 import javax.management.OperationsException ; 39 import javax.management.QueryExp ; 40 import javax.management.ReflectionException ; 41 import javax.management.loading.ClassLoaderRepository ; 42 43 import org.apache.geronimo.gbean.GBeanInfo; 44 import org.apache.geronimo.kernel.GBeanNotFoundException; 45 import org.apache.geronimo.kernel.InternalKernelException; 46 import org.apache.geronimo.kernel.NoSuchAttributeException; 47 import org.apache.geronimo.kernel.NoSuchOperationException; 48 import org.apache.geronimo.kernel.Kernel; 49 50 54 public class MBeanServerDelegate implements MBeanServer { 55 private final Kernel kernel; 56 57 public MBeanServerDelegate(Kernel kernel) { 58 this.kernel = kernel; 59 } 60 61 public Object getAttribute(ObjectName name, String attribute) throws MBeanException , AttributeNotFoundException , InstanceNotFoundException , ReflectionException { 62 try { 63 return kernel.getAttribute(name, attribute); 64 } catch (NoSuchAttributeException e) { 65 throw new AttributeNotFoundException (attribute); 66 } catch (GBeanNotFoundException e) { 67 throw new InstanceNotFoundException (name.getCanonicalName()); 68 } catch (InternalKernelException e) { 69 throw new MBeanException (unwrapInternalKernelException(e)); 70 } catch (Exception e) { 71 throw new MBeanException (e); 72 } 73 } 74 75 public AttributeList getAttributes(ObjectName name, String [] attributes) throws InstanceNotFoundException , ReflectionException { 76 AttributeList attributeList = new AttributeList (attributes.length); 77 for (int i = 0; i < attributes.length; i++) { 78 String attribute = attributes[i]; 79 try { 80 Object value = kernel.getAttribute(name, attribute); 81 attributeList.add(i, new Attribute (attribute, value)); 82 } catch (NoSuchAttributeException e) { 83 } catch (GBeanNotFoundException e) { 85 throw new InstanceNotFoundException (name.getCanonicalName()); 86 } catch (InternalKernelException e) { 87 throw new ReflectionException (unwrapInternalKernelException(e)); 88 } catch (Exception e) { 89 } 91 } 92 return attributeList; 93 } 94 95 public String getDefaultDomain() { 96 return kernel.getKernelName(); 97 } 98 99 public Integer getMBeanCount() { 100 return new Integer (kernel.listGBeans((ObjectName )null).size()); 101 } 102 103 public MBeanInfo getMBeanInfo(ObjectName name) throws InstanceNotFoundException , ReflectionException { 104 GBeanInfo gbeanInfo; 105 try { 106 gbeanInfo = kernel.getGBeanInfo(name); 107 } catch (GBeanNotFoundException e) { 108 throw new InstanceNotFoundException (name.toString()); 109 } catch (InternalKernelException e) { 110 throw new ReflectionException (unwrapInternalKernelException(e)); 111 } 112 return JMXUtil.toMBeanInfo(gbeanInfo); 113 } 114 115 public Object invoke(ObjectName name, String operationName, Object [] params, String [] signature) throws InstanceNotFoundException , MBeanException , ReflectionException { 116 try { 117 return kernel.invoke(name, operationName, params, signature); 118 } catch (NoSuchOperationException e) { 119 throw new ReflectionException (new NoSuchMethodException (e.getMessage())); 120 } catch (GBeanNotFoundException e) { 121 throw new InstanceNotFoundException (name.getCanonicalName()); 122 } catch (InternalKernelException e) { 123 throw new MBeanException (unwrapInternalKernelException(e)); 124 } catch (Exception e) { 125 throw new MBeanException (e); 126 } 127 } 128 129 public boolean isRegistered(ObjectName name) { 130 return kernel.isLoaded(name); 131 } 132 133 public Set queryNames(ObjectName pattern, QueryExp query) { 134 Set names = kernel.listGBeans(pattern); 135 if (query == null) { 136 return names; 137 } 138 139 Set filteredNames = new HashSet (names.size()); 140 for (Iterator iterator = names.iterator(); iterator.hasNext();) { 141 query.setMBeanServer(this); 143 144 ObjectName name = (ObjectName ) iterator.next(); 145 try { 146 if (query.apply(name)) { 147 filteredNames.add(name); 148 } 149 } catch (Exception e) { 150 } 152 } 153 return filteredNames; 154 } 155 156 public Set queryMBeans(ObjectName pattern, QueryExp query) { 157 Set names = queryNames(pattern, query); 158 Set objectInstances = new HashSet (names.size()); 159 for (Iterator iterator = names.iterator(); iterator.hasNext();) { 160 ObjectName name = (ObjectName ) iterator.next(); 161 try { 162 objectInstances.add(getObjectInstance(name)); 163 } catch (InstanceNotFoundException e) { 164 } 166 } 167 return objectInstances; 168 } 169 170 public void setAttribute(ObjectName name, Attribute attribute) throws InstanceNotFoundException , AttributeNotFoundException , MBeanException { 171 String attributeName = attribute.getName(); 172 Object attributeValue = attribute.getValue(); 173 try { 174 kernel.setAttribute(name, attributeName, attributeValue); 175 } catch (NoSuchAttributeException e) { 176 throw new AttributeNotFoundException (attributeName); 177 } catch (GBeanNotFoundException e) { 178 throw new InstanceNotFoundException (name.getCanonicalName()); 179 } catch (InternalKernelException e) { 180 throw new MBeanException (unwrapInternalKernelException(e)); 181 } catch (Exception e) { 182 throw new MBeanException (e); 183 } 184 } 185 186 public AttributeList setAttributes(ObjectName name, AttributeList attributes) throws InstanceNotFoundException , ReflectionException { 187 AttributeList set = new AttributeList (attributes.size()); 188 for (Iterator iterator = attributes.iterator(); iterator.hasNext();) { 189 Attribute attribute = (Attribute ) iterator.next(); 190 String attributeName = attribute.getName(); 191 Object attributeValue = attribute.getValue(); 192 try { 193 kernel.setAttribute(name, attributeName, attributeValue); 194 set.add(attribute); 195 } catch (NoSuchAttributeException e) { 196 } catch (GBeanNotFoundException e) { 198 throw new InstanceNotFoundException (name.getCanonicalName()); 199 } catch (InternalKernelException e) { 200 throw new ReflectionException (unwrapInternalKernelException(e)); 201 } catch (Exception e) { 202 } 204 } 205 return set; 206 } 207 208 public String [] getDomains() { 209 Set domains = new HashSet (); 210 Set names = kernel.listGBeans((ObjectName )null); 211 for (Iterator iterator = names.iterator(); iterator.hasNext();) { 212 ObjectName objectName = (ObjectName ) iterator.next(); 213 domains.add(objectName.getDomain()); 214 } 215 return (String []) domains.toArray(new String [domains.size()]); 216 } 217 218 public ObjectInstance getObjectInstance(ObjectName objectName) throws InstanceNotFoundException { 219 try { 220 GBeanInfo gbeanInfo = kernel.getGBeanInfo(objectName); 221 return new ObjectInstance (objectName, gbeanInfo.getClassName()); 222 } catch (GBeanNotFoundException e) { 223 throw new InstanceNotFoundException (objectName.getCanonicalName()); 224 } 225 } 226 227 public ClassLoader getClassLoaderFor(ObjectName objectName) throws InstanceNotFoundException { 228 try { 229 return kernel.getClassLoaderFor(objectName); 230 } catch (GBeanNotFoundException e) { 231 throw new InstanceNotFoundException (objectName.getCanonicalName()); 232 } 233 } 234 235 private static Exception unwrapInternalKernelException(InternalKernelException e) { 236 if (e.getCause() instanceof Exception ) { 237 return (Exception ) e.getCause(); 238 } 239 return e; 240 } 241 242 248 public void addNotificationListener(ObjectName objectName, NotificationListener notificationListener, NotificationFilter notificationFilter, Object o) throws InstanceNotFoundException { 249 throw new SecurityException ("Operation not allowed"); 250 } 251 252 public void addNotificationListener(ObjectName objectName, ObjectName objectName1, NotificationFilter notificationFilter, Object o) throws InstanceNotFoundException { 253 throw new SecurityException ("Operation not allowed"); 254 } 255 256 public void removeNotificationListener(ObjectName objectName, ObjectName objectName1) throws InstanceNotFoundException , ListenerNotFoundException { 257 throw new SecurityException ("Operation not allowed"); 258 } 259 260 public void removeNotificationListener(ObjectName objectName, NotificationListener notificationListener) throws InstanceNotFoundException , ListenerNotFoundException { 261 throw new SecurityException ("Operation not allowed"); 262 } 263 264 public void removeNotificationListener(ObjectName objectName, ObjectName objectName1, NotificationFilter notificationFilter, Object o) throws InstanceNotFoundException , ListenerNotFoundException { 265 throw new SecurityException ("Operation not allowed"); 266 } 267 268 public void removeNotificationListener(ObjectName objectName, NotificationListener notificationListener, NotificationFilter notificationFilter, Object o) throws InstanceNotFoundException , ListenerNotFoundException { 269 throw new SecurityException ("Operation not allowed"); 270 } 271 272 public boolean isInstanceOf(ObjectName objectName, String s) throws InstanceNotFoundException { 273 throw new SecurityException ("Operation not allowed"); 274 } 275 276 public ObjectInstance createMBean(String s, ObjectName objectName) throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException { 277 throw new SecurityException ("Operation not allowed"); 278 } 279 280 public ObjectInstance createMBean(String s, ObjectName objectName, ObjectName objectName1) throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , InstanceNotFoundException { 281 throw new SecurityException ("Operation not allowed"); 282 } 283 284 public ObjectInstance createMBean(String s, ObjectName objectName, Object [] objects, String [] strings) throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException { 285 throw new SecurityException ("Operation not allowed"); 286 } 287 288 public ObjectInstance createMBean(String s, ObjectName objectName, ObjectName objectName1, Object [] objects, String [] strings) throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , InstanceNotFoundException { 289 throw new SecurityException ("Operation not allowed"); 290 } 291 292 public Object instantiate(String s) throws ReflectionException , MBeanException { 293 throw new SecurityException ("Operation not allowed"); 294 } 295 296 public Object instantiate(String s, ObjectName objectName) throws ReflectionException , MBeanException , InstanceNotFoundException { 297 throw new SecurityException ("Operation not allowed"); 298 } 299 300 public Object instantiate(String s, Object [] objects, String [] strings) throws ReflectionException , MBeanException { 301 throw new SecurityException ("Operation not allowed"); 302 } 303 304 public Object instantiate(String s, ObjectName objectName, Object [] objects, String [] strings) throws ReflectionException , MBeanException , InstanceNotFoundException { 305 throw new SecurityException ("Operation not allowed"); 306 } 307 308 public ObjectInstance registerMBean(Object o, ObjectName objectName) throws InstanceAlreadyExistsException , MBeanRegistrationException , NotCompliantMBeanException { 309 throw new SecurityException ("Operation not allowed"); 310 } 311 312 public ObjectInputStream deserialize(String s, ObjectName objectName, byte[] bytes) throws InstanceNotFoundException , OperationsException , ReflectionException { 313 throw new SecurityException ("Operation not allowed"); 314 } 315 316 public ObjectInputStream deserialize(String s, byte[] bytes) throws OperationsException , ReflectionException { 317 throw new SecurityException ("Operation not allowed"); 318 } 319 320 public ObjectInputStream deserialize(ObjectName objectName, byte[] bytes) throws InstanceNotFoundException , OperationsException { 321 throw new SecurityException ("Operation not allowed"); 322 } 323 324 public ClassLoader getClassLoader(ObjectName objectName) throws InstanceNotFoundException { 325 throw new SecurityException ("Operation not allowed"); 326 } 327 328 public ClassLoaderRepository getClassLoaderRepository() { 329 return new ClassLoaderRepository () { 330 public Class loadClass(String className) throws ClassNotFoundException { 331 throw new ClassNotFoundException (className); 332 } 333 334 public Class loadClassWithout(ClassLoader loader, String className) throws ClassNotFoundException { 335 throw new ClassNotFoundException (className); 336 } 337 338 public Class loadClassBefore(ClassLoader loader, String className) throws ClassNotFoundException { 339 throw new ClassNotFoundException (className); 340 } 341 }; 342 } 343 344 public void unregisterMBean(ObjectName objectName) throws InstanceNotFoundException , MBeanRegistrationException { 345 throw new SecurityException ("Operation not allowed"); 346 } 347 } 348 | Popular Tags |