001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.server;
028:
029: import java.io.IOException;
030: import java.io.OutputStream;
031: import java.util.ArrayList;
032: import java.util.List;
033:
034: /**
035: * This is a <i>temporary</i> translator class for
036: * OutputBundles to Lists of NodeEvents.
037: * <p>
038: * See the server example "GuiConsole" for an example
039: * use of ObjectBundles.
040: *
041: * @deprecated use OutputBundles directly, which is more
042: * efficient. <b>Note</b> that NodeEvent will also be
043: * deprecated soon.
044: */
045: public final class NodeEventTranslator {
046:
047: private NodeEventTranslator() {
048: // just utility methods!
049: }
050:
051: /**
052: * Translate a single NodeEvent into an OutputBundle.
053: */
054: public static OutputBundle fromNodeEvents(NodeEvent ne) {
055: List l = new ArrayList(1);
056: l.add(ne);
057: return fromNodeEvents(l);
058: }
059:
060: /**
061: * Translate a List of NodeEvents ("l") into an OutputBundle.
062: */
063: public static OutputBundle fromNodeEvents(List l) {
064: OutputBundle ob = new OutputBundle();
065: fromNodeEvents(ob, l);
066: return ob;
067: }
068:
069: /**
070: * Translate an OutputBundle ("ob") into a List of NodeEvents.
071: */
072: public static List toNodeEvents(OutputBundle ob) {
073: List l = new ArrayList();
074: toNodeEvents(l, ob);
075: return l;
076: }
077:
078: /**
079: * Translate a List of NodeEvents ("l") into an OutputBundle "ob".
080: * <p>
081: * Assumes that no other thread(s) are using using either
082: * "ob" and/or "l" (thread-safety).
083: */
084: public static void fromNodeEvents(OutputBundle ob, // to ob
085: List l // from l
086: ) {
087: List idleUpdates = ob.getIdleUpdates();
088: DualStreamBuffer dsb = ob.getDualStreamBuffer();
089: OutputStream out = dsb.getOutputStream(true);
090: OutputStream err = dsb.getOutputStream(false);
091: int n = l.size();
092: for (int i = 0; i < n; i++) {
093: NodeEvent ni = (NodeEvent) l.get(i);
094: switch (ni.getType()) {
095: case NodeEvent.STANDARD_OUT: {
096: String msg = ni.getMessage();
097: byte[] b = msg.getBytes();
098: try {
099: out.write(b);
100: } catch (IOException ioe) {
101: throw new InternalError("never?");
102: }
103: }
104: break;
105: case NodeEvent.STANDARD_ERR: {
106: String msg = ni.getMessage();
107: byte[] b = msg.getBytes();
108: try {
109: err.write(b);
110: } catch (IOException ioe) {
111: throw new InternalError("never?");
112: }
113: }
114: break;
115: case NodeEvent.HEARTBEAT:
116: // ignore
117: break;
118: case NodeEvent.PROCESS_CREATED:
119: ob.setCreated(true);
120: break;
121: case NodeEvent.IDLE_UPDATE: {
122: String msg = ni.getMessage();
123: idleUpdates.add(msg);
124: }
125: break;
126: case NodeEvent.PROCESS_DESTROYED:
127: ob.setDestroyed(true);
128: break;
129: default:
130: break;
131: }
132: }
133: }
134:
135: /**
136: * Translate an OutputBundle ("ob") into a List of NodeEvents ("l").
137: * <p>
138: * Assumes that no other thread(s) are using using either
139: * "ob" and/or "l" (thread-safety).
140: */
141: public static void toNodeEvents(final List l, // to l
142: OutputBundle ob // to ob
143: ) {
144: if (ob.getCreated()) {
145: l.add(new NodeEvent(NodeEvent.PROCESS_CREATED));
146: }
147: List idleUpdates = ob.getIdleUpdates();
148: if (idleUpdates != null) {
149: int n = idleUpdates.size();
150: for (int i = 0; i < n; i++) {
151: String si = (String) idleUpdates.get(i);
152: l.add(new NodeEvent(NodeEvent.IDLE_UPDATE, si));
153: }
154: }
155: OutputStream out = new OutputStream() {
156: public void write(int b) {
157: String msg = Byte.toString((byte) b);
158: l.add(new NodeEvent(NodeEvent.STANDARD_OUT, msg));
159: }
160:
161: public void write(byte[] b, int off, int len) {
162: String msg = new String(b, off, len);
163: l.add(new NodeEvent(NodeEvent.STANDARD_OUT, msg));
164: }
165: };
166: OutputStream err = new OutputStream() {
167: public void write(int b) {
168: String msg = Byte.toString((byte) b);
169: l.add(new NodeEvent(NodeEvent.STANDARD_ERR, msg));
170: }
171:
172: public void write(byte[] b, int off, int len) {
173: String msg = new String(b, off, len);
174: l.add(new NodeEvent(NodeEvent.STANDARD_ERR, msg));
175: }
176: };
177: DualStreamBuffer dsb = ob.getDualStreamBuffer();
178: try {
179: dsb.writeTo(out, err);
180: } catch (IOException ioe) {
181: throw new InternalError("never?");
182: }
183: if (ob.getDestroyed()) {
184: l.add(new NodeEvent(NodeEvent.PROCESS_DESTROYED));
185: }
186: }
187:
188: }
|