Di Qian | 38c02a7 | 2019-11-18 19:14:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.cluster; |
| 17 | |
| 18 | import com.android.tradefed.config.Configuration; |
| 19 | import com.android.tradefed.config.ConfigurationUtil; |
| 20 | import com.android.tradefed.result.LegacySubprocessResultsReporter; |
| 21 | |
| 22 | import org.kxml2.io.KXmlSerializer; |
| 23 | |
| 24 | import java.io.File; |
| 25 | import java.io.IOException; |
| 26 | import java.io.PrintWriter; |
| 27 | |
| 28 | /** |
| 29 | * Build a wrapper TF config XML for an existing TF config. |
| 30 | * |
| 31 | * <p>A wrapper XML allows to enable subprocess reporting on an existing TF config. |
| 32 | */ |
| 33 | public class SubprocessConfigBuilder { |
| 34 | private static final String INCLUDE_NAME = "include"; |
| 35 | private static final String REPORTER_CLASS = LegacySubprocessResultsReporter.class.getName(); |
| 36 | private static final String OPTION_KEY = "subprocess-report-port"; |
| 37 | private static final String CONFIG_DESCRIPTION = "Cluster Command Launcher config"; |
| 38 | |
| 39 | private File mWorkdir; |
| 40 | |
| 41 | private String mOriginalConfig; |
| 42 | |
| 43 | private String mPort; |
| 44 | |
| 45 | public SubprocessConfigBuilder setWorkingDir(File dir) { |
| 46 | mWorkdir = dir; |
| 47 | return this; |
| 48 | } |
| 49 | |
| 50 | public SubprocessConfigBuilder setOriginalConfig(String config) { |
| 51 | mOriginalConfig = config; |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | public SubprocessConfigBuilder setPort(String port) { |
| 56 | mPort = port; |
| 57 | return this; |
| 58 | } |
| 59 | |
| 60 | public File build() throws IOException { |
| 61 | // Make a new config name based on the original config name to make it possible to find |
| 62 | // out the original command line from a modified one. |
| 63 | // FIXME: Find a better way to preserve the original command line. |
Daniel Peykov | f6f231e | 2020-01-14 20:51:21 -0800 | [diff] [blame] | 64 | String configName = "_" + mOriginalConfig.replace("/", "$") + ".xml"; |
Di Qian | 38c02a7 | 2019-11-18 19:14:07 -0800 | [diff] [blame] | 65 | // mOriginalConfig is from another test suite, so its content is hard to know at this |
| 66 | // time. So it doesn't load mOriginalConfig as IConfiguration and add additional config. |
| 67 | // Instead, it creates a wrapper config including mOriginalConfig. |
| 68 | File f = new File(mWorkdir, configName); |
Julien Desprez | d794d68 | 2020-01-28 14:52:56 -0800 | [diff] [blame] | 69 | PrintWriter writer = new PrintWriter(f); |
Di Qian | 38c02a7 | 2019-11-18 19:14:07 -0800 | [diff] [blame] | 70 | KXmlSerializer serializer = new KXmlSerializer(); |
| 71 | serializer.setOutput(writer); |
| 72 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 73 | serializer.startDocument("UTF-8", null); |
| 74 | serializer.startTag(null, ConfigurationUtil.CONFIGURATION_NAME); |
| 75 | serializer.attribute( |
| 76 | null, Configuration.CONFIGURATION_DESCRIPTION_TYPE_NAME, CONFIG_DESCRIPTION); |
| 77 | |
| 78 | serializer.startTag(null, INCLUDE_NAME); |
| 79 | serializer.attribute(null, ConfigurationUtil.NAME_NAME, mOriginalConfig); |
| 80 | serializer.endTag(null, INCLUDE_NAME); |
| 81 | |
| 82 | if (mPort != null) { |
| 83 | serializer.startTag(null, Configuration.RESULT_REPORTER_TYPE_NAME); |
| 84 | serializer.attribute(null, ConfigurationUtil.CLASS_NAME, REPORTER_CLASS); |
| 85 | |
| 86 | serializer.startTag(null, ConfigurationUtil.OPTION_NAME); |
| 87 | serializer.attribute(null, ConfigurationUtil.NAME_NAME, OPTION_KEY); |
| 88 | serializer.attribute(null, ConfigurationUtil.VALUE_NAME, mPort); |
| 89 | serializer.endTag(null, ConfigurationUtil.OPTION_NAME); |
| 90 | |
| 91 | serializer.endTag(null, Configuration.RESULT_REPORTER_TYPE_NAME); |
| 92 | } |
| 93 | |
| 94 | serializer.endTag(null, ConfigurationUtil.CONFIGURATION_NAME); |
| 95 | serializer.endDocument(); |
| 96 | |
| 97 | writer.close(); |
| 98 | return f; |
| 99 | } |
| 100 | } |