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