Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
   * as indicated by the @authors tag. All rights reserved.
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
  * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * 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,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  */
 package org.jboss.as.domain.controller.operations;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILURE_DESCRIPTION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.INCLUDES;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.PROFILE;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RESULT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
 
 import java.util.Map;
 import java.util.Set;
 
Outputs the profile as a series of operations needed to construct the profile

Author(s):
Kabir Khan
Version:
$Revision: 1.1 $
 
 public class ProfileDescribeHandler implements OperationStepHandler {
 
     public static final ProfileDescribeHandler INSTANCE = new ProfileDescribeHandler();
 
     private static final Set<Action.ActionEffectDESCRIBE_EFFECTS =
             Collections.unmodifiableSet(EnumSet.of(....));
 
     private ProfileDescribeHandler() {
     }
 
 
     @Override
     public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
 
         final String opName = operation.require().asString();
         final PathAddress address = PathAddress.pathAddress(operation.require());
 
         AuthorizationResult authResult = context.authorize(operation);
         if (authResult.getDecision() != ..) {
             throw ..unauthorized(opNameaddressauthResult.getExplanation());
         }
 
         final ModelNode result = new ModelNode();
         final ModelNode profile =  Resource.Tools.readModel(context.readResource(.));
         result.setEmptyList();
 
         final ImmutableManagementResourceRegistration registry = context.getResourceRegistration();
         final AtomicReference<ModelNodefailureRef = new AtomicReference<ModelNode>();
 
         final ModelNode subsystemResults = new ModelNode().setEmptyList();
         final Map<StringModelNodeincludeResults = new HashMap<StringModelNode>();
 
         // Add a step at end to assemble all the data
         // Add steps in the reverse of expected order, as Stage.IMMEDIATE adds to the top of the list
         context.addStep(new OperationStepHandler() {
             @Override
             public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                 boolean failed = false;
                 if (failureRef.get() != null) {
                     // One of our subsystems failed
                     context.getFailureDescription().set(failureRef.get());
                     failed = true;
                 } else {
                     for (ModelNode includeRsp : includeResults.values()) {
                         if (includeRsp.hasDefined()) {
                             context.getFailureDescription().set(includeRsp.get());
                            failed = true;
                            break;
                        }
                        ModelNode includeResult = includeRsp.get();
                        if (includeResult.isDefined()) {
                            for (ModelNode op : includeResult.asList()) {
                                result.add(op);
                            }
                        }
                    }
                }
                if (!failed) {
                    for (ModelNode subsysRsp : subsystemResults.asList()) {
                        result.add(subsysRsp);
                    }
                    context.getResult().set(result);
                }
                context.stepCompleted();
            }
        }, ..true);
        if (profile.hasDefined()) {
            for (final String subsystemName : profile.get().keys()) {
                final ModelNode subsystemRsp = new ModelNode();
                PathElement pe = PathElement.pathElement(subsystemName);
                PathAddress fullAddress = address.append(pe);
                final ModelNode subsystemAddress = fullAddress.toModelNode();
                final ModelNode newOp = operation.clone();
                newOp.get().set(subsystemAddress);
                PathAddress relativeAddress = PathAddress.pathAddress(pe);
                OperationStepHandler subsysHandler = registry.getOperationHandler(relativeAddressopName);
                if (subsysHandler == null) {
                    String errMsg;
                    ImmutableManagementResourceRegistration child = registry.getSubModel(relativeAddress);
                    if (child == null) {
                       errMsg = ..noSuchResourceType(fullAddress);
                    } else {
                        errMsg = ..noHandlerForOperation(opNamefullAddress);
                    }
                    throw new OperationFailedException(new ModelNode(errMsg));
                }
                // Step to store subsystem ops in overall list
                context.addStep(new OperationStepHandler() {
                    @Override
                    public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                        if (failureRef.get() == null) {
                            if (subsystemRsp.hasDefined()) {
                                failureRef.set(subsystemRsp.get());
                            } else if (subsystemRsp.hasDefined()) {
                                for (ModelNode op : subsystemRsp.require().asList()) {
                                    subsystemResults.add(op);
                                }
                            }
                        }
                        context.stepCompleted();
                    }
                }, ..true);
                // Step to determine subsystem ops
                context.addStep(subsystemRspnewOpsubsysHandler..true);
            }
        }
        if (profile.hasDefined()) {
            // Call this op for each included profile
            for (ModelNode include : profile.get().asList()) {
                final String includeName = include.asString();
                final ModelNode includeRsp = new ModelNode();
                includeResults.put(includeNameincludeRsp);
                final ModelNode includeAddress = address.subAddress(0, address.size() - 1).append(PathElement.pathElement(includeName)).toModelNode();
                final ModelNode newOp = operation.clone();
                newOp.get().set(includeAddress);
                context.addStep(includeRspnewOp..true);
            }
        }
        context.stepCompleted();
    }
New to GrepCode? Check out our FAQ X