1 /*
   2  * Copyright (c) 2016, 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 import java.io.IOException;
  25 import java.nio.ByteBuffer;
  26 
  27 /**
  28  * Generic JDWP command
  29  * @param <T> the corresponding JDWP reply class, to construct a reply object
  30  */
  31 public abstract class JdwpCmd<T extends JdwpReply> {
  32 
  33     private ByteBuffer data;
  34     private static int id = 1;
  35     private final byte FLAGS = 0;
  36     private T reply;
  37     private final int dataLen;
  38     private final int HEADER_LEN = 11;
  39 
  40     /**
  41      * JDWWp command
  42      * @param cmd command code
  43      * @param cmdSet command set
  44      * @param replyClz command reply class
  45      * @param dataLen length of additional data for the command
  46      */
  47     JdwpCmd(int cmd, int cmdSet, Class<T> replyClz, int dataLen) {
  48         this.dataLen = dataLen;
  49         data = ByteBuffer.allocate(HEADER_LEN + dataLen);
  50         data.putInt(HEADER_LEN + dataLen);
  51         data.putInt(id++);
  52         data.put(FLAGS);
  53         data.put((byte) cmdSet);
  54         data.put((byte) cmd);
  55         if (replyClz != null) {
  56             try {
  57                 reply = replyClz.newInstance();
  58             } catch (Exception ex) {
  59                 ex.printStackTrace();
  60             }
  61         }
  62     }
  63 
  64     JdwpCmd(int cmd, int cmdSet, Class<T> replyClz) {
  65         this(cmd, cmdSet, replyClz, 0);
  66     }
  67 
  68     int getDataLength() {
  69         return dataLen;
  70     }
  71 
  72     public final T send(JdwpChannel channel) throws IOException {
  73         System.err.println("Sending command: " + this);
  74         channel.write(data.array(), HEADER_LEN + getDataLength());
  75         if (reply != null) {
  76             reply.initFromStream(channel.getInputStream());
  77         }
  78         return (T) reply;
  79     }
  80 
  81     protected void putRefId(long refId) {
  82         data.putLong(refId);
  83     }
  84 
  85     protected void putInt(int val) {
  86         data.putInt(val);
  87     }
  88 
  89     protected static int refLen() {
  90         return 8;
  91     }
  92 
  93 }