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.datasources;
12  
13  import java.util.ArrayList;
14  import java.util.Collection;
15  import java.util.Collections;
16  import java.util.List;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.springframework.beans.factory.annotation.Value;
22  import org.springframework.stereotype.Controller;
23  import org.springframework.web.bind.annotation.RequestMapping;
24  import org.springframework.web.servlet.ModelAndView;
25  
26  import psiprobe.controllers.AbstractTomcatContainerController;
27  import psiprobe.model.ApplicationResource;
28  import psiprobe.model.DataSourceInfo;
29  import psiprobe.model.DataSourceInfoGroup;
30  
31  /**
32   * Produces a list of all datasources configured within the container grouped by JDBC URL.
33   */
34  @Controller
35  public class ListAllJdbcResourceGroupsController extends AbstractTomcatContainerController {
36  
37    @RequestMapping(path = "/datasourcegroups.htm")
38    @Override
39    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
40        throws Exception {
41      return super.handleRequest(request, response);
42    }
43  
44    @Override
45    protected ModelAndView handleRequestInternal(HttpServletRequest request,
46        HttpServletResponse response) throws Exception {
47  
48      List<DataSourceInfoGroup> dataSourceGroups = new ArrayList<>();
49      List<DataSourceInfo> dataSources = new ArrayList<>();
50  
51      List<ApplicationResource> privateResources = getContainerWrapper().getPrivateDataSources();
52      List<ApplicationResource> globalResources = getContainerWrapper().getGlobalDataSources();
53  
54      // filter out anything that is not a datasource
55      // and use only those datasources that are properly configured
56      // as aggregated totals would not make any sense otherwise
57      filterValidDataSources(privateResources, dataSources);
58      filterValidDataSources(globalResources, dataSources);
59  
60      // sort datasources by JDBC URL
61      Collections.sort(dataSources, (ds1, ds2) -> {
62        String jdbcUrl1 = ds1.getJdbcUrl();
63        String jdbcUrl2 = ds2.getJdbcUrl();
64  
65        // here we rely on the the filter not to add any datasources with a null jdbcUrl to the list
66  
67        return jdbcUrl1.compareToIgnoreCase(jdbcUrl2);
68      });
69  
70      // group datasources by JDBC URL and calculate aggregated totals
71      DataSourceInfoGroup dsGroup = null;
72      for (DataSourceInfo ds : dataSources) {
73        if (dsGroup == null || !dsGroup.getJdbcUrl().equalsIgnoreCase(ds.getJdbcUrl())) {
74          dsGroup = new DataSourceInfoGroup().builder(ds);
75          dataSourceGroups.add(dsGroup);
76        } else {
77          dsGroup.addDataSourceInfo(ds);
78        }
79      }
80  
81      return new ModelAndView(getViewName(), "dataSourceGroups", dataSourceGroups);
82    }
83  
84    /**
85     * Filter valid data sources.
86     *
87     * @param resources the resources
88     * @param dataSources the data sources
89     */
90    protected void filterValidDataSources(Collection<ApplicationResource> resources,
91        Collection<DataSourceInfo> dataSources) {
92  
93      for (ApplicationResource res : resources) {
94        if (res.isLookedUp() && res.getDataSourceInfo() != null
95            && res.getDataSourceInfo().getJdbcUrl() != null) {
96          dataSources.add(res.getDataSourceInfo());
97        }
98      }
99    }
100 
101   @Value("datasourcegroup")
102   @Override
103   public void setViewName(String viewName) {
104     super.setViewName(viewName);
105   }
106 
107 }