Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012, 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.domain.controller.operations.deployment;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.BYTES;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CONTENT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HASH;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.INPUT_STREAM_INDEX;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.URL;
 import static org.jboss.as.domain.controller.DomainControllerMessages.MESSAGES;
 
 import java.net.URL;
 import java.util.List;
 
Utility method for storing deployment content.

Author(s):
Brian Stansberry (c) 2011 Red Hat Inc.
 
 
     private DeploymentUploadUtil() {
     }

    
Store the deployment contents and attach a "transformed" slave operation to the operation context.

Parameters:
context the operation context
operation the original operation
contentRepository the content repository
Returns:
the hash of the uploaded deployment content
Throws:
java.io.IOException
org.jboss.as.controller.OperationFailedException
 
     public static byte[] storeContentAndTransformOperation(OperationContext contextModelNode operationContentRepository contentRepositorythrows IOExceptionOperationFailedException {
         if (!operation.hasDefined()) {
             throw createFailureException(.invalidContentDeclaration());
         }
         final ModelNode content = operation.get().get(0);
         if (content.hasDefined()) {
             // This should be handled as part of the OSH
             throw createFailureException(.invalidContentDeclaration());
         }
         final byte[] hash = storeDeploymentContent(contextoperationcontentRepository);
 
         // Clear the contents and update with the hash
         final ModelNode slave = operation.clone();
         slave.get().setEmptyList().add().get().set(hash);
         // Add the domain op transformer
         if (transformers == null) {
             context.attach(.transformers = new ArrayList<DomainOperationTransformer>());
         }
         transformers.add(new CompositeOperationAwareTransformer(slave));
         return hash;
     }
 
     private static byte[] storeDeploymentContent(OperationContext contextModelNode operationContentRepository contentRepositorythrows IOExceptionOperationFailedException {
         InputStream in = getContents(contextoperation);
         return contentRepository.addContent(in);
     }
 
     private static InputStream getContents(OperationContext contextModelNode operationthrows OperationFailedException {
        if(! operation.hasDefined()) {
        }
        return getInputStream(contextoperation.require().get(0));
    }
    private static InputStream getInputStream(OperationContext contextModelNode contentthrows OperationFailedException {
        InputStream in = null;
        String message = "";
        if (content.hasDefined()) {
            int streamIndex = content.get().asInt();
            if (streamIndex > context.getAttachmentStreamCount() - 1) {
                message = .invalidValue(streamIndex, (context.getAttachmentStreamCount() - 1));
                throw createFailureException(message);
            }
            message = .nullStream(streamIndex);
            in = context.getAttachmentStream(streamIndex);
        } else if (content.hasDefined()) {
            in = new ByteArrayInputStream(content.get().asBytes());
            message = .invalidByteStream();
        } else if (content.hasDefined()) {
            final String urlSpec = content.get().asString();
            try {
                message = .invalidUrlStream();
                in = new URL(urlSpec).openStream();
            } catch (MalformedURLException e) {
                throw createFailureException(message);
            } catch (IOException e) {
                throw createFailureException(message);
            }
        }
        if (in == null) {
            throw createFailureException(message);
        }
        return in;
    }
    private static OperationFailedException createFailureException(String msg) {
        return new OperationFailedException(new ModelNode().set(msg));
    }
New to GrepCode? Check out our FAQ X