1 /* 2 * Copyright (C) 2020 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.remote; 17 18 import com.android.tradefed.build.BuildRetrievalError; 19 import com.android.tradefed.invoker.tracing.CloseableTraceScope; 20 21 import java.io.File; 22 import java.util.concurrent.ExecutionException; 23 import java.util.concurrent.ExecutorService; 24 import java.util.concurrent.Future; 25 26 /** A extension of standard file to carry a build related metadata. */ 27 public class ExtendedFile extends File { 28 29 private String mBuildId; 30 private String mBuildTarget; 31 private String mBranch; 32 private String mRemoteFilePath; 33 34 private Future<BuildRetrievalError> mParallelDownload; 35 private ExecutorService mExecutorService; 36 ExtendedFile(String path)37 ExtendedFile(String path) { 38 super(path); 39 } 40 ExtendedFile(File file, String buildId, String buildTarget)41 public ExtendedFile(File file, String buildId, String buildTarget) { 42 super(file.getAbsolutePath()); 43 mBuildId = buildId; 44 mBuildTarget = buildTarget; 45 } 46 ExtendedFile(File file, String buildId, String buildTarget, String branch)47 public ExtendedFile(File file, String buildId, String buildTarget, String branch) { 48 this(file, buildId, buildTarget); 49 mBranch = branch; 50 } 51 ExtendedFile( File file, String buildId, String buildTarget, String branch, String remoteFilePath)52 public ExtendedFile( 53 File file, String buildId, String buildTarget, String branch, String remoteFilePath) { 54 this(file, buildId, buildTarget, branch); 55 mRemoteFilePath = remoteFilePath; 56 } 57 58 /** Returns the buildid metadata. */ getBuildId()59 public String getBuildId() { 60 return mBuildId; 61 } 62 63 /** Returns the target metadata. */ getBuildTarget()64 public String getBuildTarget() { 65 return mBuildTarget; 66 } 67 68 /** Returns the branch metadata. */ getBranch()69 public String getBranch() { 70 return mBranch; 71 } 72 73 /** Returns the remote file path metadata. */ getRemoteFilePath()74 public String getRemoteFilePath() { 75 return mRemoteFilePath; 76 } 77 setDownloadFuture(Future<BuildRetrievalError> download)78 public void setDownloadFuture(Future<BuildRetrievalError> download) { 79 mParallelDownload = download; 80 } 81 setDownloadFuture( ExecutorService serviceUsed, Future<BuildRetrievalError> download)82 public void setDownloadFuture( 83 ExecutorService serviceUsed, Future<BuildRetrievalError> download) { 84 mExecutorService = serviceUsed; 85 mParallelDownload = download; 86 } 87 cancelDownload()88 public void cancelDownload() { 89 if (!isDownloadingInParallel()) { 90 return; 91 } 92 try { 93 mParallelDownload.cancel(true); 94 } catch (RuntimeException ignored) { 95 // Ignore 96 } finally { 97 if (mExecutorService != null) { 98 mExecutorService.shutdown(); 99 } 100 } 101 } 102 waitForDownload()103 public void waitForDownload() throws BuildRetrievalError { 104 if (!isDownloadingInParallel()) { 105 return; 106 } 107 try (CloseableTraceScope ignored = new CloseableTraceScope("wait_for_" + mRemoteFilePath)) { 108 BuildRetrievalError error = mParallelDownload.get(); 109 if (error == null) { 110 return; 111 } 112 throw error; 113 } catch (ExecutionException | InterruptedException e) { 114 throw new BuildRetrievalError( 115 String.format("Error during parallel download: %s", e.getMessage()), e); 116 } finally { 117 if (mExecutorService != null) { 118 mExecutorService.shutdown(); 119 } 120 } 121 } 122 isDownloadingInParallel()123 public boolean isDownloadingInParallel() { 124 return mParallelDownload != null; 125 } 126 isDoneDownloadingInParallel()127 public boolean isDoneDownloadingInParallel() { 128 if (mParallelDownload == null) { 129 return true; 130 } 131 return mParallelDownload.isDone(); 132 } 133 } 134