if (name == null) throw new CaptchaServiceException("Service registration name can't be null"); ArrayList mbeanServers = MBeanServerFactory.findMBeanServer(null); if (mbeanServers.size() == 0) { throw new CaptchaServiceException("No current MBean Server, skiping the registering process"); } else { MBeanServer mbeanServer = (MBeanServer) mbeanServers.get(0); try { ObjectName objectName = new ObjectName(name); mbeanServer.registerMBean(service, objectName); } catch (MalformedObjectNameException e) { throw new CaptchaServiceException(e); } catch (InstanceAlreadyExistsException e) { throw new CaptchaServiceException(e); } catch (MBeanRegistrationException e) {    throw new CaptchaServiceException("An unexpected exception has been raised : " + "CaptchaService needs maintenance !", e); } catch (NotCompliantMBeanException e) {  throw new CaptchaServiceException("Exception trying to register the service to" + " the MBean server", e); } } }
}
    public static void registerToMBeanServer(ManageableCaptchaService service, String name)            throws CaptchaServiceException {        if (name == null) throw new CaptchaServiceException("Service registration name can't be null");        ArrayList mbeanServers = MBeanServerFactory.findMBeanServer(null);        if (mbeanServers.size() == 0) {            throw new CaptchaServiceException("No current MBean Server, skiping the registering process");        } else {            MBeanServer mbeanServer = (MBeanServer) mbeanServers.get(0);            try {                ObjectName objectName = new ObjectName(name);                mbeanServer.registerMBean(service, objectName);            } catch (MalformedObjectNameException e) {                throw new CaptchaServiceException(e);            } catch (InstanceAlreadyExistsException e) {                throw new CaptchaServiceException(e);            } catch (MBeanRegistrationException e) {                // this exception should never be raised (raised                // only by an MBean that implements the MBeanRegistration                // interface.                throw new CaptchaServiceException("An unexpected exception has been raised : "                        + "CaptchaService needs maintenance !",                        e);            } catch (NotCompliantMBeanException e) {                // this should never happens                throw new CaptchaServiceException("Exception trying to register the service to"                        + " the MBean server",                        e);            }        }    }