001: /*
002: * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
003: * NETSCAPE COMMUNICATIONS CORPORATION
004: *
005: * Copyright (c) 1996 Netscape Communications Corporation.
006: * All Rights Reserved.
007: * Use of this Source Code is subject to the terms of the applicable
008: * license agreement from Netscape Communications Corporation.
009: */
010:
011: package soif;
012:
013: import communications.PostConnection;
014: import util.ReportError;
015:
016: import java.net.URL;
017:
018: /**
019: * Thread that puts SOIF.
020: * <p>
021: * <b>Open Issues</b>
022: * <ul>
023: * <li>Currently the bare minimum.
024: * As the server stabilizes, more will be done here to do error checking etc.
025: * <li>Doesn't currently support sending an RDM header
026: * because current targets for this don't require it.
027: * That may change.
028: * <li>May not need CSID?
029: * <li>Not used in product so not field tested.
030: */
031: public class SOIFPutter extends Thread {
032: /**
033: * Processing status.
034: */
035: private int status;
036:
037: /**
038: * Debug flag.
039: */
040: public boolean debug;
041:
042: /**
043: * Processing status: completed successfully.
044: */
045: public final static int COMPLETE = 0;
046: /**
047: * Processing status: completed successfully.
048: */
049: public final static int PREPARED = 1;
050: /**
051: * Processing status: completed successfully.
052: */
053: public final static int PROCESSING = 2;
054: /**
055: * Processing status: completed successfully.
056: */
057: public final static int ABEND = 3;
058:
059: /**
060: * Source of SOIF to which a PostConnection will be
061: * established.
062: */
063: private String target;
064:
065: /**
066: * Target Compass Server ID.
067: */
068: protected CSID csid;
069:
070: /**
071: * The SOIF.
072: */
073: private String soif;
074:
075: /**
076: * Constructor.
077: * @param CGILocation location of the CGI target of the rdm request
078: * @param exe CGI executable which will fill rdm request
079: * @param csid compass server id
080: * @param soif soif to send
081: */
082: public SOIFPutter(String CGILocation, String exe, CSID csid,
083: SOIF soif) {
084: this (CGILocation, exe, csid, ((soif == null) ? null : soif
085: .toStringList()));
086: }
087:
088: /**
089: * Constructor.
090: * @param CGILocation location of the CGI target of the rdm request
091: * @param exe CGI executable which will fill rdm request
092: * @param csid compass server id
093: * @param soif soif to send
094: */
095: public SOIFPutter(String CGILocation, String exe, CSID csid,
096: String soif) {
097: if (soif == null)
098: throw new IllegalArgumentException(Messages.NOSOIF);
099: status = PREPARED;
100: target = CGILocation + exe;
101: this .csid = csid;
102: this .soif = soif;
103: }
104:
105: /**
106: * Set processing status. If the current status is ABEND, it
107: * will not be changed.
108: */
109: protected final void setStatus(int i) {
110: if (status != ABEND)
111: status = i;
112: }
113:
114: /**
115: * Return processing status.
116: */
117: public final int getStatus() {
118: return status;
119: }
120:
121: /**
122: * Return whether or not processing is complete.
123: */
124: public final boolean finished() {
125: return ((status == COMPLETE) || (status == ABEND));
126: }
127:
128: /**
129: * Wait for the putter to finish.
130: */
131: public void waitFor() {
132: while (!finished()) {
133: try {
134: Thread.sleep(100);
135: } catch (InterruptedException e) {
136: break;
137: }
138: }
139: }
140:
141: /**
142: * Wait for the putter to finish.
143: * @param max don't wait longer than max (increments are 100)
144: */
145: public void waitFor(int max) {
146: int counter = 0;
147: while ((!finished()) && (max > counter)) {
148: counter += 100;
149: try {
150: Thread.sleep(100);
151: } catch (InterruptedException e) {
152: break;
153: }
154: }
155: }
156:
157: /**
158: * Put some of that SOIF.
159: */
160: public void putSOIF() {
161: URL url;
162: PostConnection pc = null;
163: String s = "";
164: StringBuffer sb = new StringBuffer();
165: boolean errors = false;
166: String tmps = "unknown";
167: SOIF sl = null;
168: SOIFParser slp = null;
169:
170: try {
171: s = PostConnection.doWrite(target, soif, true);
172: if (debug) {
173: System.out.println(s);
174: }
175: if (s == null) {
176: return;
177: }
178: if (s.indexOf("@") != -1) {
179: slp = new SOIFParser(s.substring(s.indexOf("@")));
180: if (slp.isValid())
181: sl = slp.getSOIF();
182: else {
183: sl = null;
184: errors = true;
185: return;
186: }
187: }
188: } finally {
189: if (errors) {
190: status = ABEND;
191: return;
192: }
193: }
194:
195: if (sl == null) {
196: ReportError.reportError(ReportError.INTERNAL, "SOIFGetter",
197: "getSOIF", Messages.NOSOIF);
198: status = ABEND;
199: return;
200: }
201:
202: if (debug)
203: System.out.println(sl.toStringList());
204:
205: RDMHeader rdmh = new RDMHeader(sl);
206: if (rdmh.getRDMErrorMessage() != null) {
207: ReportError.reportError(ReportError.INTERNAL, "SOIFGetter",
208: "getSOIF", "RDM Error ["
209: + rdmh.getRDMErrorMessage() + "]");
210: status = ABEND;
211: return;
212: }
213: }
214:
215: /**
216: * Run the thread, put the SOIF.
217: */
218: public void run() {
219: status = PROCESSING;
220:
221: putSOIF();
222: if (status != ABEND)
223: status = COMPLETE;
224:
225: System.out.println(Messages.COMPLETESOIFTHREAD);
226: }
227:
228: }
|