class JmxRegistryServiceImpl implements JmxRegistryService, JmxManageable, Service {

    @Override
    public ObjectName register(JmxManageable service) {
        final JmxObjectInfo info = service.getJmxObjectInfo();
        validate(info);
        String serviceName = info.getObjectName();
        if (!serviceName.matches("[\\w-]+")) {
            throw new JmxRegistrationException(service.getClass(), serviceName);
        }
        final ObjectName objectName;
        synchronized (INTERNAL_LOCK) {
            if (serviceToName.containsKey(service)) {
                throw new JmxRegistrationException("Already registered instance of " + service.getClass());
            }
            objectName = getObjectName(serviceName, nameToService.keySet());
            addService(objectName, service);

            if (started) {
                try {
                    getMBeanServer().registerMBean(info.getInstance(), objectName);
                    return objectName;
                }
                catch (Exception e) {
                    removeService(objectName);
                    throw new JmxRegistrationException(e);
                }
            }
            else {
                return objectName;
            }
        }
    }

}