Permalink
Browse files

AS7-6246: on unbind remove binding service name from RuntimeBindRelea…

…seService; on RuntimeBindReleaseService skip already removed services; adds writable jndi support for mbean registration related callbacks; other minor fixes/enhancements to writable service store.
  • Loading branch information...
1 parent 2bd88a3 commit 779c87c50cf16ac11b5741936f443c2ce5b02909 @emmartins emmartins committed with n1hility Dec 23, 2012
@@ -43,5 +43,6 @@
<module name="org.jboss.msc"/>
<module name="org.jboss.logging"/>
<module name="org.jboss.remoting3.remoting-jmx" services="import" />
+ <module name="org.jboss.as.naming"/>
</dependencies>
</module>
View
@@ -90,6 +90,10 @@
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
+ <artifactId>jboss-as-naming</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.as</groupId>
<artifactId>jboss-as-subsystem-test</artifactId>
<scope>test</scope>
</dependency>
@@ -29,6 +29,8 @@
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
+
+import org.jboss.as.naming.WritableServiceBasedNamingStore;
import org.jboss.msc.inject.Injector;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceName;
@@ -49,14 +51,16 @@
private final InjectedValue<T> value = new InjectedValue<T>();
private final String name;
private ObjectName objectName;
+ private final ServiceName duServiceName;
/**
* Create an instance.
*
* @param name The name to use as an ObjectName
*/
- public MBeanRegistrationService(final String name) {
+ public MBeanRegistrationService(final String name, final ServiceName duServiceName) {
this.name = name;
+ this.duServiceName = duServiceName;
}
/**
@@ -65,8 +69,8 @@ public MBeanRegistrationService(final String name) {
* @param name The name to use as an ObjectName
* @param value The object to register
*/
- public MBeanRegistrationService(final String name, final Value<T> value) {
- this.name = name;
+ public MBeanRegistrationService(final String name, final ServiceName duServiceName, final Value<T> value) {
+ this(name, duServiceName);
this.value.inject(value.getValue());
}
@@ -85,11 +89,14 @@ public synchronized void start(final StartContext context) throws StartException
throw MESSAGES.mbeanRegistrationFailed(e, name);
}
+ WritableServiceBasedNamingStore.pushOwner(duServiceName);
try {
ROOT_LOGGER.debugf("Registering [%s] with name [%s]", value, objectName);
mBeanServer.registerMBean(value, objectName);
} catch (Exception e) {
throw MESSAGES.mbeanRegistrationFailed(e, name);
+ } finally {
+ WritableServiceBasedNamingStore.popOwner();
}
}
@@ -103,10 +110,13 @@ public synchronized void stop(StopContext context) {
ROOT_LOGGER.cannotUnregisterObject();
}
final MBeanServer mBeanServer = getMBeanServer();
+ WritableServiceBasedNamingStore.pushOwner(duServiceName);
try {
mBeanServer.unregisterMBean(objectName);
} catch (Exception e) {
ROOT_LOGGER.unregistrationFailure(e, objectName);
+ } finally {
+ WritableServiceBasedNamingStore.popOwner();
}
}
@@ -58,10 +58,14 @@ public WritableServiceBasedNamingStore(ServiceRegistry serviceRegistry, ServiceN
this.serviceTarget = serviceTarget;
}
- @SuppressWarnings("unchecked")
public void bind(final Name name, final Object object) throws NamingException {
final ServiceName deploymentUnitServiceName = requireOwner();
final ServiceName bindName = buildServiceName(name);
+ bind(name, bindName, object, deploymentUnitServiceName);
+ }
+
+ @SuppressWarnings("unchecked")
+ private void bind(final Name name, final ServiceName bindName, final Object object, final ServiceName deploymentUnitServiceName) throws NamingException {
try {
final BinderService binderService = new BinderService(name.toString());
final BindListener listener = new BindListener();
@@ -86,31 +90,40 @@ public void bind(final Name name, final Object object, final Class<?> bindType)
}
public void rebind(Name name, Object object) throws NamingException {
+ final ServiceName deploymentUnitServiceName = requireOwner();
+ final ServiceName bindName = buildServiceName(name);
try {
- unbind(name);
+ unbind(name, bindName);
} catch (NamingException ignore) {
// rebind may fail if there is no existing binding
}
-
- bind(name, object);
+ bind(name, bindName, object, deploymentUnitServiceName);
}
public void rebind(final Name name, final Object object, final Class<?> bindType) throws NamingException {
rebind(name, object);
}
+ @SuppressWarnings("unchecked")
public void unbind(final Name name) throws NamingException {
- requireOwner();
+ final ServiceName deploymentUnitServiceName = requireOwner();
final ServiceName bindName = buildServiceName(name);
+ // do the unbinding
+ unbind(name, bindName);
+ // remove the service name from runtime bindings management service
+ final Set<ServiceName> duBindingReferences = (Set<ServiceName>) getServiceRegistry().getService(JndiNamingDependencyProcessor.serviceName(deploymentUnitServiceName)).getValue();
+ duBindingReferences.remove(bindName);
+ }
+ private void unbind(final Name name, final ServiceName bindName) throws NamingException {
final ServiceController<?> controller = getServiceRegistry().getService(bindName);
if (controller == null) {
throw MESSAGES.cannotResolveService(bindName);
}
-
final UnbindListener listener = new UnbindListener();
controller.addListener(listener);
try {
+ // when added, the listener stops the binding service
listener.await();
} catch (Exception e) {
throw namingException("Failed to unbind [" + bindName + "]", e);
@@ -29,6 +29,7 @@
import org.jboss.as.naming.service.BinderService;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceContainer;
+import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
@@ -66,7 +67,10 @@ public void stop(StopContext context) {
ServiceContainer container = context.getController().getServiceContainer();
for (ServiceName serviceName : serviceNames) {
try {
- ((BinderService) container.getService(serviceName).getService()).release();
+ final ServiceController<?> serviceController = container.getService(serviceName);
+ if(serviceController != null) {
+ ((BinderService)serviceController.getService()).release();
+ }
} catch (Throwable e) {
NamingLogger.ROOT_LOGGER.failedToReleaseBinderService(e);
}
@@ -58,11 +58,14 @@ public final Object getValue() {
protected void invokeLifecycleMethod(final Method method, final LifecycleContext context) throws InvocationTargetException, IllegalAccessException {
if (method != null) {
WritableServiceBasedNamingStore.pushOwner(duServiceName);
- final ClassLoader old = SecurityActions.setThreadContextClassLoader(mBeanInstance.getClass().getClassLoader());
try {
- method.invoke(mBeanInstance);
+ final ClassLoader old = SecurityActions.setThreadContextClassLoader(mBeanInstance.getClass().getClassLoader());
+ try {
+ method.invoke(mBeanInstance);
+ } finally {
+ SecurityActions.resetThreadContextClassLoader(old);
+ }
} finally {
- SecurityActions.resetThreadContextClassLoader(old);
WritableServiceBasedNamingStore.popOwner();
}
}
@@ -61,6 +61,11 @@
private boolean installed;
/**
+ * the msc deployable unit service name to be used on writable naming store invocation context setup
+ */
+ private final ServiceName duServiceName;
+
+ /**
*
* @param mBeanName
* @param mBeanInstance
@@ -99,6 +104,7 @@
this.mBeanName = mBeanName;
this.target = target;
+ this.duServiceName = duServiceName;
}
Service<Object> getCreateDestroyService() {
@@ -130,7 +136,7 @@ void install() {
startStopServiceBuilder.install();
// Add service to register the mbean in the mbean server
- final MBeanRegistrationService<Object> mbeanRegistrationService = new MBeanRegistrationService<Object>(mBeanName);
+ final MBeanRegistrationService<Object> mbeanRegistrationService = new MBeanRegistrationService<Object>(mBeanName, duServiceName);
target.addService(MBeanRegistrationService.SERVICE_NAME.append(mBeanName), mbeanRegistrationService)
.addDependency(MBeanServerService.SERVICE_NAME, MBeanServer.class, mbeanRegistrationService.getMBeanServerInjector())
.addDependency(startStopServiceName, Object.class, mbeanRegistrationService.getValueInjector())
@@ -24,17 +24,21 @@
import javax.management.Attribute;
import javax.management.ObjectName;
+import javax.naming.InitialContext;
import org.jboss.system.ServiceMBeanSupport;
/**
* An MBean that extends legacy {@link ServiceMBeanSupport}.
- *
+ *
* @author Eduardo Martins
- *
+ *
*/
public class TestService extends ServiceMBeanSupport implements TestServiceMBean {
+ private static final String NAME = "java:global/env/foo/legacy";
+ private static final String VALUE = "BAR";
+
@Override
protected void createService() throws Exception {
getLog().info("createService()");
@@ -44,12 +48,14 @@ protected void createService() throws Exception {
@Override
protected void startService() throws Exception {
getLog().info("startService()");
+ new InitialContext().bind(NAME, VALUE);
setTestResultMBeanAttribute("StartServiceInvoked", true);
}
@Override
protected void stopService() throws Exception {
getLog().info("stopService()");
+ new InitialContext().unbind(NAME);
setTestResultMBeanAttribute("StopServiceInvoked", true);
}

0 comments on commit 779c87c

Please sign in to comment.