1 package org.apache.jcs.servlet;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import javax.servlet.http.HttpServlet;
23
24 /***
25 * Description of the Class
26 *
27 */
28 public class ListTestCacheServlet
29 extends HttpServlet
30 {
31
32 private static final long serialVersionUID = 1L;
33
34 // /*** Description of the Field */
35 // protected static CacheHub cacheMgr;
36 // /*** Description of the Field */
37 // protected static ICache zipListCache;
38 // /*** Description of the Field */
39 // protected static ICache zipToCityCache;
40 // // you can also access via the access classes
41 // // these provide some useful methods, They are necessary for groups.
42 // /*** Description of the Field */
43 // protected static IGroupCacheAccess cityGroupCache;
44 //
45 // private static int numToShow = 1000;
46 //
47 //
48 // /*** Description of the Method */
49 // public void init( ServletConfig config )
50 // throws ServletException
51 // {
52 // cacheMgr = GroupCacheHub.getInstance();
53 // // use deafult cattr
54 // zipListCache = cacheMgr.getCache( "zipListCache" );
55 //
56 // // I want to modify the defualts so I will get the
57 // // cattr and modify it. Otherwise I could call
58 // // zipToCityCache = cacheMgr.getCache( "zipToCityCache" );
59 // ICompositeCacheAttributes cattr = cacheMgr.getDefaultCacheAttributes();
60 // cattr.setMaxObjects( 10000 );
61 // zipToCityCache = cacheMgr.getCache( "zipToCityCache", cattr );
62 // // get another copy of the cattr
63 // cattr = cacheMgr.getDefaultCacheAttributes();
64 // cattr.setMaxObjects( 10000 );
65 // try
66 // {
67 // cityGroupCache = GroupCacheAccess.getGroupAccess( "cityGroupCache", cattr
68 // );
69 // }
70 // catch ( Exception e )
71 // {
72 // log.error( e );
73 // }
74 // }
75 // // end init
76 //
77 // /*** Description of the Method */
78 // public void service( HttpServletRequest req, HttpServletResponse res )
79 // throws ServletException,
80 // IOException
81 // {
82 //
83 // Hashtable params = new Hashtable();
84 // res.setContentType( "text/html" );
85 // PrintWriter out = res.getWriter();
86 //
87 // try
88 // {
89 //
90 // out.println( "<html><body bgcolor=#FFFFFF>" );
91 // out.println( "<a href=?task=zipList>List of Zip Codes</a><br>" );
92 //
93 // out.println( "<br>" );
94 // out.println( "<form method=get action=\"\">" );
95 // out.println( "<input type=hidden name=task value=cityForZip>" );
96 // out.println( "<input type=text name=zip value=>" );
97 // out.println( "<input type=submit value='Find City'>" );
98 // out.println( "</form>" );
99 // out.println( "<br>" );
100 // out.println( "<br>" );
101 //
102 // String paramName;
103 // String paramValue;
104 // // GET PARAMETERS INTO HASHTABLE
105 // for ( Enumeration e = req.getParameterNames(); e.hasMoreElements(); )
106 // {
107 // paramName = ( String ) e.nextElement();
108 // paramValue = req.getParameter( paramName );
109 // params.put( paramName, paramValue );
110 // if ( log.isDebugEnabled() )
111 // {
112 // log.debug( paramName + "=" + paramValue );
113 // }
114 // }
115 //
116 // String task = ( String ) params.get( "task" );
117 // if ( task == null )
118 // {
119 // task = "zipList";
120 // }
121 //
122 // if ( task.equals( "cityForZip" ) )
123 // {
124 // getCity( params, out );
125 // }
126 // else if ( task.equals( "zipList" ) )
127 // {
128 // out.println( "Showing first " + numToShow + " <br>" );
129 // getList( params, out );
130 // }
131 // else if ( task.equals( "zipForCity" ) )
132 // {
133 // getZipForCity( params, out );
134 // }
135 // }
136 // //CATCH EXCEPTIONS
137 // catch ( Exception e )
138 // {
139 // log.error( e );
140 // }
141 // // end try{
142 // finally
143 // {
144 // String isRedirect = ( String ) params.get( "isRedirect" );
145 // if ( isRedirect == null )
146 // {
147 // isRedirect = "N";
148 // }
149 // if ( log.isDebugEnabled() )
150 // {
151 // log.debug( "isRedirect = " + isRedirect );
152 // }
153 // String url;
154 // if ( isRedirect.equals( "Y" ) )
155 // {
156 // url = ( String ) params.get( "url" );
157 // if ( log.isDebugEnabled() )
158 // {
159 // log.debug( "url = " + url );
160 // }
161 // res.sendRedirect( url );
162 // // will not work if there's a previously sent header
163 // out.println( "<br>\n" );
164 // out.println( " <script>" );
165 // out.println( " location.href='" + url + "'; " );
166 // out.println( " </script> " );
167 // out.flush();
168 // }
169 // else
170 // {
171 // url = "";
172 // }
173 // out.println( "</body></html>" );
174 // }
175 //
176 // }
177 // //end service()
178 //
179 // /*** Gets the city attribute of the ListTestCacheServlet object */
180 // private void getCity( Hashtable params, PrintWriter out )
181 // {
182 //
183 // try
184 // {
185 // String zip = ( String ) params.get( "zip" );
186 //
187 // String city = ( String ) zipToCityCache.get( zip );
188 // if ( city == null )
189 // {
190 // out.println( "<br>The city is NOT in the cache.<br>" );
191 // out.println( "<br>looking in file.<br>" );
192 // city = findCity( zip );
193 // if ( city == null )
194 // {
195 // out.println( "<br>Couldn't find city.<br>" );
196 // }
197 // else
198 // {
199 // out.println( "Zip code " + zip + " is in <b>" );
200 // out.println( "<a href=\"?task=zipForCity&city=" + city + "\">" + city +
201 // "</a>" );
202 // out.println( "</b> city. <br>" );
203 // }
204 // }
205 // else
206 // {
207 // out.println( "Zip code " + zip + " is in <b>" );
208 // out.println( "<a href=\"?task=zipForCity&city=" + city + "\">" + city +
209 // "</a>" );
210 // out.println( "</b> city. <br>" );
211 // }
212 // }
213 // catch ( Exception e )
214 // {
215 // log.error( e );
216 // }
217 //
218 // }
219 // // end getCity
220 //
221 // /*** Gets the list attribute of the ListTestCacheServlet object */
222 // private void getList( Hashtable params, PrintWriter out )
223 // {
224 //
225 // try
226 // {
227 // ArrayList zipList = ( ArrayList ) zipListCache.get( "zipList" );
228 //
229 // // counter
230 // long counter = 0;
231 //
232 // if ( zipList == null )
233 // {
234 // out.println( "<br>The list was not in the cache.<br>" );
235 //
236 // zipList = new ArrayList();
237 //
238 // // use some od the data from jisp examples
239 //
240 // String s_nameInput = "zipcodes.txt";
241 // // open text file
242 // BufferedReader reader = null;
243 //
244 // try
245 // {
246 // reader = new BufferedReader( new FileReader( s_nameInput ) );
247 // }
248 // catch ( FileNotFoundException not_found )
249 // {
250 // System.err.println( "ERROR: file " + s_nameInput + " was not found" );
251 // return;
252 // }
253 //
254 // // flag to break out of loops if an exception is thrown
255 // boolean broken = false;
256 //
257 // while ( reader.ready() )
258 // {
259 //
260 // // read a line
261 // String line = reader.readLine();
262 //
263 // if ( line.length() < 1 )
264 // {
265 // break;
266 // }
267 //
268 // // count it
269 // ++counter;
270 //
271 // //if ((counter % 100) == 0)
272 // // System.out.println(" finished line #" + counter);
273 //
274 // int i;
275 //
276 // // extract ZIP
277 // String textZIP = "";
278 //
279 // for ( i = 0; ( line.charAt( i ) != ',' ); ++i )
280 // {
281 // textZIP += line.charAt( i );
282 // }
283 //
284 // String zip = textZIP;
285 //
286 // // extract city name
287 // ++i;
288 //
289 // String city = line.substring( i );
290 //
291 // zipToCityCache.put( zip, city );
292 //
293 // // key, group, value
294 // // could create a group if we need to retrieve the list of elements
295 // cityGroupCache.putInGroup( city + ":" + zip, city, zip );
296 // // or you could just use the : for partial removal if all you need
297 // // to do is remove, can't do partial lookup like this
298 // // if you wanted to store info instead of the zip this would
299 // // be usefule
300 // //cityGroupCache.put( city + ":" + zip, zip);
301 //
302 // zipList.add( zip );
303 // if ( counter < numToShow )
304 // {
305 // out.println( "<a href=?task=cityForZip&zip=" + zip + ">" + zip + "<br>"
306 // );
307 // }
308 // }
309 // out.println( "<br>Finished creating list of " + counter + ".<br>" );
310 //
311 // zipListCache.put( "zipList", zipList );
312 //
313 // }
314 // else
315 // {
316 // Iterator it = zipList.iterator();
317 // while ( it.hasNext() && counter < numToShow )
318 // {
319 // counter++;
320 // String zip = ( String ) it.next();
321 // out.println( "<a href=?task=cityForZip&zip=" + zip + ">" + zip +
322 // "</a><br>" );
323 // }
324 //
325 // }
326 //
327 // }
328 // catch ( Exception e )
329 // {
330 // log.error( e );
331 // }
332 //
333 // }
334 // // end zipList
335 //
336 // /*** Description of the Method */
337 // private String findCity( String zip2Find )
338 // {
339 //
340 // try
341 // {
342 //
343 // // use some od the data from jisp examples
344 //
345 // String s_nameInput = "zipcodes.txt";
346 // // open text file
347 // BufferedReader reader = null;
348 //
349 // try
350 // {
351 // reader = new BufferedReader( new FileReader( s_nameInput ) );
352 // }
353 // catch ( FileNotFoundException not_found )
354 // {
355 // System.err.println( "ERROR: file " + s_nameInput + " was not found" );
356 // return null;
357 // }
358 //
359 // // flag to break out of loops if an exception is thrown
360 // boolean broken = false;
361 //
362 // // counter
363 // long counter = 0;
364 // while ( reader.ready() )
365 // {
366 //
367 // // read a line
368 // String line = reader.readLine();
369 //
370 // if ( line.length() < 1 )
371 // {
372 // break;
373 // }
374 //
375 // // count it
376 // ++counter;
377 //
378 // //if ((counter % 100) == 0)
379 // // System.out.println(" finished line #" + counter);
380 //
381 // int i;
382 //
383 // // extract ZIP
384 // String textZIP = "";
385 //
386 // for ( i = 0; ( line.charAt( i ) != ',' ); ++i )
387 // {
388 // textZIP += line.charAt( i );
389 // }
390 //
391 // String zip = textZIP;
392 //
393 // // extract city name
394 // ++i;
395 //
396 // String city = line.substring( i );
397 //
398 // if ( zip2Find.equals( zip ) )
399 // {
400 // zipToCityCache.put( zip, city );
401 // return city;
402 // }
403 // }
404 //
405 // }
406 // catch ( Exception e )
407 // {
408 // log.error( e );
409 // }
410 //
411 // return null;
412 // }
413 // // end findCity
414 //
415 // /***
416 // * Gets the zipForCity attribute of the ListTestCacheServlet object
417 // */
418 // private void getZipForCity( Hashtable params, PrintWriter out )
419 // {
420 //
421 // try
422 // {
423 // String city = ( String ) params.get( "city" );
424 //
425 // // providing this feature adds a significant burden to the cache,
426 // // but it makes it able to completely implement the seesion api
427 // Enumeration en = this.cityGroupCache.getAttributeNames( city );
428 // if ( en == null )
429 // {
430 // out.println( "<br>There is no info for this city. <br>" );
431 // }
432 // else
433 // {
434 // while ( en.hasMoreElements() )
435 // {
436 // out.println( "Zip code " + ( String ) en.nextElement() + " is in <b>" );
437 // out.println( "<a href=\"?task=zipForCity&city=" + city + "\">" + city +
438 // "</a>" );
439 // out.println( "</b> city. <br>" );
440 // }
441 // }
442 //
443 // }
444 // catch ( Exception e )
445 // {
446 // log.error( e );
447 // }
448 //
449 // }
450 // // end getZipForCity
451
452 }
453 // end class