/jboss-as-7.1.1.Final/host-controller/src/main/java/org/jboss/as/domain/controller/operations/deployment/AbstractDeploymentHandler.java
Java | 116 lines | 71 code | 13 blank | 32 comment | 11 complexity | 379cb0d52fd28c7c13f869f356697736 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1/*
2 * JBoss, Home of Professional Open Source.
3 * Copyright (c) 2011, Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22package org.jboss.as.domain.controller.operations.deployment;
23
24import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.BYTES;
25import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.INPUT_STREAM_INDEX;
26import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.URL;
27import static org.jboss.as.domain.controller.DomainControllerMessages.MESSAGES;
28
29import java.io.ByteArrayInputStream;
30import java.io.IOException;
31import java.io.InputStream;
32import java.net.MalformedURLException;
33import java.net.URL;
34import java.util.Arrays;
35import java.util.List;
36
37import org.jboss.as.controller.OperationContext;
38import org.jboss.as.controller.OperationFailedException;
39import org.jboss.dmr.ModelNode;
40
41/**
42 * @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
43 */
44abstract class AbstractDeploymentHandler {
45
46 protected static final List<String> CONTENT_ADDITION_PARAMETERS = Arrays.asList(INPUT_STREAM_INDEX, BYTES, URL);
47
48 protected static String asString(final ModelNode node, final String name) {
49 return node.has(name) ? node.require(name).asString() : null;
50 }
51
52 protected static OperationFailedException createFailureException(String format, Object... params) {
53 return createFailureException(String.format(format, params));
54 }
55
56 protected static OperationFailedException createFailureException(Throwable cause, String format, Object... params) {
57 return createFailureException(cause, String.format(format, params));
58 }
59
60 protected static OperationFailedException createFailureException(String msg) {
61 return new OperationFailedException(new ModelNode().set(msg));
62 }
63
64 protected static OperationFailedException createFailureException(Throwable cause, String msg) {
65 return new OperationFailedException(cause, new ModelNode().set(msg));
66 }
67
68 protected static InputStream getInputStream(OperationContext context, ModelNode operation) throws OperationFailedException {
69 InputStream in = null;
70 String message = "";
71 if (operation.hasDefined(INPUT_STREAM_INDEX)) {
72 int streamIndex = operation.get(INPUT_STREAM_INDEX).asInt();
73 message = MESSAGES.nullStream(streamIndex);
74 in = context.getAttachmentStream(streamIndex);
75 } else if (operation.hasDefined(BYTES)) {
76 message = MESSAGES.invalidByteStream();
77 in = new ByteArrayInputStream(operation.get(BYTES).asBytes());
78 } else if (operation.hasDefined(URL)) {
79 final String urlSpec = operation.get(URL).asString();
80 try {
81 message = MESSAGES.invalidUrlStream();
82 in = new URL(urlSpec).openStream();
83 } catch (MalformedURLException e) {
84 throw createFailureException(message);
85 } catch (IOException e) {
86 throw createFailureException(message);
87 }
88 }
89 if (in == null) {
90 throw createFailureException(message);
91 }
92 return in;
93 }
94
95 /**
96 * Checks to see if a valid deployment parameter has been defined.
97 *
98 * @param operation the operation to check.
99 *
100 * @return {@code true} of the parameter is valid, otherwise {@code false}.
101 */
102 protected static boolean hasValidContentAdditionParameterDefined(ModelNode operation) {
103 for (String s : CONTENT_ADDITION_PARAMETERS) {
104 if (operation.hasDefined(s)) {
105 return true;
106 }
107 }
108 return false;
109 }
110
111 protected static void validateOnePieceOfContent(final ModelNode content) throws OperationFailedException {
112 // TODO: implement overlays
113 if (content.asList().size() != 1)
114 throw createFailureException(MESSAGES.as7431());
115 }
116}