Home | History | Annotate | Download | only in config
      1 /*
      2  * Copyright (C) 2017 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.config;
     17 
     18 import com.android.tradefed.log.LogUtil.CLog;
     19 import com.android.tradefed.sandbox.ISandbox;
     20 import com.android.tradefed.sandbox.SandboxConfigDump.DumpCmd;
     21 import com.android.tradefed.sandbox.SandboxConfigUtil;
     22 import com.android.tradefed.util.FileUtil;
     23 import com.android.tradefed.util.IRunUtil;
     24 import com.android.tradefed.util.keystore.IKeyStoreClient;
     25 
     26 import java.io.File;
     27 import java.util.Map;
     28 
     29 /** Special Configuration factory to handle creation of configurations for Sandboxing purpose. */
     30 public class SandboxConfigurationFactory extends ConfigurationFactory {
     31 
     32     private static SandboxConfigurationFactory sInstance = null;
     33 
     34     /** Get the singleton {@link IConfigurationFactory} instance. */
     35     public static SandboxConfigurationFactory getInstance() {
     36         if (sInstance == null) {
     37             sInstance = new SandboxConfigurationFactory();
     38         }
     39         return sInstance;
     40     }
     41 
     42     /** {@inheritDoc} */
     43     @Override
     44     ConfigurationDef getConfigurationDef(
     45             String name, boolean isGlobal, Map<String, String> templateMap)
     46             throws ConfigurationException {
     47         // TODO: Extend ConfigurationDef to possibly create a different IConfiguration type and
     48         // handle more elegantly the parent/subprocess incompatibilities.
     49         ConfigurationDef def = new ConfigurationDef(name);
     50         new ConfigLoader(isGlobal).loadConfiguration(name, def, null, templateMap);
     51         return def;
     52     }
     53 
     54     /**
     55      * Create a {@link IConfiguration} based on the command line and sandbox provided.
     56      *
     57      * @param args the command line for the run.
     58      * @param keyStoreClient the {@link IKeyStoreClient} where to load the key from.
     59      * @param sandbox the {@link ISandbox} used for the run.
     60      * @param runUtil the {@link IRunUtil} to run commands.
     61      * @return a {@link IConfiguration} valid for the sandbox.
     62      * @throws ConfigurationException
     63      */
     64     public IConfiguration createConfigurationFromArgs(
     65             String[] args, IKeyStoreClient keyStoreClient, ISandbox sandbox, IRunUtil runUtil)
     66             throws ConfigurationException {
     67         IConfiguration config = null;
     68         File xmlConfig = null;
     69         try {
     70             runUtil.unsetEnvVariable(GlobalConfiguration.GLOBAL_CONFIG_VARIABLE);
     71             File tfDir = sandbox.getTradefedEnvironment(args);
     72             // TODO: dump using the keystore too
     73             xmlConfig =
     74                     SandboxConfigUtil.dumpConfigForVersion(
     75                             tfDir, runUtil, args, DumpCmd.NON_VERSIONED_CONFIG);
     76             // Get the non version part of the configuration in order to do proper allocation
     77             // of devices and such.
     78             config =
     79                     super.createConfigurationFromArgs(
     80                             new String[] {xmlConfig.getAbsolutePath()}, null, keyStoreClient);
     81             // Reset the command line to the original one.
     82             config.setCommandLine(args);
     83             config.setConfigurationObject(Configuration.SANDBOX_TYPE_NAME, sandbox);
     84         } catch (ConfigurationException e) {
     85             CLog.e(e);
     86             sandbox.tearDown();
     87             throw e;
     88         } finally {
     89             FileUtil.deleteFile(xmlConfig);
     90         }
     91         return config;
     92     }
     93 }
     94