Source Code Cross Referenced for CommStatusServlet.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 org.cougaar.core.blackboard.BlackboardClient;
030:        import org.cougaar.core.service.BlackboardService;
031:        import org.cougaar.core.service.AlarmService;
032:        import org.cougaar.core.servlet.BaseServletComponent;
033:        import org.cougaar.util.UnaryPredicate;
034:
035:        import javax.servlet.Servlet;
036:        import javax.servlet.http.HttpServlet;
037:        import javax.servlet.http.HttpServletRequest;
038:        import javax.servlet.http.HttpServletResponse;
039:        import java.io.IOException;
040:        import java.io.PrintWriter;
041:        import java.util.Collection;
042:        import java.util.Iterator;
043:
044:        /**
045:         * This servlet publishes a CommStatus object to the agent's blackboard.
046:         *
047:         * commStatus?commUp=false&connectedAgentName=<AGENT NAME>
048:         * <AGENT NAME> refers to the agent that has lost communications
049:         */
050:
051:        public class CommStatusServlet extends BaseServletComponent implements 
052:                BlackboardClient {
053:            private BlackboardService blackboard;
054:            private AlarmService alarmService;
055:
056:            protected String getPath() {
057:                return "/" + getRelativePath();
058:            }
059:
060:            protected String getRelativePath() {
061:                return "commStatus";
062:            }
063:
064:            protected Servlet createServlet() {
065:                blackboard = (BlackboardService) serviceBroker.getService(this ,
066:                        BlackboardService.class, null);
067:                if (blackboard == null) {
068:                    throw new RuntimeException(
069:                            "Unable to obtain blackboard service");
070:                }
071:                alarmService = (AlarmService) serviceBroker.getService(this ,
072:                        AlarmService.class, null);
073:                if (alarmService == null) {
074:                    throw new RuntimeException("Unable to set alarm service");
075:                }
076:                return new MyServlet();
077:            }
078:
079:            public void setBlackboardService(BlackboardService blackboard) {
080:                this .blackboard = blackboard;
081:            }
082:
083:            // BlackboardClient method:
084:            public String getBlackboardClientName() {
085:                return toString();
086:            }
087:
088:            public long currentTimeMillis() {
089:                return alarmService.currentTimeMillis();
090:            }
091:
092:            // unused BlackboardClient method:
093:            public boolean triggerEvent(Object event) {
094:                return false;
095:            }
096:
097:            public void unload() {
098:                super .unload();
099:                // release the blackboard service
100:                if (blackboard != null) {
101:                    serviceBroker.releaseService(this , BlackboardService.class,
102:                            blackboard);
103:                    blackboard = null;
104:                }
105:
106:                // release the alarm service
107:                if (alarmService != null) {
108:                    serviceBroker.releaseService(this , AlarmService.class,
109:                            alarmService);
110:                    alarmService = null;
111:                }
112:            }
113:
114:            private class MyServlet extends HttpServlet {
115:                UnaryPredicate commStatusObjectsQuery = new UnaryPredicate() {
116:                    public boolean execute(Object o) {
117:                        return o instanceof  CommStatus;
118:                    }
119:                };
120:
121:                public void doGet(HttpServletRequest req,
122:                        HttpServletResponse res) throws IOException {
123:                    execute(req, res);
124:                }
125:
126:                public void doPost(HttpServletRequest req,
127:                        HttpServletResponse res) throws IOException {
128:                    execute(req, res);
129:                }
130:
131:                private void errorMsg(String msg, PrintWriter pw) {
132:                    pw.println("<BR>Error.  " + msg + "</body></html>");
133:                    pw.flush();
134:                }
135:
136:                public void execute(HttpServletRequest req,
137:                        HttpServletResponse res) throws IOException {
138:                    String connectedAgentName = req
139:                            .getParameter("connectedAgentName");
140:                    //parse the commUp
141:                    String commUpStr = req.getParameter("commUp");
142:                    boolean commUp;
143:                    CommStatus cs = null;
144:
145:                    //initialize the PrintWriter
146:                    PrintWriter out = res.getWriter();
147:                    out.println("<html><head></head><body>");
148:
149:                    if (commUpStr == null && connectedAgentName == null) {
150:                        blackboard.openTransaction();
151:                        Collection col = blackboard
152:                                .query(commStatusObjectsQuery);
153:                        for (Iterator i = col.iterator(); i.hasNext();) {
154:                            CommStatus temp = (CommStatus) i.next();
155:                            out
156:                                    .println("<BR>"
157:                                            + "CSS Found matching CommStatus object for connectedAgentName: "
158:                                            + temp.getConnectedAgentName()
159:                                            + " isCommUp " + temp.isCommUp()
160:                                            + "</body></html>");
161:
162:                        }
163:                        blackboard.closeTransaction();
164:                        return;
165:                    }
166:
167:                    if (commUpStr != null) {
168:                        commUp = commUpStr.trim().equals("true");
169:                    } else {
170:                        errorMsg("commUpStr incorrectly specified: "
171:                                + commUpStr, out);
172:                        return;
173:                    }
174:
175:                    if (connectedAgentName != null) {
176:                        //iterate over the existing CommStatusObjects to see if one already exists
177:                        blackboard.openTransaction();
178:                        Collection col = blackboard
179:                                .query(commStatusObjectsQuery);
180:                        for (Iterator i = col.iterator(); i.hasNext();) {
181:                            CommStatus temp = (CommStatus) i.next();
182:                            if (temp.connectedAgentName
183:                                    .equals(connectedAgentName)) {
184:                                cs = temp;
185:                                System.out
186:                                        .println("\n CSS Found matching CommStatus object for connectedAgentName: "
187:                                                + cs.getConnectedAgentName()
188:                                                + " isCommUp " + cs.isCommUp());
189:                                break;
190:                            }
191:                        }
192:                        blackboard.closeTransaction();
193:                    } else {
194:                        errorMsg("ConnectedAgentName not specified", out);
195:                        return;
196:                    }
197:
198:                    if (cs == null) { //if an existing CommStatus object was not found then create one
199:                        cs = new CommStatus(connectedAgentName);
200:                        if (commUp == false) {
201:                            cs.setCommLoss(currentTimeMillis());
202:                            System.out
203:                                    .println("\n CSS Setting Comm LOSS for NEW CommStatus object for connectedAgentName: "
204:                                            + connectedAgentName
205:                                            + " isCommUp "
206:                                            + cs.isCommUp());
207:                        } else {
208:                            cs.setCommRestore(currentTimeMillis());
209:                            System.out
210:                                    .println("\n CSS Setting Comm RESTORE for NEW CommStatus object for connectedAgentName: "
211:                                            + connectedAgentName
212:                                            + " isCommUp "
213:                                            + cs.isCommUp());
214:                        }
215:                        blackboard.openTransaction();
216:                        blackboard.publishAdd(cs);
217:                        blackboard.closeTransaction();
218:                    } else { //otherwise update the existing CommStatus object
219:                        if (commUp == false) {
220:                            cs.setCommLoss(currentTimeMillis());
221:                            System.out
222:                                    .println("\n CSS Setting Comm LOSS for MATCHING CommStatus object for connectedAgentName: "
223:                                            + connectedAgentName
224:                                            + " isCommUp "
225:                                            + cs.isCommUp());
226:                        } else {
227:                            cs.setCommRestore(currentTimeMillis());
228:                            System.out
229:                                    .println("\n CSS Setting Comm RESTORE for MATCHING CommStatus object for connectedAgentName: "
230:                                            + connectedAgentName
231:                                            + " isCommUp "
232:                                            + cs.isCommUp());
233:                        }
234:                        blackboard.openTransaction();
235:                        blackboard.publishChange(cs);
236:                        blackboard.closeTransaction();
237:                    }
238:
239:                    out.println("<BR>Success. </body></html>");
240:                    out.flush();
241:                }
242:            }
243:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.