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