1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.failurehandler.action;
  25 
  26 import jdk.test.failurehandler.ProcessInfoGatherer;
  27 import jdk.test.failurehandler.EnvironmentInfoGatherer;
  28 import jdk.test.failurehandler.HtmlSection;
  29 import jdk.test.failurehandler.Utils;
  30 
  31 import java.io.PrintWriter;
  32 import java.util.ArrayList;
  33 import java.util.List;
  34 import java.util.Properties;
  35 
  36 public class ActionSet implements ProcessInfoGatherer, EnvironmentInfoGatherer {
  37     private static final String ENVIRONMENT_PROPERTY = "environment";
  38     private static final String ON_PID_PROPERTY = "onTimeout";
  39 
  40     private final ActionHelper helper;
  41 
  42     public String getName() {
  43         return name;
  44     }
  45 
  46     private final String name;
  47     private final List<SimpleAction> environmentActions;
  48     private final List<PatternAction> processActions;
  49 
  50 
  51     public ActionSet(ActionHelper helper, PrintWriter log, String name) {
  52         this.helper = helper;
  53         this.name = name;
  54 
  55         Properties p = Utils.getProperties(name);
  56         environmentActions = getSimpleActions(log, p, ENVIRONMENT_PROPERTY);
  57         processActions = getPatternActions(log, p, ON_PID_PROPERTY);
  58     }
  59 
  60     private List<SimpleAction> getSimpleActions(PrintWriter log, Properties p,
  61                                                 String key) {
  62         String[] tools = getTools(log, p, key);
  63         List<SimpleAction> result = new ArrayList<>(tools.length);
  64         for (String tool : tools) {
  65             try {
  66                 SimpleAction action = new SimpleAction(
  67                         Utils.prependPrefix(name, tool), tool, p);
  68                 result.add(action);
  69             } catch (Exception e) {
  70                 log.printf("ERROR: %s cannot be created : %s %n",
  71                         tool, e.getMessage());
  72                 e.printStackTrace(log);
  73             }
  74         }
  75         return result;
  76     }
  77 
  78     private List<PatternAction> getPatternActions(PrintWriter log,
  79                                                   Properties p, String key) {
  80         String[] tools = getTools(log, p, key);
  81         List<PatternAction> result = new ArrayList<>(tools.length);
  82         for (String tool : tools) {
  83             try {
  84                 PatternAction action = new PatternAction(
  85                         Utils.prependPrefix(name, tool), tool, p);
  86                 result.add(action);
  87             } catch (Exception e) {
  88                 log.printf("ERROR: %s cannot be created : %s %n",
  89                         tool, e.getMessage());
  90                 e.printStackTrace(log);
  91             }
  92         }
  93         return result;
  94     }
  95 
  96     private String[] getTools(PrintWriter writer, Properties p, String key) {
  97         String value = p.getProperty(key);
  98         if (value == null || value.isEmpty()) {
  99             writer.printf("ERROR: '%s' property is empty%n", key);
 100             return new String[]{};
 101         }
 102         return value.split(" ");
 103     }
 104 
 105 
 106     @Override
 107     public void gatherProcessInfo(HtmlSection section, long pid) {
 108         String pidStr = "" + pid;
 109         for (PatternAction action : processActions) {
 110             if (action.isJavaOnly()) {
 111                 if (helper.isJava(pid, section.getWriter())) {
 112                     helper.runPatternAction(action, section, pidStr);
 113                 }
 114             } else {
 115                 helper.runPatternAction(action, section, pidStr);
 116             }
 117         }
 118     }
 119 
 120     @Override
 121     public void gatherEnvironmentInfo(HtmlSection section) {
 122         for (SimpleAction action : environmentActions) {
 123             helper.runPatternAction(action, section);
 124         }
 125     }
 126 }