001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.csmart.ui.servlet;
028:
029: import org.cougaar.core.servlet.SimpleServletSupport;
030: import org.cougaar.core.util.UID;
031: import org.cougaar.planning.ldm.asset.Asset;
032: import org.cougaar.planning.ldm.asset.CommunityPG;
033: import org.cougaar.planning.ldm.plan.HasRelationships;
034: import org.cougaar.tools.csmart.ui.monitor.PropertyNames;
035: import org.cougaar.util.PropertyTree;
036: import org.cougaar.util.UnaryPredicate;
037: import org.cougaar.util.log.Logger;
038:
039: import javax.servlet.ServletException;
040: import javax.servlet.ServletOutputStream;
041: import javax.servlet.http.HttpServlet;
042: import javax.servlet.http.HttpServletRequest;
043: import javax.servlet.http.HttpServletResponse;
044: import java.io.IOException;
045: import java.io.ObjectOutputStream;
046: import java.util.Collection;
047: import java.util.Date;
048: import java.util.Iterator;
049: import java.util.Vector;
050:
051: /**
052: * Returns agent name and community name from Entity object. <br>
053: * Expects no input.
054: *
055: * <p>
056: * Can be loaded manually by including this line in an agent's .ini configuration file: <pre/>
057: * plugin = org.cougaar.core.servlet.SimpleServletComponent(org.cougaar.tools.csmart.ui.servlet.CommunityProviderServlet,
058: * /CSMART_CommunityProviderServlet) </pre>
059: *
060: * <p>
061: * Is loaded from a URL on a CSMART machine, on agent 'Agent':
062: * http://localhost:port/$Agent/CSMART_CommunityProviderServlet
063: */
064: public class CommunityProviderServlet extends HttpServlet {
065: private SimpleServletSupport support;
066:
067: public void setSimpleServletSupport(SimpleServletSupport support) {
068: this .support = support;
069: if (!("/CSMART_CommunityProviderServlet".equals(support
070: .getPath()))) {
071: support.getLog().error(
072: "Incorrect servlet path: " + support.getPath());
073: }
074: }
075:
076: public void doGet(HttpServletRequest request,
077: HttpServletResponse response) throws IOException,
078: ServletException {
079: // create a new "CommunityProvider" context per request
080: CommunityProvider cp = new CommunityProvider(support);
081: cp.execute(request, response);
082: }
083:
084: public void doPost(HttpServletRequest request,
085: HttpServletResponse response) throws IOException,
086: ServletException {
087: // create a new "CommunityProvider" context per request
088: CommunityProvider cp = new CommunityProvider(support);
089: cp.execute(request, response);
090: }
091:
092: /**
093: * This inner class does all the work.
094: * <p>
095: * A new class is created per request, to keep all the
096: * instance fields separate. If there was only one
097: * instance then multiple simultaneous requests would
098: * corrupt the instance fields (e.g. the "out" stream).
099: * <p>
100: * This acts as a <b>context</b> per request.
101: */
102: private static class CommunityProvider {
103:
104: /*
105: * parameters from the URL:
106: */
107: ServletOutputStream out;
108:
109: /* since "ClusterProvider" is a static inner class, here
110: * we hold onto the support API.
111: *
112: * this makes it clear that ClusterProvider only uses
113: * the "support" from the outer class.
114: */
115: private SimpleServletSupport support;
116: private Logger log;
117:
118: // inner class constructor
119: public CommunityProvider(SimpleServletSupport support) {
120: this .support = support;
121: this .log = support.getLog();
122: }
123:
124: /**
125: * Called when a request is received from a client.
126: * Get the POST data; parse the request; get the objects
127: * that match the request; send them to the client.
128: */
129: public void execute(HttpServletRequest request,
130: HttpServletResponse response) throws IOException,
131: ServletException {
132:
133: /*
134: this.out = response.getOutputStream();
135:
136: //There is no mode, as it only returns the list of agent
137: //names, so do nothing but set up for parameter parsing.
138: */
139:
140: if (request.getQueryString() == null) {
141: response.setStatus(HttpServletResponse.SC_OK);
142: }
143:
144: // check for illegal arguments
145: if (request.getQueryString() != null) {
146: response.sendError(HttpServletResponse.SC_BAD_REQUEST,
147: "<html><body><br><font size=4>"
148: + "<font size = 5><B>"
149: + request.getQueryString()
150: + "</B></font>"
151: + " Is Not A Legal Parameter List<br>"
152: + "This servlet expects no parameters"
153: + "</font></body></html>");
154: }
155:
156: this .out = response.getOutputStream();
157:
158: // need try/catch here or caller sends exceptions to client as html
159: try {
160:
161: if (log.isDebugEnabled()) {
162: log
163: .debug("CSMART_CommunityProviderServlet received query..........");
164: }
165:
166: StringBuffer buf = request.getRequestURL();
167: Vector collection = getSelfInformation(buf);
168: //out.print(collection);
169:
170: ObjectOutputStream p = new ObjectOutputStream(out);
171: p.writeObject(collection);
172:
173: if (log.isDebugEnabled()) {
174: log.debug("Sent agent urls");
175: }
176:
177: } catch (Exception e) {
178: if (log.isErrorEnabled()) {
179: log
180: .error(
181: "CSMART_CommunityProviderServlet Exception",
182: e);
183: }
184: }
185: }
186:
187: /**
188: * Get object which describes this agent and its community.
189: */
190: private static UnaryPredicate getSelfPred() {
191: return new UnaryPredicate() {
192: public boolean execute(Object obj) {
193: if (obj instanceof Asset) {
194: Asset asset = (Asset) obj;
195: if ((asset instanceof HasRelationships)
196: && ((HasRelationships) asset).isLocal()
197: && asset.hasClusterPG())
198: return true;
199: }
200: return false;
201: }
202: };
203: }
204:
205: /**
206: * Get agent and community name for this agent.
207: * Returns a vector which contains a single PropertyTree which contains the
208: * properties for this agent.
209: */
210: private Vector getSelfInformation(StringBuffer buf) {
211: Collection container = support
212: .queryBlackboard(getSelfPred());
213: Iterator iter = container.iterator();
214: Vector results = new Vector(1);
215: // int n = 0; // unique index for relationships
216: while (iter.hasNext()) {
217: Asset asset = (Asset) iter.next();
218: PropertyTree properties = new PropertyTree();
219: properties.put(PropertyNames.UID_ATTR,
220: getUIDAsString(asset.getUID()));
221: // String name = asset.getItemIdentificationPG().getNomenclature();
222: // THIS METHOD OF ACCESSING AGENT NAME MUST MATCH HOW OTHER ServletS
223: // (ESPECIALLY CSMART_PlanServlet) ACCESS THE AGENT NAME SO COMPARISONS
224: // CAN BE MADE AT THE CLIENT
225: String name = support.getEncodedAgentName();
226: properties.put(PropertyNames.AGENT_NAME, name);
227: CommunityPG communityPG = asset
228: .getCommunityPG(new Date().getTime());
229: String communityName = null;
230: if (communityPG != null) {
231: Collection communities = communityPG
232: .getCommunities();
233: if (communities.size() > 1)
234: if (log.isWarnEnabled()) {
235: log
236: .warn("CSMART_CommunityProviderServlet: WARNING: "
237: + "handling agents in multiple communities is not implemented.");
238: }
239: Iterator i = communities.iterator();
240: while (i.hasNext()) {
241: communityName = (String) i.next();
242: break;
243: }
244: } else {
245: communityName = "COUGAAR";
246: }
247: if (communityName != null)
248: properties.put(PropertyNames.AGENT_COMMUNITY_NAME,
249: communityName);
250:
251: // reconstruct url
252: String url = buf.toString();
253: //URL url = psc.lookupURL(psc.getServerPluginSupport().getClusterIDAsString());
254: if (url != null)
255: properties.put(PropertyNames.AGENT_URL, url
256: .toString());
257: results.add(properties);
258: }
259: return results;
260: }
261:
262: private static final String getUIDAsString(final UID uid) {
263: return ((uid != null) ? uid.toString() : "null");
264: }
265: }
266: }
|