1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tradefed.command.remote; 17 18 import org.json.JSONArray; 19 import org.json.JSONException; 20 import org.json.JSONObject; 21 22 /** 23 * Remote operation for adding a command to the local tradefed scheduler. 24 */ 25 class AddCommandOp extends RemoteOperation<Void> { 26 27 private static final String COMMAND_ARGS = "commandArgs"; 28 private static final String TIME = "time"; 29 private final long mTotalTime; 30 private final String[] mCommandArgs; 31 AddCommandOp(long totalTime, String... commandArgs)32 AddCommandOp(long totalTime, String... commandArgs) { 33 mTotalTime = totalTime; 34 mCommandArgs = commandArgs; 35 } 36 37 /** 38 * Factory method for creating a {@link AddCommandOp} from JSON data. 39 * 40 * @param jsonData the data as a {@link JSONObject} 41 * @return a {@link AddCommandOp} 42 * @throws JSONException if failed to extract out data 43 */ createFromJson(JSONObject jsonData)44 static AddCommandOp createFromJson(JSONObject jsonData) throws JSONException { 45 long totalTime = jsonData.getLong(TIME); 46 JSONArray jsonArgs = jsonData.getJSONArray(COMMAND_ARGS); 47 String[] commandArgs = new String[jsonArgs.length()]; 48 for (int i = 0; i < commandArgs.length; i++) { 49 commandArgs[i] = jsonArgs.getString(i); 50 } 51 return new AddCommandOp(totalTime, commandArgs); 52 } 53 54 @Override getType()55 protected OperationType getType() { 56 return OperationType.ADD_COMMAND; 57 } 58 59 @Override packIntoJson(JSONObject j)60 protected void packIntoJson(JSONObject j) throws JSONException { 61 j.put(TIME, mTotalTime); 62 JSONArray jsonArgs = new JSONArray(); 63 for (String arg : mCommandArgs) { 64 jsonArgs.put(arg); 65 } 66 j.put(COMMAND_ARGS, jsonArgs); 67 } 68 getCommandArgs()69 public String[] getCommandArgs() { 70 return mCommandArgs; 71 } 72 getTotalTime()73 public long getTotalTime() { 74 return mTotalTime; 75 } 76 } 77