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;
  25 
  26 import jdk.test.failurehandler.action.ActionSet;
  27 import jdk.test.failurehandler.action.ActionHelper;
  28 
  29 import java.io.PrintWriter;
  30 import java.util.ArrayList;
  31 import java.util.LinkedList;
  32 import java.util.List;
  33 import java.util.Queue;
  34 
  35 public class ToolKit implements EnvironmentInfoGatherer, ProcessInfoGatherer {
  36     private final List<ActionSet> actions = new ArrayList<>();
  37     private final ActionHelper helper;
  38 
  39     public ToolKit(ActionHelper helper, PrintWriter log, String... names) {
  40         this.helper = helper;
  41         for (String name : names) {
  42             actions.add(new ActionSet(helper, log, name));
  43         }
  44     }
  45 
  46     @Override
  47     public void gatherEnvironmentInfo(HtmlSection section) {
  48         for (ActionSet set : actions) {
  49             set.gatherEnvironmentInfo(section);
  50         }
  51     }
  52 
  53     @Override
  54     public void gatherProcessInfo(HtmlSection section, long pid) {
  55         Queue<Long> pids = new LinkedList<>();
  56         pids.add(pid);
  57         for (Long p = pids.poll(); p != null; p = pids.poll()) {
  58             HtmlSection pidSection = section.createChildren("" + p);
  59             for (ActionSet set : actions) {
  60                 set.gatherProcessInfo(pidSection, p);
  61             }
  62             List<Long> children = helper.getChildren(pidSection, p);
  63             if (!children.isEmpty()) {
  64                 HtmlSection s = pidSection.createChildren("children");
  65                 for (Long c : children) {
  66                     s.link(section, c.toString(), c.toString());
  67                 }
  68                 pids.addAll(children);
  69             }
  70         }
  71     }
  72 }