1 /*
2  * Copyright (C) 2014 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 import java.util.ArrayList;
23 import java.util.List;
24 
25 /**
26  * Remote operation for adding a command file to the local tradefed scheduler.
27  */
28 class AddCommandFileOp extends RemoteOperation<Void> {
29 
30     private static final String COMMAND_FILE = "commandFile";
31     private static final String EXTRA_ARGS = "extraArgs";
32     private final String mCommandFile;
33     private final List<String> mExtraArgs;
34 
AddCommandFileOp(String commandFile, List<String> extraArgs)35     AddCommandFileOp(String commandFile, List<String> extraArgs) {
36         mCommandFile = commandFile;
37         mExtraArgs = extraArgs;
38     }
39 
40     /**
41      * Factory method for creating a {@link AddCommandFileOp} from JSON data.
42      *
43      * @param jsonData the data as a {@link JSONObject}
44      * @return a {@link AddCommandFileOp}
45      * @throws JSONException if failed to extract out data
46      */
createFromJson(JSONObject jsonData)47     static AddCommandFileOp createFromJson(JSONObject jsonData) throws JSONException {
48         String cmdFile = jsonData.getString(COMMAND_FILE);
49         JSONArray argArray = jsonData.getJSONArray(EXTRA_ARGS);
50         List<String> argList = new ArrayList<String>(argArray.length());
51         for (int i=0; i < argArray.length(); i++) {
52             argList.add(argArray.getString(i));
53         }
54         return new AddCommandFileOp(cmdFile, argList);
55     }
56 
57     @Override
getType()58     protected OperationType getType() {
59         return OperationType.ADD_COMMAND_FILE;
60     }
61 
62     @Override
packIntoJson(JSONObject j)63     protected void packIntoJson(JSONObject j) throws JSONException {
64         j.put(COMMAND_FILE, mCommandFile);
65         JSONArray argArray = new JSONArray(mExtraArgs);
66         j.put(EXTRA_ARGS, argArray);
67     }
68 
getCommandFile()69     public String getCommandFile() {
70         return mCommandFile;
71     }
72 
73     /**
74      * Returns the extra arguments to add to each command parsed from command file
75      */
getExtraArgs()76     public List<String> getExtraArgs() {
77         return mExtraArgs;
78     }
79 }
80