Skip to content

Commit

Permalink
[WFLY-11239] Refactoring SAR subsystem to don't use deprecated Servic…
Browse files Browse the repository at this point in the history
…eBuilder methods.
  • Loading branch information
ropalka committed Oct 31, 2018
1 parent 8751fc6 commit 0a2f152
Show file tree
Hide file tree
Showing 12 changed files with 477 additions and 220 deletions.
36 changes: 20 additions & 16 deletions sar/src/main/java/org/jboss/as/service/AbstractService.java
Expand Up @@ -28,41 +28,50 @@
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.jboss.as.server.deployment.SetupAction;
import org.jboss.msc.service.LifecycleContext;
import org.jboss.msc.service.Service;
import org.jboss.msc.value.InjectedValue;
import org.jboss.msc.Service;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StopContext;
import org.wildfly.security.manager.WildFlySecurityManager;

/**
* Abstract service class.
*
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
abstract class AbstractService implements Service<Object> {
abstract class AbstractService implements Service {

private final Object mBeanInstance;
protected final Object mBeanInstance;
private final List<SetupAction> setupActions;
private final ClassLoader mbeanContextClassLoader;
protected final InjectedValue<ExecutorService> executor = new InjectedValue<ExecutorService>();
private final Consumer<Object> mBeanInstanceConsumer;
protected final Supplier<ExecutorService> executorSupplier;


/**
* @param mBeanInstance
* @param setupActions actions to setup the thread local context
*/
protected AbstractService(final Object mBeanInstance, final List<SetupAction> setupActions, final ClassLoader mbeanContextClassLoader) {
protected AbstractService(final Object mBeanInstance, final List<SetupAction> setupActions, final ClassLoader mbeanContextClassLoader, final Consumer<Object> mBeanInstanceConsumer, final Supplier<ExecutorService> executorSupplier) {
this.mBeanInstance = mBeanInstance;
this.setupActions = setupActions;
this.mbeanContextClassLoader = mbeanContextClassLoader;
this.mBeanInstanceConsumer = mBeanInstanceConsumer;
this.executorSupplier = executorSupplier;
}

/**
* {@inheritDoc}
*/
public final Object getValue() {
return mBeanInstance;
@Override
public void start(final StartContext context) {
mBeanInstanceConsumer.accept(mBeanInstance);
}

@Override
public void stop(final StopContext context) {
mBeanInstanceConsumer.accept(null);
}

protected void invokeLifecycleMethod(final Method method, final LifecycleContext context) throws InvocationTargetException, IllegalAccessException {
Expand All @@ -86,9 +95,4 @@ protected void invokeLifecycleMethod(final Method method, final LifecycleContext
}
}
}

public InjectedValue<ExecutorService> getExecutorInjector() {
return executor;
}

}
49 changes: 43 additions & 6 deletions sar/src/main/java/org/jboss/as/service/CreateDestroyService.java
Expand Up @@ -22,9 +22,16 @@

package org.jboss.as.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.jboss.as.naming.ManagedReference;
import org.jboss.as.server.deployment.SetupAction;
Expand All @@ -47,37 +54,41 @@ final class CreateDestroyService extends AbstractService {
private final Method destroyMethod;

private final ServiceComponentInstantiator componentInstantiator;
private final Map<Method, Supplier<Object>> injections = new HashMap<>();
private ManagedReference managedReference;

CreateDestroyService(final Object mBeanInstance, final Method createMethod, final Method destroyMethod, ServiceComponentInstantiator componentInstantiator,
List<SetupAction> setupActions, final ClassLoader mbeanContextClassLoader) {
super(mBeanInstance, setupActions, mbeanContextClassLoader);
List<SetupAction> setupActions, final ClassLoader mbeanContextClassLoader, final Consumer<Object> mBeanInstanceConsumer, final Supplier<ExecutorService> executorSupplier) {
super(mBeanInstance, setupActions, mbeanContextClassLoader, mBeanInstanceConsumer, executorSupplier);
this.createMethod = createMethod;
this.destroyMethod = destroyMethod;
this.componentInstantiator = componentInstantiator;
}

/** {@inheritDoc} */
public void start(final StartContext context) throws StartException {
public void start(final StartContext context) {
super.start(context);
if (SarLogger.ROOT_LOGGER.isTraceEnabled()) {
SarLogger.ROOT_LOGGER.tracef("Creating Service: %s", context.getController().getName());
}
final Runnable task = new Runnable() {
@Override
public void run() {
try {
injectDependencies();
invokeLifecycleMethod(createMethod, context);
if (componentInstantiator != null) {
managedReference = componentInstantiator.initializeInstance(getValue());
managedReference = componentInstantiator.initializeInstance(mBeanInstance);
}
context.complete();
} catch (Throwable e) {
uninjectDependencies();
context.failed(new StartException(SarLogger.ROOT_LOGGER.failedExecutingLegacyMethod("create()"), e));
}
}
};
try {
executor.getValue().submit(task);
executorSupplier.get().submit(task);
} catch (RejectedExecutionException e) {
task.run();
} finally {
Expand All @@ -87,6 +98,7 @@ public void run() {

/** {@inheritDoc} */
public void stop(final StopContext context) {
super.stop(context);
if (SarLogger.ROOT_LOGGER.isTraceEnabled()) {
SarLogger.ROOT_LOGGER.tracef("Destroying Service: %s", context.getController().getName());
}
Expand All @@ -101,17 +113,42 @@ public void run() {
} catch (Exception e) {
SarLogger.ROOT_LOGGER.error(SarLogger.ROOT_LOGGER.failedExecutingLegacyMethod("destroy()"), e);
} finally {
uninjectDependencies();
context.complete();
}
}
};
try {
executor.getValue().submit(task);
executorSupplier.get().submit(task);
} catch (RejectedExecutionException e) {
task.run();
} finally {
context.asynchronous();
}
}

void inject(final Method setter, final Supplier<Object> injectionSupplier) {
injections.put(setter, injectionSupplier);
}

private void injectDependencies() throws IllegalAccessException, InvocationTargetException {
Method setter;
Object arg;
for (final Entry<Method, Supplier<Object>> injection : injections.entrySet()) {
setter = injection.getKey();
arg = injection.getValue().get();
setter.invoke(mBeanInstance, arg);
}
}

private void uninjectDependencies() {
Method setter;
for (final Entry<Method, Supplier<Object>> injection : injections.entrySet()) {
try {
setter = injection.getKey();
setter.invoke(mBeanInstance, (Object[]) null);
} catch (final Throwable ignored) {}
}
}

}
38 changes: 38 additions & 0 deletions sar/src/main/java/org/jboss/as/service/DelegatingSupplier.java
@@ -0,0 +1,38 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2018, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.service;

import java.util.function.Supplier;

/**
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
abstract class DelegatingSupplier implements Supplier<Object> {

protected volatile Supplier<Object> objectSupplier;

void setObjectSupplier(final Supplier<Object> objectSupplier) {
this.objectSupplier = objectSupplier;
}

}

This file was deleted.

124 changes: 124 additions & 0 deletions sar/src/main/java/org/jboss/as/service/MBeanRegistrationService.java
@@ -0,0 +1,124 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2010, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.service;

import static org.jboss.as.service.logging.SarLogger.ROOT_LOGGER;

import java.lang.management.ManagementFactory;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.function.Supplier;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import org.jboss.as.server.deployment.SetupAction;
import org.jboss.msc.Service;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;

/**
* Service used to register and unregister an mbean with an mbean server.
*
* @author John Bailey
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
final class MBeanRegistrationService implements Service {
static final ServiceName SERVICE_NAME = ServiceName.JBOSS.append("mbean", "registration");
private final Supplier<MBeanServer> mBeanServerSupplier;
private final Supplier<Object> objectSupplier;
private final String name;
private ObjectName objectName;
private final List<SetupAction> setupActions;

public MBeanRegistrationService(final String name, final List<SetupAction> setupActions,
final Supplier<MBeanServer> mBeanServerSupplier,
final Supplier<Object> objectSupplier) {
this.name = name;
this.setupActions = setupActions;
this.mBeanServerSupplier = mBeanServerSupplier;
this.objectSupplier = objectSupplier;
}

public synchronized void start(final StartContext context) throws StartException {
final MBeanServer mBeanServer = getMBeanServer();
final Object value = objectSupplier.get();
try {
objectName = new ObjectName(name);
} catch (MalformedObjectNameException e) {
throw ROOT_LOGGER.mbeanRegistrationFailed(e, name);
}
try {
for (SetupAction action : setupActions) {
action.setup(Collections.<String, Object>emptyMap());
}
try {
ROOT_LOGGER.debugf("Registering [%s] with name [%s]", value, objectName);
mBeanServer.registerMBean(value, objectName);
} catch (Exception e) {
throw ROOT_LOGGER.mbeanRegistrationFailed(e, name);
}
} finally {
ListIterator<SetupAction> it = setupActions.listIterator(setupActions.size());
while (it.hasPrevious()) {
SetupAction action = it.previous();
action.teardown(Collections.<String, Object>emptyMap());
}
}
}

public synchronized void stop(final StopContext context) {
if (objectName == null) {
ROOT_LOGGER.cannotUnregisterObject();
}
final MBeanServer mBeanServer = getMBeanServer();
try {
for (SetupAction action : setupActions) {
action.setup(Collections.<String, Object>emptyMap());
}
try {
mBeanServer.unregisterMBean(objectName);
} catch (Exception e) {
ROOT_LOGGER.unregistrationFailure(e, objectName);
}
} finally {
ListIterator<SetupAction> it = setupActions.listIterator(setupActions.size());
while (it.hasPrevious()) {
SetupAction action = it.previous();
action.teardown(Collections.<String, Object>emptyMap());
}
}
}

private MBeanServer getMBeanServer() {
MBeanServer mBeanServer = mBeanServerSupplier.get();
if (mBeanServer == null) {
mBeanServer = ManagementFactory.getPlatformMBeanServer();
}
return mBeanServer;
}

}

0 comments on commit 0a2f152

Please sign in to comment.