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.CONTENT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.DEPLOYMENT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FULL_REPLACE_DEPLOYMENT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HASH;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RUNTIME_NAME;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SERVER_GROUP;
 import static org.jboss.as.domain.controller.DomainControllerMessages.MESSAGES;
 import static org.jboss.as.domain.controller.operations.deployment.AbstractDeploymentHandler.createFailureException;
 import static org.jboss.as.server.controller.resources.DeploymentAttributes.CONTENT_HASH;
 
 
Handles replacement in the runtime of one deployment by another.

Author(s):
Brian Stansberry (c) 2011 Red Hat Inc.
 
 public class DeploymentFullReplaceHandler implements OperationStepHandler {
 
     public static final String OPERATION_NAME = ;
 
     private final ContentRepository contentRepository;
     private final HostFileRepository fileRepository;

    
Constructor for a master Host Controller
 
     public DeploymentFullReplaceHandler(final ContentRepository contentRepository) {
         this. = contentRepository;
         this. = null;
     }

    
Constructor for a slave Host Controller
 
     public DeploymentFullReplaceHandler(final HostFileRepository fileRepository) {
         this. = null;
         this. = fileRepository;
     }
 
     public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
 
         // Validate op. Store any corrected values back to the op before manipulating further
         ModelNode correctedOperation = operation.clone();
             def.validateAndSet(operationcorrectedOperation);
         }
 
         // Pull data from the op
         final String name = ..resolveModelAttribute(contextcorrectedOperation).asString();
         final PathElement deploymentPath = PathElement.pathElement(name);
         String runtimeName = correctedOperation.hasDefined()
                 ? ..resolveModelAttribute(contextcorrectedOperation).asString() : name;
         // clone the content param, so we can modify it to our own content
         ModelNode content = correctedOperation.require().clone();
 
         // Throw a specific exception if the replaced deployment doesn't already exist
         // BES 2013/10/30 -- this is pointless; the readResourceForUpdate call will throw
         // an exception with an equally informative message if the deployment doesn't exist
 //        final Resource root = context.readResource(PathAddress.EMPTY_ADDRESS);
 //        boolean exists = root.hasChild(deploymentPath);
 //        if (!exists) {
 //            throw createFailureException(MESSAGES.noDeploymentContentWithName(name));
//        }
        final ModelNode deploymentModel = context.readResourceForUpdate(PathAddress.pathAddress(deploymentPath)).getModel();
        // Keep track of hash we are replacing so we can drop it from the content repo if all is well
        ModelNode replacedContent = deploymentModel.get().get(0);
        final byte[] replacedHash = replacedContent.hasDefined(.getName())
                ? .resolveModelAttribute(contextreplacedContent).asBytes() : null;
        // Set up the new content attribute
        final byte[] newHash;
        ModelNode contentItemNode = content.require(0);
        if (contentItemNode.hasDefined()) {
            newHash = contentItemNode.require().asBytes();
            if ( != null) {
                // We are the master DC. Validate that we actually have this content.
                if (!.hasContent(newHash)) {
                    throw createFailureException(.noDeploymentContentWithHash(HashUtil.bytesToHexString(newHash)));
                }
            } else {
                // We are a slave controller
                // Ensure the local repo has the files
                .getDeploymentFiles(newHash);
            }
        } else if (DeploymentHandlerUtils.hasValidContentAdditionParameterDefined(contentItemNode)) {
            if ( == null) {
                // This is a slave DC. We can't handle this operation; it should have been fixed up on the master DC
                throw createFailureException(.slaveCannotAcceptUploads());
            }
            try {
                // Store and transform operation
                newHash = DeploymentUploadUtil.storeContentAndTransformOperation(contextcorrectedOperation);
            } catch (IOException e) {
                throw createFailureException(e.toString());
            }
            // Replace the op-provided content node with one that has a hash
            contentItemNode = new ModelNode();
            contentItemNode.get().set(newHash);
            content = new ModelNode();
            content.add(contentItemNode);
        } else {
            // Unmanaged content, the user is responsible for replication
            newHash = null;
        }
        // Store state to the model
        deploymentModel.get().set(runtimeName);
        deploymentModel.get().set(content);
        // Update server groups
        final Resource root = context.readResource(.);
        if (root.hasChild(PathElement.pathElement())) {
            for (final Resource.ResourceEntry serverGroupResource : root.getChildren()) {
                Resource deploymentResource = serverGroupResource.getChild(deploymentPath);
                if (deploymentResource != null) {
                    deploymentResource.getModel().get().set(runtimeName);
                }
            }
        }
        context.completeStep(new OperationContext.ResultHandler() {
            @Override
            public void handleResult(ResultAction resultActionOperationContext contextModelNode operation) {
                if (resultAction == .) {
                    if (replacedHash != null  && (newHash == null || !Arrays.equals(replacedHashnewHash))) {
                        // The old content is no longer used; clean from repos
                        if( != null) {
                            .removeContent(replacedHashname);
                        } else {
                            .deleteDeployment(replacedHash);
                        }
                    }
                    if (newHash != null &&  != null) {
                        .addContentReference(newHashname);
                    }
                } else if (newHash != null && (replacedHash == null || !Arrays.equals(replacedHashnewHash))) {
                    // Due to rollback, the new content isn't used; clean from repos
                    if ( != null ) {
                        .removeContent(newHashname);
                    } else {
                        .deleteDeployment(newHash);
                    }
                }
            }
        });
    }
New to GrepCode? Check out our FAQ X