This project has retired. For details please refer to its
Attic page.
LoaderServer 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.inputtools.mdl;
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 LoaderServer {
29
30 String name;
31 private static Log log = LogFactory.getLog(LoaderServer.class);
32 private static FileLock lock = null;
33 private static FileOutputStream pidFileOutput = null;
34
35 public LoaderServer(String name) {
36 this.name = name;
37 }
38
39 public void init() throws IOException {
40 String pidLong = ManagementFactory.getRuntimeMXBean().getName();
41 String[] items = pidLong.split("@");
42 String pid = items[0];
43 String chukwaPath = System.getProperty("CHUKWA_HOME");
44 StringBuffer pidFilesb = new StringBuffer();
45 pidFilesb.append(chukwaPath).append("/var/run/").append(name)
46 .append(".pid");
47 try {
48 File pidFile = new File(pidFilesb.toString());
49
50 pidFileOutput = new FileOutputStream(pidFile);
51 pidFileOutput.write(pid.getBytes());
52 pidFileOutput.flush();
53 FileChannel channel = pidFileOutput.getChannel();
54 LoaderServer.lock = channel.tryLock();
55 if (LoaderServer.lock != null) {
56 log.info("Initlization succeeded...");
57 } else {
58 throw (new IOException());
59 }
60 } catch (IOException ex) {
61 System.out.println("Initializaiton failed: can not write pid file.");
62 log.error("Initialization failed...");
63 log.error(ex.getMessage());
64 System.exit(-1);
65 throw ex;
66
67 }
68
69 }
70
71 public void clean() {
72 String chukwaPath = System.getenv("CHUKWA_HOME");
73 StringBuffer pidFilesb = new StringBuffer();
74 pidFilesb.append(chukwaPath).append("/var/run/").append(name)
75 .append(".pid");
76 String pidFileName = pidFilesb.toString();
77
78 File pidFile = new File(pidFileName);
79 if (!pidFile.exists()) {
80 log.error("Delete pid file, No such file or directory: " + pidFileName);
81 } else {
82 try {
83 lock.release();
84 pidFileOutput.close();
85 } catch (IOException e) {
86 log.error("Unable to release file lock: " + pidFileName);
87 }
88 }
89
90 boolean result = pidFile.delete();
91 if (!result) {
92 log.error("Delete pid file failed, " + pidFileName);
93 }
94 }
95
96 }