/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, 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.server.deployment;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
import static org.jboss.as.server.controller.resources.DeploymentAttributes.ENABLED;

import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.OperationStepHandler;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.registry.Resource;
import org.jboss.dmr.ModelNode;
import org.jboss.msc.service.ServiceController;

/**
 * @author Jason T. Greene
 */
public class DeploymentStatusHandler implements OperationStepHandler {

    public static final OperationStepHandler INSTANCE = new DeploymentStatusHandler();
    private static final ModelNode NO_METRICS = new ModelNode("no metrics available");

    @Override
    public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
        final PathAddress address = PathAddress.pathAddress(operation.get(OP_ADDR));
        final PathElement element = address.getLastElement();
        final ModelNode deployment = Resource.Tools.readModel(context.readResource(PathAddress.EMPTY_ADDRESS));
        final boolean isEnabled = ENABLED.resolveModelAttribute(context, deployment).asBoolean();
        context.addStep(new OperationStepHandler() {
            @Override
            public void execute(final OperationContext context, final ModelNode operation) throws OperationFailedException {
                final ModelNode result = context.getResult();
                if (!isEnabled) {
                    result.set(AbstractDeploymentUnitService.DeploymentStatus.STOPPED.toString());
                } else {
                    final ServiceController<?> controller = context.getServiceRegistry(false).getService(Services.deploymentUnitName(element.getValue()));
                    if (controller != null) {
                        if (controller.getSubstate() == ServiceController.Substate.WONT_START &&
                                controller.getState() == ServiceController.State.DOWN) {
                            result.set(AbstractDeploymentUnitService.DeploymentStatus.STOPPED.toString());
                        } else {
                            result.set(((AbstractDeploymentUnitService) controller.getService()).getStatus().toString());
                        }
                    } else {
                        result.set(NO_METRICS);
                    }
                }
                context.stepCompleted();
            }
        }, OperationContext.Stage.RUNTIME);

        context.stepCompleted();
    }
}

@@ -1077,32 +1077,39 @@
 ic org.jboss.as.
+server.
 controller.descr
@@ -1107,54 +1107,46 @@
 ler.
-descriptions.ModelDescriptionConstants.OP_ADDR
+resources.DeploymentAttributes.ENABLED
 ;%0Aim
@@ -1219,23 +1219,28 @@
 ributes.
-ENABLED
+RUNTIME_NAME
 ;%0A%0Aimpor
@@ -1441,52 +1441,8 @@
 ss;%0A
-import org.jboss.as.controller.PathElement;%0A
 impo
@@ -1973,155 +1973,8 @@
 n %7B%0A
-        final PathAddress address = PathAddress.pathAddress(operation.get(OP_ADDR));%0A        final PathElement element = address.getLastElement();%0A
     
@@ -2175,24 +2175,127 @@
 sBoolean();%0A
+        final String runtimeName = RUNTIME_NAME.resolveModelAttribute(context, deployment).asString();%0A
         cont
@@ -2839,26 +2839,19 @@
 ame(
-element.getValue()
+runtimeName
 ));%0A

Use runtime name when finding deployment service to read status