Source Code Cross Referenced for SDUseCaseServlet.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 2002-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.servlet.BaseServletComponent;
032:        import org.cougaar.core.servlet.ComponentServlet;
033:        import org.cougaar.logistics.plugin.utils.ALStatusChangeMessage;
034:        import org.cougaar.util.UnaryPredicate;
035:
036:        import org.cougaar.core.agent.service.alarm.ExecutionTimer;
037:
038:        import javax.servlet.Servlet;
039:        import javax.servlet.http.HttpServlet;
040:        import javax.servlet.http.HttpServletRequest;
041:        import javax.servlet.http.HttpServletResponse;
042:        import java.io.IOException;
043:        import java.io.PrintWriter;
044:        import java.util.Collection;
045:        import java.util.Date;
046:        import java.util.Iterator;
047:        import java.text.SimpleDateFormat;
048:        import java.text.ParsePosition;
049:
050:        public class SDUseCaseServlet extends ComponentServlet implements 
051:                BlackboardClient {
052:
053:            private BlackboardService blackboard;
054:            private static String ROLE = "PackagedPOLSupplyProvider";
055:            private static String[] ROLE_NAMES = { "AmmunitionProvider",
056:                    "FuelSupplyProvider", "PackagedPOLSupplyProvider",
057:                    "SparePartsProvider", "StrategicTransportationProvider",
058:                    "SubsistenceSupplyProvider" };
059:            private static String PATH = "/SD_Use_Case";
060:            private static String ROLE_PARAMETER = "role";
061:            private static String END_DATE_PARAMETER = "enddate";
062:
063:            public void init() {
064:                // get the blackboard service
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:            }
072:
073:            public void unload() {
074:                super .unload();
075:                // release the blackboard service
076:                if (blackboard != null) {
077:                    serviceBroker.releaseService(this , BlackboardService.class,
078:                            blackboard);
079:                    blackboard = null;
080:                }
081:            }
082:
083:            protected String getPath() {
084:                return PATH;
085:            }
086:
087:            //     public void setBlackboardService(BlackboardService blackboard) {
088:            // 	this.blackboard = blackboard;
089:            //     }
090:
091:            // BlackboardClient method:
092:            public String getBlackboardClientName() {
093:                return toString();
094:            }
095:
096:            // BlackboardClient method:
097:            public long currentTimeMillis() {
098:                return new ExecutionTimer().currentTimeMillis();
099:            }
100:
101:            //     // unused BlackboardClient method:
102:            //     public boolean triggerEvent(Object event) {
103:            // 	return false;
104:            //     }
105:
106:            UnaryPredicate pred = new UnaryPredicate() {
107:                public boolean execute(Object o) {
108:                    return o instanceof  ALStatusChangeMessage;
109:                }
110:            };
111:
112:            public void doGet(HttpServletRequest req, HttpServletResponse res)
113:                    throws IOException {
114:                PrintWriter out = res.getWriter();
115:                Collection col;
116:                String end_date_string = req.getParameter(END_DATE_PARAMETER);
117:                String role = req.getParameter(ROLE_PARAMETER);
118:                SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
119:                Date end_date = null;
120:                if (end_date_string != null) {
121:                    end_date = dateFormat.parse(end_date_string,
122:                            new ParsePosition(0));
123:                }
124:                long now = currentTimeMillis();
125:                printTitles(out);
126:                printInfo(now, out);
127:                try {
128:                    blackboard.openTransaction();
129:                    col = blackboard.query(pred);
130:
131:                    if (!col.isEmpty()) {
132:                        Iterator it = col.iterator();
133:                        ALStatusChangeMessage message = (ALStatusChangeMessage) it
134:                                .next();
135:                        printChangeInfo(message, false, out);
136:
137:                    } else {
138:                        if (end_date_string == null) {
139:                            printForm(req, out);
140:                        } else if (end_date == null) {
141:                            printError("Could not read date; try again.", out);
142:                            printForm(req, out);
143:                        } else if (end_date.getTime() < now) {
144:                            printError(
145:                                    "End date must be in the future; try again.",
146:                                    out);
147:                            printForm(req, out);
148:                        } else {
149:                            ALStatusChangeMessage message = new ALStatusChangeMessage(
150:                                    role, false, end_date);
151:                            blackboard.publishAdd(message);
152:                            printChangeInfo(message, true, out);
153:                        }
154:                    }
155:                } catch (Exception e) {
156:                    printError("Servlet error: " + e, out);
157:                } finally {
158:                    blackboard.closeTransactionDontReset();
159:                    out.println("</body>");
160:                    out.flush();
161:                }
162:            }
163:
164:            private void printTitles(PrintWriter out) {
165:                String title = "Service Discovery Use Case Servlet";
166:                out.println("<HEAD><TITLE>" + title + "</TITLE></HEAD>"
167:                        + "<BODY><H1>" + title + "</H1>");
168:            }
169:
170:            private void printInfo(long now, PrintWriter out) {
171:                String localAgent = getEncodedAgentName();
172:                out.println("<p>\n" + "Agent: " + localAgent + "<p>\n"
173:                        + "Current Society Time: " + new Date(now));
174:            }
175:
176:            private void printForm(HttpServletRequest req, PrintWriter out) {
177:                String optionString = "";
178:                for (int i = 0; i < ROLE_NAMES.length; i++) {
179:                    optionString = optionString.concat("<option value=\""
180:                            + ROLE_NAMES[i] + "\">" + ROLE_NAMES[i]
181:                            + "</option>" + "\n");
182:                }
183:
184:                out.print("<form method=\"GET\" action=\""
185:                        + req.getRequestURI() + "\">\n" + "Enter role: "
186:                        + "<select size=1 name=\"" + ROLE_PARAMETER + "\">\n"
187:                        + optionString + "</select>"
188:                        + "<p>Enter new end date (MM/DD/YYYY): "
189:                        + "<input type=\"text\" name=\"" + END_DATE_PARAMETER
190:                        + "\" size=40>\n"
191:                        + "<p><input type=\"submit\" value=\"Submit\">\n"
192:                        + "</form>\n");
193:            }
194:
195:            private void printChangeInfo(ALStatusChangeMessage message,
196:                    boolean newChange, PrintWriter out) {
197:                out.println("<p>\n");
198:                if (newChange) {
199:                    out.println("Successfully posted provider change:");
200:                } else {
201:                    out.println("Provider already changed:");
202:                }
203:                out.println("<ul><li>Role: " + message.getRole()
204:                        + "<li>End Date: " + message.getEndDate()
205:                        + "<li>Registry Updated: " + message.registryUpdated()
206:                        + "</ul>");
207:            }
208:
209:            private void printError(String text, PrintWriter out) {
210:                out.println("<p>\n" + "<b>" + text + "</b>" + "<p>");
211:            }
212:
213:            private String headWithTitle(String title) {
214:                return "<HEAD><TITLE>" + title + "</TITLE></HEAD>";
215:            }
216:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.