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.logistics.ui.inventory;
028:
029: import java.util.Hashtable;
030: import java.util.Collections;
031: import java.util.Enumeration;
032: import java.util.Vector;
033:
034: import java.awt.Component;
035: import javax.swing.JOptionPane;
036:
037: import java.io.InputStream;
038: import java.io.ObjectInputStream;
039: import java.io.InputStreamReader;
040: import java.io.BufferedReader;
041: import java.nio.charset.Charset;
042: import java.io.StringWriter;
043:
044: import org.cougaar.util.log.Logging;
045: import org.cougaar.util.log.Logger;
046:
047: /**
048: * <pre>
049: *
050: * The InventoryConnectionManager manages the connnection between
051: * the LogisticsInventoryGUI and the servlet. It gets the data
052: * necessary to populate the gui screens. The GUI asks for data
053: * the connection manager gets it and the Frame and Panels are populated
054: * with the returned data.
055: *
056: * @see InventoryDataSource
057: * @see InventoryFileManager
058: * @see InventoryUIFrame
059: *
060: **/
061:
062: public class InventoryConnectionManager implements InventoryDataSource {
063:
064: public static final String SERV_ID = "log_inventory";
065: public static final int INITIAL_XML_SIZE = 800000;
066:
067: final public static String ASSET = "ASSET";
068: final public static String ASSET_AND_CLASSTYPE = ASSET + ":"
069: + "CLASS_TYPE:";
070: final public static String GET_ALL_CLASS_TYPES = "ALL";
071: final public static String[] ASSET_CLASS_TYPES = {
072: GET_ALL_CLASS_TYPES, "Ammunition", "BulkPOL",
073: "Subsistence", "ClassVIIIMedical", "PackagedPOL",
074: "Consumable" };
075:
076: protected Component parentComponent;
077: protected String servProt;
078: protected String servHost;
079: protected String servPort;
080: protected Hashtable orgURLs;
081:
082: protected Hashtable visitedOrgURLs;
083:
084: protected String invXMLStr;
085:
086: protected Logger logger;
087:
088: public InventoryConnectionManager(Component parent) {
089: this (parent, "http", "localhost", "8800");
090: }
091:
092: public InventoryConnectionManager(Component parent,
093: String targetProtocol, String targetHost, String targetPort) {
094: parentComponent = parent;
095: servHost = targetHost;
096: servPort = targetPort;
097: servProt = targetProtocol;
098: logger = Logging.getLogger(this );
099: visitedOrgURLs = new Hashtable();
100:
101: // Support HTTPS with client-cert authentication
102: doSecureUserAuthInit();
103: }
104:
105: // Invoke the NAI security code, if available
106: private void doSecureUserAuthInit() {
107: String securityUIClass = System
108: .getProperty("org.cougaar.ui.userAuthClass");
109:
110: if (securityUIClass == null) {
111: securityUIClass = "org.cougaar.core.security.userauth.UserAuthenticatorImpl";
112: }
113:
114: Class cls = null;
115: try {
116: cls = Class.forName(securityUIClass);
117: } catch (ClassNotFoundException e) {
118: if (logger.isInfoEnabled())
119: logger.info("Not using secure User Authentication: "
120: + securityUIClass);
121: } catch (ExceptionInInitializerError e) {
122: if (logger.isWarnEnabled())
123: logger.warn(
124: "Unable to use secure User Authentication: "
125: + securityUIClass + ". ", e);
126: } catch (LinkageError e) {
127: if (logger.isInfoEnabled())
128: logger.info("Not using secure User Authentication: "
129: + securityUIClass);
130: }
131:
132: if (cls != null) {
133: try {
134: cls.newInstance();
135: } catch (Exception e) {
136: if (logger.isWarnEnabled())
137: logger.warn(
138: "Error using secure User Authentication ("
139: + securityUIClass + "): ", e);
140: }
141: }
142: }
143:
144: public static InventoryConnectionManager queryUserForConnection(
145: Component parent, InventoryDataSource dataSource) {
146: InventoryConnectionManager returnManager;
147:
148: if ((dataSource != null)
149: && (dataSource instanceof InventoryConnectionManager)) {
150: returnManager = (InventoryConnectionManager) dataSource;
151: } else {
152: returnManager = new InventoryConnectionManager(parent);
153: }
154: if (returnManager.getOrgHostAndPort()) {
155: return returnManager;
156: }
157:
158: return null;
159: }
160:
161: public String getCurrentInventoryData() {
162: return invXMLStr;
163: }
164:
165: public String getInventoryData(String orgName, String assetName) {
166: InputStream is = null;
167: ConnectionHelper connection = null;
168: String orgURL = (String) orgURLs.get(orgName);
169:
170: if (logger.isInfoEnabled()) {
171: logger.info("ExecutingQuery: " + assetName + " to: "
172: + orgURL + " for: " + SERV_ID);
173: }
174:
175: //System.out.println("InventoryConnectionManager>>getInventoryData-ExecutingQuery: org-"+ orgName + " asset-" + assetName + " to: " + orgURL + " for: " + SERV_ID);
176:
177: try {
178: connection = new ConnectionHelper(orgURL, SERV_ID);
179: connection.sendData(assetName);
180: is = connection.getInputStream();
181: } catch (Exception e) {
182: displayErrorString(e.toString());
183: }
184:
185: invXMLStr = null;
186: StringWriter writer = new StringWriter(INITIAL_XML_SIZE);
187: try {
188: //ObjectInputStream p = new ObjectInputStream(is);
189: //invXMLStr = (String)p.readObject();
190: BufferedReader p = new BufferedReader(
191: new InputStreamReader(is, Charset.forName("ASCII")));
192: writer.write(p.readLine() + "\n");
193: String currLine = p.readLine();
194: while (currLine != null) {
195: writer.write(currLine + "\n");
196: currLine = p.readLine();
197: }
198: p.close();
199: writer.flush();
200: invXMLStr = writer.toString();
201: connection.closeConnection();
202: } catch (Exception e) {
203: displayErrorString("getInventoryData:Object read exception: "
204: + e);
205: }
206:
207: //parse here
208:
209: return invXMLStr;
210: }
211:
212: public Vector getAssetNames(String orgName, String supplyType) {
213: String orgURL = (String) orgURLs.get(orgName);
214: //When querying for all just ASSET
215: String queryStr = ASSET;
216:
217: if ((supplyType != null)
218: && (!(supplyType.equals(GET_ALL_CLASS_TYPES)))) {
219: queryStr = (ASSET_AND_CLASSTYPE + supplyType);
220: }
221:
222: //logger.debug("Submitting: " + queryStr + " to: " + orgURL +
223: // " for: " + SERV_ID);
224:
225: System.out.println("Submitting: " + queryStr + " to: " + orgURL
226: + " for: " + SERV_ID);
227:
228: ConnectionHelper connection = null;
229: InputStream is = null;
230: try {
231: connection = new ConnectionHelper(orgURL, SERV_ID);
232:
233: connection.sendData(queryStr);
234:
235: is = connection.getInputStream();
236: } catch (Exception e) {
237: displayErrorString(e.toString());
238: return null;
239: }
240: Vector assetNames = null;
241: try {
242: ObjectInputStream p = new ObjectInputStream(is);
243: assetNames = (Vector) p.readObject();
244: p.close();
245: connection.closeConnection();
246: } catch (Exception e) {
247: displayErrorString("Object read exception: " + "_2_"
248: + e.toString());
249: return null;
250: }
251: Collections.sort(assetNames);
252: if ((assetNames != null) && (!assetNames.isEmpty())) {
253: visitedOrgURLs.put(orgName, orgURL);
254: }
255: return assetNames;
256: }
257:
258: public String[] getSupplyTypes() {
259: return ASSET_CLASS_TYPES;
260: }
261:
262: public String getDefaultOrganizationName() {
263: Vector orgNames = getSortedOrgNames();
264: if (orgNames == null)
265: return null;
266: return (String) orgNames.elementAt(1);
267: }
268:
269: public Vector getOrgNames(String agentPath, String orgPopMethod) {
270: if (orgPopMethod.equals(InventorySelectionEvent.ORGS_HIST)) {
271: orgURLs = visitedOrgURLs;
272: return getSortedOrgNames();
273: } else if (orgPopMethod
274: .equals(InventorySelectionEvent.ORGS_ALL)) {
275: return getOrgNames(agentPath, true);
276: }
277: String newAgentPath = agentPath;
278: if (newAgentPath == null) {
279: newAgentPath = ".";
280: }
281: return getOrgNames(newAgentPath, false);
282: }
283:
284: protected Vector getOrgNames(String agentPath, boolean getAll) {
285: logger.debug("Getting Org List");
286: ConnectionHelper connection = null;
287: try {
288: connection = new ConnectionHelper(getURLString());
289: if (getAll) {
290: orgURLs = connection
291: .getAllClusterIdsAndURLs(parentComponent);
292: } else {
293: orgURLs = connection.getClusterIdsAndURLs(
294: parentComponent, agentPath);
295: }
296: connection.closeConnection();
297: connection = null;
298: if (orgURLs == null) {
299: logger.warn("No ORG/Agents");
300: return null;
301: }
302: } catch (Exception e) {
303: logger.error(e.toString());
304: return null;
305: }
306: return getSortedOrgNames();
307: }
308:
309: protected Vector getSortedOrgNames() {
310: if (orgURLs != null) {
311: Enumeration names = orgURLs.keys();
312: Vector vNames = new Vector();
313: while (names.hasMoreElements())
314: vNames.addElement(names.nextElement());
315: Collections.sort(vNames);
316: return vNames;
317: }
318: return null;
319: }
320:
321: protected void displayErrorString(String reply) {
322: JOptionPane.showMessageDialog(parentComponent, reply, reply,
323: JOptionPane.ERROR_MESSAGE);
324: }
325:
326: public String getURLString() {
327: return servProt + "://" + servHost + ":" + servPort + "/";
328: }
329:
330: public boolean getOrgHostAndPort() {
331:
332: String msg = "Enter cluster Log Plan Server location as host:port";
333: String s = ConnectionHelper.getClusterHostPort(parentComponent,
334: msg, servHost, servPort);
335: if ((s == null) || (s.trim().equals(""))) {
336: displayErrorString("Entered Nothing. Cannot get connection.");
337: return false;
338: }
339: s = s.trim();
340: if (s.length() != 0) {
341: String[] substrings = s.split("://");
342: if (substrings.length >= 2) {
343: servProt = substrings[0];
344: String hostAndPort = substrings[1];
345: int i = hostAndPort.indexOf(":");
346: if (i != -1) {
347: servHost = hostAndPort.substring(0, i);
348: servPort = hostAndPort.substring(i + 1);
349: //System.out.println("getOrgHostAndPort url = " + getURLString());
350: return true;
351: }
352: }
353: }
354: displayErrorString("Improper Format for url. Cannot get connection.");
355: return false;
356: }
357: }
|