This project has retired. For details please refer to its
Attic page.
PidFile xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.chukwa.util;
20
21
22 import java.io.*;
23 import java.lang.management.ManagementFactory;
24 import java.nio.channels.*;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 public class PidFile extends Thread {
29
30 String name;
31 private static Log log = LogFactory.getLog(PidFile.class);
32 private static FileLock lock = null;
33 private static FileOutputStream pidFileOutput = null;
34 private static final String DEFAULT_CHUKWA_HOME;
35
36 static {
37
38 File chukwaHome = new File(System.getProperty("java.io.tmpdir"), "chukwa");
39 try {
40 File tmpFile = File.createTempFile("chukwa", "discovertmp");
41 File tmpDir = tmpFile.getParentFile();
42 tmpFile.delete();
43 chukwaHome = new File(tmpDir, "chukwa");
44 chukwaHome.mkdir();
45 } catch(IOException e) {
46 log.debug(ExceptionUtil.getStackTrace(e));
47 } finally {
48 DEFAULT_CHUKWA_HOME = chukwaHome.getAbsolutePath();
49 }
50 };
51
52 public PidFile(String name) {
53 this.name = name;
54 try {
55 init();
56 } catch (IOException ex) {
57 clean();
58 System.exit(-1);
59 }
60 }
61
62 public void init() throws IOException {
63 String pidLong = ManagementFactory.getRuntimeMXBean().getName();
64 String[] items = pidLong.split("@");
65 String pid = items[0];
66 String chukwaPath = System.getProperty("CHUKWA_HOME");
67 if(chukwaPath == null) {
68 chukwaPath = DEFAULT_CHUKWA_HOME;
69 }
70 StringBuffer pidFilesb = new StringBuffer();
71 String pidDir = System.getenv("CHUKWA_PID_DIR");
72 if (pidDir == null) {
73 pidDir = chukwaPath + File.separator + "var" + File.separator + "run";
74 }
75 pidFilesb.append(pidDir).append(File.separator).append(name).append(".pid");
76 try {
77 File existsFile = new File(pidDir);
78 if (!existsFile.exists()) {
79 boolean success = (new File(pidDir)).mkdirs();
80 if (!success) {
81 throw (new IOException());
82 }
83 }
84 File pidFile = new File(pidFilesb.toString());
85
86 pidFileOutput = new FileOutputStream(pidFile);
87 pidFileOutput.write(pid.getBytes());
88 pidFileOutput.flush();
89 FileChannel channel = pidFileOutput.getChannel();
90 PidFile.lock = channel.tryLock();
91 if (PidFile.lock != null) {
92 log.debug("Initlization succeeded...");
93 } else {
94 throw (new IOException("Can not get lock on pid file: " + pidFilesb));
95 }
96 } catch (IOException ex) {
97 System.out.println("Initialization failed: can not write pid file to " + pidFilesb);
98 log.error("Initialization failed...");
99 log.error(ex.getMessage());
100 System.exit(-1);
101 throw ex;
102
103 }
104
105 }
106
107 public void clean() {
108 String chukwaPath = System.getenv("CHUKWA_HOME");
109 if(chukwaPath == null) {
110 chukwaPath = DEFAULT_CHUKWA_HOME;
111 }
112 StringBuffer pidFilesb = new StringBuffer();
113 String pidDir = System.getenv("CHUKWA_PID_DIR");
114 if (pidDir == null) {
115 pidDir = chukwaPath + File.separator + "var" + File.separator + "run";
116 }
117 pidFilesb.append(pidDir).append(File.separator).append(name).append(".pid");
118 String pidFileName = pidFilesb.toString();
119
120 File pidFile = new File(pidFileName);
121 if (!pidFile.exists()) {
122 log.error("Delete pid file, No such file or directory: " + pidFileName);
123 } else {
124 try {
125 lock.release();
126 pidFileOutput.close();
127 } catch (IOException e) {
128 log.error("Unable to release file lock: " + pidFileName);
129 }
130 }
131
132 boolean result = pidFile.delete();
133 if (!result) {
134 log.error("Delete pid file failed, " + pidFileName);
135 }
136 }
137
138 public void run() {
139 clean();
140 }
141 }