1 /*
2  * Copyright 2008-2019 by Emeric Vernat
3  *
4  *     This file is part of Java Melody.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.bull.javamelody.internal.model;
19
20 import java.io.File;
21 import java.io.FilenameFilter;
22 import java.io.Serializable;
23 import java.lang.management.ManagementFactory;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.Date;
28 import java.util.List;
29
30 /**
31  * HsErrPid files.
32  * @author Emeric Vernat
33  */

34 public final class HsErrPid implements Serializable {
35     private static final String XX_ERROR_FILE = "-XX:ErrorFile=";
36
37     private static final long serialVersionUID = 1L;
38
39     private static final FilenameFilter FILENAME_FILTER = new FilenameFilter() {
40         @Override
41         public boolean accept(File dir, String name) {
42             return name.startsWith("hs_err_pid") && name.endsWith(".log");
43         }
44     };
45
46     private final String file;
47
48     private final Date date;
49
50     /**
51      * Comparateur de HsErrPid par date.
52      */

53     static final class HsErrPidComparator implements Comparator<HsErrPid>, Serializable {
54         private static final long serialVersionUID = 1L;
55
56         /** {@inheritDoc} */
57         @Override
58         public int compare(HsErrPid hsErrPid1, HsErrPid hsErrPid2) {
59             return hsErrPid2.getDate().compareTo(hsErrPid1.getDate());
60         }
61     }
62
63     private HsErrPid(String file, Date date) {
64         super();
65         this.file = file;
66         this.date = date;
67     }
68
69     public static List<HsErrPid> buildHsErrPidList() {
70         final List<File> directories = new ArrayList<>();
71         // locations of fatal error log:
72         // http://www.oracle.com/technetwork/java/javase/felog-138657.html
73         directories.add(new File("./"));
74         // linux:
75         directories.add(new File("/tmp"));
76         // windows:
77         final String tmp = System.getenv("TMP");
78         if (tmp != null) {
79             directories.add(new File(tmp));
80         }
81         final List<String> args = ManagementFactory.getRuntimeMXBean().getInputArguments();
82         for (final String arg : args) {
83             if (arg.startsWith(XX_ERROR_FILE)) {
84                 final String errorFile = arg.substring(XX_ERROR_FILE.length());
85                 final File dir = new File(errorFile).getParentFile();
86                 if (dir != null) {
87                     directories.add(dir);
88                 }
89             }
90         }
91
92         final List<HsErrPid> result = new ArrayList<>();
93         for (final File dir : directories) {
94             final File[] files = dir.listFiles(FILENAME_FILTER);
95             if (files != null) {
96                 for (final File file : files) {
97                     result.add(new HsErrPid(file.getAbsolutePath(), new Date(file.lastModified())));
98                 }
99             }
100         }
101         return result;
102     }
103
104     public static List<HsErrPid> getHsErrPidList(List<JavaInformations> javaInformationsList) {
105         final List<HsErrPid> result = new ArrayList<>();
106         for (final JavaInformations javaInformations : javaInformationsList) {
107             final List<HsErrPid> hsErrPidList = javaInformations.getHsErrPidList();
108             if (hsErrPidList != null) {
109                 result.addAll(hsErrPidList);
110             }
111         }
112         Collections.sort(result, new HsErrPidComparator());
113         return result;
114     }
115
116     public String getFile() {
117         return file;
118     }
119
120     public Date getDate() {
121         return date;
122     }
123 }
124