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