Source Code Cross Referenced for JspHelper.java in  » Sevlet-Container » apache-tomcat-6.0.14 » org » apache » catalina » manager » 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 » Sevlet Container » apache tomcat 6.0.14 » org.apache.catalina.manager 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.catalina.manager;
019:
020:        import java.io.IOException;
021:        import java.io.Writer;
022:        import java.text.DateFormat;
023:        import java.text.NumberFormat;
024:        import java.text.SimpleDateFormat;
025:        import java.util.Date;
026:        import java.util.Locale;
027:
028:        import org.apache.catalina.Session;
029:        import org.apache.catalina.manager.util.SessionUtils;
030:
031:        /**
032:         * Helper JavaBean for JSPs, because JSTL 1.1/EL 2.0 is too dumb to
033:         * to what I need (call methods with parameters), or I am too dumb to use it correctly. :)
034:         * @author Cédrik LIME
035:         */
036:        public class JspHelper {
037:
038:            private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
039:            private static final String DATE_FORMAT = "yyyy-MM-dd";
040:            private static final String TIME_FORMAT = "HH:mm:ss";
041:
042:            /**
043:             * Public constructor, so that this class can be considered a JavaBean
044:             */
045:            private JspHelper() {
046:                super ();
047:            }
048:
049:            /**
050:             * Try to get user locale from the session, if possible.
051:             * IMPLEMENTATION NOTE: this method has explicit support for Tapestry 3 and Struts 1.x
052:             * @param in_session
053:             * @return String
054:             */
055:            public static String guessDisplayLocaleFromSession(
056:                    Session in_session) {
057:                return localeToString(SessionUtils
058:                        .guessLocaleFromSession(in_session));
059:            }
060:
061:            private static String localeToString(Locale locale) {
062:                if (locale != null) {
063:                    return locale.toString();//locale.getDisplayName();
064:                } else {
065:                    return "";
066:                }
067:            }
068:
069:            /**
070:             * Try to get user name from the session, if possible.
071:             * @param in_session
072:             * @return String
073:             */
074:            public static String guessDisplayUserFromSession(Session in_session) {
075:                Object user = SessionUtils.guessUserFromSession(in_session);
076:                return escapeXml(user);
077:            }
078:
079:            public static String getDisplayCreationTimeForSession(
080:                    Session in_session) {
081:                try {
082:                    DateFormat formatter = new SimpleDateFormat(
083:                            DATE_TIME_FORMAT);
084:                    return formatter.format(new Date(in_session
085:                            .getCreationTime()));
086:                } catch (IllegalStateException ise) {
087:                    //ignore: invalidated session
088:                    return "";
089:                }
090:            }
091:
092:            public static String getDisplayLastAccessedTimeForSession(
093:                    Session in_session) {
094:                try {
095:                    DateFormat formatter = new SimpleDateFormat(
096:                            DATE_TIME_FORMAT);
097:                    return formatter.format(new Date(in_session
098:                            .getLastAccessedTime()));
099:                } catch (IllegalStateException ise) {
100:                    //ignore: invalidated session
101:                    return "";
102:                }
103:            }
104:
105:            public static String getDisplayUsedTimeForSession(Session in_session) {
106:                return secondsToTimeString(SessionUtils
107:                        .getUsedTimeForSession(in_session) / 1000);
108:            }
109:
110:            public static String getDisplayTTLForSession(Session in_session) {
111:                return secondsToTimeString(SessionUtils
112:                        .getTTLForSession(in_session) / 1000);
113:            }
114:
115:            public static String getDisplayInactiveTimeForSession(
116:                    Session in_session) {
117:                return secondsToTimeString(SessionUtils
118:                        .getInactiveTimeForSession(in_session) / 1000);
119:            }
120:
121:            public static String secondsToTimeString(long in_seconds) {
122:                StringBuffer buff = new StringBuffer(9);
123:                long rest = in_seconds;
124:                long hour = rest / 3600;
125:                rest = rest % 3600;
126:                long minute = rest / 60;
127:                rest = rest % 60;
128:                long second = rest;
129:                if (hour < 10) {
130:                    buff.append('0');
131:                }
132:                buff.append(hour);
133:                buff.append(':');
134:                if (minute < 10) {
135:                    buff.append('0');
136:                }
137:                buff.append(minute);
138:                buff.append(':');
139:                if (second < 10) {
140:                    buff.append('0');
141:                }
142:                buff.append(second);
143:                return buff.toString();
144:            }
145:
146:            /**
147:             * Following copied from org.apache.taglibs.standard.tag.common.core.OutSupport v1.1.2
148:             * 
149:             *  Optimized to create no extra objects and write directly
150:             *  to the JspWriter using blocks of escaped and unescaped characters
151:             *
152:             */
153:            private static void writeEscapedXml(char[] buffer, int length,
154:                    Writer w) throws IOException {
155:                int start = 0;
156:
157:                for (int i = 0; i < length; i++) {
158:                    char c = buffer[i];
159:                    if (c <= HIGHEST_SPECIAL) {
160:                        char[] escaped = specialCharactersRepresentation[c];
161:                        if (escaped != null) {
162:                            // add unescaped portion
163:                            if (start < i) {
164:                                w.write(buffer, start, i - start);
165:                            }
166:                            // add escaped xml
167:                            w.write(escaped);
168:                            start = i + 1;
169:                        }
170:                    }
171:                }
172:                // add rest of unescaped portion
173:                if (start < length) {
174:                    w.write(buffer, start, length - start);
175:                }
176:            }
177:
178:            /*
179:             * Following copied from org.apache.taglibs.standard.tag.common.core.Util v1.1.2
180:             */
181:
182:            private static final int HIGHEST_SPECIAL = '>';
183:            private static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][];
184:            static {
185:                specialCharactersRepresentation['&'] = "&amp;".toCharArray();
186:                specialCharactersRepresentation['<'] = "&lt;".toCharArray();
187:                specialCharactersRepresentation['>'] = "&gt;".toCharArray();
188:                specialCharactersRepresentation['"'] = "&#034;".toCharArray();
189:                specialCharactersRepresentation['\''] = "&#039;".toCharArray();
190:            }
191:
192:            public static String escapeXml(Object obj) {
193:                String value = null;
194:                try {
195:                    value = (obj == null) ? null : String.valueOf(obj);
196:                } catch (Exception e) {
197:                    // Ignore
198:                }
199:                return escapeXml(value);
200:            }
201:
202:            /**
203:             * Performs the following substring replacements
204:             * (to facilitate output to XML/HTML pages):
205:             *
206:             *    & -> &amp;
207:             *    < -> &lt;
208:             *    > -> &gt;
209:             *    " -> &#034;
210:             *    ' -> &#039;
211:             *
212:             * See also OutSupport.writeEscapedXml().
213:             */
214:            public static String escapeXml(String buffer) {
215:                if (buffer == null) {
216:                    return "";
217:                }
218:                int start = 0;
219:                int length = buffer.length();
220:                char[] arrayBuffer = buffer.toCharArray();
221:                StringBuffer escapedBuffer = null;
222:
223:                for (int i = 0; i < length; i++) {
224:                    char c = arrayBuffer[i];
225:                    if (c <= HIGHEST_SPECIAL) {
226:                        char[] escaped = specialCharactersRepresentation[c];
227:                        if (escaped != null) {
228:                            // create StringBuffer to hold escaped xml string
229:                            if (start == 0) {
230:                                escapedBuffer = new StringBuffer(length + 5);
231:                            }
232:                            // add unescaped portion
233:                            if (start < i) {
234:                                escapedBuffer.append(arrayBuffer, start, i
235:                                        - start);
236:                            }
237:                            start = i + 1;
238:                            // add escaped xml
239:                            escapedBuffer.append(escaped);
240:                        }
241:                    }
242:                }
243:                // no xml escaping was necessary
244:                if (start == 0) {
245:                    return buffer;
246:                }
247:                // add rest of unescaped portion
248:                if (start < length) {
249:                    escapedBuffer.append(arrayBuffer, start, length - start);
250:                }
251:                return escapedBuffer.toString();
252:            }
253:
254:            public static String formatNumber(long number) {
255:                return NumberFormat.getNumberInstance().format(number);
256:            }
257:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.