1
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
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
53 static final class HsErrPidComparator implements Comparator<HsErrPid>, Serializable {
54 private static final long serialVersionUID = 1L;
55
56
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
72
73 directories.add(new File("./"));
74
75 directories.add(new File("/tmp"));
76
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