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