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.quickcheck;
12  
13  import jakarta.inject.Inject;
14  import jakarta.servlet.http.HttpServletRequest;
15  import jakarta.servlet.http.HttpServletResponse;
16  
17  import java.io.ByteArrayOutputStream;
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.nio.charset.StandardCharsets;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.catalina.Context;
28  import org.springframework.web.servlet.ModelAndView;
29  
30  import psiprobe.beans.ContainerListenerBean;
31  import psiprobe.controllers.AbstractTomcatContainerController;
32  import psiprobe.model.ApplicationResource;
33  import psiprobe.model.DataSourceInfo;
34  import psiprobe.model.TomcatTestReport;
35  
36  /**
37   * "Quick check" base controller.
38   */
39  public class BaseTomcatAvailabilityController extends AbstractTomcatContainerController {
40  
41    /** The container listener bean. */
42    @Inject
43    protected ContainerListenerBean containerListenerBean;
44  
45    @Override
46    public ModelAndView handleRequestInternal(HttpServletRequest request,
47        HttpServletResponse response) throws Exception {
48  
49      final long start = System.currentTimeMillis();
50      TomcatTestReport tomcatTestReport = new TomcatTestReport();
51  
52      // check datasource status
53      tomcatTestReport.setDatasourceUsageScore(0);
54  
55      boolean allContextsAvailable = true;
56      if (containerWrapper.getResourceResolver().supportsPrivateResources()) {
57        for (Context appContext : containerWrapper.getTomcatContainer().findContexts()) {
58          allContextsAvailable =
59              allContextsAvailable && containerWrapper.getTomcatContainer().getAvailable(appContext);
60  
61          List<ApplicationResource> applicationResources = containerWrapper.getResourceResolver()
62              .getApplicationResources(appContext, containerWrapper);
63  
64          for (ApplicationResource appResource : applicationResources) {
65            DataSourceInfo dsi = appResource.getDataSourceInfo();
66            if (dsi != null && dsi.getBusyScore() > tomcatTestReport.getDatasourceUsageScore()) {
67              tomcatTestReport.setContextName(appContext.getName());
68              tomcatTestReport.setDatasourceUsageScore(dsi.getBusyScore());
69              tomcatTestReport.setDataSourceName(appResource.getName());
70            }
71          }
72        }
73  
74        tomcatTestReport.setWebappAvailabilityTest(
75            allContextsAvailable ? TomcatTestReport.TEST_PASSED : TomcatTestReport.TEST_FAILED);
76  
77      } else {
78        List<ApplicationResource> resources =
79            containerWrapper.getResourceResolver().getApplicationResources();
80        for (ApplicationResource resource : resources) {
81          DataSourceInfo dsi = resource.getDataSourceInfo();
82          if (dsi != null && dsi.getBusyScore() > tomcatTestReport.getDatasourceUsageScore()) {
83            tomcatTestReport.setDatasourceUsageScore(dsi.getBusyScore());
84            tomcatTestReport.setDataSourceName(resource.getName());
85          }
86        }
87      }
88      tomcatTestReport.setDatasourceTest(TomcatTestReport.TEST_PASSED);
89  
90      // try to allocate some memory
91      String word = "hello";
92      int count = TomcatTestReport.DEFAULT_MEMORY_SIZE / word.length();
93  
94      try {
95        ByteArrayOutputStream bos = new ByteArrayOutputStream();
96        for (; count > 0; count--) {
97          bos.write(word.getBytes(StandardCharsets.UTF_8));
98        }
99        tomcatTestReport.setMemoryTest(TomcatTestReport.TEST_PASSED);
100     } catch (IOException e) {
101       tomcatTestReport.setMemoryTest(TomcatTestReport.TEST_FAILED);
102       logger.trace("", e);
103     }
104 
105     // try to open some files
106     int fileCount = tomcatTestReport.getDefaultFileCount();
107     List<File> files = new ArrayList<>();
108     List<OutputStream> fileStreams = new ArrayList<>();
109 
110     try {
111       for (; fileCount > 0; fileCount--) {
112         File file = Path.of(System.getProperty("java.io.tmpdir"), "tctest_" + fileCount).toFile();
113         try (OutputStream fos = Files.newOutputStream(file.toPath())) {
114           files.add(file);
115           fileStreams.add(fos);
116           fos.write("this is a test".getBytes(StandardCharsets.UTF_8));
117         }
118       }
119       tomcatTestReport.setFileTest(TomcatTestReport.TEST_PASSED);
120     } catch (IOException e) {
121       tomcatTestReport.setFileTest(TomcatTestReport.TEST_FAILED);
122       logger.trace("", e);
123     } finally {
124       for (OutputStream fileStream : fileStreams) {
125         try {
126           fileStream.close();
127         } catch (IOException e) {
128           logger.trace("", e);
129         }
130       }
131       for (File file : files) {
132         Files.delete(file.toPath());
133       }
134     }
135 
136     tomcatTestReport.setTestDuration(System.currentTimeMillis() - start);
137 
138     // TODO JWL 9/19/2026 - implement a real service time test or remove this from the report
139     long maxServiceTime = 0;
140     tomcatTestReport.setMaxServiceTime(maxServiceTime);
141 
142     return new ModelAndView(getViewName(), "testReport", tomcatTestReport);
143   }
144 
145 }