Source code: com/tripi/asp/AspThread.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 java.net.URL;
29
30 /**
31 * AspThread performs the actual processing of an ASP process.
32 * @author Terence Haddock
33 * @version 0.9
34 */
35 public class AspThread implements Runnable
36 {
37 /** Debugging category */
38 private Category DBG = Category.getInstance(AspThread.class);
39
40 /** Running context */
41 AspContext context;
42
43 /** Filename of ASP script */
44 String filename;
45
46 /** Exception thrown during execution */
47 AspException ex = null;
48
49 /**
50 * Constructor.
51 * @param context Global context
52 * @param filename Filename of ASP code
53 */
54 public AspThread(AspContext context, String filename)
55 {
56 this.context = context;
57 this.filename = filename;
58 }
59
60 /**
61 * Executes this thread, processing the ASP code.
62 */
63 public void run()
64 {
65 try {
66 AspHandler handler = new AspHandler(context, filename);
67 if (DBG.isDebugEnabled()) DBG.debug("Parsing");
68 Node node = handler.parse();
69
70 if (DBG.isDebugEnabled()) DBG.debug("Preparing");
71 node.prepare(context);
72
73 if (DBG.isDebugEnabled()) DBG.debug("Executing");
74 node.execute(context);
75
76 if (DBG.isDebugEnabled()) DBG.debug("Done");
77 } catch (AspException ex)
78 {
79 if (DBG.isDebugEnabled()) DBG.debug("AspException");
80 if (!(ex instanceof AspExitScriptException))
81 DBG.error("AspException", ex);
82 this.ex = ex;
83 } catch (RuntimeException ex)
84 {
85 if (DBG.isDebugEnabled()) DBG.debug("RuntimeException");
86 DBG.error("Uncaught RuntimeException", ex);
87 } catch (Exception ex)
88 {
89 if (DBG.isDebugEnabled()) DBG.debug("Exception");
90 DBG.error("Uncaught exception", ex);
91 }
92 }
93
94 /**
95 * Static class which checks if a timeout has occured.
96 * @param fileScope file-specific scope
97 * @throws AspTimeoutException if a timeout has occured
98 */
99 public static void checkTimeout(AspContext context) throws AspException
100 {
101 IdentNode timeout = new IdentNode("!timeout");
102 if (context.inScope(timeout)) {
103 throw new AspTimeoutException();
104 }
105 }
106
107 /**
108 * Static method which marks that a timeout has occured
109 * @param fileScope file-specific scope
110 */
111 public static void timeout(AspContext globalScope) throws AspException
112 {
113 IdentNode timeout = new IdentNode("!timeout");
114 /* This will go into the global context */
115 /* This forceScope statement is here to prevent an exception when
116 option explicit is in effect */
117 globalScope.forceScope(timeout);
118 globalScope.setValue(timeout, "Yes");
119 }
120
121 /**
122 * Obtains the exception thrown.
123 * @return exception which was thrown, or null if none was thrown.
124 */
125 public Exception getException()
126 {
127 return ex;
128 }
129 }