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.config;
17 
18 import com.android.tradefed.log.LogUtil.CLog;
19 import com.android.tradefed.testtype.IRemoteTest;
20 import com.android.tradefed.testtype.suite.retry.RetryRescheduler;
21 
22 /** Factory that handles retrying a command. */
23 public class RetryConfigurationFactory extends ConfigurationFactory {
24 
25     private static RetryConfigurationFactory sInstance = null;
26 
27     /** Get the singleton {@link IConfigurationFactory} instance. */
getInstance()28     public static RetryConfigurationFactory getInstance() {
29         if (sInstance == null) {
30             sInstance = new RetryConfigurationFactory();
31         }
32         return sInstance;
33     }
34 
35     /** Create a retry configuration out of the retry runner. */
createRetryConfiguration(IConfiguration retryConfig)36     public IConfiguration createRetryConfiguration(IConfiguration retryConfig)
37             throws ConfigurationException {
38         if (retryConfig.getTests().size() != 1) {
39             throw new ConfigurationException(
40                     String.format(
41                             "%s should only have one runner inside it.", retryConfig.getName()));
42         }
43         IRemoteTest rerunner = retryConfig.getTests().get(0);
44         if (!(rerunner instanceof RetryRescheduler)) {
45             CLog.e("The runner inside the retry configuration is not a RetryRescheduler type.");
46             return retryConfig;
47         }
48         // Then use the retry runner to generate the configuration to actually run.
49         RetryRescheduler retryRunner = (RetryRescheduler) rerunner;
50         retryRunner.setConfiguration(retryConfig);
51         try {
52             retryRunner.run(null, null);
53             return retryRunner.getRetryConfiguration();
54         } catch (Throwable e) {
55             throw new ConfigurationException(e.getMessage(), e);
56         }
57     }
58 }
59