54
54
* A servlet to monitor the database. It returns status information for the database based on the JMX interface. For
55
55
* simplicity, the JMX beans provided by eXist are organized into categories. One calls the servlet with one or more
56
56
* categories in parameter "c", e.g.:
57
- *
57
+ * <p>
58
58
* /exist/jmx?c=instances&c=memory
59
- *
59
+ * <p>
60
60
* If no parameter is specified, all categories will be returned. Valid categories are "memory", "instances", "disk",
61
61
* "system", "caches", "locking", "processes", "sanity", "all".
62
- *
62
+ * <p>
63
63
* The servlet can also be used to test if the database is responsive by using parameter "operation=ping" and a timeout
64
64
* (t=timeout-in-milliseconds). For example, the following call
65
- *
65
+ * <p>
66
66
* /exist/jmx?operation=ping&t=1000
67
- *
67
+ * <p>
68
68
* will wait for a response within 1000ms. If the ping returns within the specified timeout, the servlet returns the
69
69
* attributes of the SanityReport JMX bean, which will include an element <jmx:Status>PING_OK</jmx:Status>.
70
70
* If the ping takes longer than the timeout, you'll instead find an element <jmx:error> in the returned XML. In
71
71
* this case, additional information on running queries, memory consumption and database locks will be provided.
72
- *
72
+ * <p>
73
73
* @author wolf
74
74
*/
75
75
public class JMXServlet extends HttpServlet {
@@ -92,10 +92,9 @@ public class JMXServlet extends HttpServlet {
92
92
93
93
private Path dataDir ;
94
94
private Path tokenFile ;
95
-
96
-
95
+
97
96
@ Override
98
- protected void doGet (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
97
+ protected void doGet (final HttpServletRequest request , final HttpServletResponse response ) throws ServletException , IOException {
99
98
100
99
// Verify if request is from localhost or if user has specific servlet/container managed role.
101
100
if (isFromLocalHost (request )) {
@@ -116,8 +115,8 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
116
115
writeXmlData (request , response );
117
116
}
118
117
119
- private void writeXmlData (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
120
- Element root = null ;
118
+ private void writeXmlData (final HttpServletRequest request , final HttpServletResponse response ) throws ServletException , IOException {
119
+ Element root ;
121
120
122
121
final String operation = request .getParameter ("operation" );
123
122
if ("ping" .equals (operation )) {
@@ -143,15 +142,15 @@ private void writeXmlData(HttpServletRequest request, HttpServletResponse respon
143
142
if (mbean == null ) {
144
143
throw new ServletException ("to call an operation, you also need to specify parameter 'mbean'" );
145
144
}
146
- String [] args = request .getParameterValues ("args" );
145
+ final String [] args = request .getParameterValues ("args" );
147
146
try {
148
147
root = client .invoke (mbean , operation , args );
149
148
if (root == null ) {
150
149
throw new ServletException ("operation " + operation + " not found on " + mbean );
151
150
}
152
- } catch (InstanceNotFoundException e ) {
151
+ } catch (final InstanceNotFoundException e ) {
153
152
throw new ServletException ("mbean " + mbean + " not found: " + e .getMessage (), e );
154
- } catch (MalformedObjectNameException | IntrospectionException | ReflectionException | MBeanException e ) {
153
+ } catch (final MalformedObjectNameException | IntrospectionException | ReflectionException | MBeanException e ) {
155
154
throw new ServletException (e .getMessage (), e );
156
155
}
157
156
} else {
@@ -182,7 +181,7 @@ private void writeXmlData(HttpServletRequest request, HttpServletResponse respon
182
181
}
183
182
184
183
@ Override
185
- public void init (ServletConfig config ) throws ServletException {
184
+ public void init (final ServletConfig config ) throws ServletException {
186
185
super .init (config );
187
186
188
187
// Setup JMS client
@@ -228,10 +227,10 @@ void registerLocalHostAddresses() {
228
227
} else {
229
228
localhostAddresses .add (hostAddress );
230
229
}
231
- }
232
- }
230
+ }
231
+ }
233
232
234
- }
233
+ }
235
234
}
236
235
237
236
} catch (final SocketException e ) {
@@ -251,7 +250,7 @@ void registerLocalHostAddresses() {
251
250
* @param request The HTTP request
252
251
* @return TRUE if request is from LOCALHOST otherwise FALSE
253
252
*/
254
- boolean isFromLocalHost (HttpServletRequest request ) {
253
+ boolean isFromLocalHost (final HttpServletRequest request ) {
255
254
String remoteAddr = request .getRemoteAddr ();
256
255
remoteAddr = remoteAddr .startsWith ("[" ) ? remoteAddr .substring (1 , remoteAddr .length () - 1 ) : remoteAddr ;
257
256
return localhostAddresses .contains (remoteAddr );
@@ -263,8 +262,8 @@ boolean isFromLocalHost(HttpServletRequest request) {
263
262
* @param request The HTTP request
264
263
* @return TRUE if request contains correct value for token, else FALSE
265
264
*/
266
- boolean hasSecretToken (HttpServletRequest request , String token ) {
267
- String [] tokenValue = request .getParameterValues (TOKEN_KEY );
265
+ boolean hasSecretToken (final HttpServletRequest request , final String token ) {
266
+ final String [] tokenValue = request .getParameterValues (TOKEN_KEY );
268
267
return ArrayUtils .contains (tokenValue , token );
269
268
}
270
269
@@ -286,7 +285,7 @@ private void obtainTokenFileReference() {
286
285
*/
287
286
private String getToken () {
288
287
289
- Properties props = new Properties ();
288
+ final Properties props = new Properties ();
290
289
String token = null ;
291
290
292
291
// Read if possible
@@ -295,7 +294,7 @@ private String getToken() {
295
294
try (final InputStream is = Files .newInputStream (tokenFile )) {
296
295
props .load (is );
297
296
token = props .getProperty (TOKEN_KEY );
298
- } catch (IOException ex ) {
297
+ } catch (final IOException ex ) {
299
298
LOG .error (ex .getMessage ());
300
299
}
301
300
@@ -313,7 +312,7 @@ private String getToken() {
313
312
// Write data to file
314
313
try (final OutputStream os = Files .newOutputStream (tokenFile )) {
315
314
props .store (os , "JMXservlet token: http://localhost:8080/exist/status?token=......" );
316
- } catch (IOException ex ) {
315
+ } catch (final IOException ex ) {
317
316
LOG .error (ex .getMessage ());
318
317
}
319
318
0 commit comments