Source Code Cross Referenced for ConditionWorker.java in  » Science » Cougaar12_4 » org » cougaar » logistics » servlet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Cougaar12_4 » org.cougaar.logistics.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.servlet;
028:
029:        import java.io.*;
030:        import java.net.*;
031:        import java.util.*;
032:        import java.text.*;
033:        import javax.servlet.http.HttpServlet;
034:        import javax.servlet.http.HttpServletRequest;
035:        import javax.servlet.http.HttpServletResponse;
036:        import javax.servlet.ServletException;
037:
038:        import org.cougaar.core.servlet.SimpleServletSupport;
039:        import org.cougaar.core.wp.ListAllAgents;
040:
041:        import org.cougaar.planning.ldm.asset.Asset;
042:
043:        import org.cougaar.planning.servlet.data.xml.XMLWriter;
044:        import org.cougaar.planning.servlet.data.xml.XMLable;
045:        import org.cougaar.planning.servlet.ServletWorker;
046:
047:        /**
048:         * <pre>
049:         * Servlet worker
050:         *
051:         * Takes one parameter: 
052:         * - conditionValue, which controls the level of the double condition named 
053:         * in the component parameter *conditionName* in all agents with ConditionSetterServlets 
054:         * with the same condition name.
055:         *
056:         * An example URL is :
057:         * 
058:         * http://localhost:8800/$TRANSCOM/conditionsetter
059:         *
060:         * NOTE : If any agent hangs, the whole request will hang...
061:         * </pre>
062:         */
063:        public class ConditionWorker extends ServletWorker {
064:
065:            public NumberFormat numberFormat = new DecimalFormat("#.#");
066:
067:            /**
068:             * This is the path for my Servlet, relative to the
069:             * Agent's URLEncoded name.
070:             * <p>
071:             * For example, on Agent "X" the URI request path
072:             * will be "/$X/conditionsetter".
073:             */
074:            private final String myPath = "/conditionsetter";
075:
076:            /**
077:             * Pretty to-String for debugging.
078:             */
079:            public String toString() {
080:                return getClass().getName() + "(" + myPath + ")";
081:            }
082:
083:            /**
084:             * Main method. <p>
085:             * <p>
086:             * Setting debug level to *info* will give more information about which agents 
087:             * could not have their condition set.
088:             */
089:            public void execute(HttpServletRequest request,
090:                    HttpServletResponse response, SimpleServletSupport support)
091:                    throws IOException, ServletException {
092:                this .support = (ConditionSupport) support;
093:                ConditionSupport conditionSupport = (ConditionSupport) support;
094:
095:                XMLable responseData = null;
096:
097:                super .execute(request, response, support);
098:
099:                if (setAllAgents) {
100:                    format = FORMAT_HTML;
101:
102:                    List knownAgents = getAllEncodedAgentNames();
103:                    List validAgents = new ArrayList();
104:                    for (Iterator iter = knownAgents.iterator(); iter.hasNext();) {
105:                        String agentName = (String) iter.next();
106:                        StringBuffer buf = new StringBuffer();
107:                        buf.append("http://");
108:                        buf.append(request.getServerName());
109:                        buf.append(":");
110:                        buf.append(request.getServerPort());
111:                        buf.append("/$");
112:                        buf.append(agentName);
113:                        buf.append(support.getPath());
114:                        buf.append("?" + ConditionServlet.CONDITION_PARAM + "="
115:                                + numberFormat.format(doubleCondition));
116:
117:                        String url = buf.toString();
118:
119:                        if (support.getLog().isInfoEnabled()) {
120:                            support.getLog().info(
121:                                    " In "
122:                                            + support.getAgentIdentifier()
123:                                            + ", setting "
124:                                            + conditionSupport
125:                                                    .getConditionName()
126:                                            + " on " + agentName + ", URL: "
127:                                            + url);
128:                        }
129:
130:                        URL myURL = new URL(url);
131:                        HttpURLConnection myConnection = (HttpURLConnection) myURL
132:                                .openConnection();
133:                        try {
134:                            if (myConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
135:                                if (myConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
136:                                    support
137:                                            .getLog()
138:                                            .info(
139:                                                    support
140:                                                            .getAgentIdentifier()
141:                                                            + " - servlet not at agent.  Got NOT FOUND code reading from URL "
142:                                                            + myURL);
143:                                } else {
144:                                    support
145:                                            .getLog()
146:                                            .error(
147:                                                    support
148:                                                            .getAgentIdentifier()
149:                                                            + " - got error reading from URL "
150:                                                            + myURL
151:                                                            + " code was "
152:                                                            + myConnection
153:                                                                    .getResponseCode());
154:                                }
155:                            } else {
156:                                // throws an FileNotFoundExcep if no servlet at that agent
157:                                InputStream is = myConnection.getInputStream();
158:
159:                                ObjectInputStream ois = new ObjectInputStream(
160:                                        is);
161:
162:                                try {
163:                                    ConditionData settingData = (ConditionData) ois
164:                                            .readObject();
165:                                    if (settingData.wasSet()) {
166:                                        validAgents.add(agentName);
167:                                    } else {
168:                                        support
169:                                                .getLog()
170:                                                .info(
171:                                                        support
172:                                                                .getAgentIdentifier()
173:                                                                + " - Could not set "
174:                                                                + conditionSupport
175:                                                                        .getConditionName()
176:                                                                + " on agent "
177:                                                                + agentName);
178:                                    }
179:                                } catch (Exception e) {
180:                                    support.getLog().error(
181:                                            "Got exception " + e, e);
182:                                } finally {
183:                                    ois.close();
184:                                }
185:                            }
186:                        } catch (FileNotFoundException fnf) {
187:                            if (support.getLog().isInfoEnabled())
188:                                support
189:                                        .getLog()
190:                                        .info(
191:                                                "Skipping agent "
192:                                                        + agentName
193:                                                        + " that has no Condition servlet.");
194:                        } catch (Exception other) {
195:                            if (support.getLog().isWarnEnabled())
196:                                support
197:                                        .getLog()
198:                                        .warn(
199:                                                "Skipping agent "
200:                                                        + agentName
201:                                                        + " that has returned an exception.",
202:                                                other);
203:                        } finally {
204:                            myConnection.disconnect();
205:                        }
206:                    }
207:
208:                    responseData = new ResponseData(doubleCondition,
209:                            validAgents, conditionSupport.getConditionName());
210:                } else {
211:                    boolean val = setDoubleCondition(doubleCondition); // set in getSettings
212:                    responseData = new ConditionData(val); // return whether set successfully
213:                }
214:
215:                writeResponse(responseData, response.getOutputStream(),
216:                        request, support, format);
217:            }
218:
219:            protected List getAllEncodedAgentNames() {
220:                try {
221:                    // do full WP list (deprecated!)
222:                    Set s = ListAllAgents.listAllAgents(support
223:                            .getWhitePagesService());
224:                    // URLEncode the names and sort
225:                    List l = ListAllAgents.encodeAndSort(s);
226:                    return l;
227:                } catch (Exception e) {
228:                    throw new RuntimeException("List all agents failed", e);
229:                }
230:            }
231:
232:            protected static class ResponseData implements  XMLable,
233:                    Serializable {
234:                double doubleCondition;
235:                List validAgents;
236:                String conditionName;
237:
238:                public ResponseData(double cpu, List agents,
239:                        String conditionName) {
240:                    doubleCondition = cpu;
241:                    validAgents = agents;
242:                    this .conditionName = conditionName;
243:                }
244:
245:                public void toXML(XMLWriter w) throws IOException {
246:                    w.optagln("setting");
247:                    w.write("Set condition " + conditionName + " to "
248:                            + doubleCondition + " on " + validAgents
249:                            + " agents.");
250:                    w.cltagln("setting");
251:                }
252:            }
253:
254:            /** 
255:             * <pre>
256:             * sets both recurse and format 
257:             *
258:             * recurse is either true or false
259:             * format  is either data, xml, or html
260:             *
261:             * see class description for what these values mean
262:             * </pre>
263:             */
264:            protected void getSettings(String name, String value) {
265:                super .getSettings(name, value);
266:
267:                if (eq(ConditionServlet.SET_ALL_AGENTS, name)) {
268:                    setAllAgents = true;
269:                } else if (eq(ConditionServlet.CONDITION_PARAM, name)) {
270:                    try {
271:                        doubleCondition = numberFormat.parse(value)
272:                                .doubleValue();
273:                    } catch (Exception e) {
274:                        support.getLog().error(
275:                                "bad format for decimal cpu level = " + value);
276:                    }
277:                } else {
278:                    if (support.getLog().isDebugEnabled())
279:                        support.getLog().debug(
280:                                "NOTE : Ignoring parameter named " + name);
281:                }
282:            }
283:
284:            protected boolean setDoubleCondition(double value) {
285:                boolean successful = false;
286:                ConditionServlet.DoubleCondition doubleCondition = support
287:                        .getCondition();
288:
289:                if (doubleCondition != null) {
290:                    doubleCondition.setValue(new Double(value));
291:
292:                    if (support.getLog().isInfoEnabled())
293:                        support.getLog().info(
294:                                support.getAgentIdentifier() + " - Setting "
295:                                        + support.getConditionName() + " = "
296:                                        + doubleCondition.getValue());
297:
298:                    try {
299:                        support.getBlackboardService().openTransaction();
300:                        support.getBlackboardService().publishChange(
301:                                doubleCondition);
302:                        successful = true;
303:                    } catch (Exception exc) {
304:                        support.getLog().error(
305:                                "Could not publish "
306:                                        + support.getConditionName()
307:                                        + " condition???", exc);
308:                    } finally {
309:                        support.getBlackboardService()
310:                                .closeTransactionDontReset();
311:                    }
312:                } else if (support.getLog().isInfoEnabled())
313:                    support
314:                            .getLog()
315:                            .info(
316:                                    support.getAgentIdentifier()
317:                                            + " - condition service could not find condition "
318:                                            + support.getConditionName());
319:
320:                return successful;
321:            }
322:
323:            protected double doubleCondition;
324:            protected boolean setAllAgents = false;
325:
326:            protected ConditionSupport support;
327:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.