View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers.system;
12  
13  import jakarta.servlet.http.HttpServletRequest;
14  import jakarta.servlet.http.HttpServletResponse;
15  
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  import org.springframework.beans.factory.annotation.Value;
19  import org.springframework.stereotype.Controller;
20  import org.springframework.web.bind.ServletRequestUtils;
21  import org.springframework.web.bind.annotation.RequestMapping;
22  import org.springframework.web.servlet.ModelAndView;
23  import org.springframework.web.servlet.mvc.ParameterizableViewController;
24  import org.springframework.web.servlet.view.RedirectView;
25  
26  /**
27   * Advises Java to run GC asap.
28   */
29  @Controller
30  public class AdviseGarbageCollectionController extends ParameterizableViewController {
31  
32    /** The Constant logger. */
33    private static final Logger logger =
34        LoggerFactory.getLogger(AdviseGarbageCollectionController.class);
35  
36    /** The replace pattern. */
37    private String replacePattern;
38  
39    /**
40     * Gets the replace pattern.
41     *
42     * @return the replace pattern
43     */
44    public String getReplacePattern() {
45      return replacePattern;
46    }
47  
48    /**
49     * Sets the replace pattern.
50     *
51     * @param replacePattern the new replace pattern
52     */
53    @Value("^http(s)?://[a-zA-Z\\-\\.0-9]+(:[0-9]+)?")
54    public void setReplacePattern(String replacePattern) {
55      this.replacePattern = replacePattern;
56    }
57  
58    @RequestMapping(path = "/adm/advisegc.htm")
59    @Override
60    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
61        throws Exception {
62      return super.handleRequest(request, response);
63    }
64  
65    @Override
66    protected ModelAndView handleRequestInternal(HttpServletRequest request,
67        HttpServletResponse response) throws Exception {
68  
69      boolean finalization = ServletRequestUtils.getBooleanParameter(request, "fin", false);
70  
71      String referer = request.getHeader("Referer");
72      String redirectUrl;
73      if (referer != null) {
74        redirectUrl = referer.replaceAll(replacePattern, "");
75      } else {
76        redirectUrl = request.getContextPath() + getViewName();
77      }
78      if (finalization) {
79        Runtime.getRuntime().runFinalization();
80        logger.debug("Advised finalization");
81      } else {
82        Runtime.getRuntime().gc();
83        logger.debug("Advised Garbage Collection");
84      }
85      logger.debug("Redirected to {}", redirectUrl);
86      return new ModelAndView(new RedirectView(redirectUrl));
87    }
88  
89    @Value("/sysinfo.htm")
90    @Override
91    public void setViewName(String viewName) {
92      super.setViewName(viewName);
93    }
94  
95  }