private final InternalJMXRegistrator internalJMXRegistrator;
- public BaseJMXRegistrator(MBeanServer configMBeanServer) {
- internalJMXRegistrator = new InternalJMXRegistrator(configMBeanServer);
+ public BaseJMXRegistrator(final MBeanServer configMBeanServer) {
+ internalJMXRegistrator = InternalJMXRegistrator.create(configMBeanServer);
}
- public BaseJMXRegistrator(InternalJMXRegistrator internalJMXRegistrator) {
+ public BaseJMXRegistrator(final InternalJMXRegistrator internalJMXRegistrator) {
this.internalJMXRegistrator = internalJMXRegistrator;
}
- public TransactionJMXRegistrator createTransactionJMXRegistrator(
- String transactionName) {
- return new TransactionJMXRegistrator(
- internalJMXRegistrator.createChild(), transactionName);
+ public TransactionJMXRegistrator createTransactionJMXRegistrator(final String transactionName) {
+ return new TransactionJMXRegistrator(internalJMXRegistrator.createChild(), transactionName);
}
public ModuleJMXRegistrator createModuleJMXRegistrator() {
return new ModuleJMXRegistrator(internalJMXRegistrator.createChild());
}
- public RootRuntimeBeanRegistratorImpl createRuntimeBeanRegistrator(
- ModuleIdentifier moduleIdentifier) {
- return new RootRuntimeBeanRegistratorImpl(internalJMXRegistrator.createChild(),
- moduleIdentifier);
+ public RootRuntimeBeanRegistratorImpl createRuntimeBeanRegistrator(final ModuleIdentifier moduleIdentifier) {
+ return new RootRuntimeBeanRegistratorImpl(internalJMXRegistrator.createChild(), moduleIdentifier);
}
- public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
+ public Set<ObjectName> queryNames(final ObjectName name, final QueryExp query) {
return internalJMXRegistrator.queryNames(name, query);
}
public class ConfigRegistryJMXRegistrator implements AutoCloseable {
private final InternalJMXRegistrator internalJMXRegistrator;
- public ConfigRegistryJMXRegistrator(MBeanServer configMBeanServer) {
- internalJMXRegistrator = new InternalJMXRegistrator(configMBeanServer);
+ public ConfigRegistryJMXRegistrator(final MBeanServer configMBeanServer) {
+ internalJMXRegistrator = InternalJMXRegistrator.create(configMBeanServer);
}
- public AutoCloseable registerToJMX(ConfigRegistryImplMXBean configRegistry)
+ public AutoCloseable registerToJMX(final ConfigRegistryImplMXBean configRegistry)
throws InstanceAlreadyExistsException {
- return internalJMXRegistrator.registerMBean(configRegistry,
- ConfigRegistryMXBean.OBJECT_NAME);
+ return internalJMXRegistrator.registerMBean(configRegistry, ConfigRegistryMXBean.OBJECT_NAME);
}
- public AutoCloseable registerToJMXNoNotifications(ConfigRegistryImplMXBean configRegistry)
+ public AutoCloseable registerToJMXNoNotifications(final ConfigRegistryImplMXBean configRegistry)
throws InstanceAlreadyExistsException {
- return internalJMXRegistrator.registerMBean(configRegistry,
- ConfigRegistryMXBean.OBJECT_NAME_NO_NOTIFICATIONS);
+ return internalJMXRegistrator.registerMBean(configRegistry, ConfigRegistryMXBean.OBJECT_NAME_NO_NOTIFICATIONS);
}
@Override
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.config.manager.impl.jmx;
+
+import com.google.common.base.Preconditions;
+import javax.management.ObjectName;
+
+final class InternalJMXRegistration implements AutoCloseable {
+ private final InternalJMXRegistrator internalJMXRegistrator;
+ private final ObjectName on;
+
+ InternalJMXRegistration(final InternalJMXRegistrator internalJMXRegistrator, final ObjectName on) {
+ this.internalJMXRegistrator = Preconditions.checkNotNull(internalJMXRegistrator);
+ this.on = on;
+ }
+
+ @Override
+ public void close() {
+ internalJMXRegistrator.unregisterMBean(on);
+ }
+}
*/
package org.opendaylight.controller.config.manager.impl.jmx;
-import java.io.Closeable;
+import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class InternalJMXRegistrator implements Closeable {
- private static final Logger LOG = LoggerFactory
- .getLogger(InternalJMXRegistrator.class);
- private final MBeanServer configMBeanServer;
+abstract class InternalJMXRegistrator implements AutoCloseable {
+ private static final class Root extends InternalJMXRegistrator {
+ private final MBeanServer mbeanServer;
- public InternalJMXRegistrator(MBeanServer configMBeanServer) {
- this.configMBeanServer = configMBeanServer;
+ Root(final MBeanServer mbeanServer) {
+ this.mbeanServer = Preconditions.checkNotNull(mbeanServer);
+ }
+
+ @Override
+ MBeanServer getMBeanServer() {
+ return mbeanServer;
+ }
}
- static class InternalJMXRegistration implements AutoCloseable {
- private final InternalJMXRegistrator internalJMXRegistrator;
- private final ObjectName on;
+ private static final class Nested extends InternalJMXRegistrator {
+ private final InternalJMXRegistrator parent;
- InternalJMXRegistration(InternalJMXRegistrator internalJMXRegistrator,
- ObjectName on) {
- this.internalJMXRegistrator = internalJMXRegistrator;
- this.on = on;
+ Nested(final InternalJMXRegistrator parent) {
+ this.parent = Preconditions.checkNotNull(parent);
+ }
+
+ @Override
+ MBeanServer getMBeanServer() {
+ return parent.getMBeanServer();
}
@Override
public void close() {
- internalJMXRegistrator.unregisterMBean(on);
+ try {
+ super.close();
+ } finally {
+ parent.removeChild(this);
+ }
}
}
+ private static final Logger LOG = LoggerFactory.getLogger(InternalJMXRegistrator.class);
@GuardedBy("this")
- private final Set<ObjectName> registeredObjectNames = new HashSet<>();
+ private final Set<ObjectName> registeredObjectNames = new HashSet<>(1);
@GuardedBy("this")
- private final List<InternalJMXRegistrator> children = new ArrayList<>();
+ private final List<Nested> children = new ArrayList<>();
+
+ public static InternalJMXRegistrator create(final MBeanServer configMBeanServer) {
+ return new Root(configMBeanServer);
+ }
- public synchronized InternalJMXRegistration registerMBean(Object object,
- ObjectName on) throws InstanceAlreadyExistsException {
+ public final synchronized InternalJMXRegistration registerMBean(final Object object, final ObjectName on)
+ throws InstanceAlreadyExistsException {
try {
- configMBeanServer.registerMBean(object, on);
- } catch (MBeanRegistrationException | NotCompliantMBeanException e) {
- throw new IllegalStateException(e);
+ getMBeanServer().registerMBean(object, on);
+ } catch (NotCompliantMBeanException e) {
+ throw new IllegalArgumentException("Object does not comply to JMX", e);
+ } catch (MBeanRegistrationException e) {
+ throw new IllegalStateException("Failed to register " + on, e);
}
+
registeredObjectNames.add(on);
return new InternalJMXRegistration(this, on);
}
- private synchronized void unregisterMBean(ObjectName on) {
+ final synchronized void unregisterMBean(final ObjectName on) {
// first check that on was registered using this instance
boolean removed = registeredObjectNames.remove(on);
- if (!removed) {
- throw new IllegalStateException("Cannot unregister - ObjectName not found in 'registeredObjectNames': " + on);
- }
+ Preconditions.checkState(removed, "Cannot unregister - ObjectName not found in 'registeredObjectNames': {}", on);
+
try {
- configMBeanServer.unregisterMBean(on);
- } catch (InstanceNotFoundException | MBeanRegistrationException e) {
- throw new IllegalStateException(e);
+ getMBeanServer().unregisterMBean(on);
+ } catch (InstanceNotFoundException e) {
+ LOG.warn("MBean {} not found on server", on, e);
+ } catch (MBeanRegistrationException e) {
+ throw new IllegalStateException("Failed to unregister MBean " + on, e);
}
}
- public synchronized InternalJMXRegistrator createChild() {
- InternalJMXRegistrator child = new InternalJMXRegistrator(configMBeanServer);
+ public final synchronized InternalJMXRegistrator createChild() {
+ final Nested child = new Nested(this);
children.add(child);
return child;
}
* Allow close to be called multiple times.
*/
@Override
- public synchronized void close() {
- // close children
- for (InternalJMXRegistrator child : children) {
- child.close();
- }
- // close registered ONs
- for (ObjectName on : registeredObjectNames) {
- try {
- configMBeanServer.unregisterMBean(on);
- } catch (Exception e) {
- LOG.warn("Ignoring error while unregistering {}", on, e);
- }
- }
- registeredObjectNames.clear();
+ public void close() {
+ internalClose();
}
- public <T> T newMBeanProxy(ObjectName objectName, Class<T> interfaceClass) {
- return JMX.newMBeanProxy(configMBeanServer, objectName, interfaceClass);
+ public final <T> T newMBeanProxy(final ObjectName objectName, final Class<T> interfaceClass) {
+ return JMX.newMBeanProxy(getMBeanServer(), objectName, interfaceClass);
}
- public <T> T newMBeanProxy(ObjectName objectName, Class<T> interfaceClass,
- boolean notificationBroadcaster) {
- return JMX.newMBeanProxy(configMBeanServer, objectName, interfaceClass,
- notificationBroadcaster);
+ public final <T> T newMBeanProxy(final ObjectName objectName, final Class<T> interfaceClass,
+ final boolean notificationBroadcaster) {
+ return JMX.newMBeanProxy(getMBeanServer(), objectName, interfaceClass, notificationBroadcaster);
}
- public <T> T newMXBeanProxy(ObjectName objectName, Class<T> interfaceClass) {
- return JMX
- .newMXBeanProxy(configMBeanServer, objectName, interfaceClass);
+ public final <T> T newMXBeanProxy(final ObjectName objectName, final Class<T> interfaceClass) {
+ return JMX.newMXBeanProxy(getMBeanServer(), objectName, interfaceClass);
}
- public <T> T newMXBeanProxy(ObjectName objectName, Class<T> interfaceClass,
- boolean notificationBroadcaster) {
- return JMX.newMXBeanProxy(configMBeanServer, objectName,
- interfaceClass, notificationBroadcaster);
+ public final <T> T newMXBeanProxy(final ObjectName objectName, final Class<T> interfaceClass,
+ final boolean notificationBroadcaster) {
+ return JMX.newMXBeanProxy(getMBeanServer(), objectName, interfaceClass, notificationBroadcaster);
}
- public Set<ObjectName> getRegisteredObjectNames() {
+ public final Set<ObjectName> getRegisteredObjectNames() {
return Collections.unmodifiableSet(registeredObjectNames);
}
- public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
- Set<ObjectName> result = configMBeanServer.queryNames(name, query);
+ public final Set<ObjectName> queryNames(final ObjectName name, final QueryExp query) {
// keep only those that were registered using this instance
- return getSameNames(result);
+ return getSameNames(getMBeanServer().queryNames(name, query));
+ }
+
+ abstract MBeanServer getMBeanServer();
+
+ synchronized void removeChild(final InternalJMXRegistrator child) {
+ children.remove(child);
}
- private synchronized Set<ObjectName> getSameNames(Set<ObjectName> superSet) {
- Set<ObjectName> result = new HashSet<>(superSet);
+ private synchronized void internalClose() {
+ // close all children
+ for (InternalJMXRegistrator child : children) {
+ // This bypasses the call to removeChild(), preventing a potential deadlock when children are being closed
+ // concurrently
+ child.internalClose();
+ }
+ children.clear();
+
+ // close registered ONs
+ for (ObjectName on : registeredObjectNames) {
+ try {
+ getMBeanServer().unregisterMBean(on);
+ } catch (Exception e) {
+ LOG.warn("Ignoring error while unregistering {}", on, e);
+ }
+ }
+ registeredObjectNames.clear();
+ }
+
+ private synchronized Set<ObjectName> getSameNames(final Set<ObjectName> superSet) {
+ final Set<ObjectName> result = new HashSet<>(superSet);
result.retainAll(registeredObjectNames);
+
for (InternalJMXRegistrator child : children) {
result.addAll(child.getSameNames(superSet));
}
return result;
}
-
}
*/
package org.opendaylight.controller.config.manager.impl.jmx;
+import com.google.common.base.Preconditions;
import java.io.Closeable;
import javax.annotation.concurrent.ThreadSafe;
import javax.management.InstanceAlreadyExistsException;
import javax.management.ObjectName;
import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
-import org.opendaylight.controller.config.manager.impl.jmx.InternalJMXRegistrator.InternalJMXRegistration;
/**
* This subclass is used for registering readable module into JMX, it is also
public class ModuleJMXRegistrator implements Closeable {
private final InternalJMXRegistrator childJMXRegistrator;
- public ModuleJMXRegistrator(InternalJMXRegistrator internalJMXRegistrator) {
- this.childJMXRegistrator = internalJMXRegistrator.createChild();
+ ModuleJMXRegistrator(final InternalJMXRegistrator internalJMXRegistrator) {
+ this.childJMXRegistrator = Preconditions.checkNotNull(internalJMXRegistrator);
}
static class ModuleJMXRegistration implements AutoCloseable {
private final InternalJMXRegistration internalJMXRegistration;
- ModuleJMXRegistration(InternalJMXRegistration registration) {
+ ModuleJMXRegistration(final InternalJMXRegistration registration) {
this.internalJMXRegistration = registration;
}
}
}
- public ModuleJMXRegistration registerMBean(Object object, ObjectName on)
+ public ModuleJMXRegistration registerMBean(final Object object, final ObjectName on)
throws InstanceAlreadyExistsException {
ObjectNameUtil.checkType(on, ObjectNameUtil.TYPE_MODULE);
if (ObjectNameUtil.getTransactionName(on) != null) {
*/
package org.opendaylight.controller.config.manager.impl.jmx;
+import com.google.common.base.Preconditions;
import java.util.Collections;
import javax.management.InstanceAlreadyExistsException;
import javax.management.ObjectName;
private final ModuleIdentifier moduleIdentifier;
private final ObjectName defaultRuntimeON;
- public RootRuntimeBeanRegistratorImpl(
- InternalJMXRegistrator internalJMXRegistrator,
- ModuleIdentifier moduleIdentifier) {
- this.internalJMXRegistrator = internalJMXRegistrator;
+ RootRuntimeBeanRegistratorImpl(final InternalJMXRegistrator internalJMXRegistrator,
+ final ModuleIdentifier moduleIdentifier) {
+ this.internalJMXRegistrator = Preconditions.checkNotNull(internalJMXRegistrator);
this.moduleIdentifier = moduleIdentifier;
defaultRuntimeON = ObjectNameUtil.createRuntimeBeanName(
moduleIdentifier.getFactoryName(),
}
@Override
- public HierarchicalRuntimeBeanRegistrationImpl registerRoot(
- RuntimeBean mxBean) {
+ public HierarchicalRuntimeBeanRegistrationImpl registerRoot(final RuntimeBean mxBean) {
try {
internalJMXRegistrator.registerMBean(mxBean, defaultRuntimeON);
} catch (InstanceAlreadyExistsException e) {
internalJMXRegistrator.close();
}
- static IllegalStateException sanitize(InstanceAlreadyExistsException e,
- ModuleIdentifier moduleIdentifier, ObjectName on) {
+ static IllegalStateException sanitize(final InstanceAlreadyExistsException e,
+ final ModuleIdentifier moduleIdentifier, final ObjectName on) {
throw new IllegalStateException("Could not register runtime bean in "
+ moduleIdentifier + " under name " + on, e);
import javax.management.InstanceAlreadyExistsException;
import javax.management.ObjectName;
import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
-import org.opendaylight.controller.config.manager.impl.jmx.InternalJMXRegistrator.InternalJMXRegistration;
public interface ServiceReferenceRegistrator extends AutoCloseable {
*/
package org.opendaylight.controller.config.manager.impl.jmx;
+import com.google.common.base.Preconditions;
import java.io.Closeable;
import java.util.Set;
import javax.management.InstanceAlreadyExistsException;
import javax.management.ObjectName;
import javax.management.QueryExp;
import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
-import org.opendaylight.controller.config.manager.impl.jmx.InternalJMXRegistrator.InternalJMXRegistration;
/**
* Contains constraints on passed {@link ObjectName} parameters. Only allow (un)
private final InternalJMXRegistrator childJMXRegistrator;
private final String transactionName;
- TransactionJMXRegistrator(InternalJMXRegistrator internalJMXRegistrator,
- String transactionName) {
- this.childJMXRegistrator = internalJMXRegistrator.createChild();
+ TransactionJMXRegistrator(final InternalJMXRegistrator internalJMXRegistrator,
+ final String transactionName) {
+ this.childJMXRegistrator = Preconditions.checkNotNull(internalJMXRegistrator);
this.transactionName = transactionName;
}
public static class TransactionJMXRegistration implements AutoCloseable {
private final InternalJMXRegistration registration;
- TransactionJMXRegistration(InternalJMXRegistration registration) {
+ TransactionJMXRegistration(final InternalJMXRegistration registration) {
this.registration = registration;
}
}
}
- public TransactionJMXRegistration registerMBean(Object object, ObjectName on)
+ public TransactionJMXRegistration registerMBean(final Object object, final ObjectName on)
throws InstanceAlreadyExistsException {
if (!transactionName.equals(ObjectNameUtil.getTransactionName(on))) {
throw new IllegalArgumentException(
childJMXRegistrator.registerMBean(object, on));
}
- public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
+ public Set<ObjectName> queryNames(final ObjectName name, final QueryExp query) {
return childJMXRegistrator.queryNames(name, query);
}
import javax.management.ObjectName;
import javax.management.QueryExp;
import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
-import org.opendaylight.controller.config.manager.impl.jmx.InternalJMXRegistrator.InternalJMXRegistration;
public class TransactionModuleJMXRegistrator implements Closeable, NestableJMXRegistrator {
private final InternalJMXRegistrator currentJMXRegistrator;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
-
import com.google.common.base.Preconditions;
import java.io.File;
import java.io.IOException;
import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
-import org.opendaylight.controller.config.manager.impl.jmx.InternalJMXRegistrator;
import org.opendaylight.controller.config.manager.impl.jmx.JMXNotifierConfigRegistry;
import org.opendaylight.controller.config.manager.impl.osgi.mapping.BindingContextProvider;
import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolImpl;
* {@link #initConfigTransactionManagerImpl(org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver)}
* typically during setting up the each test.
*/
-public abstract class AbstractConfigTest extends
- AbstractLockedPlatformMBeanServerTest {
+public abstract class AbstractConfigTest extends AbstractLockedPlatformMBeanServerTest {
protected ConfigRegistryJMXRegistrator configRegistryJMXRegistrator;
protected ConfigRegistryImpl configRegistry;
private JMXNotifierConfigRegistry notifyingConfigRegistry;
protected ConfigRegistryJMXClient configRegistryClient;
protected BaseJMXRegistrator baseJmxRegistrator;
- protected InternalJMXRegistrator internalJmxRegistrator;
@Mock
protected BundleContext mockedContext;
@Mock
protected ServiceRegistration<?> mockedServiceRegistration;
+ protected BundleContextServiceRegistrationHandler currentBundleContextServiceRegistrationHandler;
@Before
public void setUpMocks() {
MockitoAnnotations.initMocks(this);
}
-
// Default handler for OSGi service registration
protected static class RecordingBundleContextServiceRegistrationHandler implements BundleContextServiceRegistrationHandler {
private final List<RegistrationHolder> registrations = new LinkedList<>();
@Override
- public void handleServiceRegistration(Class<?> clazz, Object serviceInstance, Dictionary<String, ?> props) {
-
+ public void handleServiceRegistration(final Class<?> clazz, final Object serviceInstance, final Dictionary<String, ?> props) {
registrations.add(new RegistrationHolder(clazz, serviceInstance, props));
}
protected final Object instance;
protected final Dictionary<String, ?> props;
- public RegistrationHolder(Class<?> clazz, Object instance, Dictionary<String, ?> props) {
+ public RegistrationHolder(final Class<?> clazz, final Object instance, final Dictionary<String, ?> props) {
this.clazz = clazz;
this.instance = instance;
this.props = props;
}
}
-
}
- protected BundleContextServiceRegistrationHandler currentBundleContextServiceRegistrationHandler;
-
- protected BundleContextServiceRegistrationHandler getBundleContextServiceRegistrationHandler(Class<?> serviceType) {
+ protected BundleContextServiceRegistrationHandler getBundleContextServiceRegistrationHandler(final Class<?> serviceType) {
return currentBundleContextServiceRegistrationHandler;
}
// this method should be called in @Before
- protected void initConfigTransactionManagerImpl(
- ModuleFactoriesResolver resolver) {
+ protected void initConfigTransactionManagerImpl(final ModuleFactoriesResolver resolver) {
- final MBeanServer platformMBeanServer = ManagementFactory
- .getPlatformMBeanServer();
+ final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(platformMBeanServer);
initBundleContext();
- internalJmxRegistrator = new InternalJMXRegistrator(platformMBeanServer);
- baseJmxRegistrator = new BaseJMXRegistrator(internalJmxRegistrator);
+ baseJmxRegistrator = new BaseJMXRegistrator(platformMBeanServer);
configRegistry = new ConfigRegistryImpl(resolver, platformMBeanServer, baseJmxRegistrator, new BindingContextProvider() {
@Override
transaction.commit();
}
- protected void assertStatus(CommitStatus status, int expectedNewInstances,
- int expectedRecreatedInstances, int expectedReusedInstances) {
+ protected void assertStatus(final CommitStatus status, final int expectedNewInstances,
+ final int expectedRecreatedInstances, final int expectedReusedInstances) {
assertEquals("New instances mismatch in " + status, expectedNewInstances, status.getNewInstances().size());
- assertEquals("Recreated instances mismatch in " + status, expectedRecreatedInstances, status.getRecreatedInstances()
- .size());
- assertEquals("Reused instances mismatch in " + status, expectedReusedInstances, status.getReusedInstances()
- .size());
+ assertEquals("Recreated instances mismatch in " + status, expectedRecreatedInstances,
+ status.getRecreatedInstances().size());
+ assertEquals("Reused instances mismatch in " + status, expectedReusedInstances,
+ status.getReusedInstances().size());
}
- protected void assertBeanCount(int i, String configMXBeanName) {
- assertEquals(i, configRegistry.lookupConfigBeans(configMXBeanName)
- .size());
+ protected void assertBeanCount(final int i, final String configMXBeanName) {
+ assertEquals(i, configRegistry.lookupConfigBeans(configMXBeanName).size());
}
/**
* @param implementationName
* @return
*/
- protected ClassBasedModuleFactory createClassBasedCBF(
- Class<? extends Module> configBeanClass, String implementationName) {
+ protected ClassBasedModuleFactory createClassBasedCBF(final Class<? extends Module> configBeanClass,
+ final String implementationName) {
return new ClassBasedModuleFactory(implementationName, configBeanClass);
}
}
public static interface BundleContextServiceRegistrationHandler {
-
void handleServiceRegistration(Class<?> clazz, Object serviceInstance, Dictionary<String, ?> props);
-
}
private class RegisterServiceAnswer implements Answer<ServiceRegistration<?>> {
-
@Override
- public ServiceRegistration<?> answer(InvocationOnMock invocation) throws Throwable {
+ public ServiceRegistration<?> answer(final InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Preconditions.checkArgument(args.length == 3, "Unexpected arguments size (expected 3 was %s)", args.length);
Object serviceTypeRaw = args[0];
Object serviceInstance = args[1];
+ @SuppressWarnings("unchecked")
Dictionary<String, ?> props = (Dictionary<String, ?>) args[2];
if (serviceTypeRaw instanceof Class) {
Class<?> serviceType = (Class<?>) serviceTypeRaw;
invokeServiceHandler(serviceInstance, serviceType, props);
-
- } else if(serviceTypeRaw instanceof String[]) {
+ } else if (serviceTypeRaw instanceof String[]) {
for (String className : (String[]) serviceTypeRaw) {
invokeServiceHandler(serviceInstance, className, props);
}
throw new IllegalStateException("Not handling service registration of type, Unknown type" + serviceTypeRaw);
}
-
return mockedServiceRegistration;
}
- public void invokeServiceHandler(Object serviceInstance, String className, Dictionary<String, ?> props) {
+ public void invokeServiceHandler(final Object serviceInstance, final String className, final Dictionary<String, ?> props) {
try {
Class<?> serviceType = Class.forName(className);
invokeServiceHandler(serviceInstance, serviceType, props);
}
}
- private void invokeServiceHandler(Object serviceInstance, Class<?> serviceType, Dictionary<String, ?> props) {
+ private void invokeServiceHandler(final Object serviceInstance, final Class<?> serviceType, final Dictionary<String, ?> props) {
BundleContextServiceRegistrationHandler serviceRegistrationHandler = getBundleContextServiceRegistrationHandler(serviceType);
if (serviceRegistrationHandler != null) {
* @param innerObject jmx proxy which will be wrapped and returned
*/
protected <T> T rethrowCause(final T innerObject) {
-
- Object proxy = Proxy.newProxyInstance(innerObject.getClass().getClassLoader(),
+ @SuppressWarnings("unchecked")
+ final T proxy = (T)Proxy.newProxyInstance(innerObject.getClass().getClassLoader(),
innerObject.getClass().getInterfaces(), new InvocationHandler() {
@Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
try {
return method.invoke(innerObject, args);
} catch (InvocationTargetException e) {
}
}
);
- return (T) proxy;
+ return proxy;
}
/**
* @param dir to be cleaned
* @throws IOException
*/
- protected void cleanDirectory(File dir) throws IOException {
+ protected void cleanDirectory(final File dir) throws IOException {
if (!dir.isDirectory()) {
throw new IllegalStateException("dir must be a directory");
}
file.delete();
}
}
-
}