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.mlm.ui.planviewer;
028:
029: import java.io.BufferedReader;
030: import java.io.ByteArrayOutputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.InputStreamReader;
034: import java.io.PrintWriter;
035: import java.net.HttpURLConnection;
036: import java.net.MalformedURLException;
037: import java.net.URL;
038: import java.net.URLConnection;
039: import java.util.Hashtable;
040:
041: import org.cougaar.util.OptionPane;
042:
043: /**
044: * Creates connection between client and XML Plan Server.
045: */
046:
047: public class ConnectionHelper {
048: private URL url;
049: private URLConnection connection;
050: static final String DebugPSP_package = "alpine/demo";
051: static final String DebugPSP_id = "DEBUG.PSP";
052: String clusterURL;
053: boolean isDebugPSP = false;
054:
055: /**
056: * If you create a ConnectionHelper instance with 0 params. must
057: * call setConnection()... to intialize connection
058: **/
059: public ConnectionHelper() {
060: }
061:
062: /**
063: Connects to the debug PSP at the specified cluster,
064: where cluster is specified by URL (host and port).
065: */
066:
067: public ConnectionHelper(String clusterURL)
068: throws MalformedURLException, IOException {
069: this (clusterURL, DebugPSP_package, DebugPSP_id);
070: isDebugPSP = true;
071: }
072:
073: public ConnectionHelper(String clusterURL, String path)
074: throws MalformedURLException, IOException {
075: this .clusterURL = clusterURL;
076: url = new URL(clusterURL + path);
077: connection = url.openConnection();
078: connection.setDoInput(true);
079: connection.setDoOutput(true);
080: }
081:
082: /**
083: Connects to the specified PSP at the specified cluster,
084: where cluster is specified by URL (host and port).
085: */
086:
087: public ConnectionHelper(String clusterURL, String PSP_package,
088: String PSP_id) throws MalformedURLException, IOException {
089: this (clusterURL, PSP_package + "/" + PSP_id);
090: }
091:
092: public void setConnection(String completeURLString)
093: throws Exception {
094: if (connection != null) {
095: new RuntimeException(
096: "ConnectionHelper.connection already set!");
097: }
098: clusterURL = completeURLString; // messy -- but should be okay for now
099: url = new URL(completeURLString);
100: connection = url.openConnection();
101: connection.setDoInput(true);
102: connection.setDoOutput(true);
103: }
104:
105: /**
106: * getURL - returns URL for the connection
107: *
108: * @return URL for the connection
109: */
110: public URL getURL() {
111: return url;
112: }
113:
114: /**
115: Sends data on the connection.
116: */
117:
118: public void sendData(String s) throws IOException {
119: ((HttpURLConnection) connection).setRequestMethod("PUT");
120: PrintWriter pw = new PrintWriter(connection.getOutputStream());
121: pw.println(s);
122: pw.flush();
123: }
124:
125: /**
126: Returns input stream for the connection.
127: */
128:
129: public InputStream getInputStream() throws IOException {
130: return connection.getInputStream();
131: }
132:
133: public URLConnection getConnection() {
134: return connection;
135: }
136:
137: /**
138: Sends data and returns response in buffer.
139: */
140:
141: public byte[] getResponse() throws IOException {
142: ByteArrayOutputStream os = new ByteArrayOutputStream();
143: InputStream is = getInputStream();
144: byte b[] = new byte[512];
145: int len;
146: while ((len = is.read(b, 0, 512)) > -1)
147: os.write(b, 0, len);
148: return os.toByteArray();
149: }
150:
151: /**
152: Returns a list of cluster identifiers from the debug PSP
153: located at the cluster specified in this class's constructor.
154: */
155:
156: public Hashtable getClusterIdsAndURLs()
157: throws MalformedURLException, ClassNotFoundException,
158: IOException {
159: ConnectionHelper debugConnection;
160: Hashtable results = new Hashtable();
161: Hashtable results2 = new Hashtable();
162:
163: try {
164: /*
165: get host:port from clusterURL
166: open url connection to host:port/agents?all&text
167: get input stream from connection
168: wrap in BufferedReader
169: read lines of response into temporary ArrayLIst (get agent-names)
170: fill "results" with (agent-name, http://host:port/$ + agent-name + /)
171: return results
172:
173: */
174:
175: int p = clusterURL.lastIndexOf(":");
176: URL url = new URL(clusterURL
177: + "agents?scope=all&format=text");
178: //System.out.println(url.toString());
179: URLConnection urlconn = null;
180: InputStream in;
181: try {
182: urlconn = url.openConnection();
183: in = urlconn.getInputStream();
184: BufferedReader input = new BufferedReader(
185: new InputStreamReader(in));
186:
187: try {
188: while (input != null) {
189: String n = input.readLine();
190: if (n == null)
191: break;
192: String u = ((clusterURL.substring(0, p + 1))
193: + "8800/$" + n + "/");
194: //System.out.println(u);
195: results.put(n, u);
196: }
197: } catch (IOException e) {
198: System.out.println("Error reading agent list: "
199: + e.getMessage());
200: }
201: } catch (IOException e) {
202: System.out.println("Error reaching agents list: "
203: + e.getMessage());
204: }
205:
206: // we don't want this code:
207: /*
208: if (!isDebugPSP) {
209: int i = clusterURL.lastIndexOf(":");
210: if (i == -1)
211: throw new MalformedURLException("Expected host:port");
212: debugConnection = new ConnectionHelper(clusterURL.substring(0,i+1) + "5555/");
213: } else {
214: debugConnection = this;
215: }
216: debugConnection.sendData("LIST_CLUSTERS");
217: ObjectInputStream ois =
218: new ObjectInputStream(debugConnection.getInputStream());
219: while (true) {
220: String nextName = (String)ois.readObject();
221: String nextURL = (String)ois.readObject();
222: results.put(nextName, nextURL);
223: }
224: //
225:
226: } catch (java.io.EOFException e) {
227: // ignore this one
228: }
229: */
230: } catch (Exception e) {
231: // ignore
232: }
233:
234: return results;
235: }
236:
237: /**
238: * getClusterInfo - get the location of the cluster
239: * Only used when running in an application
240: */
241: public static String getClusterHostPort(java.awt.Component parent) {
242: return getClusterHostPort(parent,
243: "Enter location of a cluster.");
244: }
245:
246: public static String getClusterHostPort(java.awt.Component parent,
247: Object msg) {
248: return (String) OptionPane.showInputDialog(parent, msg,
249: "Cluster Location", OptionPane.QUESTION_MESSAGE,
250: //null, null, "localhost:5555");
251: null, null, "localhost:8800");
252:
253: }
254:
255: public static Hashtable getClusterInfo(java.awt.Component parent) {
256: return getClusterInfo(parent, "Enter location of a cluster.");
257: }
258:
259: public static Hashtable getClusterInfo(java.awt.Component parent,
260: Object msg) {
261: String host = getClusterHostPort(parent, msg);
262: if (host == null) {
263: return null;
264: }
265: try {
266: host = host.trim();
267: if (host.length() == 0) {
268: //host = "localhost:5555";
269: host = "localhost:8800";
270: } else {
271: if (host.indexOf(':') < 0) {
272: //host += ":5555";
273: host += ":8800";
274: }
275: }
276: ConnectionHelper connection = new ConnectionHelper(
277: "http://" + host + "/");
278: return connection.getClusterIdsAndURLs();
279: } catch (Exception e) {
280: return null;
281: }
282: }
283: }
|