1 /* 2 * Copyright (C) 2018 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.host; 17 18 import com.android.tradefed.config.ConfigurationException; 19 import com.android.tradefed.config.Option; 20 import com.android.tradefed.config.OptionClass; 21 22 import java.io.File; 23 import java.util.HashMap; 24 import java.util.Map; 25 26 /** Manager host resource. The host resources are local files. */ 27 @OptionClass(alias = "local-hrm", global_namespace = false) 28 public class LocalHostResourceManager implements IHostResourceManager { 29 30 @Option( 31 name = "host-resource", 32 description = "The host resources to download for the current host." 33 ) 34 private Map<String, String> mHostResources = new HashMap<>(); 35 36 @Option(name = "disable", description = "Disable the host resource manager or not.") 37 private boolean mDisable = false; 38 39 private Map<String, File> mDownloadedHostResources = new HashMap<>(); 40 41 /** {@inheritDoc} */ 42 @Override setup()43 public void setup() throws ConfigurationException { 44 if (mDisable) { 45 return; 46 } 47 for (Map.Entry<String, String> entry : mHostResources.entrySet()) { 48 File localFile = fetchHostResource(entry.getKey(), entry.getValue()); 49 mDownloadedHostResources.put(entry.getKey(), localFile); 50 } 51 } 52 53 /** {@inheritDoc} */ 54 @Override getFile(String name)55 public File getFile(String name) { 56 return mDownloadedHostResources.get(name); 57 } 58 59 /** {@inheritDoc} */ 60 @Override cleanup()61 public void cleanup() { 62 if (mDisable) { 63 return; 64 } 65 for (Map.Entry<String, File> entry : mDownloadedHostResources.entrySet()) { 66 clearHostResource(entry.getKey(), entry.getValue()); 67 } 68 } 69 70 /** 71 * Clear a local host resource. 72 * 73 * @param name the id of the host resource. 74 * @param localFile the local file. 75 */ clearHostResource(String name, File localFile)76 protected void clearHostResource(String name, File localFile) { 77 // For LocalHostResourceManager, do nothing. 78 } 79 80 /** 81 * Use a local file a host resource. 82 * 83 * @param name the name of the host resource. 84 * @param value the local path of the host resource. 85 * @return the local file. 86 */ fetchHostResource(String name, String value)87 protected File fetchHostResource(String name, String value) throws ConfigurationException { 88 File localFile = new File(value); 89 if (!localFile.exists()) { 90 throw new ConfigurationException(String.format("File %s doesn't exist.", value)); 91 } 92 return localFile; 93 } 94 } 95