Source Code Cross Referenced for BaseSampleServlet.java in  » J2EE » JOnAS-4.8.6 » emb » sample » 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 » J2EE » JOnAS 4.8.6 » emb.sample.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999-2005 Bull S.A.
004:         * Contact: jonas-team@objectweb.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * Initial developer(s): Brice Ruzand
022:         *
023:         * --------------------------------------------------------------------------
024:         * $Id$
025:         * --------------------------------------------------------------------------
026:         */
027:        package emb.sample.servlet;
028:
029:        import java.io.Serializable;
030:        import java.text.DecimalFormat;
031:        import java.text.SimpleDateFormat;
032:        import java.util.Date;
033:        import java.util.Random;
034:
035:        import javax.emb.MediaEntityLocal;
036:        import javax.emb.MediaEntityLocalHome;
037:        import javax.emb.MediaException;
038:        import javax.naming.InitialContext;
039:        import javax.naming.NamingException;
040:        import javax.servlet.ServletException;
041:        import javax.servlet.http.HttpServlet;
042:        import javax.servlet.http.HttpServletRequest;
043:        import javax.servlet.http.HttpServletResponse;
044:
045:        import emb.sample.MediaSampleException;
046:        import emb.sample.session.MediaSampleSessionLocal;
047:        import emb.sample.session.MediaSampleSessionLocalHome;
048:
049:        /**
050:         * Base classes for all sample
051:         *
052:         * @author Brice Ruzand
053:         */
054:        public class BaseSampleServlet extends HttpServlet {
055:
056:            /**
057:             * serialVersionUID
058:             */
059:            private static final long serialVersionUID = 428471464384262530L;
060:
061:            /*
062:             * =======================================================================
063:             * Sample apps
064:             * =======================================================================
065:             */
066:
067:            /**
068:             * Retrive servlet URI
069:             */
070:            protected static final String RETRIEVE_SERVLET = "/RetrieveSampleMedia";
071:
072:            /**
073:             * Retrive servlet URI
074:             */
075:            protected static final String ACTION_DISPATCHER_SERVLET = "/ActionDispatcher";
076:
077:            /**
078:             * template jsp URI
079:             */
080:            protected static final String TEMPLATE_JSP = "/jsps/templatePage.jsp";
081:
082:            /**
083:             * tranforme Byte in KB
084:             */
085:            public static final double BYTE_IN_KB = 1024D;
086:
087:            /**
088:             * Random seed
089:             */
090:            protected static final Random RANDOM = new Random();
091:
092:            /**
093:             * Default format for time stamp
094:             */
095:            public static final SimpleDateFormat TIME_STAMP_FORMAT = new SimpleDateFormat(
096:                    "yy-MM-dd HH:mm:ss.SSS");
097:
098:            /**
099:             * Default format for size
100:             */
101:            public static final DecimalFormat SIZE_FORMAT = new DecimalFormat(
102:                    "0.0");
103:
104:            /**
105:             * Display medi info in XHTML
106:             *
107:             * @param sp StringPrinter
108:             * @param meb The media entity
109:             */
110:            protected void displayInfo(StringPrinter sp, MediaEntityLocal meb) {
111:
112:                try {
113:                    sp.println("Name : " + meb.getName() + "<br/>");
114:                    sp.println("MimeType : " + meb.getMimeType() + "<br/>");
115:                    sp.println("Size : "
116:                            + SIZE_FORMAT.format(meb.getSize() / BYTE_IN_KB)
117:                            + "&nbsp;KB<br/>");
118:                    String desc = meb.getDescription();
119:                    if (desc == null) {
120:                        desc = "";
121:                    }
122:                    sp.println("Description : <div>" + meb.getDescription()
123:                            + "</div><br/>");
124:                    sp.println("LastModified : <div>"
125:                            + TIME_STAMP_FORMAT.format(new Date(meb
126:                                    .getLastModified())) + "</div><br/>");
127:                } catch (MediaException e) {
128:                    sp
129:                            .println("<span class=error>Error while retriving medi info</span>");
130:                }
131:            }
132:
133:            /**
134:             * Get a session bean to manage Media creation
135:             *
136:             * @param request the HttpServlet request
137:             * @return the MediaSampleSessionLocal
138:             * @throws MediaSampleException if session cannot be created
139:             */
140:            protected static MediaSampleSessionLocal getSession(
141:                    HttpServletRequest request) throws MediaSampleException {
142:                try {
143:                    MediaSampleSessionLocal sessionBean = (MediaSampleSessionLocal) request
144:                            .getSession().getAttribute("sessionBean");
145:
146:                    if (sessionBean == null) {
147:                        sessionBean = getSessionHome().create();
148:                        request.getSession().setAttribute("sessionBean",
149:                                sessionBean);
150:                        throw new MediaSampleException(
151:                                "Your session has exprired, please reload some medias");
152:                    }
153:                    return sessionBean;
154:                } catch (MediaSampleException e) {
155:                    throw e;
156:                } catch (Exception e) {
157:                    // e.printStackTrace();
158:                    throw new MediaSampleException(
159:                            "Unable to create a new session", e);
160:
161:                }
162:            }
163:
164:            /*
165:             * =======================================================================
166:             * Errors
167:             * =======================================================================
168:             */
169:
170:            /**
171:             * Method to handle all exceptions of all Music Application Servlets in a
172:             * uniform way using the specified error JSP (/jps/templatePage.jsp).
173:             * <p>
174:             * In case of error a ServletException will be thrown.
175:             *
176:             * @param exception java.lang.Exception
177:             * @param errorClass class where the error locate
178:             * @param request com.sun.server.http.HttpServletRequest
179:             * @param response com.sun.server.http.HttpServletResponse
180:             * @throws ServletException This exception will be thrown if the
181:             *             errorhandling fails
182:             */
183:            protected void exceptionHandler(Throwable exception,
184:                    Class errorClass, HttpServletRequest request,
185:                    HttpServletResponse response) throws ServletException {
186:
187:                StringPrinter errorMessage = new StringPrinter();
188:
189:                try {
190:
191:                    if (exception instanceof  MediaSampleException) {
192:
193:                        errorMessage.println("<h2>There was an error</h2>");
194:                        errorMessage.println(exception.getLocalizedMessage());
195:                        request.setAttribute("workspaceContent", errorMessage);
196:                        getServletConfig().getServletContext()
197:                                .getRequestDispatcher(TEMPLATE_JSP).forward(
198:                                        request, response);
199:
200:                    } else {
201:
202:                        errorMessage.println(errorClass.getName() + ":");
203:                        errorMessage.println("");
204:                        errorMessage.println(exception.toString());
205:                        errorMessage.println("");
206:                        errorLog(errorClass.getName() + ": " + exception);
207:
208:                        StackTraceElement[] ste;
209:                        if (exception.getCause() != null) {
210:                            ste = exception.getCause().getStackTrace();
211:                        } else {
212:                            ste = exception.getStackTrace();
213:                        }
214:
215:                        for (int i = 0; i < ste.length; i++) {
216:
217:                            errorMessage.println(ste[i].toString());
218:                            // when the source cause is found stop stack
219:                            if (ste[i].getClassName().startsWith("emb.sample")) {
220:                                break;
221:                            }
222:                        }
223:
224:                        request.setAttribute("errorMessage", errorMessage);
225:                        getServletConfig().getServletContext()
226:                                .getRequestDispatcher(TEMPLATE_JSP).forward(
227:                                        request, response);
228:
229:                    }
230:
231:                } catch (Exception e) {
232:                    // if standard error reporter fails throw exception
233:                    throw new ServletException(errorMessage.toString(), e);
234:                }
235:            }
236:
237:            /**
238:             * Method to print error messages to stderr and for syschronisation purpose
239:             * to stdout too.
240:             *
241:             * @param msg java.lang.String
242:             */
243:            protected static void errorLog(String msg) {
244:                final int defBuffer = 1024;
245:                StringBuffer bfr = new StringBuffer(defBuffer);
246:
247:                bfr.append("[");
248:                bfr.append(TIME_STAMP_FORMAT.format(new Date(System
249:                        .currentTimeMillis())));
250:                bfr.append("] EMB SAMPLE ERROR ");
251:                bfr.append(msg);
252:
253:                System.err.println(bfr.toString());
254:            }
255:
256:            /**
257:             * Class to use printer
258:             *
259:             * @author Brice Ruzand
260:             */
261:            protected class StringPrinter implements  Serializable {
262:
263:                /**
264:                 * serialVersionUID
265:                 */
266:                private static final long serialVersionUID = -2865716341020911272L;
267:
268:                /**
269:                 * The String Buffer
270:                 */
271:                private StringBuffer buffer;
272:
273:                /**
274:                 * Default Constructor
275:                 */
276:                public StringPrinter() {
277:
278:                    final int newbuffsize = 128;
279:                    buffer = new StringBuffer(newbuffsize);
280:                }
281:
282:                /**
283:                 * Print a line in the printer
284:                 *
285:                 * @param str the string to print
286:                 */
287:                public void println(String str) {
288:                    buffer.append(str);
289:                    buffer.append("\r\n");
290:                }
291:
292:                /**
293:                 * @see java.lang.Object#toString()
294:                 */
295:                public String toString() {
296:                    return buffer.toString();
297:                }
298:            }
299:
300:            /*
301:             * =======================================================================
302:             * EJB TOOLS
303:             * =======================================================================
304:             */
305:
306:            /**
307:             * Media Enotity Bean Home reference
308:             */
309:            private static final String SESSION_HOME_REF = "java:comp/env/ejb/embSample/MediaSampleSession";
310:
311:            /**
312:             * Media Entity Bean Home reference
313:             */
314:            private static final String MEB_HOME_REF = "java:comp/env/ejb/emb/MediaEntity";
315:
316:            /**
317:             * cached Home
318:             */
319:            private static MediaSampleSessionLocalHome mebSessionHome = null;
320:
321:            /**
322:             * cached Home
323:             */
324:            private static MediaEntityLocalHome mebHome = null;
325:
326:            /**
327:             * Provide a cached Home access
328:             *
329:             * @return MediaEntityLocalHome
330:             * @throws NamingException in case MediaEntityLocalHome is not found
331:             */
332:            protected static MediaSampleSessionLocalHome getSessionHome()
333:                    throws NamingException {
334:                if (mebSessionHome == null) {
335:                    mebSessionHome = (MediaSampleSessionLocalHome) new InitialContext()
336:                            .lookup(SESSION_HOME_REF);
337:                }
338:                return mebSessionHome;
339:            }
340:
341:            /**
342:             * Provide a cached Home access
343:             *
344:             * @return MediaEntityLocalHome
345:             * @throws NamingException in case MediaEntityLocalHome is not found
346:             */
347:            protected static MediaEntityLocalHome getMebHome()
348:                    throws NamingException {
349:                if (mebHome == null) {
350:                    mebHome = (MediaEntityLocalHome) new InitialContext()
351:                            .lookup(MEB_HOME_REF);
352:                }
353:                return mebHome;
354:            }
355:
356:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.