Source code: com/tripi/asp/Server.java
1 /**
2 * ArrowHead ASP Server
3 * This is a source file for the ArrowHead ASP Server - an 100% Java
4 * VBScript interpreter and ASP server.
5 *
6 * For more information, see http://www.tripi.com/arrowhead
7 *
8 * Copyright (C) 2002 Terence Haddock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 package com.tripi.asp;
26
27 import org.apache.log4j.Category;
28 import javax.servlet.ServletConfig;
29 import javax.servlet.ServletContext;
30
31 import java.util.Enumeration;
32 import java.util.Hashtable;
33
34 /**
35 * The Server class implements the ASP object "Server", which contains
36 * utility functions.
37 *
38 * @author Terence Haddock
39 * @version 0.9
40 */
41 public class Server
42 {
43 /**
44 * Debugging category
45 */
46 static Category DBG = Category.getInstance(Server.class);
47
48 /** Script timeout */
49 public int ScriptTimeout = 90;
50
51 /** Hashtable of all the object names configured */
52 Hashtable configuredNames;
53
54 /** Servlet context */
55 ServletContext servletContext;
56
57 /** Local ASP Context */
58 AspContext localContext;
59
60 /**
61 * Constructor.
62 * @param config ServletConfig configuration
63 */
64 public Server(ServletConfig config, AspContext localContext)
65 {
66 if (DBG.isDebugEnabled()) DBG.debug("Creating Server Class");
67 if (config == null)
68 {
69 DBG.debug("NULL Config Received");
70 return;
71 }
72 configuredNames = new Hashtable();
73 Enumeration enNames = config.getInitParameterNames();
74 while (enNames.hasMoreElements())
75 {
76 String name = (String)enNames.nextElement();
77 if (DBG.isDebugEnabled()) DBG.debug("Name: " + name);
78 if (name.startsWith("object.")) {
79 configuredNames.put(new IdentNode(name),
80 config.getInitParameter(name));
81 }
82 }
83 if (DBG.isDebugEnabled()) DBG.debug("Finish init-ting Server class");
84 this.servletContext = config.getServletContext();
85 this.localContext = localContext;
86 }
87
88 /**
89 * CreateObject ASP function.
90 * @param objID Object ID, usually "java:(java class name)"
91 * @return new object which corresponds to the object ID
92 */
93 public Object CreateObject(String objID)
94 throws ClassNotFoundException,
95 InstantiationException,
96 IllegalAccessException
97 {
98 if (DBG.isDebugEnabled())
99 DBG.debug("Creating object: " + objID);
100 /* If the name begins with "java:", take them as-is */
101 if (objID.startsWith("java:")) {
102 String javaClass = objID.substring(5);
103 return Class.forName(javaClass).newInstance();
104 }
105 /* Pre-pend 'object.' to the string */
106 String classID = "object." + objID;
107 /* Convert to an ident node */
108 IdentNode iClassID = new IdentNode(classID);
109 /* Check for existence in the configured names */
110 Object obj;
111 if (configuredNames.containsKey(iClassID)) {
112 String javaClass = (String)configuredNames.get(iClassID);
113 obj = Class.forName(javaClass).newInstance();
114 } else {
115 /* Initialize the class */
116 obj = Class.forName(classID).newInstance();
117 }
118 /* Store the object into the context's OnPageEnd object to call */
119 if (localContext != null)
120 {
121 localContext.callOnPageStart(obj);
122 localContext.addOnPageEnd(obj);
123 } else {
124 if (DBG.isDebugEnabled())
125 DBG.debug("No localcontext to set up OnPageEnd");
126 }
127 return obj;
128 }
129
130 /**
131 * HTML encodes a string.
132 * @param str String
133 * @return string HTML-encoded
134 */
135 public static String htmlEncode(String str)
136 {
137 StringBuffer buf = new StringBuffer();
138
139 for (int i = 0; i < str.length(); i++)
140 {
141 char ch = str.charAt(i);
142 switch(ch) {
143 case '<':buf.append("<");break;
144 case '>':buf.append(">");break;
145 case '&':buf.append("&");break;
146 case '"':buf.append(""");break;
147 default:buf.append(ch);
148 }
149 }
150 return buf.toString();
151 }
152
153 /**
154 * URL Encodes a string.
155 * @param str String
156 * @return String, URL encoded
157 */
158 public static String URLEncode(String str)
159 {
160 StringBuffer buf = new StringBuffer();
161
162 for (int i = 0; i < str.length(); i++)
163 {
164 char ch = str.charAt(i);
165 switch(ch) {
166 case ' ':buf.append("+");break;
167 case '?':buf.append("%3F");break;
168 case '&':buf.append("%26");break;
169 case '%':buf.append("%25");break;
170 case '=':buf.append("%3D");break;
171 default:buf.append(ch);
172 }
173 }
174 return buf.toString();
175 }
176
177 /**
178 * ASP-accessable function to obtain the local path based on a virtual
179 * path.
180 *
181 * @param path Virtual path
182 * @return Absolute path
183 */
184 public String MapPath(String path)
185 {
186 return servletContext.getRealPath(path);
187 }
188 };