Source Code Cross Referenced for Scripting.java in  » Web-Framework » jWebApp » jwebapp » request » 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 Framework » jWebApp » jwebapp.request 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.  
003:         * All Rights Reserved.
004:         *
005:         * This file is part of jWebApp.
006:         *
007:         * jWebApp is free software; you can redistribute it and/or modify it under 
008:         * the terms of the GNU General Public License (Version 2) as published by 
009:         * the Free Software Foundation.
010:         *
011:         * jWebApp is distributed in the hope that it will be useful, but WITHOUT 
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
013:         * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
014:         * for more details.
015:         *
016:         * You should have received a copy of the GNU General Public License 
017:         * along with jWebApp; if not, write to the Free Software Foundation, 
018:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019:         */package jwebapp.request;
020:
021:        import java.net.MalformedURLException;
022:        import jwebtk.scripting.ScriptingException;
023:        import jwebtk.scripting.ScriptingManager;
024:        import java.io.BufferedReader;
025:        import java.io.IOException;
026:        import java.io.InputStreamReader;
027:        import java.net.URL;
028:        import java.util.Enumeration;
029:        import java.util.Hashtable;
030:        import java.util.logging.Logger;
031:        import jwebapp.controller.RequestHandler;
032:        import jwebapp.controller.ServerInterface;
033:
034:        /**
035:         * <p>Provides unlimited scriping abilities in place of writing request handlers.
036:         * This class allows you to use any Bean Scripting Framework (BSF) compatable 
037:         * scripting language such as:
038:         * <pre>
039:         *      javascript, jacl, netrexx, java, javaclass, bml, 
040:         *      vbscript, jscript, perlscript, perl, jpython, 
041:         *      jython, lotusscript, xslt, pnuts, beanbasic, 
042:         *      beanshell, ruby, judoscript
043:         * </pre>
044:         * <p>or any other scripting language that works under BSF.  Simply copy the JAR files
045:         * for the scripting language into the appropriate lib directory.  And define:
046:         * <pre>
047:         *     &lt;request id="yourRequestId" handlerClass="jwebapp.requestHandlers.Scripting"&gt;
048:         *         &lt;parameter name="scriptFileName" value="/WEB-INF/YourScript.bsh" /&gt;
049:         *     &lt;/request&gt;
050:         * </pre>
051:         * <p>The script has access to an implicit object named "serverInterface" of the ServerInterface 
052:         * class.  The script also has implicit object access to any additional parameters defined 
053:         * for the request.  The script should return the same forwarding values as any 
054:         * RequestHandler method.
055:         * 
056:         * <p>The script file is located with:
057:         * <pre>
058:         *     getServletContext().getResource(scriptFileName);
059:         * </pre>
060:         * <p>So the script file can be located anywhere including the WEB-INF directory.
061:         * 
062:         * <p>The extension of the defined 'scriptFileName' defines the scripting language being used 
063:         * and that scripting language will automatically be loaded to execute the script.
064:         * 
065:         * <p>However, if you need to you can define:
066:         * <pre>
067:         *     &lt;parameter name="scriptingLanguage" value="" /&gt;
068:         * </pre>
069:         * <p>To override the script language associated with the script extension.
070:         * <p>You can also optionally define the BSF engine and its classpath, use:
071:         * <pre>
072:         *     &lt;parameter name="scriptingEngineClass" value="" /&gt;
073:         *     &lt;parameter name="scriptingClasspath" value="" /&gt;
074:         * </pre>
075:         * 
076:         * <p>You can also define:
077:         * <pre>
078:         *     &lt;parameter name="noCache" value="true" /&gt;
079:         * </pre>
080:         * <p>To turn script caching off.  This is very usefull during the development of the scripts.
081:         */
082:
083:        @SuppressWarnings("unchecked")
084:        // working to complete a Java 1.5 version
085:        public class Scripting extends RequestHandler {
086:            private static Logger logger = Logger.getLogger(Scripting.class
087:                    .getName());
088:            private static Hashtable scriptHash = new Hashtable();
089:
090:            public String processRequest(ServerInterface serverInterface)
091:                    throws MalformedURLException, IOException,
092:                    ScriptingException {
093:                String filename = null, language = null, classpath = null, engineClass = null, script = null;
094:                Enumeration en = serverInterface.getRequestData()
095:                        .getParameterHash().keys();
096:                Hashtable objectsHash = new Hashtable();
097:                boolean noCache = false;
098:
099:                objectsHash.put("serverInterface", serverInterface);
100:
101:                while (en.hasMoreElements()) {
102:                    String name = (String) en.nextElement();
103:
104:                    if (name.equalsIgnoreCase("scriptFileName"))
105:                        filename = serverInterface.getRequestData()
106:                                .getParameter(name);
107:                    else if (name.equalsIgnoreCase("scriptingLanguage"))
108:                        language = serverInterface.getRequestData()
109:                                .getParameter(name);
110:                    else if (name.equalsIgnoreCase("scriptingClasspath"))
111:                        classpath = serverInterface.getRequestData()
112:                                .getParameter(name);
113:                    else if (name.equalsIgnoreCase("scriptingEngineClass"))
114:                        engineClass = serverInterface.getRequestData()
115:                                .getParameter(name);
116:                    else if (name.equalsIgnoreCase("noCache"))
117:                        noCache = true;
118:                    else
119:                        objectsHash.put(name, serverInterface.getRequestData()
120:                                .getParameter(name));
121:                }
122:
123:                if (filename == null) {
124:                    serverInterface.setErrorMessage(
125:                            ServerInterface.VALIDATION_ERROR,
126:                            "scriptFileName must be defined");
127:
128:                    logger.severe("scriptFileName must be defined");
129:
130:                    return VALIDATION_ERROR;
131:                }
132:
133:                URL scriptUrl = serverInterface.getServletContext()
134:                        .getResource(filename);
135:
136:                if (scriptUrl == null) {
137:                    serverInterface.setErrorMessage(
138:                            ServerInterface.VALIDATION_ERROR,
139:                            "scriptFileName '" + filename
140:                                    + "' - resource not found");
141:
142:                    logger.severe("scriptFileName '" + filename
143:                            + "' - resource not found");
144:
145:                    return VALIDATION_ERROR;
146:                }
147:
148:                if (language == null)
149:                    try {
150:                        language = ScriptingManager.getLanguage(filename);
151:                    } catch (Exception e) {
152:                    }
153:
154:                if (noCache)
155:                    script = loadFile(scriptUrl).toString();
156:                else if ((script = (String) scriptHash.get(filename)) == null)
157:                    scriptHash.put(filename, script = loadFile(scriptUrl)
158:                            .toString());
159:
160:                String returnValue = null;
161:
162:                if (language != null && classpath != null
163:                        && engineClass != null)
164:                    returnValue = (String) ScriptingManager.executeScript(
165:                            language, classpath, engineClass, script,
166:                            objectsHash);
167:                else if (language != null)
168:                    returnValue = (String) ScriptingManager.executeScript(
169:                            language, script, objectsHash);
170:                else
171:                    returnValue = (String) ScriptingManager.executeScript(
172:                            script, objectsHash);
173:
174:                if (returnValue != null)
175:                    return returnValue;
176:
177:                return NO_FORWARDING;
178:            }
179:
180:            static StringBuffer loadFile(URL filename) throws IOException {
181:                StringBuffer stringBuffer = new StringBuffer();
182:
183:                BufferedReader rd = new BufferedReader(new InputStreamReader(
184:                        filename.openStream()));
185:                String str;
186:
187:                while ((str = rd.readLine()) != null)
188:                    stringBuffer.append(str + "\n");
189:
190:                rd.close();
191:
192:                return stringBuffer;
193:            }
194:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.