Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.device.IDeviceRecovery; |
| 19 | import com.android.tradefed.log.ILeveledLogOutput; |
| 20 | import com.android.tradefed.result.ITestInvocationListener; |
| 21 | import com.android.tradefed.targetsetup.IBuildProvider; |
| 22 | import com.android.tradefed.targetsetup.ITargetPreparer; |
| 23 | |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 24 | import java.util.Collection; |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 25 | import java.util.HashSet; |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 26 | import java.util.Hashtable; |
| 27 | import java.util.Map; |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 28 | import java.util.Set; |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 29 | |
| 30 | import junit.framework.Test; |
| 31 | |
| 32 | /** |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 33 | * A concrete {@link IConfiguration} implementation that stores the loaded config objects in a map |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 34 | */ |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 35 | // TODO: make package-private |
| 36 | public class Configuration implements IConfiguration { |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 37 | |
| 38 | // names for built in configuration objects |
Brett Chabot | c819fc1 | 2010-05-04 22:41:05 -0700 | [diff] [blame] | 39 | public static final String BUILD_PROVIDER_NAME = "build_provider"; |
| 40 | public static final String TARGET_PREPARER_NAME = "target_preparer"; |
| 41 | public static final String TEST_NAME = "test"; |
| 42 | public static final String DEVICE_RECOVERY_NAME = "device_recovery"; |
| 43 | public static final String LOGGER_NAME = "logger"; |
| 44 | public static final String RESULT_REPORTER_NAME = "result_reporter"; |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 45 | |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 46 | private static Set<String> sObjNames = null; |
| 47 | |
| 48 | static Set<String> getConfigObjNames() { |
| 49 | if (sObjNames == null) { |
| 50 | sObjNames = new HashSet<String>(); |
| 51 | sObjNames.add(BUILD_PROVIDER_NAME); |
| 52 | sObjNames.add(TARGET_PREPARER_NAME); |
| 53 | sObjNames.add(TEST_NAME); |
| 54 | sObjNames.add(DEVICE_RECOVERY_NAME); |
| 55 | sObjNames.add(LOGGER_NAME); |
| 56 | sObjNames.add(RESULT_REPORTER_NAME); |
| 57 | } |
| 58 | return sObjNames; |
| 59 | } |
| 60 | |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 61 | /** Mapping of config object name to config object. */ |
| 62 | private Map<String, Object> mConfigMap; |
| 63 | |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 64 | protected Configuration() { |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 65 | mConfigMap = new Hashtable<String, Object>(); |
| 66 | } |
| 67 | |
| 68 | /** |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 69 | * @param configObjects |
| 70 | */ |
| 71 | public Configuration(Map<String, Object> configObjects) { |
| 72 | mConfigMap = configObjects; |
| 73 | } |
| 74 | |
| 75 | /** |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 76 | * Adds a loaded object to this configuration. |
| 77 | * |
| 78 | * @param name the unique name of the configuration object |
| 79 | * @param configObject the configuration object |
| 80 | */ |
Brett Chabot | c819fc1 | 2010-05-04 22:41:05 -0700 | [diff] [blame] | 81 | protected void addObject(String name, Object configObject) { |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 82 | mConfigMap.put(name, configObject); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * {@inheritDoc} |
| 87 | */ |
| 88 | public IBuildProvider getBuildProvider() throws ConfigurationException { |
| 89 | return (IBuildProvider)getConfigurationObject(BUILD_PROVIDER_NAME, |
| 90 | IBuildProvider.class); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * {@inheritDoc} |
| 95 | */ |
| 96 | public ITargetPreparer getTargetPreparer() throws ConfigurationException { |
| 97 | return (ITargetPreparer)getConfigurationObject(TARGET_PREPARER_NAME, |
| 98 | ITargetPreparer.class); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * {@inheritDoc} |
| 103 | */ |
| 104 | public Test getTest() throws ConfigurationException { |
| 105 | return (Test)getConfigurationObject(TEST_NAME, |
| 106 | Test.class); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * {@inheritDoc} |
| 111 | */ |
| 112 | public IDeviceRecovery getDeviceRecovery() throws ConfigurationException { |
| 113 | return (IDeviceRecovery)getConfigurationObject(DEVICE_RECOVERY_NAME, |
| 114 | IDeviceRecovery.class); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritDoc} |
| 119 | */ |
| 120 | public ILeveledLogOutput getLogOutput() throws ConfigurationException { |
| 121 | return (ILeveledLogOutput)getConfigurationObject(LOGGER_NAME, |
| 122 | ILeveledLogOutput.class); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * {@inheritDoc} |
| 127 | */ |
| 128 | public ITestInvocationListener getTestInvocationListener() throws ConfigurationException { |
| 129 | return (ITestInvocationListener)getConfigurationObject(RESULT_REPORTER_NAME, |
| 130 | ITestInvocationListener.class); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * {@inheritDoc} |
| 135 | */ |
| 136 | public Object getConfigurationObject(String name, Class<?> expectedType) |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 137 | throws ConfigurationException { |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 138 | Object configObject = mConfigMap.get(name); |
| 139 | if (configObject == null) { |
| 140 | throw new ConfigurationException(String.format( |
| 141 | "Could not find config object with name %s", name)); |
| 142 | } else if (!expectedType.isInstance(configObject)) { |
| 143 | throw new ConfigurationException(String.format( |
| 144 | "The config object %s is not the correct type. Expected %s ", name, |
| 145 | expectedType.getCanonicalName())); |
| 146 | } |
| 147 | return configObject; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * {@inheritDoc} |
| 153 | */ |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 154 | public Collection<Object> getConfigurationObjects() { |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 155 | return mConfigMap.values(); |
| 156 | } |
Brett Chabot | 74121d8 | 2010-01-28 20:14:27 -0800 | [diff] [blame] | 157 | } |