Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/tripi/asp/GlobalScope.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  
29  import java.util.Hashtable;
30  
31  import javax.servlet.ServletConfig;
32  import javax.servlet.ServletContext;
33  import javax.servlet.http.HttpSession;
34  
35  
36  /**
37   * The GlobalScope class contains a reference to variables and functions
38   * global to the entire ASP application.
39   *
40   * @author Terence Haddock
41   */
42  public class GlobalScope
43  {
44      /** Debugging category */
45      static final private Category DBG =
46          Category.getInstance(GlobalScope.class);
47  
48      /** The scope universal to all ASP scripts. */
49      Hashtable universalScope = null;
50  
51      /** AspContext object for this global scope */
52      AspContext globalContext = null;
53  
54      /** Server object for this global scope */
55      Server server = null;
56  
57      /** Application object for this global scope */
58      Application application = null;
59  
60      /** Constructor */
61      private GlobalScope() {};
62  
63      /** 
64       * Obtains the global scope for the specified Servlet context.
65       * @param ctx ServletContext this global scope should be based on.
66       * @return new or existing global scope relative to servlet context.
67       */
68      public static GlobalScope getGlobalScope(ServletContext ctx)
69          throws AspException
70      {
71          GlobalScope scope;
72          synchronized(ctx)
73          {
74              scope = (GlobalScope)ctx.getAttribute("ArrowHeadASP_GlobalScope");
75              if (scope == null) {
76                  scope = new GlobalScope();
77                  ctx.setAttribute("ArrowHeadASP_GlobalScope", scope);
78              }
79          }
80          return scope;
81      }
82  
83      /**
84       * Initialized this scope.
85       */
86      protected void initializeScope(ServletConfig config) throws AspException
87      {
88          ServletContext ctx = config.getServletContext();
89          final IdentNode applicationIdent = new IdentNode("application");
90          final IdentNode serverIdent = new IdentNode("server");
91  
92          universalScope = new Hashtable();
93          VBScriptGlobals.initScope(universalScope);
94          globalContext = new AspContext(universalScope);
95  
96          /* Set the application-level Server object */
97          server = new Server(config, null);
98          JavaObjectNode serverObj = new JavaObjectNode(server);
99          universalScope.put(serverIdent, serverObj);
100 
101         /* Set the application-level Application object */
102         application = new Application();
103         JavaObjectNode applicationObj = new JavaObjectNode(application);
104         universalScope.put(applicationIdent, applicationObj);
105 
106         /* Load the global.asa file */
107         String global = config.getInitParameter("global.asa");
108         if (global==null) global = java.io.File.separatorChar + "global.asa";
109         AspFileFactory fileFactory = new AspFileFactory(globalContext);
110         global = fileFactory.resolveFile(null, global, false);
111 
112         if (DBG.isDebugEnabled()) {
113             DBG.debug("Reading global.asa file: " + global);
114         }
115 
116         java.io.File testExists = new java.io.File(global);
117         if (testExists.exists())
118         {
119             AspHandler handler = new AspHandler(globalContext, global);
120             if (DBG.isDebugEnabled()) {
121                 DBG.debug("Parsing global.asa...");
122             }
123             Node node = handler.parse();
124             if (DBG.isDebugEnabled()) {
125                 DBG.debug("Preparing global.asa...");
126             }
127             node.prepare(globalContext);
128         } else {
129             if (DBG.isDebugEnabled()) DBG.debug("global.asa file does not exist");
130         }
131     }
132 
133     /**
134      * Obtains a new context relative to the global context.
135      * @return New context.
136      */
137     public AspContext createContext()
138     {
139         return new AspContext(universalScope);
140     }
141 
142     /**
143      * Get the global context.
144      * @return AspContext object this global scope defines.
145      */
146     public AspContext getGlobalContext()
147     {
148         return globalContext;
149     }
150 
151     /**
152      * Utility function to call the APPLICATION_ONSTART VBScript subroutine.
153      */
154     public static void callApplicationOnStart(ServletConfig config)
155         throws AspException
156     {
157         ServletContext ctx = config.getServletContext();
158         final IdentNode application_onstartIdent =
159             new IdentNode("Application_OnStart");
160 
161         GlobalScope globalScope = getGlobalScope(ctx);
162         if (DBG.isDebugEnabled()) DBG.debug("Global Scope: " + globalScope);
163 
164         AspContext tmpContext = (AspContext)globalScope.getGlobalContext().clone();
165         if (DBG.isDebugEnabled()) DBG.debug("Global Context: " + tmpContext);
166 
167         Object obj = tmpContext.getValue(application_onstartIdent);
168         if (!(obj instanceof UndefinedValueNode)) {
169             if (DBG.isDebugEnabled())
170                 DBG.debug("Calling APPLICATION_ONSTART (" + obj + ")");
171             ((FunctionNode)obj).execute(new VarListNode(), tmpContext);
172         } else {
173             if (DBG.isDebugEnabled())
174                 DBG.debug("APPLICATION_ONSTART does not exist.");
175         }
176     }
177 
178     /**
179      * Utility function to call the SESSION_ONSTART VBScript subroutine.
180      * @param session Session to use when calling SESSION_ONSTART
181      */
182     public static void callSessionOnStart(HttpSession session, AspCollection contents)
183         throws AspException
184     {
185         if (DBG.isDebugEnabled())
186         {
187             DBG.debug("Session: " + session.getId());
188             DBG.debug("Collection: " + contents);
189         }
190         final IdentNode sessionIdent = new IdentNode("Session");
191         final IdentNode session_onstartIdent = new IdentNode("Session_OnStart");
192 
193         ServletContext ctx = session.getServletContext();
194         if (DBG.isDebugEnabled()) DBG.debug("Servlet Context: " + ctx);
195 
196         GlobalScope globalScope = getGlobalScope(ctx);
197         if (DBG.isDebugEnabled()) DBG.debug("Global Scope: " + globalScope);
198 
199         AspContext tmpContext = (AspContext)globalScope.getGlobalContext().clone();
200         if (DBG.isDebugEnabled()) DBG.debug("Global Context: " + tmpContext);
201 
202         Session tmpSession = new Session(session, contents);
203         JavaObjectNode sessionObj = new JavaObjectNode(tmpSession);
204         tmpContext.setValue(sessionIdent, sessionObj);
205 
206         Object obj = tmpContext.getValue(session_onstartIdent);
207         if (!(obj instanceof UndefinedValueNode)) {
208             if (DBG.isDebugEnabled())
209                 DBG.debug("Calling SESSION_ONSTART (" + obj + ")");
210             ((FunctionNode)obj).execute(new VarListNode(), tmpContext);
211         } else {
212             if (DBG.isDebugEnabled())
213                 DBG.debug("SESSION_ONSTART does not exist.");
214         }
215     }
216 
217     /**
218      * Utility function to call the SESSION_ONEND VBScript subroutine.
219      * @param session Session to use when calling SESSION_ONEND
220      */
221     public static void callSessionOnEnd(HttpSession session, AspCollection contents)
222         throws AspException
223     {
224         if (DBG.isDebugEnabled())
225         {
226             DBG.debug("Session: " + session.getId());
227             DBG.debug("Collection: " + contents);
228         }
229         final IdentNode sessionIdent = new IdentNode("Session");
230         final IdentNode session_onendIdent = new IdentNode("Session_OnEnd");
231 
232         ServletContext ctx = session.getServletContext();
233         if (DBG.isDebugEnabled()) DBG.debug("Servlet Context: " + ctx);
234 
235         GlobalScope globalScope = getGlobalScope(ctx);
236         if (DBG.isDebugEnabled()) DBG.debug("Global Scope: " + globalScope);
237 
238         AspContext tmpContext = (AspContext)globalScope.getGlobalContext().clone();
239         if (DBG.isDebugEnabled()) DBG.debug("Global Context: " + tmpContext);
240 
241         Session tmpSession = new Session(session, contents);
242         JavaObjectNode sessionObj = new JavaObjectNode(tmpSession);
243         tmpContext.setValue(sessionIdent, sessionObj);
244 
245         Object obj = tmpContext.getValue(session_onendIdent);
246         if (!(obj instanceof UndefinedValueNode)) {
247             if (DBG.isDebugEnabled())
248                 DBG.debug("Calling SESSION_ONEND (" + obj + ")");
249             ((FunctionNode)obj).execute(new VarListNode(), tmpContext);
250         } else {
251             if (DBG.isDebugEnabled())
252                 DBG.debug("SESSION_ONEND does not exist.");
253         }
254     }
255 }