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.tools.logging.catalina;
12  
13  import java.io.File;
14  import java.nio.file.Path;
15  import java.text.SimpleDateFormat;
16  import java.util.Date;
17  
18  import psiprobe.tools.Instruments;
19  import psiprobe.tools.logging.AbstractLogDestination;
20  
21  /**
22   * The Class CatalinaLoggerAccessor.
23   */
24  public class CatalinaLoggerAccessor extends AbstractLogDestination {
25  
26    @Override
27    public boolean isContext() {
28      return true;
29    }
30  
31    @Override
32    public String getName() {
33      return null;
34    }
35  
36    @Override
37    public String getLogType() {
38      return "catalina";
39    }
40  
41    @Override
42    public File getFile() {
43      String dir = (String) invokeMethod(getTarget(), "getDirectory", null, null);
44      String prefix = (String) invokeMethod(getTarget(), "getPrefix", null, null);
45      String suffix = (String) invokeMethod(getTarget(), "getSuffix", null, null);
46      boolean timestamp = Instruments.getField(getTarget(), "timestamp") != null;
47      String date = timestamp ? new SimpleDateFormat("yyyy-MM-dd").format(new Date()) : "";
48  
49      File file =
50          notNull(date, dir, prefix, suffix) ? Path.of(dir, prefix + date + suffix).toFile() : null;
51      if (file != null && !file.isAbsolute()) {
52        return Path.of(System.getProperty("catalina.base"), file.getPath()).toFile();
53      }
54      return file;
55    }
56  
57    /**
58     * Not null.
59     *
60     * @param strings the strings
61     *
62     * @return true, if successful
63     */
64    private boolean notNull(String... strings) {
65      for (String string : strings) {
66        if (string == null) {
67          return false;
68        }
69      }
70      return true;
71    }
72  
73  }