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