2 Replies Latest reply on Jan 9, 2004 6:34 PM by John Emerson

    how to 'clone' an XMBean

    John Emerson Newbie

      I have an XMBean that deploys without problems and runs correctly. I want to use it as a template to create a set of 'cloned' MBeans that differ only in ObjectName. I created an Operation on the XMBean called 'clone' that almost works:

       void clone() {
       ObjectName svc = this.getServiceName();
       try {
       MBeanServer server = this.getServer();
       MBeanInfo info = server.getMBeanInfo(svc);
       XMBean xmb = new XMBean((ModelMBeanInfo) info);
       ObjectName newObjectName = new ObjectName(svc.getDomain(), "name", "clone");
       server.registerMBean(xmb, newObjectName);
       } catch (Exception e) {

      The registerMBean() call fails, I think since I should pass ModelMBeanInfo to the XMBean constructor, not MBeanInfo. I get no runtime cast exception.

      Is this approach viable, and if so, how can I actually get a hold of the ModelMBeanInfo required to create 'xmb'? If not, what is the right way to go about this?

      Thanks in advance,

        • 1. Re: how to 'clone' an XMBean

          Where do you set the resource reference your XMBean is supposed to manage?

          • 2. Re: how to 'clone' an XMBean
            John Emerson Newbie

            The following appears to solve this problem. I'd appreciate any comments. Thanks in advance,

            // get the ModelMBeanInfo, is there any easier way???
            MBeanRegistry registry = (MBeanRegistry) MBeanProxyExt.create(MBeanRegistry.class,
            ServerConstants.MBEAN_REGISTRY, server);
            MBeanEntry entry = registry.get(this.serviceName);
            XMBean myXMBean = (XMBean) entry.getMBean();
            ModelMBeanInfo minfo = myXMBean.getModelMBeanInfo();

            // create a new instance of the invoking subclass, clone isn't supported
            Object newInstance = this.getClass().getConstructor(new Class[] {}).newInstance(new Object[] {});

            // create the new XMBean with the newInstance as its resource
            XMBean xmb = new XMBean(newInstance, OBJECT_REF);

            // clone the ModelMBeanInfo, is this necessary???? It's probably cloned in setModelMBeanInfo below - check
            minfo = (ModelMBeanInfo) minfo.clone();

            // set the persist name to point to a new file according to 'newName'
            minfo.getMBeanDescriptor().setField(ModelMBeanConstants.PERSIST_NAME, newName + ".ser");

            // set the management interface

            // register the new bean with the server
            server.registerMBean(xmb, newObjName);