PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/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. */
  22. package org.jboss.as.domain.controller.operations.deployment;
  23. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.BYTES;
  24. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.INPUT_STREAM_INDEX;
  25. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.URL;
  26. import static org.jboss.as.domain.controller.DomainControllerMessages.MESSAGES;
  27. import java.io.ByteArrayInputStream;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.net.MalformedURLException;
  31. import java.net.URL;
  32. import java.util.Arrays;
  33. import java.util.List;
  34. import org.jboss.as.controller.OperationContext;
  35. import org.jboss.as.controller.OperationFailedException;
  36. import org.jboss.dmr.ModelNode;
  37. /**
  38. * @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
  39. */
  40. abstract class AbstractDeploymentHandler {
  41. protected static final List<String> CONTENT_ADDITION_PARAMETERS = Arrays.asList(INPUT_STREAM_INDEX, BYTES, URL);
  42. protected static String asString(final ModelNode node, final String name) {
  43. return node.has(name) ? node.require(name).asString() : null;
  44. }
  45. protected static OperationFailedException createFailureException(String format, Object... params) {
  46. return createFailureException(String.format(format, params));
  47. }
  48. protected static OperationFailedException createFailureException(Throwable cause, String format, Object... params) {
  49. return createFailureException(cause, String.format(format, params));
  50. }
  51. protected static OperationFailedException createFailureException(String msg) {
  52. return new OperationFailedException(new ModelNode().set(msg));
  53. }
  54. protected static OperationFailedException createFailureException(Throwable cause, String msg) {
  55. return new OperationFailedException(cause, new ModelNode().set(msg));
  56. }
  57. protected static InputStream getInputStream(OperationContext context, ModelNode operation) throws OperationFailedException {
  58. InputStream in = null;
  59. String message = "";
  60. if (operation.hasDefined(INPUT_STREAM_INDEX)) {
  61. int streamIndex = operation.get(INPUT_STREAM_INDEX).asInt();
  62. message = MESSAGES.nullStream(streamIndex);
  63. in = context.getAttachmentStream(streamIndex);
  64. } else if (operation.hasDefined(BYTES)) {
  65. message = MESSAGES.invalidByteStream();
  66. in = new ByteArrayInputStream(operation.get(BYTES).asBytes());
  67. } else if (operation.hasDefined(URL)) {
  68. final String urlSpec = operation.get(URL).asString();
  69. try {
  70. message = MESSAGES.invalidUrlStream();
  71. in = new URL(urlSpec).openStream();
  72. } catch (MalformedURLException e) {
  73. throw createFailureException(message);
  74. } catch (IOException e) {
  75. throw createFailureException(message);
  76. }
  77. }
  78. if (in == null) {
  79. throw createFailureException(message);
  80. }
  81. return in;
  82. }
  83. /**
  84. * Checks to see if a valid deployment parameter has been defined.
  85. *
  86. * @param operation the operation to check.
  87. *
  88. * @return {@code true} of the parameter is valid, otherwise {@code false}.
  89. */
  90. protected static boolean hasValidContentAdditionParameterDefined(ModelNode operation) {
  91. for (String s : CONTENT_ADDITION_PARAMETERS) {
  92. if (operation.hasDefined(s)) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. protected static void validateOnePieceOfContent(final ModelNode content) throws OperationFailedException {
  99. // TODO: implement overlays
  100. if (content.asList().size() != 1)
  101. throw createFailureException(MESSAGES.as7431());
  102. }
  103. }