This project has retired. For details please refer to its Attic page.
Exec 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  package org.apache.hadoop.chukwa.inputtools.plugin.metrics;
19  
20  
21  import java.util.Timer;
22  import java.util.TimerTask;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.chukwa.inputtools.plugin.IPlugin;
26  import org.apache.hadoop.chukwa.util.DaemonWatcher;
27  import org.json.simple.JSONObject;
28  
29  public class Exec extends TimerTask {
30    private static Log log = LogFactory.getLog(Exec.class);
31    private String cmde = null;
32    private IPlugin plugin = null;
33  
34    public Exec(String[] cmds) {
35      StringBuffer c = new StringBuffer();
36      for (String cmd : cmds) {
37        c.append(cmd);
38        c.append(" ");
39      }
40      cmde = c.toString();
41      plugin = new ExecHelper(cmds);
42    }
43  
44    public void run() {
45      try {
46        JSONObject result = plugin.execute();
47        int status = (Integer) result.get("status");
48        if (status < 0) {
49          System.out.println("Error");
50          log.warn("[ChukwaError]:" + Exec.class + ", "
51              + result.get("stderr"));
52        } else {
53          log.info(result.get("stdout"));
54        }
55      } catch (Exception e) {
56        log.error("Exec output unparsable:" + this.cmde);
57      }
58    }
59  
60    public String getCmde() {
61      return cmde;
62    }
63  
64    public static void main(String[] args) {
65      DaemonWatcher.createInstance(System.getProperty("RECORD_TYPE") + "-data-loader");
66      int period = 60;
67      try {
68        if (System.getProperty("PERIOD") != null) {
69          period = Integer.parseInt(System.getProperty("PERIOD"));
70        }
71      } catch (NumberFormatException ex) {
72        ex.printStackTrace();
73        System.out
74            .println("Usage: java -DPERIOD=nn -DRECORD_TYPE=recordType Exec [cmd]");
75        System.out.println("PERIOD should be numeric format of seconds.");
76        System.exit(0);
77      }
78      Timer timer = new Timer();
79      timer.schedule(new Exec(args), 0, period * 1000);
80    }
81  }