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.deployment;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADDRESS;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CONTENT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.DEPLOYMENT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SERVER_GROUP;
 import static org.jboss.as.server.controller.resources.DeploymentAttributes.CONTENT_HASH;
 import static org.jboss.as.server.controller.resources.DeploymentAttributes.ENABLED;
 import static org.jboss.as.server.controller.resources.DeploymentAttributes.RUNTIME_NAME;
 import static org.jboss.as.server.controller.resources.DeploymentAttributes.RUNTIME_NAME_NILLABLE;
 import static org.jboss.as.server.controller.resources.DeploymentAttributes.SERVER_GROUP_ADD_ATTRIBUTES;
 
 import java.util.List;
 import java.util.Set;
 
Handles addition of a deployment to a server group.

Author(s):
Brian Stansberry (c) 2011 Red Hat Inc.
 
 
     public static final String OPERATION_NAME = ;
 
     private final HostFileRepository fileRepository;
 
     public ServerGroupDeploymentAddHandler(final HostFileRepository fileRepository) {
         this. = fileRepository;
     }

    
 
     public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
         final ModelNode opAddr = operation.get();
         final PathAddress address = PathAddress.pathAddress(opAddr);
         final String name = address.getLastElement().getValue();
 
         final Resource deploymentResource = context.readResourceFromRoot(PathAddress.pathAddress(PathElement.pathElement(name)));
         ModelNode deployment = deploymentResource.getModel();
 
         for (ModelNode content : deployment.require().asList()) {
             if ((content.hasDefined(.getName()))) {
                 .validateOperation(content);
                 // Ensure the local repo has the files
                 .getDeploymentFiles(.resolveModelAttribute(contextcontent).asBytes());
             }
         }
 
         final Resource resource = context.createResource(.);
         final ModelNode subModel = resource.getModel();
 
         for (AttributeDefinition def : ) {
             def.validateAndSet(operationsubModel);
         }
         if (!.resolveModelAttribute(contextsubModel).isDefined()) {
             .validateAndSet(deploymentsubModel);
         }
 
         // Add a step to validate uniqueness of runtime names
         context.addStep(new OperationStepHandler() {
             @Override
             public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                 validateRuntimeNames(namecontextaddress);
             }
         }, ..);
        context.stepCompleted();
    }
    private void validateRuntimeNames(String deploymentNameOperationContext contextPathAddress addressthrows OperationFailedException {
        ModelNode deployment = context.readResource(.).getModel();
        if (.resolveModelAttribute(contextdeployment).asBoolean()) {
            Resource root = context.readResourceFromRoot(.);
            ModelNode domainDeployment = root.getChild(PathElement.pathElement(deploymentName)).getModel();
            String runtimeName = getRuntimeName(deploymentNamedeploymentdomainDeployment);
            PathAddress sgAddress = address.subAddress(0, address.size() - 1);
            Resource serverGroup = root.navigate(sgAddress);
            for (Resource.ResourceEntry re : serverGroup.getChildren()) {
                String reName = re.getName();
                if (!deploymentName.equals(reName)) {
                    ModelNode otherDepl = re.getModel();
                    if (.resolveModelAttribute(contextotherDepl).asBoolean()) {
                        domainDeployment = root.getChild(PathElement.pathElement(reName)).getModel();
                        String otherRuntimeName = getRuntimeName(reNameotherDepldomainDeployment);
                        if (runtimeName.equals(otherRuntimeName)) {
                            throw ..runtimeNameMustBeUnique(reNameruntimeName,
                                    sgAddress.getLastElement().getValue());
                        }
                    }
                }
            }
        }
        context.stepCompleted();
    }
    private static String getRuntimeName(String nameModelNode deploymentModelNode domainDeployment) {
        if (deployment.hasDefined(.)) {
            return deployment.get(.).asString();
        } else if (domainDeployment.hasDefined(.)) {
            return domainDeployment.get(.).asString();
        }
        return name;
    }
    private void isRuntimeNameUniqueForServerGroup(String serverGroupNameOperationContext contextString nameString runtimeNamethrows OperationFailedException {
        if(serverGroupName != null) {
            PathAddress address = PathAddress.pathAddress(PathAddress.pathAddress(serverGroupName), PathElement.pathElement());
            Set<Resource.ResourceEntrydeployments = context.readResourceFromRoot(address).getChildren();
            for(Resource.ResourceEntry existingDeployment : deployments) {
                ModelNode existingDeploymentModel = existingDeployment.getModel();
                if(existingDeploymentModel.hasDefined(.getName()) && !name.equals(existingDeployment.getName())) {
                    if(existingDeploymentModel.get(.getName()).asString().equals(runtimeName)) {
                        throw ..runtimeNameMustBeUnique(existingDeployment.getName(), runtimeNameserverGroupName);
                    }
                }
            }
        }
    }
    private String getServerGroupName(ModelNode operation) {
        if(operation.hasDefined()) {
            List<Propertyaddress = operation.get().asPropertyList();
            for(Property prop : address) {
                if(.equals(prop.getName())) {
                    return prop.getValue().asString();
                }
            }
        }
        return null;
    }
New to GrepCode? Check out our FAQ X