1 17 package org.apache.geronimo.kernel.jmx; 18 19 import java.util.Date ; 20 import java.util.Set ; 21 import javax.management.AttributeNotFoundException ; 22 import javax.management.InstanceNotFoundException ; 23 import javax.management.JMException ; 24 import javax.management.JMRuntimeException ; 25 import javax.management.MBeanServerConnection ; 26 import javax.management.ObjectName ; 27 28 import org.apache.geronimo.gbean.GBeanData; 29 import org.apache.geronimo.gbean.GBeanInfo; 30 import org.apache.geronimo.kernel.DependencyManager; 31 import org.apache.geronimo.kernel.GBeanAlreadyExistsException; 32 import org.apache.geronimo.kernel.GBeanNotFoundException; 33 import org.apache.geronimo.kernel.InternalKernelException; 34 import org.apache.geronimo.kernel.Kernel; 35 import org.apache.geronimo.kernel.lifecycle.LifecycleMonitor; 36 import org.apache.geronimo.kernel.proxy.ProxyManager; 37 38 41 public class KernelDelegate implements Kernel { 42 private final MBeanServerConnection mbeanServer; 43 private final ProxyManager proxyManager; 44 45 public KernelDelegate(MBeanServerConnection mbeanServer) { 46 this.mbeanServer = mbeanServer; 47 proxyManager = new JMXProxyManager(this); 48 } 49 50 public Date getBootTime() { 51 return (Date ) getKernelAttribute("bootTime"); 52 } 53 54 public String getKernelName() { 55 return (String ) getKernelAttribute("kernelName"); 56 } 57 58 public void loadGBean(GBeanData gbeanData, ClassLoader classLoader) throws GBeanAlreadyExistsException, InternalKernelException { 59 try { 60 invokeKernel("loadGBean", new Object [] {gbeanData, classLoader}, new String [] {GBeanData.class.getName(), ClassLoader .class.getName()}); 61 } catch (GBeanAlreadyExistsException e) { 62 throw e; 63 } catch (RuntimeException e) { 64 throw e; 65 } catch (Exception e) { 66 throw new InternalKernelException(e); 67 } 68 } 69 70 public void startGBean(ObjectName name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException { 71 try { 72 invokeKernel("startGBean", new Object [] {name}, new String [] {ObjectName .class.getName()}); 73 } catch (GBeanNotFoundException e) { 74 throw e; 75 } catch (RuntimeException e) { 76 throw e; 77 } catch (Exception e) { 78 throw new InternalKernelException(e); 79 } 80 } 81 82 public void startRecursiveGBean(ObjectName name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException { 83 try { 84 invokeKernel("startRecursiveGBean", new Object [] {name}, new String [] {ObjectName .class.getName()}); 85 } catch (GBeanNotFoundException e) { 86 throw e; 87 } catch (RuntimeException e) { 88 throw e; 89 } catch (Exception e) { 90 throw new InternalKernelException(e); 91 } 92 } 93 94 public void stopGBean(ObjectName name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException { 95 try { 96 invokeKernel("stopGBean", new Object [] {name}, new String [] {ObjectName .class.getName()}); 97 } catch (GBeanNotFoundException e) { 98 throw e; 99 } catch (RuntimeException e) { 100 throw e; 101 } catch (Exception e) { 102 throw new InternalKernelException(e); 103 } 104 } 105 106 public void unloadGBean(ObjectName name) throws GBeanNotFoundException { 107 try { 108 invokeKernel("unloadGBean", new Object [] {name}, new String [] {ObjectName .class.getName()}); 109 } catch (GBeanNotFoundException e) { 110 throw e; 111 } catch (RuntimeException e) { 112 throw e; 113 } catch (Exception e) { 114 throw new InternalKernelException(e); 115 } 116 } 117 118 public int getGBeanState(ObjectName name) throws GBeanNotFoundException { 119 try { 120 return ((Integer ) invokeKernel("getGBeanState", new Object []{name}, new String []{ObjectName .class.getName()})).intValue(); 121 } catch (GBeanNotFoundException e) { 122 throw e; 123 } catch (RuntimeException e) { 124 throw e; 125 } catch (Exception e) { 126 throw new InternalKernelException(e); 127 } 128 } 129 130 public long getGBeanStartTime(ObjectName name) throws GBeanNotFoundException { 131 try { 132 return ((Long ) invokeKernel("getGBeanStartTime", new Object []{name}, new String []{ObjectName .class.getName()})).longValue(); 133 } catch (GBeanNotFoundException e) { 134 throw e; 135 } catch (RuntimeException e) { 136 throw e; 137 } catch (Exception e) { 138 throw new InternalKernelException(e); 139 } 140 } 141 142 public boolean isGBeanEnabled(ObjectName name) throws GBeanNotFoundException { 143 try { 144 return ((Boolean ) invokeKernel("isGBeanEnabled", new Object [] {name}, new String [] {ObjectName .class.getName()})).booleanValue(); 145 } catch (GBeanNotFoundException e) { 146 throw e; 147 } catch (RuntimeException e) { 148 throw e; 149 } catch (Exception e) { 150 throw new InternalKernelException(e); 151 } 152 } 153 154 public void setGBeanEnabled(ObjectName name, boolean enabled) throws GBeanNotFoundException { 155 try { 156 invokeKernel("setGBeanEnabled", new Object [] {name}, new String [] {ObjectName .class.getName()}); 157 } catch (GBeanNotFoundException e) { 158 throw e; 159 } catch (RuntimeException e) { 160 throw e; 161 } catch (Exception e) { 162 throw new InternalKernelException(e); 163 } 164 } 165 166 public Object getAttribute(ObjectName objectName, String attributeName) throws Exception { 167 try { 168 return invokeKernel("getAttribute", new Object []{objectName, attributeName}, new String []{ObjectName .class.getName(), String .class.getName()}); 169 } catch (RuntimeException e) { 170 throw e; 171 } catch (Exception e) { 172 throw new InternalKernelException(e); 173 } 174 } 175 176 public void setAttribute(ObjectName objectName, String attributeName, Object attributeValue) throws Exception { 177 try { 178 invokeKernel("setAttribute", new Object []{objectName, attributeName, attributeValue}, new String []{ObjectName .class.getName(), String .class.getName(), Object .class.getName()}); 179 } catch (RuntimeException e) { 180 throw e; 181 } catch (Exception e) { 182 throw new InternalKernelException(e); 183 } 184 } 185 186 public Object invoke(ObjectName objectName, String methodName) throws Exception { 187 try { 188 return invokeKernel("invoke", new Object []{objectName, methodName}, new String []{ObjectName .class.getName(), String .class.getName()}); 189 } catch (RuntimeException e) { 190 throw e; 191 } catch (Exception e) { 192 throw new InternalKernelException(e); 193 } 194 } 195 196 public Object invoke(ObjectName objectName, String methodName, Object [] args, String [] types) throws Exception { 197 try { 198 return invokeKernel("invoke", new Object []{objectName, methodName, args, types}, new String []{ObjectName .class.getName(), String .class.getName(), Object [].class.getName(), String [].class.getName()}); 199 } catch (RuntimeException e) { 200 throw e; 201 } catch (Exception e) { 202 throw new InternalKernelException(e); 203 } 204 } 205 206 public boolean isLoaded(ObjectName name) { 207 try { 208 return ((Boolean ) invokeKernel("isLoaded", new Object []{name}, new String []{ObjectName .class.getName()})).booleanValue(); 209 } catch (RuntimeException e) { 210 throw e; 211 } catch (Exception e) { 212 throw new InternalKernelException(e); 213 } 214 } 215 216 public GBeanInfo getGBeanInfo(ObjectName name) throws GBeanNotFoundException { 217 try { 218 return (GBeanInfo) invokeKernel("getGBeanInfo", new Object [] {name}, new String [] {ObjectName .class.getName()}); 219 } catch (GBeanNotFoundException e) { 220 throw e; 221 } catch (RuntimeException e) { 222 throw e; 223 } catch (Exception e) { 224 throw new InternalKernelException(e); 225 } 226 } 227 228 public Set listGBeans(ObjectName pattern) throws InternalKernelException { 229 try { 230 return (Set ) invokeKernel("listGBeans", new Object [] {pattern}, new String [] {ObjectName .class.getName()}); 231 } catch (RuntimeException e) { 232 throw e; 233 } catch (Exception e) { 234 throw new InternalKernelException(e); 235 } 236 } 237 238 public Set listGBeans(Set patterns) throws InternalKernelException { 239 try { 240 return (Set ) invokeKernel("listGBeans", new Object [] {patterns}, new String [] {Set .class.getName()}); 241 } catch (RuntimeException e) { 242 throw e; 243 } catch (Exception e) { 244 throw new InternalKernelException(e); 245 } 246 } 247 248 public void registerShutdownHook(Runnable hook) { 249 try { 250 invokeKernel("registerShutdownHook", new Object [] {hook}, new String [] {Runnable .class.getName()}); 251 } catch (RuntimeException e) { 252 throw e; 253 } catch (Exception e) { 254 throw new InternalKernelException(e); 255 } 256 } 257 258 public void unregisterShutdownHook(Runnable hook) { 259 try { 260 invokeKernel("unregisterShutdownHook", new Object [] {hook}, new String [] {Runnable .class.getName()}); 261 } catch (RuntimeException e) { 262 throw e; 263 } catch (Exception e) { 264 throw new InternalKernelException(e); 265 } 266 } 267 268 public void shutdown() { 269 try { 270 invokeKernel("shutdown", new Object [] {}, new String [] {}); 271 } catch (RuntimeException e) { 272 throw e; 273 } catch (Exception e) { 274 throw new InternalKernelException(e); 275 } 276 } 277 278 public ClassLoader getClassLoaderFor(ObjectName name) throws GBeanNotFoundException, InternalKernelException { 279 try { 280 return (ClassLoader ) invokeKernel("getClassLoaderFor", new Object [] {name}, new String [] {ObjectName .class.getName()}); 281 } catch (GBeanNotFoundException e) { 282 throw e; 283 } catch (RuntimeException e) { 284 throw e; 285 } catch (Exception e) { 286 throw new InternalKernelException(e); 287 } 288 } 289 290 public GBeanData getGBeanData(ObjectName name) throws GBeanNotFoundException, InternalKernelException { 291 try { 292 return (GBeanData) invokeKernel("getGBeanData", new Object [] {name}, new String [] {ObjectName .class.getName()}); 293 } catch (GBeanNotFoundException e) { 294 throw e; 295 } catch (RuntimeException e) { 296 throw e; 297 } catch (Exception e) { 298 throw new InternalKernelException(e); 299 } 300 } 301 302 private Object getKernelAttribute(String attributeName) throws InternalKernelException { 303 try { 304 return mbeanServer.getAttribute(Kernel.KERNEL, attributeName); 305 } catch (Exception e) { 306 Throwable cause = unwrapJMException(e); 307 if (cause instanceof InstanceNotFoundException ) { 308 throw new InternalKernelException("Kernel is not loaded"); 309 } else if (cause instanceof AttributeNotFoundException ) { 310 throw new InternalKernelException("KernelDelegate is out of synch with Kernel"); 311 } else { 312 throw new InternalKernelException(cause); 313 } 314 } 315 } 316 317 private Object invokeKernel(String methodName, Object [] args, String [] types) throws InternalKernelException, Exception { 318 try { 319 return mbeanServer.invoke(Kernel.KERNEL, methodName, args, types); 320 } catch (Exception e) { 321 Throwable cause = unwrapJMException(e); 322 if (cause instanceof InstanceNotFoundException ) { 323 throw new InternalKernelException("Kernel is not loaded"); 324 } else if (cause instanceof NoSuchMethodException ) { 325 throw new InternalKernelException("KernelDelegate is out of synch with Kernel"); 326 } else if (cause instanceof JMException ) { 327 throw new InternalKernelException(cause); 328 } else if (cause instanceof JMRuntimeException ) { 329 throw new InternalKernelException(cause); 330 } else if (cause instanceof Error ) { 331 throw (Error ) cause; 332 } else if (cause instanceof Exception ) { 333 throw (Exception ) cause; 334 } else { 335 throw new InternalKernelException("Unknown throwable", cause); 336 } 337 } 338 } 339 340 public boolean isRunning() { 341 return ((Boolean ) getKernelAttribute("running")).booleanValue(); 342 } 343 344 public DependencyManager getDependencyManager() { 345 throw new UnsupportedOperationException ("Dependency manager is not accessable by way of a remote connection"); 346 } 347 348 public LifecycleMonitor getLifecycleMonitor() { 349 throw new UnsupportedOperationException ("Lifecycle monitor is not accessable by way of a remote connection"); 350 } 351 352 public ProxyManager getProxyManager() { 353 return proxyManager; 354 } 355 356 public void boot() throws Exception { 357 throw new UnsupportedOperationException ("A remote kernel can not be booted"); 358 } 359 360 private Throwable unwrapJMException(Throwable cause) { 361 while ((cause instanceof JMException || cause instanceof JMRuntimeException ) && cause.getCause() != null) { 362 cause = cause.getCause(); 363 } 364 return cause; 365 } 366 } 367 | Popular Tags |