|
3 | 3 | import okhttp3.Headers; |
4 | 4 | import org.slf4j.Logger; |
5 | 5 |
|
6 | | -import java.io.IOException; |
| 6 | +import java.io.InputStream; |
7 | 7 | import java.net.MalformedURLException; |
8 | 8 | import java.net.URL; |
9 | 9 | import java.time.Duration; |
10 | 10 | import java.util.Objects; |
| 11 | +import java.util.Properties; |
11 | 12 |
|
12 | 13 | final class FPContext { |
13 | 14 |
|
@@ -58,12 +59,7 @@ final class FPContext { |
58 | 59 | this.refreshInterval = config.refreshInterval; |
59 | 60 | this.location = config.location; |
60 | 61 | this.httpConfiguration = config.httpConfiguration; |
61 | | - String sdkVersion = DEFAULT_SDK_VERSION; |
62 | | - try { |
63 | | - sdkVersion = Utils.readSdkVersion(); |
64 | | - } catch (IOException e) { |
65 | | - logger.error("read sdk version error", e); |
66 | | - } |
| 62 | + String sdkVersion = getVersion(); |
67 | 63 | this.headers = config.httpConfiguration.headers.newBuilder() |
68 | 64 | .add(GET_SDK_KEY_HEADER, serverSdkKey) |
69 | 65 | .add(USER_AGENT_HEADER, SDK_FLAG_PREFIX + sdkVersion) |
@@ -97,4 +93,33 @@ public HttpConfiguration getHttpConfiguration() { |
97 | 93 | public Headers getHeaders() { |
98 | 94 | return headers; |
99 | 95 | } |
| 96 | + |
| 97 | + private synchronized String getVersion() { |
| 98 | + String version = null; |
| 99 | + try { |
| 100 | + Properties p = new Properties(); |
| 101 | + InputStream is = getClass() |
| 102 | + .getResourceAsStream("/META-INF/maven/com.featureprobe/server-sdk-java/pom.properties"); |
| 103 | + if (is != null) { |
| 104 | + p.load(is); |
| 105 | + version = p.getProperty("version", ""); |
| 106 | + } |
| 107 | + } catch (Exception e) { |
| 108 | + |
| 109 | + } |
| 110 | + if (version == null) { |
| 111 | + Package aPackage = getClass().getPackage(); |
| 112 | + if (aPackage != null) { |
| 113 | + version = aPackage.getImplementationVersion(); |
| 114 | + if (version == null) { |
| 115 | + version = aPackage.getSpecificationVersion(); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + if (version == null) { |
| 120 | + version = ""; |
| 121 | + } |
| 122 | + return version; |
| 123 | + } |
| 124 | + |
100 | 125 | } |
0 commit comments