1
2
3
4
5
6
7
8
9
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.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.catalina.Context;
27 import org.springframework.web.servlet.ModelAndView;
28
29 import psiprobe.beans.ContainerListenerBean;
30 import psiprobe.controllers.AbstractTomcatContainerController;
31 import psiprobe.model.ApplicationResource;
32 import psiprobe.model.DataSourceInfo;
33 import psiprobe.model.TomcatTestReport;
34
35
36
37
38 public class BaseTomcatAvailabilityController extends AbstractTomcatContainerController {
39
40
41 @Inject
42 private ContainerListenerBean containerListenerBean;
43
44
45
46
47
48
49 public ContainerListenerBean getContainerListenerBean() {
50 return containerListenerBean;
51 }
52
53
54
55
56
57
58 public void setContainerListenerBean(ContainerListenerBean containerListenerBean) {
59 this.containerListenerBean = containerListenerBean;
60 }
61
62 @Override
63 public ModelAndView handleRequestInternal(HttpServletRequest request,
64 HttpServletResponse response) throws Exception {
65
66 final long start = System.currentTimeMillis();
67 TomcatTestReport tomcatTestReport = new TomcatTestReport();
68
69
70 tomcatTestReport.setDatasourceUsageScore(0);
71
72 boolean allContextsAvailable = true;
73 if (getContainerWrapper().getResourceResolver().supportsPrivateResources()) {
74 for (Context appContext : getContainerWrapper().getTomcatContainer().findContexts()) {
75 allContextsAvailable = allContextsAvailable
76 && getContainerWrapper().getTomcatContainer().getAvailable(appContext);
77
78 List<ApplicationResource> applicationResources = getContainerWrapper().getResourceResolver()
79 .getApplicationResources(appContext, getContainerWrapper());
80
81 for (ApplicationResource appResource : applicationResources) {
82 DataSourceInfo dsi = appResource.getDataSourceInfo();
83 if (dsi != null && dsi.getBusyScore() > tomcatTestReport.getDatasourceUsageScore()) {
84 tomcatTestReport.setContextName(appContext.getName());
85 tomcatTestReport.setDatasourceUsageScore(dsi.getBusyScore());
86 tomcatTestReport.setDataSourceName(appResource.getName());
87 }
88 }
89 }
90
91 tomcatTestReport.setWebappAvailabilityTest(
92 allContextsAvailable ? TomcatTestReport.TEST_PASSED : TomcatTestReport.TEST_FAILED);
93
94 } else {
95 List<ApplicationResource> resources =
96 getContainerWrapper().getResourceResolver().getApplicationResources();
97 for (ApplicationResource resource : resources) {
98 DataSourceInfo dsi = resource.getDataSourceInfo();
99 if (dsi != null && dsi.getBusyScore() > tomcatTestReport.getDatasourceUsageScore()) {
100 tomcatTestReport.setDatasourceUsageScore(dsi.getBusyScore());
101 tomcatTestReport.setDataSourceName(resource.getName());
102 }
103 }
104 }
105 tomcatTestReport.setDatasourceTest(TomcatTestReport.TEST_PASSED);
106
107
108 String word = "hello";
109 int count = TomcatTestReport.DEFAULT_MEMORY_SIZE / word.length();
110
111 try {
112 ByteArrayOutputStream bos = new ByteArrayOutputStream();
113 for (; count > 0; count--) {
114 bos.write(word.getBytes(StandardCharsets.UTF_8));
115 }
116 tomcatTestReport.setMemoryTest(TomcatTestReport.TEST_PASSED);
117 } catch (IOException e) {
118 tomcatTestReport.setMemoryTest(TomcatTestReport.TEST_FAILED);
119 logger.trace("", e);
120 }
121
122
123 File tmpDir = new File(System.getProperty("java.io.tmpdir"));
124 int fileCount = tomcatTestReport.getDefaultFileCount();
125 List<File> files = new ArrayList<>();
126 List<OutputStream> fileStreams = new ArrayList<>();
127
128 try {
129 for (; fileCount > 0; fileCount--) {
130 File file = new File(tmpDir, "tctest_" + fileCount);
131 try (OutputStream fos = Files.newOutputStream(file.toPath())) {
132 files.add(file);
133 fileStreams.add(fos);
134 fos.write("this is a test".getBytes(StandardCharsets.UTF_8));
135 }
136 }
137 tomcatTestReport.setFileTest(TomcatTestReport.TEST_PASSED);
138 } catch (IOException e) {
139 tomcatTestReport.setFileTest(TomcatTestReport.TEST_FAILED);
140 logger.trace("", e);
141 } finally {
142 for (OutputStream fileStream : fileStreams) {
143 try {
144 fileStream.close();
145 } catch (IOException e) {
146 logger.trace("", e);
147 }
148 }
149 for (File file : files) {
150 Files.delete(file.toPath());
151 }
152 }
153
154 tomcatTestReport.setTestDuration(System.currentTimeMillis() - start);
155
156 long maxServiceTime = 0;
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 tomcatTestReport.setMaxServiceTime(maxServiceTime);
174
175 return new ModelAndView(getViewName(), "testReport", tomcatTestReport);
176 }
177
178 }