View Javadoc

1   package org.apache.jcs.admin.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.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.jcs.admin.JCSAdminBean;
26  import org.apache.velocity.Template;
27  import org.apache.velocity.context.Context;
28  import org.apache.velocity.servlet.VelocityServlet;
29  
30  /***
31   * A servlet which provides HTTP access to JCS. Allows a summary of regions to
32   * be viewed, and removeAll to be run on individual regions or all regions. Also
33   * provides the ability to remove items (any number of key arguments can be
34   * provided with action 'remove'). Should be initialized with a properties file
35   * that provides at least a classpath resource loader. Since this extends
36   * VelocityServlet, which uses the singleton model for velocity, it will share
37   * configuration with any other Velocity in the same JVM.
38   * <p>
39   * Initialization in a webapp will look something like this:
40   * <p>
41   *
42   * <pre>
43   *
44   *    [servlet]
45   *        [servlet-name]JCSAdminServlet[/servlet-name]
46   *        [servlet-class]org.apache.jcs.admin.servlet.JCSAdminServlet[/servlet-class]
47   *        [init-param]
48   *            [param-name]properties[/param-name]
49   *            [param-value]WEB-INF/conf/JCSAdminServlet.velocity.properties[/param-value]
50   *        [/init-param]
51   *    [/servlet]
52   *
53   * </pre>
54   *
55   * <p>
56   * FIXME: It would be nice to use the VelocityEngine model so this can be truly
57   * standalone. Right now if you run it in the same container as, say, turbine,
58   * turbine must be run first to ensure it's config takes precedence.
59   * <p>
60   */
61  public class JCSAdminServlet
62      extends VelocityServlet
63  {
64      private static final long serialVersionUID = -5519844149238645275L;
65  
66      private static final String DEFAULT_TEMPLATE_NAME = "/org/apache/jcs/admin/servlet/JCSAdminServletDefault.vm";
67  
68      private static final String REGION_DETAIL_TEMPLATE_NAME = "/org/apache/jcs/admin/servlet/JCSAdminServletRegionDetail.vm";
69  
70      // Keys for parameters
71  
72      private static final String CACHE_NAME_PARAM = "cacheName";
73  
74      private static final String ACTION_PARAM = "action";
75  
76      private static final String KEY_PARAM = "key";
77  
78      private static final String SILENT_PARAM = "silent";
79  
80      // Possible values for 'action' parameter
81  
82      private static final String CLEAR_ALL_REGIONS_ACTION = "clearAllRegions";
83  
84      private static final String CLEAR_REGION_ACTION = "clearRegion";
85  
86      private static final String REMOVE_ACTION = "remove";
87  
88      private static final String DETAIL_ACTION = "detail";
89  
90      /***
91       * Velocity based admin servlet.
92       * <p>
93       * @param request 
94       * @param response 
95       * @param context 
96       * @return Template
97       * @throws Exception 
98       * 
99       */
100     protected Template handleRequest( HttpServletRequest request, HttpServletResponse response, Context context )
101         throws Exception
102     {
103         JCSAdminBean admin = new JCSAdminBean();
104 
105         String templateName = DEFAULT_TEMPLATE_NAME;
106 
107         // Get cacheName for actions from request (might be null)
108 
109         String cacheName = request.getParameter( CACHE_NAME_PARAM );
110 
111         // If an action was provided, handle it
112 
113         String action = request.getParameter( ACTION_PARAM );
114 
115         if ( action != null )
116         {
117             if ( action.equals( CLEAR_ALL_REGIONS_ACTION ) )
118             {
119                 admin.clearAllRegions();
120             }
121             else if ( action.equals( CLEAR_REGION_ACTION ) )
122             {
123                 if ( cacheName != null )
124                 {
125                     admin.clearRegion( cacheName );
126                 }
127             }
128             else if ( action.equals( REMOVE_ACTION ) )
129             {
130                 String[] keys = request.getParameterValues( KEY_PARAM );
131 
132                 for ( int i = 0; i < keys.length; i++ )
133                 {
134                     admin.removeItem( cacheName, keys[i] );
135                 }
136 
137                 templateName = REGION_DETAIL_TEMPLATE_NAME;
138             }
139             else if ( action.equals( DETAIL_ACTION ) )
140             {
141                 templateName = REGION_DETAIL_TEMPLATE_NAME;
142             }
143         }
144 
145         if ( request.getParameter( SILENT_PARAM ) != null )
146         {
147             // If silent parameter was passed, no output should be produced.
148 
149             return null;
150         }
151         // Populate the context based on the template
152 
153         if ( templateName == REGION_DETAIL_TEMPLATE_NAME )
154         {
155             context.put( "cacheName", cacheName );
156             context.put( "elementInfoRecords", admin.buildElementInfo( cacheName ) );
157         }
158         else if ( templateName == DEFAULT_TEMPLATE_NAME )
159         {
160             context.put( "cacheInfoRecords", admin.buildCacheInfo() );
161         }
162 
163         return getTemplate( templateName );
164     }
165 }