blob: d32aed578308b8789d6ab8fc541d7488d316a14f [file] [log] [blame]
Brett Chabot74121d82010-01-28 20:14:27 -08001/*
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 */
16package com.android.tradefed.config;
17
18import com.android.tradefed.device.IDeviceRecovery;
19import com.android.tradefed.log.ILeveledLogOutput;
20import com.android.tradefed.result.ITestInvocationListener;
21import com.android.tradefed.targetsetup.IBuildProvider;
22import com.android.tradefed.targetsetup.ITargetPreparer;
23
Brett Chabotccdf3272010-02-27 18:59:03 -080024import java.util.Collection;
Brett Chabote278a5b2010-06-01 16:20:14 -070025import java.util.HashSet;
Brett Chabot74121d82010-01-28 20:14:27 -080026import java.util.Hashtable;
27import java.util.Map;
Brett Chabote278a5b2010-06-01 16:20:14 -070028import java.util.Set;
Brett Chabot74121d82010-01-28 20:14:27 -080029
30import junit.framework.Test;
31
32/**
Brett Chabote278a5b2010-06-01 16:20:14 -070033 * A concrete {@link IConfiguration} implementation that stores the loaded config objects in a map
Brett Chabot74121d82010-01-28 20:14:27 -080034 */
Brett Chabote278a5b2010-06-01 16:20:14 -070035// TODO: make package-private
36public class Configuration implements IConfiguration {
Brett Chabot74121d82010-01-28 20:14:27 -080037
38 // names for built in configuration objects
Brett Chabotc819fc12010-05-04 22:41:05 -070039 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 Chabot74121d82010-01-28 20:14:27 -080045
Brett Chabote278a5b2010-06-01 16:20:14 -070046 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 Chabot74121d82010-01-28 20:14:27 -080061 /** Mapping of config object name to config object. */
62 private Map<String, Object> mConfigMap;
63
Brett Chabote278a5b2010-06-01 16:20:14 -070064 protected Configuration() {
Brett Chabot74121d82010-01-28 20:14:27 -080065 mConfigMap = new Hashtable<String, Object>();
66 }
67
68 /**
Brett Chabote278a5b2010-06-01 16:20:14 -070069 * @param configObjects
70 */
71 public Configuration(Map<String, Object> configObjects) {
72 mConfigMap = configObjects;
73 }
74
75 /**
Brett Chabot74121d82010-01-28 20:14:27 -080076 * 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 Chabotc819fc12010-05-04 22:41:05 -070081 protected void addObject(String name, Object configObject) {
Brett Chabot74121d82010-01-28 20:14:27 -080082 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 Chabote278a5b2010-06-01 16:20:14 -0700137 throws ConfigurationException {
Brett Chabot74121d82010-01-28 20:14:27 -0800138 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 Chabote278a5b2010-06-01 16:20:14 -0700154 public Collection<Object> getConfigurationObjects() {
Brett Chabotccdf3272010-02-27 18:59:03 -0800155 return mConfigMap.values();
156 }
Brett Chabot74121d82010-01-28 20:14:27 -0800157}