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.apps;
12  
13  import javax.inject.Inject;
14  import javax.servlet.http.HttpServletRequest;
15  import javax.servlet.http.HttpServletResponse;
16  
17  import org.apache.catalina.Context;
18  import org.springframework.web.bind.ServletRequestUtils;
19  import org.springframework.web.servlet.ModelAndView;
20  
21  import psiprobe.beans.ResourceResolver;
22  import psiprobe.controllers.AbstractContextHandlerController;
23  import psiprobe.model.Application;
24  import psiprobe.model.stats.StatsCollection;
25  import psiprobe.tools.ApplicationUtils;
26  import psiprobe.tools.SecurityUtils;
27  
28  /**
29   * Retrieves Application model object populated with application information.
30   */
31  public class BaseGetApplicationController extends AbstractContextHandlerController {
32  
33    /** denotes whether extended application information and statistics should be collected. */
34    private boolean extendedInfo;
35  
36    /** The stats collection. */
37    @Inject
38    private StatsCollection statsCollection;
39  
40    /** The collection period. */
41    private long collectionPeriod;
42  
43    /**
44     * Checks if is extended info.
45     *
46     * @return true, if is extended info
47     */
48    public boolean isExtendedInfo() {
49      return extendedInfo;
50    }
51  
52    /**
53     * Sets the extended info.
54     *
55     * @param extendedInfo the new extended info
56     */
57    public void setExtendedInfo(boolean extendedInfo) {
58      this.extendedInfo = extendedInfo;
59    }
60  
61    /**
62     * Gets the stats collection.
63     *
64     * @return the stats collection
65     */
66    public StatsCollection getStatsCollection() {
67      return statsCollection;
68    }
69  
70    /**
71     * Sets the stats collection.
72     *
73     * @param statsCollection the new stats collection
74     */
75    public void setStatsCollection(StatsCollection statsCollection) {
76      this.statsCollection = statsCollection;
77    }
78  
79    /**
80     * Gets the collection period.
81     *
82     * @return the collection period
83     */
84    public long getCollectionPeriod() {
85      return collectionPeriod;
86    }
87  
88    /**
89     * Sets the collection period.
90     *
91     * @param collectionPeriod the new collection period
92     */
93    public void setCollectionPeriod(long collectionPeriod) {
94      this.collectionPeriod = collectionPeriod;
95    }
96  
97    @Override
98    protected ModelAndView handleContext(String contextName, Context context,
99        HttpServletRequest request, HttpServletResponse response) throws Exception {
100 
101     boolean calcSize = ServletRequestUtils.getBooleanParameter(request, "size", false)
102         && SecurityUtils.hasAttributeValueRole(getServletContext());
103 
104     ResourceResolver resourceResolver = getContainerWrapper().getResourceResolver();
105     Application app = ApplicationUtils.getApplication(context,
106         isExtendedInfo() ? resourceResolver : null, calcSize, getContainerWrapper());
107 
108     if (isExtendedInfo() && getStatsCollection() != null) {
109       String avgStatisticName = "app.avg_proc_time." + app.getName();
110       app.setAvgTime(getStatsCollection().getLastValueForStat(avgStatisticName));
111     }
112 
113     return new ModelAndView(getViewName()).addObject("app", app)
114         .addObject("no_resources", !resourceResolver.supportsPrivateResources())
115         .addObject("collectionPeriod", getCollectionPeriod());
116   }
117 
118 }