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 compiler.compilercontrol.share.actions;
  25 
  26 import compiler.compilercontrol.share.scenario.State;
  27 import jdk.test.lib.Pair;
  28 import jdk.test.lib.ProcessTools;
  29 import pool.PoolHelper;
  30 
  31 import java.io.BufferedReader;
  32 import java.io.InputStreamReader;
  33 import java.io.PrintWriter;
  34 import java.io.OutputStreamWriter;
  35 import java.io.IOException;
  36 import java.lang.reflect.Executable;
  37 import java.net.InetAddress;
  38 import java.net.Socket;
  39 import java.util.Arrays;
  40 import java.util.HashMap;
  41 import java.util.List;
  42 import java.util.ListIterator;
  43 import java.util.Map;
  44 import java.util.concurrent.Callable;
  45 import java.util.stream.Collectors;
  46 
  47 public class BaseAction {
  48     private static final List<Pair<Executable, Callable<?>>> METHODS;
  49     private static final Map<String, Executable> METHODS_NAMES;
  50 
  51     static {
  52         METHODS = new PoolHelper().getAllMethods();
  53         METHODS_NAMES = METHODS.stream().collect(Collectors.toMap(
  54                 pair -> pair.first.toGenericString(),
  55                 pair -> pair.first));
  56     }
  57 
  58     public static void main(String[] args) {
  59         if (args.length < 1) {
  60             throw new Error("TESTBUG: requires port as parameter: "
  61                     + Arrays.toString(args));
  62         }
  63         int pid;
  64         try {
  65             pid = ProcessTools.getProcessId();
  66         } catch (Exception e) {
  67             throw new Error("Could not determine own pid", e);
  68         }
  69         int port = Integer.parseInt(args[0]);
  70         System.out.println("INFO: Client connection port = " + port);
  71         List<String> lines;
  72         try (
  73                 Socket socket = new Socket(InetAddress.getLocalHost(), port);
  74                 BufferedReader in = new BufferedReader(
  75                         new InputStreamReader(socket.getInputStream()));
  76                 PrintWriter out = new PrintWriter(
  77                         new OutputStreamWriter(socket.getOutputStream()))) {
  78             // send own pid to execute jcmd if needed
  79             out.println(String.valueOf(pid));
  80             out.flush();
  81             lines = in.lines().collect(Collectors.toList());
  82         } catch (IOException e) {
  83             throw new Error("Error on performing network operation", e);
  84         }
  85         check(decodeMap(lines));
  86     }
  87 
  88     private static Map<Executable, State> decodeMap(List<String> lines) {
  89         if (lines == null || lines.size() == 0) {
  90             throw new Error("TESTBUG: unexpected lines list");
  91         }
  92         Map<Executable, State> stateMap = new HashMap<>();
  93         int startIndex = 0;
  94         ListIterator<String> iterator = lines.listIterator();
  95         while (iterator.hasNext()) {
  96             int index = iterator.nextIndex();
  97             String next = iterator.next();
  98             switch (next) {
  99                 case "{" :
 100                     startIndex = index;
 101                     break;
 102                 case "}" :
 103                     // method name goes after {
 104                     Executable executable = METHODS_NAMES.get(lines.get(
 105                             ++startIndex));
 106                     // state description starts after method
 107                     State state = State.fromString(lines.subList(++startIndex,
 108                             index).toArray(new String[index - startIndex]));
 109                     stateMap.put(executable, state);
 110                     break;
 111             }
 112         }
 113         return stateMap;
 114     }
 115 
 116     protected static void check(Map<Executable, State> methodStates) {
 117         // Check each method from the pool
 118         METHODS.forEach(pair -> {
 119             Executable x = pair.first;
 120             CompileAction.checkCompiled(x, methodStates.get(x));
 121         });
 122     }
 123 }