This project has retired. For details please refer to its Attic page.
LoaderServer xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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  
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  }