PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/controller/src/main/java/org/jboss/as/controller/remote/OperationAttachmentsProxy.java

#
Java | 173 lines | 124 code | 21 blank | 28 comment | 12 complexity | 0f95380eb48de2b47debef64df69bb20 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2012, 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.controller.remote;
  23. import static org.jboss.as.controller.ControllerMessages.MESSAGES;
  24. import org.jboss.as.controller.client.OperationAttachments;
  25. import org.jboss.as.controller.client.impl.ModelControllerProtocol;
  26. import org.jboss.as.protocol.mgmt.AbstractManagementRequest;
  27. import org.jboss.as.protocol.mgmt.ActiveOperation;
  28. import org.jboss.as.protocol.mgmt.FlushableDataOutput;
  29. import org.jboss.as.protocol.mgmt.ManagementChannelAssociation;
  30. import org.jboss.as.protocol.mgmt.ManagementRequestContext;
  31. import org.jboss.as.protocol.mgmt.ProtocolUtils;
  32. import java.io.ByteArrayInputStream;
  33. import java.io.DataInput;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.util.ArrayList;
  37. import java.util.Collections;
  38. import java.util.List;
  39. /**
  40. * A attachment proxy, lazily initializing the streams.
  41. *
  42. * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
  43. */
  44. class OperationAttachmentsProxy implements OperationAttachments {
  45. final List<ProxiedInputStream> proxiedStreams;
  46. public OperationAttachmentsProxy(final ManagementChannelAssociation channelAssociation, final int batchId, final int size) {
  47. proxiedStreams = new ArrayList<ProxiedInputStream>(size);
  48. for (int i = 0 ; i < size ; i++) {
  49. proxiedStreams.add(new ProxiedInputStream(channelAssociation, batchId, i));
  50. }
  51. }
  52. static OperationAttachmentsProxy create(final ManagementChannelAssociation channelAssociation, final int batchId, final int size) {
  53. return new OperationAttachmentsProxy(channelAssociation, batchId, size);
  54. }
  55. @Override
  56. public boolean isAutoCloseStreams() {
  57. return false;
  58. }
  59. @Override
  60. public List<InputStream> getInputStreams() {
  61. List<InputStream> result = new ArrayList<InputStream>();
  62. result.addAll(proxiedStreams);
  63. return Collections.unmodifiableList(result);
  64. }
  65. @Override
  66. public void close() throws IOException {
  67. //
  68. }
  69. void shutdown(Exception error) {
  70. for (ProxiedInputStream stream : proxiedStreams) {
  71. stream.shutdown(error);
  72. }
  73. }
  74. public static class ProxiedInputStream extends InputStream {
  75. final ManagementChannelAssociation channelAssociation;
  76. final int batchId;
  77. final int index;
  78. volatile byte[] bytes;
  79. volatile ByteArrayInputStream delegate;
  80. volatile Exception error;
  81. ProxiedInputStream(final ManagementChannelAssociation channelAssociation, final int batchId, final int index) {
  82. this.channelAssociation = channelAssociation;
  83. this.batchId = batchId;
  84. this.index = index;
  85. }
  86. @Override
  87. public int read() throws IOException {
  88. if (delegate == null) {
  89. synchronized (this) {
  90. if (delegate == null) {
  91. initializeBytes();
  92. try {
  93. wait();
  94. } catch (InterruptedException e) {
  95. Thread.currentThread().interrupt();
  96. throw MESSAGES.remoteCallerThreadInterrupted();
  97. }
  98. if (error != null) {
  99. if (error instanceof IOException) {
  100. throw (IOException)error;
  101. }
  102. throw new IOException(error);
  103. }
  104. delegate = new ByteArrayInputStream(bytes);
  105. bytes = null;
  106. }
  107. }
  108. }
  109. return delegate.read();
  110. }
  111. void initializeBytes() {
  112. if (bytes == null) {
  113. try {
  114. channelAssociation.executeRequest(batchId, new AbstractManagementRequest<Object, Object>() {
  115. @Override
  116. public byte getOperationType() {
  117. return ModelControllerProtocol.GET_INPUTSTREAM_REQUEST;
  118. }
  119. @Override
  120. protected void sendRequest(ActiveOperation.ResultHandler<Object> resultHandler, ManagementRequestContext<Object> context, FlushableDataOutput output) throws IOException {
  121. output.write(ModelControllerProtocol.PARAM_INPUTSTREAM_INDEX);
  122. output.writeInt(index);
  123. }
  124. @Override
  125. public void handleRequest(DataInput input, ActiveOperation.ResultHandler<Object> resultHandler, ManagementRequestContext<Object> context) throws IOException {
  126. // TODO execute async
  127. synchronized (ProxiedInputStream.this) {
  128. ProtocolUtils.expectHeader(input, ModelControllerProtocol.PARAM_INPUTSTREAM_LENGTH);
  129. final int size = input.readInt();
  130. ProtocolUtils.expectHeader(input, ModelControllerProtocol.PARAM_INPUTSTREAM_CONTENTS);
  131. final byte[] buf = new byte[size];
  132. for (int i = 0; i < size; i++) {
  133. buf[i] = input.readByte();
  134. }
  135. bytes = buf;
  136. ProxiedInputStream.this.notifyAll();
  137. }
  138. }
  139. });
  140. } catch (IOException e) {
  141. error = e;
  142. }
  143. }
  144. }
  145. void shutdown(Exception error) {
  146. this.error = error;
  147. synchronized (this) {
  148. notifyAll();
  149. }
  150. }
  151. }
  152. }