Source Code Cross Referenced for ScriptingManager.java in  » Web-Framework » jWebApp » jwebtk » scripting » 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 » jwebtk.scripting 
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 jWebTk.
006:         *
007:         * jWebTk 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:         * jWebTk 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 jWebTk; if not, write to the Free Software Foundation, 
018:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019:         */package jwebtk.scripting;
020:
021:        import java.util.Vector;
022:        import java.util.StringTokenizer;
023:        import java.io.File;
024:        import java.net.URL;
025:        import java.net.URLClassLoader;
026:        import java.util.Enumeration;
027:        import java.util.Hashtable;
028:        import java.util.logging.Level;
029:        import java.util.logging.Logger;
030:        import org.apache.bsf.BSFManager;
031:
032:        /**
033:         * This class wraps the Apache Bean Scripting Framework (BSF).
034:         * 
035:         * The BSF jar file(s) and the scripting jar file(s) for a given language are required when using this class.
036:         */
037:
038:        @SuppressWarnings("unchecked")
039:        // working to complete a Java 1.5 version
040:        public class ScriptingManager {
041:            private Logger logger = Logger.getLogger(ScriptingManager.class
042:                    .getName());
043:            private BSFManager manager;
044:
045:            protected ScriptingManager() {
046:                manager = new BSFManager();
047:            }
048:
049:            protected ScriptingManager(String language, String classpath,
050:                    String engineClass) throws ScriptingException {
051:                if (logger.isLoggable(Level.FINE))
052:                    logger.fine("language=" + language + ", class_path="
053:                            + classpath + ", engine_class=" + engineClass);
054:
055:                BSFManager.registerScriptingEngine(language, engineClass, null);
056:
057:                manager = new BSFManager();
058:
059:                if (classpath != null) {
060:                    Vector urls = new Vector();
061:                    StringTokenizer strtok = new StringTokenizer(classpath,
062:                            ";:");
063:
064:                    while (strtok.hasMoreTokens()) {
065:                        String path = strtok.nextToken();
066:
067:                        try {
068:                            if (path.indexOf(':') == -1)
069:                                urls.addElement(new File(path).toURI().toURL());
070:                            else
071:                                urls.addElement(new URL(path));
072:                        } catch (Exception e) {
073:                            throw new ScriptingException(e);
074:                        }
075:                    }
076:
077:                    URL url_arr[] = (URL[]) urls.toArray(new URL[urls.size()]);
078:
079:                    manager.setClassLoader(new URLClassLoader(url_arr));
080:                }
081:            }
082:
083:            protected void declareObject(String name, Object obj)
084:                    throws ScriptingException {
085:                try {
086:                    manager.declareBean(name, obj, obj.getClass());
087:                } catch (Exception e) {
088:                    throw new ScriptingException(e);
089:                }
090:            }
091:
092:            protected void undeclareObject(String name)
093:                    throws ScriptingException {
094:                try {
095:                    manager.undeclareBean(name);
096:                } catch (Exception e) {
097:                    throw new ScriptingException(e);
098:                }
099:            }
100:
101:            protected Object execute(String language, String script)
102:                    throws ScriptingException {
103:                try {
104:                    return manager.eval(language, null, 0, 0, script);
105:                } catch (Exception e) {
106:                    throw new ScriptingException(e);
107:                }
108:            }
109:
110:            /**
111:             * Returns the scripting language name associated with extension.
112:             *
113:             * @param extension file extension for a given scripting language
114:             *
115:             * @return the scripting language name associated with extension.
116:             */
117:
118:            public static String getLanguage(String extension)
119:                    throws ScriptingException {
120:                try {
121:                    return BSFManager.getLangFromFilename(extension);
122:                } catch (Exception e) {
123:                    throw new ScriptingException(e);
124:                }
125:            }
126:
127:            /**
128:             * Executes a JavaScript script with the given implicit objects.
129:             *
130:             * @param script the script to execute
131:             * @param objectsToDeclare implicit objects to declare
132:             *
133:             * @return the script result
134:             */
135:
136:            public static Object executeScript(String script,
137:                    Hashtable objectsToDeclare) throws ScriptingException {
138:                try {
139:                    return executeScript("javascript", script, objectsToDeclare);
140:                } catch (Exception e) {
141:                    throw new ScriptingException(e);
142:                }
143:            }
144:
145:            /**
146:             * Executes a JavaScript script with the given implicit objects.
147:             *
148:             * @param language the language of the script
149:             * @param script the script to execute
150:             * @param objectsToDeclare implicit objects to declare
151:             *
152:             * @return the script result
153:             */
154:
155:            public static Object executeScript(String language, String script,
156:                    Hashtable objectsToDeclare) throws ScriptingException {
157:                try {
158:                    ScriptingManager scriptor = new ScriptingManager();
159:
160:                    Enumeration e = objectsToDeclare.keys();
161:                    String key;
162:
163:                    while (e.hasMoreElements()) {
164:                        key = (String) e.nextElement();
165:                        scriptor.declareObject(key, objectsToDeclare.get(key));
166:                    }
167:
168:                    return scriptor.execute(language, script);
169:                } catch (Exception e) {
170:                    throw new ScriptingException(e);
171:                }
172:            }
173:
174:            /**
175:             * Executes a JavaScript script with the given implicit objects.
176:             *
177:             * @param language the language of the script
178:             * @param classpath the classpath for the scripting language (jar/class) environment
179:             * @param engineClass the class for the scripting language
180:             * @param script the script to execute
181:             * @param objectsToDeclare implicit objects to declare
182:             *
183:             * @return the script result
184:             */
185:
186:            public static Object executeScript(String language,
187:                    String classpath, String engineClass, String script,
188:                    Hashtable objectsToDeclare) throws ScriptingException {
189:                try {
190:                    ScriptingManager scriptor = new ScriptingManager(language,
191:                            classpath, engineClass);
192:
193:                    Enumeration e = objectsToDeclare.keys();
194:                    String key;
195:
196:                    while (e.hasMoreElements()) {
197:                        key = (String) e.nextElement();
198:                        scriptor.declareObject(key, objectsToDeclare.get(key));
199:                    }
200:
201:                    return scriptor.execute(language, script);
202:                } catch (Exception e) {
203:                    throw new ScriptingException(e);
204:                }
205:            }
206:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.