Skip to content

Commit

Permalink
Add service dump mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed Oct 20, 2010
1 parent 53bff15 commit bb5e2be
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/jboss/msc/service/ServiceContainer.java
Expand Up @@ -22,6 +22,7 @@

package org.jboss.msc.service;

import java.io.PrintStream;
import java.util.concurrent.Executor;

/**
Expand Down Expand Up @@ -61,6 +62,18 @@ public interface ServiceContainer {

ServiceController<?> getService(ServiceName serviceName);

/**
* Dump a complete list of services to {@code System.out}.
*/
void dumpServices();

/**
* Dump a complete list of services to the given stream.
*
* @param stream the stream to which the service list should be written
*/
void dumpServices(PrintStream stream);

/**
* The factory class for service containers.
*/
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/org/jboss/msc/service/ServiceContainerImpl.java
Expand Up @@ -29,6 +29,7 @@
import org.jboss.msc.value.Value;
import org.jboss.msc.value.Values;

import java.io.PrintStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashSet;
Expand Down Expand Up @@ -131,7 +132,7 @@ private ShutdownHookHolder() {
synchronized (set) {
// if the shutdown hook was triggered, then no services can ever come up in any new containers.
final boolean down = ShutdownHookHolder.down;
final ServiceBuilderImpl<ServiceContainer> builder = new ServiceBuilderImpl<ServiceContainer>(this, new ImmediateValue<Service<ServiceContainer>>(new Service<ServiceContainer>() {
@SuppressWarnings({ "ThisEscapedInObjectConstruction" }) final ServiceBuilderImpl<ServiceContainer> builder = new ServiceBuilderImpl<ServiceContainer>(this, new ImmediateValue<Service<ServiceContainer>>(new Service<ServiceContainer>() {
public void start(final StartContext context) throws StartException {
}

Expand All @@ -144,6 +145,7 @@ public ServiceContainer getValue() throws IllegalStateException {
}), ROOT);
root = builder.setInitialMode(down ? ServiceController.Mode.REMOVE : ServiceController.Mode.ACTIVE).create();
if (! down) {
//noinspection ThisEscapedInObjectConstruction
set.add(new WeakReference<ServiceContainerImpl, Void>(this, null, new Reaper<ServiceContainerImpl, Void>() {
public void reap(final Reference<ServiceContainerImpl, Void> reference) {
ShutdownHookHolder.containers.remove(reference);
Expand Down Expand Up @@ -171,6 +173,21 @@ public void shutdown() {
root.setMode(ServiceController.Mode.REMOVE);
}

public void dumpServices() {
dumpServices(System.out);
}

public void dumpServices(PrintStream out) {
for (Map.Entry<ServiceName, ServiceControllerImpl<?>> entry : registry.entrySet()) {
final ServiceName name = entry.getKey();
final ServiceControllerImpl<?> controller = entry.getValue();
if (controller != null) {
final ServiceControllerImpl.Substate substate = controller.getSubState();
out.printf("Service '%s' mode %s state=%s (%s)\n", name, controller.getMode(), substate.getState(), substate);
}
}
}

protected void finalize() throws Throwable {
root.setMode(ServiceController.Mode.REMOVE);
}
Expand Down Expand Up @@ -213,7 +230,7 @@ Executor getExecutor() {
return executor != null ? executor : ExecutorHolder.VALUE;
}

private final ConcurrentMap<ServiceName, ServiceController<?>> registry = new ConcurrentHashMap<ServiceName, ServiceController<?>>();
private final ConcurrentMap<ServiceName, ServiceControllerImpl<?>> registry = new ConcurrentHashMap<ServiceName, ServiceControllerImpl<?>>();

public BatchBuilderImpl batchBuilder() {
return new BatchBuilderImpl(this);
Expand Down Expand Up @@ -316,7 +333,7 @@ private <T> void doResolve(BatchServiceBuilderImpl<T> entry, final Map<ServiceNa

final ServiceController.Mode initialMode = entry.getInitialMode();
builder.setInitialMode(ServiceController.Mode.NEVER);
final ServiceController<?> serviceController = builder.create();
final ServiceControllerImpl<?> serviceController = (ServiceControllerImpl<?>) builder.create();
if (registry.putIfAbsent(name, serviceController) != null) {
if (! entry.isIfNotExist()) {
throw new DuplicateServiceException("Duplicate service name provided: " + name);
Expand Down
Expand Up @@ -742,6 +742,12 @@ void dependencyDown() {
doExecute(tasks);
}

Substate getSubState() {
synchronized (this) {
return state;
}
}

private class StartContextImpl implements StartContext {

private ContextState state = ContextState.SYNC;
Expand Down

0 comments on commit bb5e2be

Please sign in to comment.