Source Code Cross Referenced for ServerServlet.java in  » Web-Services » restlet-1.0.8 » com » noelios » restlet » ext » 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 » Web Services » restlet 1.0.8 » com.noelios.restlet.ext.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005-2007 Noelios Consulting.
003:         * 
004:         * The contents of this file are subject to the terms of the Common Development
005:         * and Distribution License (the "License"). You may not use this file except in
006:         * compliance with the License.
007:         * 
008:         * You can obtain a copy of the license at
009:         * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010:         * language governing permissions and limitations under the License.
011:         * 
012:         * When distributing Covered Code, include this CDDL HEADER in each file and
013:         * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014:         * applicable, add the following below this CDDL HEADER, with the fields
015:         * enclosed by brackets "[]" replaced with your own identifying information:
016:         * Portions Copyright [yyyy] [name of copyright owner]
017:         */
018:
019:        package com.noelios.restlet.ext.servlet;
020:
021:        import java.io.IOException;
022:        import java.util.List;
023:
024:        import java.lang.reflect.InvocationTargetException;
025:
026:        import javax.servlet.ServletException;
027:        import javax.servlet.http.HttpServlet;
028:        import javax.servlet.http.HttpServletRequest;
029:        import javax.servlet.http.HttpServletResponse;
030:
031:        import org.restlet.Application;
032:        import org.restlet.Component;
033:        import org.restlet.Context;
034:        import org.restlet.Server;
035:        import org.restlet.data.Protocol;
036:
037:        import com.noelios.restlet.http.HttpServerHelper;
038:
039:        /**
040:         * Servlet acting like an HTTP server connector.
041:         * See <a href="/documentation/1.0/faq#02">Developper FAQ
042:         * #2</a> for details on how to integrate a Restlet application into a servlet
043:         * container.<br/> Here is a sample configuration for your Restlet webapp:
044:         * 
045:         * <pre>
046:         * &lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
047:         * &lt;!DOCTYPE web-app PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot; &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot;&gt;
048:         * &lt;web-app&gt;
049:         *         &lt;display-name&gt;Restlet adapter&lt;/display-name&gt;
050:         *                                                         
051:         *         &lt;!-- Your application class name --&gt;
052:         *         &lt;context-param&gt;
053:         *                 &lt;param-name&gt;org.restlet.application&lt;/param-name&gt;
054:         *                 &lt;param-value&gt;com.noelios.restlet.test.TraceApplication&lt;/param-value&gt;
055:         *         &lt;/context-param&gt;
056:         *                                                         
057:         *         &lt;!-- Restlet adapter --&gt;
058:         *         &lt;servlet&gt;
059:         *                 &lt;servlet-name&gt;ServerServlet&lt;/servlet-name&gt;
060:         *                 &lt;servlet-class&gt;com.noelios.restlet.ext.servlet.ServerServlet&lt;/servlet-class&gt;
061:         *         &lt;/servlet&gt;
062:         *                                                         
063:         *         &lt;!-- Catch all requests --&gt;
064:         *         &lt;servlet-mapping&gt;
065:         *                 &lt;servlet-name&gt;ServerServlet&lt;/servlet-name&gt;
066:         *                 &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
067:         *         &lt;/servlet-mapping&gt;
068:         * &lt;/web-app&gt;
069:         * </pre>
070:         * 
071:         * The enumeration of initParameters of your Servlet will be copied to the
072:         * "context.parameters" property of your application. This way, you can pass
073:         * additional initialization parameters to your Restlet application, and share
074:         * them with existing Servlets.
075:         * 
076:         * @see <a href="http://java.sun.com/j2ee/">J2EE home page</a>
077:         * @author Jerome Louvel (contact@noelios.com)
078:         */
079:        public class ServerServlet extends HttpServlet {
080:            /**
081:             * The Servlet context initialization parameter's name containing the name
082:             * of the Servlet context attribute that should be used to store the Restlet
083:             * Application instance.
084:             */
085:            private static final String NAME_APPLICATION_ATTRIBUTE = "org.restlet.attribute.application";
086:
087:            /** The default value for the NAME_APPLICATION_ATTRIBUTE parameter. */
088:            private static final String NAME_APPLICATION_ATTRIBUTE_DEFAULT = "com.noelios.restlet.ext.servlet.ServerServlet.application";
089:
090:            /**
091:             * The Servlet context initialization parameter's name containing the name
092:             * of the Servlet context attribute that should be used to store the Restlet
093:             * Component instance.
094:             */
095:            private static final String NAME_COMPONENT_ATTRIBUTE = "org.restlet.attribute.component";
096:
097:            /** The default value for the NAME_COMPONENT_ATTRIBUTE parameter. */
098:            private static final String NAME_COMPONENT_ATTRIBUTE_DEFAULT = "com.noelios.restlet.ext.servlet.ServerServlet.component";
099:
100:            /**
101:             * The Servlet context initialization parameter's name containing the name
102:             * of the Servlet context attribute that should be used to store the HTTP
103:             * server connector instance.
104:             */
105:            private static final String NAME_SERVER_ATTRIBUTE = "org.restlet.attribute.server";
106:
107:            /** The default value for the NAME_SERVER_ATTRIBUTE parameter. */
108:            private static final String NAME_SERVER_ATTRIBUTE_DEFAULT = "com.noelios.restlet.ext.servlet.ServerServlet.server";
109:
110:            /** Serial version identifier. */
111:            private static final long serialVersionUID = 1L;
112:
113:            /** The associated Restlet application. */
114:            private transient Application application;
115:
116:            /** The associated Restlet component. */
117:            private transient Component component;
118:
119:            /** The associated HTTP server helper. */
120:            private transient HttpServerHelper helper;
121:
122:            /**
123:             * Constructor.
124:             */
125:            public ServerServlet() {
126:                this .application = null;
127:                this .component = null;
128:                this .helper = null;
129:            }
130:
131:            /**
132:             * Creates the single Application used by this Servlet.
133:             * 
134:             * @param context
135:             *            The Context for the Application
136:             * 
137:             * @return The newly created Application or null if unable to create
138:             */
139:            public Application createApplication(Context context) {
140:                Application application = null;
141:                // Try to instantiate a new target application
142:                // First, find the application class name
143:                String applicationClassName = getInitParameter(Application.KEY,
144:                        null);
145:
146:                // Load the application class using the given class name
147:                if (applicationClassName != null) {
148:                    try {
149:                        // According to
150:                        // http://www.caucho.com/resin-3.0/webapp/faq.xtp#Class.forName()-doesn't-seem-to-work-right
151:                        // this approach may need to used when loading classes.
152:                        Class<?> targetClass;
153:                        ClassLoader loader = Thread.currentThread()
154:                                .getContextClassLoader();
155:
156:                        if (loader != null)
157:                            targetClass = Class.forName(applicationClassName,
158:                                    false, loader);
159:                        else
160:                            targetClass = Class.forName(applicationClassName);
161:
162:                        try {
163:                            // Create a new instance of the application class by
164:                            // invoking the constructor with the Context parameter.
165:                            application = (Application) targetClass
166:                                    .getConstructor(Context.class).newInstance(
167:                                            context);
168:                        } catch (NoSuchMethodException e) {
169:                            log(
170:                                    "[Noelios Restlet Engine] - The ServerServlet couldn't invoke the constructor of the target class. Please check this class has a constructor with a single parameter of Context. The empty constructor and the context setter wille used instead. "
171:                                            + applicationClassName, e);
172:                            // The constructor with the Context parameter does not
173:                            // exist. Instantiate an application with the default
174:                            // constructor then invoke the setContext method.
175:                            application = (Application) targetClass
176:                                    .getConstructor().newInstance();
177:                        }
178:                    } catch (ClassNotFoundException e) {
179:                        log(
180:                                "[Noelios Restlet Engine] - The ServerServlet couldn't find the target class. Please check that your classpath includes "
181:                                        + applicationClassName, e);
182:
183:                    } catch (InstantiationException e) {
184:                        log(
185:                                "[Noelios Restlet Engine] - The ServerServlet couldn't instantiate the target class. Please check this class has an empty constructor "
186:                                        + applicationClassName, e);
187:                    } catch (IllegalAccessException e) {
188:                        log(
189:                                "[Noelios Restlet Engine] - The ServerServlet couldn't instantiate the target class. Please check that you have to proper access rights to "
190:                                        + applicationClassName, e);
191:                    } catch (NoSuchMethodException e) {
192:                        log(
193:                                "[Noelios Restlet Engine] - The ServerServlet couldn't invoke the constructor of the target class. Please check this class has a constructor with a single parameter of Context "
194:                                        + applicationClassName, e);
195:                    } catch (InvocationTargetException e) {
196:                        log(
197:                                "[Noelios Restlet Engine] - The ServerServlet couldn't instantiate the target class. An exception was thrown while creating "
198:                                        + applicationClassName, e);
199:                    }
200:
201:                    if (application != null) {
202:                        // Set the context based on the Servlet's context
203:                        application.setContext(new ServletContextAdapter(this ,
204:                                application, context));
205:                    }
206:                }
207:
208:                return application;
209:            }
210:
211:            /**
212:             * Creates the associated HTTP server handling calls.
213:             * 
214:             * @param request
215:             *            The HTTP Servlet request.
216:             * @return The new HTTP server handling calls.
217:             */
218:            public HttpServerHelper createServer(HttpServletRequest request) {
219:                HttpServerHelper result = null;
220:                Component component = getComponent();
221:                Application application = getApplication();
222:
223:                if ((component != null) && (application != null)) {
224:                    // First, let's locate the closest component
225:                    Server server = new Server(component.getContext(),
226:                            (List<Protocol>) null, request.getLocalAddr(),
227:                            request.getLocalPort(), component);
228:                    result = new HttpServerHelper(server);
229:
230:                    // Attach the application
231:                    String uriPattern = request.getContextPath()
232:                            + request.getServletPath();
233:                    component.getDefaultHost().attach(uriPattern, application);
234:                }
235:
236:                return result;
237:            }
238:
239:            @Override
240:            public void destroy() {
241:                if ((getApplication() != null)
242:                        && (getApplication().isStarted())) {
243:                    try {
244:                        getApplication().stop();
245:                    } catch (Exception e) {
246:                        log(
247:                                "Error during the stopping of the Restlet Application",
248:                                e);
249:                    }
250:                }
251:
252:                super .destroy();
253:            }
254:
255:            /**
256:             * Returns the application. It creates a new one if none exists.
257:             * 
258:             * @return The application.
259:             */
260:            public Application getApplication() {
261:                Application result = this .application;
262:
263:                if (result == null) {
264:                    synchronized (ServerServlet.class) {
265:                        // Find the attribute name to use to store the application
266:                        String applicationAttributeName = getInitParameter(
267:                                NAME_APPLICATION_ATTRIBUTE,
268:                                NAME_APPLICATION_ATTRIBUTE_DEFAULT);
269:
270:                        // Look up the attribute for a target
271:                        result = (Application) getServletContext()
272:                                .getAttribute(applicationAttributeName);
273:
274:                        if (result == null) {
275:                            result = createApplication(getComponent()
276:                                    .getContext());
277:                            getServletContext().setAttribute(
278:                                    applicationAttributeName, result);
279:                        }
280:
281:                        this .application = result;
282:                    }
283:                }
284:
285:                return result;
286:            }
287:
288:            /**
289:             * Returns the component. It creates a new one if none exists.
290:             * 
291:             * @return The component.
292:             */
293:            public Component getComponent() {
294:                Component result = this .component;
295:
296:                if (result == null) {
297:                    synchronized (ServerServlet.class) {
298:                        // Find the attribute name to use to store the component
299:                        String componentAttributeName = getInitParameter(
300:                                NAME_COMPONENT_ATTRIBUTE,
301:                                NAME_COMPONENT_ATTRIBUTE_DEFAULT);
302:
303:                        // Look up the attribute for a target
304:                        result = (Component) getServletContext().getAttribute(
305:                                componentAttributeName);
306:
307:                        if (result == null) {
308:                            result = new Component();
309:                            getServletContext().setAttribute(
310:                                    componentAttributeName, result);
311:                        }
312:
313:                        this .component = result;
314:                    }
315:                }
316:
317:                return result;
318:            }
319:
320:            /**
321:             * Returns the value of a given initialization parameter, first from the
322:             * Servlet configuration, then from the Web Application context.
323:             * 
324:             * @param name
325:             *            The parameter name.
326:             * @param defaultValue
327:             *            The default to use in case the parameter is not found.
328:             * @return The value of the parameter or null.
329:             */
330:            public String getInitParameter(String name, String defaultValue) {
331:                String result = getServletConfig().getInitParameter(name);
332:
333:                if (result == null) {
334:                    result = getServletConfig().getServletContext()
335:                            .getInitParameter(name);
336:                }
337:
338:                if (result == null) {
339:                    result = defaultValue;
340:                }
341:
342:                return result;
343:            }
344:
345:            /**
346:             * Returns the associated HTTP server handling calls. It creates a new one
347:             * if none exists.
348:             * 
349:             * @param request
350:             *            The HTTP Servlet request.
351:             * @return The HTTP server handling calls.
352:             */
353:            public HttpServerHelper getServer(HttpServletRequest request) {
354:                HttpServerHelper result = this .helper;
355:
356:                if (result == null) {
357:                    synchronized (ServerServlet.class) {
358:                        // Find the attribute name to use to store the server reference
359:                        String serverAttributeName = getInitParameter(
360:                                NAME_SERVER_ATTRIBUTE,
361:                                NAME_SERVER_ATTRIBUTE_DEFAULT);
362:
363:                        // Look up the attribute for a target
364:                        result = (HttpServerHelper) getServletContext()
365:                                .getAttribute(serverAttributeName);
366:
367:                        if (result == null) {
368:                            result = createServer(request);
369:                            getServletContext().setAttribute(
370:                                    serverAttributeName, result);
371:                        }
372:
373:                        this .helper = result;
374:                    }
375:                }
376:
377:                return result;
378:            }
379:
380:            @Override
381:            public void init() throws ServletException {
382:                if ((getApplication() != null)
383:                        && (getApplication().isStopped())) {
384:                    try {
385:                        getApplication().start();
386:                    } catch (Exception e) {
387:                        log(
388:                                "Error during the starting of the Restlet Application",
389:                                e);
390:                    }
391:                }
392:            }
393:
394:            /**
395:             * Services a HTTP Servlet request as an uniform call.
396:             * 
397:             * @param request
398:             *            The HTTP Servlet request.
399:             * @param response
400:             *            The HTTP Servlet response.
401:             */
402:            public void service(HttpServletRequest request,
403:                    HttpServletResponse response) throws ServletException,
404:                    IOException {
405:                HttpServerHelper helper = getServer(request);
406:
407:                if (helper != null) {
408:                    helper.handle(new ServletCall(helper.getServer(), request,
409:                            response));
410:                } else {
411:                    log("[Noelios Restlet Engine] - Unable to get the Restlet HTTP server connector. Status code 500 returned.");
412:                    response.sendError(500);
413:                }
414:            }
415:
416:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.