/** * Build an adapted MBean for the given bean instance, if possible. * <p>The default implementation builds a JMX 1.2 StandardMBean * for the target's MBean/MXBean interface in case of an AOP proxy, * delegating the interface's management operations to the proxy. * @param bean the original bean instance * @return the adapted MBean, or {@code null} if not possible */ @SuppressWarnings("unchecked") protected DynamicMBean adaptMBeanIfPossible(Object bean) throws JMException { Class<?> targetClass = AopUtils.getTargetClass(bean); if (targetClass != bean.getClass()) { Class<?> ifc = JmxUtils.getMXBeanInterface(targetClass); if (ifc != null) { if (!ifc.isInstance(bean)) { throw new NotCompliantMBeanException("Managed bean [" + bean + "] has a target class with an MXBean interface but does not expose it in the proxy"); } return new StandardMBean(bean, ((Class<Object>) ifc), true); } else { ifc = JmxUtils.getMBeanInterface(targetClass); if (ifc != null) { if (!ifc.isInstance(bean)) { throw new NotCompliantMBeanException("Managed bean [" + bean + "] has a target class with an MBean interface but does not expose it in the proxy"); } return new StandardMBean(bean, ((Class<Object>) ifc)); } } } return null; }
/** * Instantiates and registers a StandardMBean in the MBean server. * * @param implementationClassName * The implementation class name of the MBean. * @param interfaceClassName * The management interface class name of the MBean. * @param isMXBean * If true, the resultant MBean is an MXBean. * @param name * The object name of the StandardMBean. */ @SuppressWarnings("unchecked") public void createStandardMBean( String implementationClassName, String interfaceClassName, boolean isMXBean, ObjectName name) throws Exception { Object implementation = Class.forName(implementationClassName).newInstance(); Class<Object> interfaceClass = interfaceClassName == null ? null : (Class<Object>)Class.forName(interfaceClassName); // Create the StandardMBean StandardMBean standardMBean = new StandardMBean( implementation, interfaceClass, isMXBean); // Register the StandardMBean mbeanServer.registerMBean(standardMBean, name); }
public static void main(String[] args) throws Exception { final Child child = new Child(); test(child,"Child[MBean]",false); final ChildMix childx = new ChildMix(); test(childx,"ChildMix[MXBean]",true); final ChildMixMix childmx = new ChildMixMix(); test(childmx,"ChildMixMix[MXBean]",false); final StandardMBean schild = new StandardMBean(child,ChildMBean.class); test(schild,"Child[StandarMBean(Child)]",false); final StandardMBean schildx = new StandardMBean(childx,ChildMXBean.class,true); test(schildx,"ChildMix[StandarMXBean(ChildMix)]",true); final StandardMBean schildmx = new StandardMBean(childmx,ChildMixMXBean.class,true); test(schildmx,"ChildMixMix[StandarMXBean(ChildMixMix)]",true); }
/** * Build an adapted MBean for the given bean instance, if possible. * <p>The default implementation builds a JMX 1.2 StandardMBean * for the target's MBean/MXBean interface in case of an AOP proxy, * delegating the interface's management operations to the proxy. * @param bean the original bean instance * @return the adapted MBean, or {@code null} if not possible */ @SuppressWarnings("unchecked") protected DynamicMBean adaptMBeanIfPossible(Object bean) throws JMException { Class<?> targetClass = AopUtils.getTargetClass(bean); if (targetClass != bean.getClass()) { Class<Object> ifc = (Class<Object>) JmxUtils.getMXBeanInterface(targetClass); if (ifc != null) { if (!(ifc.isInstance(bean))) { throw new NotCompliantMBeanException("Managed bean [" + bean + "] has a target class with an MXBean interface but does not expose it in the proxy"); } return new StandardMBean(bean, ifc, true); } else { ifc = (Class<Object>) JmxUtils.getMBeanInterface(targetClass); if (ifc != null) { if (!(ifc.isInstance(bean))) { throw new NotCompliantMBeanException("Managed bean [" + bean + "] has a target class with an MBean interface but does not expose it in the proxy"); } return new StandardMBean(bean, ifc); } } } return null; }
/** * Start the management service if gemfirexd.system.jmx is true. * <P> * Starting the service means: * <UL> * <LI> getting the platform MBeanServer which may require starting it * <LI> registering a Version mbean representing the system * </UL> */ public synchronized void boot(boolean create, Properties properties) throws StandardException { registeredMbeans = new HashMap<ObjectName,StandardMBean>(); systemIdentifier = Monitor.getMonitor().getUUIDFactory().createUUID().toString(); findServer(); myManagementBean = (ObjectName) registerMBean(this, ManagementMBean.class, "type=Management"); myManagementServer = mbeanServer; registerMBean( new Version( Monitor.getMonitor().getEngineVersion(), SystemPermission.ENGINE), VersionMBean.class, "type=Version,jar=gemfirexd.jar"); }
@Override protected void serviceStart() throws Exception { String uuid = UUID.randomUUID().toString(); tmpDir = Paths.get(SYSTEM_TMP, LOCALIZATION_DIR).toFile(); if (!tmpDir.exists()) { tmpDir.mkdir(); } tmpDir.setExecutable(false, false); tmpDir.setExecutable(true); // Writable only to the owner tmpDir.setWritable(false, false); tmpDir.setWritable(true); // Readable by none tmpDir.setReadable(false, false); materializeDir = Paths.get(tmpDir.getAbsolutePath(), uuid); // Random materialization directory should have the default umask materializeDir.toFile().mkdir(); LOG.debug("Initialized at dir: " + materializeDir.toString()); StandardMBean mbean = new StandardMBean(this, CertificateLocalizationMBean.class); mbeanObjectName = MBeans.register(serviceName, "CertificateLocalizer", mbean); super.serviceStart(); }
@Override public void start(final ModuleContext context) throws Exception { MBeanServer server = ServiceLocator.getRequiredService(context, MBeanServer.class); ModuleStateA moduleState = new ModuleStateA() { @Override public String getResourceIdentity() { return context.getModule().getIdentity().getCanonicalForm(); } @Override public String getModuleState() { return context.getModule().getState().toString(); } }; StandardMBean mbean = new StandardMBean(moduleState, ModuleStateA.class); server.registerMBean(mbean, getObjectName(context.getModule())); }
protected void registerJMX() { // register with jmx MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); try { String cName = this.getClass().getName(); if (cName.indexOf('.') != -1) { cName = cName.substring(cName.lastIndexOf('.')).replaceFirst("[\\.]", ""); } oName = new ObjectName(String.format("org.red5.server:type=%s,name=%s", cName, name)); // don't reregister if (!mbs.isRegistered(oName)) { mbs.registerMBean(new StandardMBean(this, ScopeMXBean.class, true), oName); } } catch (Exception e) { log.warn("Error on jmx registration", e); } }
protected void registerJMX() { // register with jmx MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); try { String cName = this.getClass().getName(); if (cName.indexOf('.') != -1) { cName = cName.substring(cName.lastIndexOf('.')).replaceFirst("[\\.]", ""); } String hostStr = host; int port = 1935; if (host != null && host.indexOf(":") > -1) { String[] arr = host.split(":"); hostStr = arr[0]; port = Integer.parseInt(arr[1]); } // Create a new mbean for this instance oName = new ObjectName(String.format("org.red5.server:type=%s,connectionType=%s,host=%s,port=%d,clientId=%s", cName, type, hostStr, port, client.getId())); if (!mbs.isRegistered(oName)) { mbs.registerMBean(new StandardMBean(this, RTMPMinaConnectionMXBean.class, true), oName); } else { log.debug("Connection is already registered in JMX"); } } catch (Exception e) { log.warn("Error on jmx registration", e); } }
public void testProxy() throws Exception { ObjectName n = new ObjectName("foo:id=" + UUID.randomUUID().toString()); Impl s = new Impl(); server.registerMBean(new StandardMBean(s, Ifc1.class), n); Ifc1 s1 = MBeanUtils.createProxy(getClass().getClassLoader(), n, Ifc1.class); assertNotNull(s1); assertEquals(s.foo, s1.getfoo()); assertEquals(s.bar, s1.getBar()); assertEquals("ab", s1.cat("a", "b")); Ifc1 s2 = MBeanUtils.createProxy(getClass().getClassLoader(), n, Ifc1.class); assertNotNull(s2); assertEquals(s.foo, s2.getfoo()); assertEquals(s.bar, s2.getBar()); assertEquals("ab", s2.cat("a", "b")); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public static boolean registerNewMBean(Class clazz, Class interfaceClass) { boolean status = false; try { String cName = clazz.getName(); if (cName.indexOf('.') != -1) { cName = cName.substring(cName.lastIndexOf('.')).replaceFirst("[\\.]", ""); } log.debug("Register name: {}", cName); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); mbs.registerMBean(new StandardMBean(Class.forName(clazz.getName()).newInstance(), interfaceClass), new ObjectName("org.red5.server:type=" + cName)); status = true; } catch (Exception e) { log.error("Could not register the {} MBean", clazz.getName(), e); } return status; }
private RedisThrottlerJmxBean registerOrGetJmxBean() { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); try { synchronized (jmxBeanReferenceCountByName) { // Get the reference count for the JMX bean. Integer jmxBeanReferenceCount = jmxBeanReferenceCountByName.get(jmxBeanName); if (jmxBeanReferenceCount == null) { jmxBeanReferenceCount = 0; } // Create or get the JMX bean. RedisThrottlerJmxBean jmxBean; try { jmxBean = new RedisThrottlerInternalJmxBean(); StandardMBean jmxBeanWrapper = new StandardMBean(jmxBean, RedisThrottlerJmxBean.class); mbs.registerMBean(jmxBeanWrapper, jmxBeanName); } catch (InstanceAlreadyExistsException ignored) { jmxBean = JMX.newMBeanProxy(mbs, jmxBeanName, RedisThrottlerJmxBean.class); } // Increment the reference count and return the JMX bean. jmxBeanReferenceCountByName.put(jmxBeanName, jmxBeanReferenceCount + 1); return jmxBean; } } catch (Throwable error) { String message = String.format("failed accessing the JMX bean (jmxBeanName=%s)", jmxBeanName); throw new RuntimeException(message, error); } }