KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.directwebremoting.dwrp.ProtocolConstants;
24 import org.directwebremoting.util.Logger;
25
26 /**
27  * InboundContext is the context for set of inbound conversions.
28  * Since a data set may be recurrsive parts of some data members may refer to
29  * others so we need to keep track of who is converted for what.
30  * @author Joe Walker [joe at getahead dot ltd dot uk]
31  */

32 public final class InboundContext
33 {
34     /**
35      * Someone wants to tell us about a new conversion context.
36      * @param context The current conversion context
37      */

38     public void pushContext(TypeHintContext context)
39     {
40         contexts.addFirst(context);
41     }
42
43     /**
44      * Someone wants to tell us about a finished conversion context.
45      */

46     public void popContext()
47     {
48         contexts.removeFirst();
49     }
50
51     /**
52      * @return The method that we are currently converting data for
53      */

54     public TypeHintContext getCurrentTypeHintContext()
55     {
56         return (TypeHintContext) contexts.getFirst();
57     }
58
59     /**
60      * Create an inbound variable.
61      * Usually called by a query parser to setup a list of known variables.
62      * This method also checks to see if the new variable is a parameter and if
63      * it is it updates the count of parameters
64      * @param callNum The call number to work on
65      * @param key The name of the variable
66      * @param type The javascript type of the variable
67      * @param value The value of the variable
68      */

69     public void createInboundVariable(int callNum, String JavaDoc key, String JavaDoc type, String JavaDoc value)
70     {
71         InboundVariable cte = new InboundVariable(this, key, type, value);
72
73         Object JavaDoc old = variables.put(key, cte);
74         if (old != null)
75         {
76             log.warn("Duplicate variable called: " + key);
77         }
78
79         String JavaDoc paramPrefix = ProtocolConstants.INBOUND_CALLNUM_PREFIX + callNum +
80                              ProtocolConstants.INBOUND_CALLNUM_SUFFIX +
81                              ProtocolConstants.INBOUND_KEY_PARAM;
82
83         if (key.startsWith(paramPrefix))
84         {
85             int i = Integer.parseInt(key.substring(paramPrefix.length())) + 1;
86             if (i > paramCount)
87             {
88                 paramCount = i;
89             }
90         }
91     }
92
93     /**
94      * Internal method to allow entries to resolve references
95      * @param name The name of the variable to lookup
96      * @return The found variable
97      */

98     public InboundVariable getInboundVariable(String JavaDoc name)
99     {
100         return (InboundVariable) variables.get(name);
101     }
102
103     /**
104      * Clear the list of converted objects.
105      * If the conversion attempt for a given method failed, we may want to try
106      * another so we will need to ditch the list of converted objects because
107      * the next method could well have different parameter types.
108      */

109     public void clearConverted()
110     {
111         converted.clear();
112     }
113
114     /**
115      * Add to the (temporary) list of converted objects
116      * @param iv The converted object
117      * @param type The type that we converted the object to
118      * @param bean The converted version
119      */

120     public void addConverted(InboundVariable iv, Class JavaDoc type, Object JavaDoc bean)
121     {
122         Conversion conversion = new Conversion(iv, type);
123         Object JavaDoc old = converted.put(conversion, bean);
124         if (old != null)
125         {
126             log.warn("Duplicate variable conversion called: " + conversion);
127         }
128     }
129
130     /**
131      * Check to see if the conversion has already been done
132      * @param iv The inbound data to check
133      * @param type The type that we want the object converted to
134      * @return The converted data or null if it has not been converted
135      */

136     public Object JavaDoc getConverted(InboundVariable iv, Class JavaDoc type)
137     {
138         Conversion conversion = new Conversion(iv, type);
139         return converted.get(conversion);
140     }
141
142     /**
143      * How many parameters are there?
144      * @return The parameter count
145      */

146     public int getParameterCount()
147     {
148         return paramCount;
149     }
150
151     /**
152      * This is a bit of a hack, needed for debug purposes - it counts the
153      * parameters (including method and script params) for a given call number
154      * @param callNum The Call number to count the parameters of
155      * @return The parameter count for a given Call
156      */

157     public int getParameterCount(int callNum)
158     {
159         int count = 0;
160         String JavaDoc prefix = ProtocolConstants.INBOUND_CALLNUM_PREFIX + callNum + ProtocolConstants.INBOUND_CALLNUM_SUFFIX + ProtocolConstants.INBOUND_KEY_PARAM;
161         for (Iterator JavaDoc it = variables.keySet().iterator(); it.hasNext();)
162         {
163             String JavaDoc key = (String JavaDoc) it.next();
164             if (key.startsWith(prefix))
165             {
166                 count++;
167             }
168         }
169         return count;
170     }
171
172     /**
173      * Get a parameter by index
174      * @param callNum The call number to work on
175      * @param index The parameter index
176      * @return The found parameter
177      */

178     public InboundVariable getParameter(int callNum, int index)
179     {
180         String JavaDoc key = ProtocolConstants.INBOUND_CALLNUM_PREFIX + callNum +
181                      ProtocolConstants.INBOUND_CALLNUM_SUFFIX +
182                      ProtocolConstants.INBOUND_KEY_PARAM + index;
183
184         return (InboundVariable) variables.get(key);
185     }
186
187     /**
188      * A debug method so people can get a list of all the variable names
189      * @return an iterator over the known variable names
190      */

191     public Iterator JavaDoc getInboundVariableNames()
192     {
193         return variables.keySet().iterator();
194     }
195
196     /**
197      * A Class to use as a key in a map for conversion purposes
198      */

199     protected static class Conversion
200     {
201         /**
202          * @param inboundVariable
203          * @param type
204          */

205         Conversion(InboundVariable inboundVariable, Class JavaDoc type)
206         {
207             this.inboundVariable = inboundVariable;
208             this.type = type;
209         }
210
211         /* (non-Javadoc)
212          * @see java.lang.Object#equals(java.lang.Object)
213          */

214         public boolean equals(Object JavaDoc obj)
215         {
216             if (!(obj instanceof Conversion))
217             {
218                 return false;
219             }
220
221             Conversion that = (Conversion) obj;
222
223             if (!this.type.equals(that.type))
224             {
225                 return false;
226             }
227
228             return this.inboundVariable.equals(that.inboundVariable);
229         }
230
231         /* (non-Javadoc)
232          * @see java.lang.Object#hashCode()
233          */

234         public int hashCode()
235         {
236             return inboundVariable.hashCode() + type.hashCode();
237         }
238
239         /* (non-Javadoc)
240          * @see java.lang.Object#toString()
241          */

242         public String JavaDoc toString()
243         {
244             return "Conversion[" + inboundVariable + "," + type.getName() + "]";
245         }
246
247         private InboundVariable inboundVariable;
248
249         private Class JavaDoc type;
250     }
251
252     /* (non-Javadoc)
253      * @see java.lang.Object#toString()
254      */

255     public String JavaDoc toString()
256     {
257         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
258         buffer.append("InboundContext[");
259         for (Iterator JavaDoc it = variables.entrySet().iterator(); it.hasNext();)
260         {
261             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
262             buffer.append(entry.getKey());
263             buffer.append('=');
264             buffer.append(entry.getValue());
265             buffer.append(',');
266         }
267         buffer.append("]");
268         return buffer.toString();
269     }
270
271     /**
272      * The stack of pushed conversion contexts.
273      * i.e. What is the context of this type conversion.
274      */

275     private LinkedList JavaDoc contexts = new LinkedList JavaDoc();
276
277     /**
278      * How many params are there?.
279      * To be more accurate, return one less than the highest numbered parameter
280      * that we have come across.
281      */

282     private int paramCount = 0;
283
284     /**
285      * A map of all the inbound variables
286      */

287     private final Map JavaDoc variables = new HashMap JavaDoc();
288
289     /**
290      * A map of all the variables converted.
291      */

292     private final Map JavaDoc converted = new HashMap JavaDoc();
293
294     /**
295      * The log stream
296      */

297     private static final Logger log = Logger.getLogger(InboundContext.class);
298 }
299
Popular Tags