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 java.io.ByteArrayOutputStream;
14  import java.io.File;
15  import java.io.IOException;
16  import java.io.OutputStream;
17  import java.nio.charset.StandardCharsets;
18  import java.nio.file.Files;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.inject.Inject;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
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   * "Quick check" base controller.
37   */
38  public class BaseTomcatAvailabilityController extends AbstractTomcatContainerController {
39  
40    /** The container listener bean. */
41    @Inject
42    private ContainerListenerBean containerListenerBean;
43  
44    /**
45     * Gets the container listener bean.
46     *
47     * @return the container listener bean
48     */
49    public ContainerListenerBean getContainerListenerBean() {
50      return containerListenerBean;
51    }
52  
53    /**
54     * Sets the container listener bean.
55     *
56     * @param containerListenerBean the new container listener bean
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      // check datasource status
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     // try to allocate some memory
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     // try to open some files
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     // TODO JWL 12/11/2016 - Why is this commented out? If not needed, delete it.
159     // check the maximum execution time
160     // List<ThreadPool> pools = containerListenerBean.getThreadPools();
161     // for (int iPool = 0; iPool < pools.size(); iPool++) {
162     // ThreadPool threadPool = (ThreadPool) pools.get(iPool);
163     // List<RequestProcessor> threads = threadPool.getRequestProcessors();
164     // for (int iThread = 0; iThread < threads.size(); iThread++) {
165     // RequestProcessor rp = (RequestProcessor) threads.get(iThread);
166     // if (rp.getStage() == 3) {
167     // // the request processor is in SERVICE state
168     // maxServiceTime = Math.max(maxServiceTime, rp.getProcessingTime());
169     // }
170     // }
171     // }
172 
173     tomcatTestReport.setMaxServiceTime(maxServiceTime);
174 
175     return new ModelAndView(getViewName(), "testReport", tomcatTestReport);
176   }
177 
178 }