• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.util;
17 
18 import com.android.tradefed.log.LogUtil.CLog;
19 
20 import java.io.File;
21 import java.io.IOException;
22 
23 /** Fetch the version of the running tradefed artifacts. */
24 public class VersionParser {
25 
26     public static final String DEFAULT_IMPLEMENTATION_VERSION = "default";
27     private static final String VERSION_FILE = "version.txt";
28 
fetchVersion()29     public static String fetchVersion() {
30         return getPackageVersion();
31     }
32 
33     /**
34      * Version fetcher on the implementation version of the jar files from this class to ensure
35      * a match in the classloader.
36      */
getPackageVersion()37     private static String getPackageVersion() {
38         Package p = VersionParser.class.getPackage();
39         if (p != null) {
40             String packageVersion = p.getImplementationVersion();
41             if (packageVersion != null && !DEFAULT_IMPLEMENTATION_VERSION.equals(packageVersion)) {
42                 return packageVersion;
43             }
44         }
45         File dir = getTradefedJarDir();
46         if (dir != null) {
47             File versionFile = new File(dir, VERSION_FILE);
48             if (versionFile.exists()) {
49                 try {
50                     String version = FileUtil.readStringFromFile(versionFile);
51                     return version.trim();
52                 } catch (IOException e) {
53                     // Failed to fetch version.
54                     CLog.e(e);
55                 }
56             }
57             CLog.e("Did not find Version file in directory: %s", dir.getAbsolutePath());
58         }
59         return null;
60     }
61 
getTradefedJarDir()62     private static File getTradefedJarDir() {
63         try {
64             File f =
65                     new File(
66                             VersionParser.class
67                                     .getProtectionDomain()
68                                     .getCodeSource()
69                                     .getLocation()
70                                     .getPath());
71             return f.getParentFile();
72         } catch (Exception e) {
73             CLog.e("Failed to find TF JAR dir:");
74             CLog.e(e);
75         }
76         return null;
77     }
78 }
79