1 package org.apache.commons.jcs.admin.servlet;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
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
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
93
94
95
96
97
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
108
109 String cacheName = request.getParameter( CACHE_NAME_PARAM );
110
111
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
156
157 return null;
158 }
159
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 }