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 17 package com.android.tradefed.util; 18 19 import com.android.tradefed.log.LogUtil.CLog; 20 21 import com.google.common.base.Strings; 22 23 import java.io.File; 24 import java.io.FileInputStream; 25 import java.io.IOException; 26 import java.io.InputStream; 27 import java.util.HashMap; 28 import java.util.Map; 29 import java.util.Properties; 30 31 /** Utility for reading configuration resources. */ 32 public class ResourceUtil { 33 34 /** 35 * Read a property configuration from the resources. Configuration must follow a "key=value" or 36 * "key:value" format. Method is safe and will return an empty map in case of error. 37 * 38 * @param resource The path of the resource to read. 39 * @return a {@link Map} of the loaded resources. 40 * @see Properties 41 */ readConfigurationFromResource(String resource)42 public static Map<String, String> readConfigurationFromResource(String resource) { 43 try (InputStream resStream = ResourceUtil.class.getResourceAsStream(resource)) { 44 return readConfigurationFromStream(resStream); 45 } catch (IOException e) { 46 CLog.e(e); 47 } 48 return new HashMap<>(); 49 } 50 51 /** 52 * Read a property configuration from a file. Configuration must follow a "key=value" or 53 * "key:value" format. Method is safe and will return an empty map in case of error. 54 * 55 * @param propertyFile The path of the resource to read. 56 * @return a {@link Map} of the loaded resources. 57 * @see Properties 58 */ readConfigurationFromFile(File propertyFile)59 public static Map<String, String> readConfigurationFromFile(File propertyFile) { 60 try (InputStream resStream = new FileInputStream(propertyFile)) { 61 return readConfigurationFromStream(resStream); 62 } catch (IOException e) { 63 CLog.e(e); 64 } 65 return new HashMap<>(); 66 } 67 68 /** 69 * Read a property configuration from a stream. Configuration must follow a "key=value" or 70 * "key:value" format. Method is safe and will return an empty map in case of error. 71 * 72 * @param propertyStream The path of the resource to read. 73 * @return a {@link Map} of the loaded resources. 74 * @see Properties 75 */ readConfigurationFromStream(InputStream propertyStream)76 public static Map<String, String> readConfigurationFromStream(InputStream propertyStream) { 77 Map<String, String> resources = new HashMap<>(); 78 try { 79 if (propertyStream == null) { 80 CLog.w("No configuration stream"); 81 return resources; 82 } 83 Properties properties = new Properties(); 84 properties.load(propertyStream); 85 for (Object key : properties.keySet()) { 86 String keyString = (String) key; 87 String valueString = (String) properties.get(key); 88 if (Strings.isNullOrEmpty(valueString)) { 89 continue; 90 } 91 resources.put(keyString, valueString); 92 } 93 } catch (IOException e) { 94 CLog.e(e); 95 } 96 return resources; 97 } 98 } 99