Source code: com/tripi/asp/Application.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 /**
30 * Application is a class which holds application-specific data.
31 * This class corresponds to the global Application object
32 * available to VBScript applications. Implementation state:
33 * <ul>
34 * <li><b>Lock</b> - Fully implemented.
35 * <li><b>Unlock</b> - Fully implemented.
36 * <li><b>Get/Set</b> - Fully implemented.
37 * </ul>
38 *
39 * @author Terence Haddock
40 * @version 0.9
41 */
42 public class Application implements SimpleMap
43 {
44 /**
45 * Debugging output class.
46 */
47 Category DBG = Category.getInstance(Application.class);
48
49 /**
50 * The thread which currently has the lock, null means no thread
51 * has the lock.
52 */
53 private Thread lock = null;
54
55 /**
56 * The contents of the variables stored in this application.
57 */
58 public AspCollection Contents = new AspCollection();
59
60 /**
61 * Obtains the value of data contained within the application
62 * object.
63 * @param obj Value to obtain.
64 * @return value of key obtained
65 * @throws AspException if an error occurs, most likely
66 * could not coerce value to string.
67 * @see SimpleMap#get(Object)
68 */
69 public Object get(Object obj)
70 throws AspException
71 {
72 return Contents.get(obj);
73 }
74
75 /**
76 * Stores an objects into the application object.
77 * @param key Key to store.
78 * @param value Value to store.
79 * @throws AspException if an error occurs, most likely
80 * could not coerce value to string.
81 * @see SimpleMap#put(Object,Object)
82 */
83 public void put(Object key, Object value)
84 throws AspException
85 {
86 Contents.put(key, value);
87 }
88
89 /**
90 * Obtains the list of keys stored in this object. Used for
91 * foreach statements.
92 * @return list of keys stores in this application object.
93 * @throws AspException if an error occurs
94 * @see SimpleMap#getKeys()
95 */
96 public java.util.Enumeration getKeys()
97 throws AspException
98 {
99 return Contents.getKeys();
100 }
101
102 /**
103 * ASP function Lock, locks the application object.
104 * @throws AspException on error
105 */
106 public synchronized void Lock() throws AspException
107 {
108 try {
109 while (lock != null)
110 {
111 wait();
112 }
113 lock = Thread.currentThread();
114 } catch (InterruptedException ex)
115 {
116 DBG.error(ex);
117 throw new AspNestedException(ex);
118 }
119 }
120
121 /**
122 * ASP function Unlock, unlocks the application object.
123 * @throws AspException on error
124 */
125 public synchronized void Unlock() throws AspException
126 {
127 if (lock == null || lock != Thread.currentThread())
128 {
129 throw new AspException("Application.Unlock() called on an " +
130 "unlocked application object");
131 }
132 lock = null;
133 notify();
134 }
135
136 /**
137 * Internal function used to unlock the current application object
138 * only if the specified thread has a lock on it.
139 * @param thread Unlock if this thread has the lock
140 */
141 synchronized void unlockIfThreadHasLock(Thread thread)
142 {
143 if (lock == thread) {
144 lock = null;
145 notify();
146 }
147 }
148 }
149