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.rmi;
028:
029: import java.net.URL;
030: import java.util.Collections;
031: import java.util.List;
032:
033: import org.cougaar.tools.server.OutputListener;
034: import org.cougaar.tools.server.OutputPolicy;
035: import org.cougaar.tools.server.ProcessDescription;
036: import org.cougaar.tools.server.RemoteListenable;
037: import org.cougaar.tools.server.RemoteProcess;
038: import org.cougaar.tools.server.system.ProcessStatus;
039:
040: /**
041: */
042: class RemoteProcessStub implements RemoteProcess {
043:
044: private ProcessDescription pd;
045: private RemoteProcessDecl rpd;
046:
047: private int exitCode;
048:
049: public RemoteProcessStub(RemoteProcessDecl rpd,
050: ProcessDescription pd) {
051: this .rpd = rpd;
052: this .pd = pd;
053: // assert pd == rpd.getProcessDescription();
054: }
055:
056: public RemoteListenable getRemoteListenable() throws Exception {
057: if (rpd == null) {
058: // "destroy()" was called
059: return new DeadListenable(pd.getName());
060: }
061: RemoteListenableDecl rld = rpd.getRemoteListenable();
062: if (rld == null) {
063: return null;
064: }
065: RemoteListenable rl = new RemoteListenableStub(rld);
066: return rl;
067: }
068:
069: //
070: // delegate the rest
071: //
072:
073: public ProcessDescription getProcessDescription() throws Exception {
074: return pd;
075: }
076:
077: public boolean isAlive() { //throws Exception
078: if (rpd == null) {
079: return false;
080: }
081: try {
082: return rpd.isAlive();
083: } catch (Exception e) {
084: return false;
085: }
086: }
087:
088: public void dumpThreads() throws Exception {
089: if (rpd == null) {
090: throw new IllegalStateException("Process " + pd.getName()
091: + " has been destroyed");
092: }
093: rpd.dumpThreads();
094: }
095:
096: public ProcessStatus[] listProcesses(boolean showAll)
097: throws Exception {
098: if (rpd == null) {
099: throw new IllegalStateException("Process " + pd.getName()
100: + " has been destroyed");
101: }
102: return rpd.listProcesses(showAll);
103: }
104:
105: public int exitValue() throws Exception {
106: if (rpd == null) {
107: return exitCode;
108: }
109: return rpd.exitValue();
110: }
111:
112: public int waitFor() throws Exception {
113: if (rpd != null) {
114: exitCode = rpd.waitFor();
115: }
116: return exitCode;
117: }
118:
119: public int waitFor(long millis) throws Exception {
120: if (rpd != null) {
121: exitCode = rpd.waitFor(millis);
122: }
123: return exitCode;
124: }
125:
126: public int destroy() throws Exception {
127: if (rpd != null) {
128: exitCode = rpd.destroy();
129: rpd = null;
130: }
131: return exitCode;
132: }
133:
134: private static class DeadListenable implements RemoteListenable {
135: private String name;
136:
137: public DeadListenable(String name) {
138: this .name = name;
139: }
140:
141: public List list() {
142: return Collections.EMPTY_LIST;
143: }
144:
145: public void addListener(URL listenerURL) {
146: throw new IllegalStateException("Process " + name
147: + " has been destroyed");
148: }
149:
150: public void removeListener(URL listenerURL) {
151: }
152:
153: public void addListener(OutputListener ol, String id) {
154: throw new IllegalStateException("Process " + name
155: + " has been destroyed");
156: }
157:
158: public void removeListener(String id) {
159: }
160:
161: public OutputPolicy getOutputPolicy() {
162: throw new IllegalStateException("Process " + name
163: + " has been destroyed");
164: }
165:
166: public void setOutputPolicy(OutputPolicy op) {
167: throw new IllegalStateException("Process " + name
168: + " has been destroyed");
169: }
170:
171: public void flushOutput() {
172: }
173: }
174: }
|