public String registerMBean(Object bean, String name, boolean replace)
            throws MalformedObjectNameException, NotCompliantMBeanException, MBeanRegistrationException{

        synchronized (mBeanServer) {

            ObjectName newBeanName = null;

            try {
                newBeanName = new ObjectName(name);
                beansNames.add(newBeanName); // Saving bean name for possible later cleanups
                mBeanServer.registerMBean(bean, newBeanName);
                return name;
            } catch (InstanceAlreadyExistsException e) {

                beansNames.remove(newBeanName); // Bean not registered, it name is not required for cleanup

                if (replace) {
                    unregisterMBean(name);
                    return registerMBean(bean, name, true);
                } else
                    return registerMBean(bean, resolveDuplicateName(name));

            }

        }

    }
public String registerMBean ( Object bean , String name , boolean replace ) throws MalformedObjectNameException , NotCompliantMBeanException , MBeanRegistrationException { synchronized ( mBeanServer ) { ObjectName newBeanName = null ; try { newBeanName = new ObjectName ( name ) ; beansNames . add ( newBeanName ) ; mBeanServer . registerMBean ( bean , newBeanName ) ; return name ; } catch ( InstanceAlreadyExistsException e ) { beansNames . remove ( newBeanName ) ; if ( replace ) { unregisterMBean ( name ) ; return registerMBean ( bean , name , true ) ; } else return registerMBean ( bean , resolveDuplicateName ( name ) ) ; } } }
Registers MBean in local MBean server.

Method has mechanism to resolve bean name duplication
depends on `replace` parameter of ths method.

If true - old bean be replaced by new one.
If false - prefix been added to bean name from method parameter

Prefix format:
copy{$number}.{$originalName}
where:
$number       - bean copy number (1 for first copy)
$originalName - original bean name passed to this method

@param bean MBean object
@param name MBean name
@param replace controls resolving of bean name duplication.
true  - old bean be replaced by new one.
false - prefix be added to bean name.

@return name of MBean string

@throws MalformedObjectNameException name from parameter is unacceptable as object name
@throws NotCompliantMBeanException thrown by MBeanServer
@throws MBeanRegistrationException thrown by MBeanServer