LCOV - code coverage report
Current view: top level - pgm/util - AbstractProgram.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 14 40 35.0 %
Date: 2022-11-19 15:00:39 Functions: 3 6 50.0 %

          Line data    Source code
       1             : // Copyright (C) 2009 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.pgm.util;
      16             : 
      17             : import com.google.gerrit.common.Die;
      18             : import com.google.gerrit.util.cli.CmdLineParser;
      19             : import com.google.gerrit.util.cli.OptionHandlers;
      20             : import java.io.StringWriter;
      21             : import org.kohsuke.args4j.CmdLineException;
      22             : import org.kohsuke.args4j.Option;
      23             : 
      24             : /** Base class for command line invocations of Gerrit Code Review. */
      25         138 : public abstract class AbstractProgram {
      26         138 :   private final Object sleepLock = new Object();
      27         138 :   private boolean running = true;
      28             : 
      29             :   @Option(name = "--show-stack-trace", usage = "display stack trace on failure")
      30             :   protected boolean showStackTrace;
      31             : 
      32             :   private String getName() {
      33           0 :     String n = getClass().getName();
      34           0 :     int dot = n.lastIndexOf('.');
      35           0 :     if (0 < dot) {
      36           0 :       n = n.substring(dot + 1);
      37             :     }
      38           0 :     return n.toLowerCase();
      39             :   }
      40             : 
      41             :   public final int main(String[] argv) throws Exception {
      42          15 :     final CmdLineParser clp = new CmdLineParser(OptionHandlers.empty(), this);
      43             :     try {
      44          15 :       clp.parseArgument(argv);
      45           0 :     } catch (CmdLineException err) {
      46           0 :       if (!clp.wasHelpRequestedByOption()) {
      47           0 :         System.err.println("fatal: " + err.getMessage());
      48           0 :         return 1;
      49             :       }
      50          15 :     }
      51             : 
      52          15 :     if (clp.wasHelpRequestedByOption()) {
      53           0 :       StringWriter msg = new StringWriter();
      54           0 :       clp.printDetailedUsage(getName(), msg);
      55           0 :       System.err.println(msg.toString());
      56           0 :       return 1;
      57             :     }
      58             : 
      59             :     try {
      60          15 :       ProxyUtil.configureHttpProxy();
      61          15 :       return run();
      62           1 :     } catch (Die err) {
      63           1 :       if (showStackTrace) {
      64           1 :         err.printStackTrace();
      65             :       } else {
      66           0 :         final Throwable cause = err.getCause();
      67           0 :         final String diemsg = err.getMessage();
      68           0 :         if (cause != null && !cause.getMessage().equals(diemsg)) {
      69           0 :           System.err.println("fatal: " + cause.getMessage().replace("\n", "\nfatal: "));
      70             :         }
      71           0 :         System.err.println("fatal: " + diemsg.replace("\n", "\nfatal: "));
      72             :       }
      73           1 :       return 128;
      74             :     }
      75             :   }
      76             : 
      77             :   /** Create a new exception to indicate we won't continue. */
      78             :   protected static Die die(String why) {
      79           1 :     return new Die(why);
      80             :   }
      81             : 
      82             :   /** Create a new exception to indicate we won't continue. */
      83             :   protected static Die die(String why, Throwable cause) {
      84           0 :     return new Die(why, cause);
      85             :   }
      86             : 
      87             :   /** Method that never returns, e.g. to keep a daemon running. */
      88             :   protected int never() {
      89           0 :     synchronized (sleepLock) {
      90           0 :       while (running) {
      91             :         try {
      92           0 :           sleepLock.wait(60 * 60 * 1000L);
      93           0 :         } catch (InterruptedException e) {
      94           0 :           continue;
      95           0 :         }
      96             :       }
      97           0 :       return 0;
      98             :     }
      99             :   }
     100             : 
     101             :   /**
     102             :    * Run this program's logic, returning the command exit status.
     103             :    *
     104             :    * <p>When this method completes, the JVM is terminated. To keep the JVM running, use {@code
     105             :    * return never()}.
     106             :    */
     107             :   public abstract int run() throws Exception;
     108             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750