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;
12  
13  import java.util.ArrayList;
14  import java.util.Collections;
15  import java.util.HashMap;
16  import java.util.List;
17  import java.util.Locale;
18  import java.util.Map;
19  import java.util.Properties;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.beans.factory.FactoryBean;
24  import org.springframework.beans.factory.annotation.Autowired;
25  import org.springframework.beans.factory.config.PropertiesFactoryBean;
26  import org.springframework.context.MessageSource;
27  import org.springframework.context.annotation.Bean;
28  import org.springframework.context.annotation.ComponentScan;
29  import org.springframework.context.annotation.Configuration;
30  import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
31  import org.springframework.context.support.ReloadableResourceBundleMessageSource;
32  import org.springframework.core.io.ClassPathResource;
33  import org.springframework.web.servlet.HandlerMapping;
34  import org.springframework.web.servlet.LocaleResolver;
35  import org.springframework.web.servlet.ThemeResolver;
36  import org.springframework.web.servlet.ViewResolver;
37  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
38  import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
39  import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
40  import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
41  import org.springframework.web.servlet.i18n.CookieLocaleResolver;
42  import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
43  import org.springframework.web.servlet.theme.FixedThemeResolver;
44  import org.springframework.web.servlet.view.InternalResourceViewResolver;
45  import org.springframework.web.servlet.view.JstlView;
46  
47  import psiprobe.beans.ClusterWrapperBean;
48  import psiprobe.beans.ContainerListenerBean;
49  import psiprobe.beans.ContainerWrapperBean;
50  import psiprobe.beans.JBossResourceResolverBean;
51  import psiprobe.beans.JvmMemoryInfoAccessorBean;
52  import psiprobe.beans.LogResolverBean;
53  import psiprobe.beans.ResourceResolver;
54  import psiprobe.beans.ResourceResolverBean;
55  import psiprobe.beans.RuntimeInfoAccessorBean;
56  import psiprobe.tools.Mailer;
57  
58  /**
59   * The Class ProbeConfig.
60   */
61  @EnableWebMvc
62  @Configuration
63  @ComponentScan(basePackages = {"psiprobe"})
64  public class ProbeConfig implements WebMvcConfigurer {
65  
66    /** The Constant logger. */
67    private static final Logger logger = LoggerFactory.getLogger(ProbeConfig.class);
68  
69    /**
70     * Gets the container listener bean.
71     *
72     * @return the container listener bean
73     */
74    @Bean(name = "containerListener")
75    public ContainerListenerBean getContainerListenerBean() {
76      logger.debug("Instantiated containerListener");
77      return new ContainerListenerBean();
78    }
79  
80    /**
81     * Gets the container wrapper bean.
82     *
83     * @return the container wrapper bean
84     */
85    @Bean(name = "containerWrapper")
86    public ContainerWrapperBean getContainerWrapperBean() {
87      logger.debug("Instantiated containerWrapper");
88      return new ContainerWrapperBean();
89    }
90  
91    /**
92     * Gets the cluster wrapper bean.
93     *
94     * @return the cluster wrapper bean
95     */
96    @Bean(name = "clusterWrapper")
97    public ClusterWrapperBean getClusterWrapperBean() {
98      logger.debug("Instantiated clusterWrapper");
99      return new ClusterWrapperBean();
100   }
101 
102   /**
103    * Gets the mailer.
104    *
105    * @return the mailer
106    */
107   @Bean(name = "mailer")
108   public Mailer getMailer() {
109     logger.debug("Instantiated mailer");
110     return new Mailer();
111   }
112 
113   /**
114    * Gets the default res.
115    *
116    * @return the default res
117    */
118   @Bean(name = "datasourceMappers")
119   public List<String> getDefaultRes() {
120     logger.debug("Instantiated datasourceMappers");
121     List<String> list = new ArrayList<>();
122     list.add("psiprobe.beans.accessors.C3P0DatasourceAccessor");
123     list.add("psiprobe.beans.accessors.Dbcp2DatasourceAccessor");
124     list.add("psiprobe.beans.accessors.HikariCpDatasourceAccessor");
125     list.add("psiprobe.beans.accessors.OracleDatasourceAccessor");
126     list.add("psiprobe.beans.accessors.OracleUcpDatasourceAccessor");
127     list.add("psiprobe.beans.accessors.OpenEjbBasicDatasourceAccessor");
128     list.add("psiprobe.beans.accessors.OpenEjbManagedDatasourceAccessor");
129     list.add("psiprobe.beans.accessors.Tomcat10DbcpDatasourceAccessor");
130     list.add("psiprobe.beans.accessors.Tomcat11DbcpDatasourceAccessor");
131     list.add("psiprobe.beans.accessors.TomcatJdbcPoolDatasourceAccessor");
132     list.add("psiprobe.beans.accessors.TomEeJdbcPoolDatasourceAccessor");
133     list.add("psiprobe.beans.accessors.ViburCpDatasourceAccessor");
134     return list;
135   }
136 
137   /**
138    * Gets the resource resolver bean.
139    *
140    * @return the resource resolver bean
141    */
142   @Bean(name = "default")
143   public ResourceResolverBean getResourceResolverBean() {
144     logger.debug("Instantiated default resourceResolverBean");
145     return new ResourceResolverBean();
146   }
147 
148   /**
149    * Gets the jboss resource resolver bean.
150    *
151    * @return the jboss resource resolver bean
152    */
153   @Bean(name = "jboss")
154   public JBossResourceResolverBean getJBossResourceResolverBean() {
155     logger.debug("Instantiated jbossResourceResolverBean");
156     return new JBossResourceResolverBean();
157   }
158 
159   /**
160    * Gets the resource resolvers.
161    *
162    * @param jbossResourceResolverBean the jboss resource resolver bean
163    * @param resourceResolverBean the resource resolver bean
164    *
165    * @return the resource resolvers
166    */
167   @Bean(name = "resourceResolvers")
168   public Map<String, ResourceResolver> getResourceResolvers(
169       @Autowired JBossResourceResolverBean jbossResourceResolverBean,
170       @Autowired ResourceResolverBean resourceResolverBean) {
171     logger.debug("Instantiated resourceResolvers");
172     Map<String, ResourceResolver> map = new HashMap<>();
173     map.put("jboss", jbossResourceResolverBean);
174     map.put("default", resourceResolverBean);
175     return map;
176   }
177 
178   /**
179    * Gets the adapter classes.
180    *
181    * @return the adapter classes
182    */
183   @Bean(name = "adapterClasses")
184   public List<String> getAdapterClasses() {
185     logger.debug("Instantiated adapterClasses");
186     List<String> list = new ArrayList<>();
187     try {
188       Properties properties = adapters().getObject();
189       if (properties == null) {
190         return Collections.emptyList();
191       }
192       for (Object adapter : properties.values()) {
193         list.add((String) adapter);
194       }
195     } catch (Exception e) {
196       logger.error("", e);
197     }
198     return list;
199   }
200 
201   /**
202    * Gets the stdout files. Any file added to this list will be displayed.
203    *
204    * @return the stdout files
205    */
206   @Bean(name = "stdoutFiles")
207   public List<String> getStdoutFiles() {
208     logger.debug("Instantiated stdoutFiles");
209     List<String> list = new ArrayList<>();
210     try {
211       Properties properties = stdout().getObject();
212       if (properties == null) {
213         return Collections.emptyList();
214       }
215       for (Object stdout : properties.values()) {
216         list.add((String) stdout);
217       }
218     } catch (Exception e) {
219       logger.error("", e);
220     }
221     return list;
222   }
223 
224   /**
225    * Standard Out Properties.
226    *
227    * @return the properties factory bean for standard out
228    */
229   @Bean(name = "stdout")
230   public FactoryBean<Properties> stdout() {
231     logger.debug("Instantiated stdout");
232     PropertiesFactoryBean bean = new PropertiesFactoryBean();
233     bean.setLocation(new ClassPathResource("stdout.properties"));
234     return bean;
235   }
236 
237   /**
238    * Adapters Properties.
239    *
240    * @return the properties factory bean for adaptors
241    */
242   @Bean(name = "adapters")
243   public FactoryBean<Properties> adapters() {
244     logger.debug("Instantiated adapters");
245     PropertiesFactoryBean bean = new PropertiesFactoryBean();
246     bean.setLocation(new ClassPathResource("adapters.properties"));
247     return bean;
248   }
249 
250   /**
251    * Gets the log resolver bean.
252    *
253    * @return the log resolver bean
254    */
255   @Bean(name = "logResolver")
256   public LogResolverBean getLogResolverBean() {
257     logger.debug("Instantiated logResolver");
258     return new LogResolverBean();
259   }
260 
261   /**
262    * Gets the jvm memory info accessor bean.
263    *
264    * @return the jvm memory info accessor bean
265    */
266   @Bean(name = "jvmMemoryInfoAccessor")
267   public JvmMemoryInfoAccessorBean getJvmMemoryInfoAccessorBean() {
268     logger.debug("Instantiated jvmMemoryInfoAccessorBean");
269     return new JvmMemoryInfoAccessorBean();
270   }
271 
272   /**
273    * Gets the runtime info accessor bean.
274    *
275    * @return the runtime info accessor bean
276    */
277   @Bean(name = "runtimeInfoAccessor")
278   public RuntimeInfoAccessorBean getRuntimeInfoAccessorBean() {
279     logger.debug("Instantiated runtimeInfoAccessorBean");
280     return new RuntimeInfoAccessorBean();
281   }
282 
283   /**
284    * Gets the internal resource view resolver.
285    *
286    * @return the internal resource view resolver
287    */
288   @Bean(name = "jspViewResolver")
289   public ViewResolver getViewResolver() {
290     logger.debug("Instantiated internalResourceViewResolver");
291     InternalResourceViewResolver resolver = new InternalResourceViewResolver();
292     resolver.setViewClass(JstlView.class);
293     resolver.setPrefix("/WEB-INF/jsp/");
294     resolver.setSuffix(".jsp");
295     return resolver;
296   }
297 
298   /**
299    * Gets the fixed theme resolver.
300    *
301    * @return the fixed theme resolver
302    */
303   @Bean(name = "themeResolver")
304   public ThemeResolver getThemeResolver() {
305     logger.debug("Instantiated fixedThemeResolver");
306     FixedThemeResolver resolver = new FixedThemeResolver();
307     resolver.setDefaultThemeName("theme-classic");
308     return resolver;
309   }
310 
311   /**
312    * Gets the reloadable resource bundle message source.
313    *
314    * @return the reloadable resource bundle message source
315    */
316   @Bean(name = "messageSource")
317   public MessageSource getMessageSource() {
318     logger.debug("Instantiated reloadableResourceBundleMessageSource");
319     ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
320     source.setBasename("/WEB-INF/messages");
321     source.setCacheSeconds(1);
322     source.setFallbackToSystemLocale(false);
323     return source;
324   }
325 
326   /**
327    * Gets the cookie locale resolver.
328    *
329    * @return the cookie locale resolver
330    */
331   @Bean(name = "localeResolver")
332   public LocaleResolver getLocaleResolver() {
333     logger.debug("Instantiated cookieLocaleResolver");
334     CookieLocaleResolver resolver = new CookieLocaleResolver();
335     resolver.setDefaultLocale(Locale.ENGLISH);
336     resolver.setCookieSecure(true);
337     resolver.setCookieHttpOnly(true);
338     return resolver;
339   }
340 
341   /**
342    * Gets the bean name url handler mapping.
343    *
344    * @param interceptor the interceptor
345    *
346    * @return the bean name url handler mapping
347    */
348   @Bean(name = "handlerMapping")
349   public HandlerMapping getHandlerMapping(@Autowired LocaleChangeInterceptor interceptor) {
350     logger.debug("Instantiated beanNameUrlHandlerMapping");
351     BeanNameUrlHandlerMapping mapping = new BeanNameUrlHandlerMapping();
352     mapping.setAlwaysUseFullPath(true);
353     mapping.setInterceptors(interceptor);
354     return mapping;
355   }
356 
357   @Override
358   public void addInterceptors(InterceptorRegistry registry) {
359     logger.debug("Registering localeChangeInterceptor");
360     registry.addInterceptor(getLocaleChangeInterceptor());
361   }
362 
363   /**
364    * Gets the locale change interceptor.
365    *
366    * @return the locale change interceptor
367    */
368   @Bean(name = "localeChangeInterceptor")
369   public LocaleChangeInterceptor getLocaleChangeInterceptor() {
370     logger.debug("Instantiated localeChangeInterceptor");
371     LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
372     interceptor.setParamName("lang");
373     return interceptor;
374   }
375 
376   /**
377    * Gets the property sources placeholder configurer.
378    *
379    * @return the property sources placeholder configurer
380    */
381   @Bean(name = "propertySourcesPlaceholderConfigurer")
382   public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
383     logger.debug("Instantiated propertySourcesPlaceholderConfigurer");
384     PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
385     configurer.setLocation(new ClassPathResource("stats.properties"));
386     configurer.setNullValue("NULL");
387 
388     Properties properties = new Properties();
389     properties.put("psiprobe.tools.mail.to", "NULL");
390     properties.put("psiprobe.tools.mail.subjectPrefix", "[PSI Probe]");
391     configurer.setProperties(properties);
392     return configurer;
393   }
394 
395   /**
396    * Version.
397    *
398    * @return the properties factory bean
399    */
400   @Bean(name = "version")
401   public PropertiesFactoryBean version() {
402     logger.debug("Instantiated version");
403     PropertiesFactoryBean bean = new PropertiesFactoryBean();
404     bean.setLocation(new ClassPathResource("version.properties"));
405     return bean;
406   }
407 
408 }