KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > extend > InboundVariable


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.extend;
17
18 import org.directwebremoting.dwrp.ProtocolConstants;
19 import org.directwebremoting.util.Logger;
20 import org.directwebremoting.util.Messages;
21
22 /**
23  * A simple struct to hold data about a single converted javascript variable.
24  * @author Joe Walker [joe at getahead dot ltd dot uk]
25  */

26 public final class InboundVariable
27 {
28     /**
29      * Parsing ctor
30      * @param context How we lookup references
31      * @param key The name of the variable that this was transfered as
32      * @param type The type information from javascript
33      * @param value The javascript variable converted to a string
34      */

35     public InboundVariable(InboundContext context, String JavaDoc key, String JavaDoc type, String JavaDoc value)
36     {
37         this.context = context;
38         this.type = type;
39         this.value = value;
40         this.key = key;
41         this.dereferenced = attemptDereference();
42     }
43
44     /**
45      * Attempt to de-reference an inbound variable.
46      * We try de-referencing as soon as possible (why? there is a good reason
47      * for it, it fixes some bug, but I can't remember what right now) However
48      * the referenced variable may not exist yet, so the de-referencing may
49      * fail, requiring us to have another go later.
50      * @return Did the dereferencing succeed?
51      */

52     private boolean attemptDereference()
53     {
54         int maxDepth = 0;
55
56         if (ProtocolConstants.TYPE_REFERENCE.equals(type))
57         {
58             while (ProtocolConstants.TYPE_REFERENCE.equals(type))
59             {
60                 InboundVariable cd = context.getInboundVariable(value);
61                 if (cd == null)
62                 {
63                     return false;
64                 }
65
66                 type = cd.type;
67                 value = cd.value;
68
69                 maxDepth++;
70                 if (maxDepth > 20)
71                 {
72                     throw new IllegalStateException JavaDoc("Max depth exceeded when dereferencing " + value);
73                 }
74             }
75
76             // For references without an explicit variable name, we use the
77
// name of the thing they point at
78
if (key == null)
79             {
80                 key = value;
81             }
82         }
83
84         return true;
85     }
86
87     /**
88      * Call <code>attemptDereference()</code>, and complain if it fails.
89      * The assumption is that when we call this it really should succeed.
90      */

91     private void forceDereference()
92     {
93         if (!dereferenced)
94         {
95             dereferenced = attemptDereference();
96             if (!dereferenced)
97             {
98                 log.error(Messages.getString("InboundVariable.MissingVariable", value));
99             }
100         }
101     }
102
103     /**
104      * @return Returns the lookup table.
105      */

106     public InboundContext getLookup()
107     {
108         forceDereference();
109         return context;
110     }
111
112     /**
113      * @return Returns the type.
114      */

115     public String JavaDoc getType()
116     {
117         forceDereference();
118         return type;
119     }
120
121     /**
122      * Was this type null on the way in
123      * @return true if the javascript variable was null or undefined.
124      */

125     public boolean isNull()
126     {
127         forceDereference();
128         return type.equals(ProtocolConstants.INBOUND_NULL);
129     }
130
131     /**
132      * @return Returns the value.
133      */

134     public String JavaDoc getValue()
135     {
136         forceDereference();
137         return value;
138     }
139
140     /* (non-Javadoc)
141      * @see java.lang.Object#toString()
142      */

143     public String JavaDoc toString()
144     {
145         forceDereference();
146         return type + ProtocolConstants.INBOUND_TYPE_SEPARATOR + value;
147     }
148
149     /* (non-Javadoc)
150      * @see java.lang.Object#equals(java.lang.Object)
151      */

152     public boolean equals(Object JavaDoc obj)
153     {
154         if (this == obj)
155         {
156             return true;
157         }
158
159         if (!(obj instanceof InboundVariable))
160         {
161             return false;
162         }
163
164         InboundVariable that = (InboundVariable) obj;
165
166         forceDereference();
167
168         if (!this.type.equals(that.type))
169         {
170             return false;
171         }
172
173         if (!this.value.equals(that.value))
174         {
175             return false;
176         }
177
178         if (this.key == null || that.key == null)
179         {
180             return false;
181         }
182
183         return this.key.equals(that.key);
184     }
185
186     /* (non-Javadoc)
187      * @see java.lang.Object#hashCode()
188      */

189     public int hashCode()
190     {
191         return value.hashCode() + type.hashCode();
192     }
193
194     /**
195      * How do be lookup references?
196      */

197     private InboundContext context;
198
199     /**
200      * The variable name
201      */

202     private String JavaDoc key;
203
204     /**
205      * The javascript declared variable type
206      */

207     private String JavaDoc type;
208
209     /**
210      * The javascript declared variable value
211      */

212     private String JavaDoc value;
213
214     /**
215      * Has this variable been successfully de-referenced
216      */

217     private boolean dereferenced;
218
219     /**
220      * The log stream
221      */

222     private static final Logger log = Logger.getLogger(InboundVariable.class);
223 }
224
Popular Tags