|
20 | 20 | import okhttp3.Headers; |
21 | 21 | import org.slf4j.Logger; |
22 | 22 |
|
| 23 | +import java.io.IOException; |
23 | 24 | import java.io.InputStream; |
24 | 25 | import java.net.MalformedURLException; |
25 | 26 | import java.net.URI; |
|
29 | 30 | import java.util.Objects; |
30 | 31 | import java.util.Properties; |
31 | 32 |
|
32 | | -final class FPContext { |
| 33 | +class FPContext { |
33 | 34 |
|
34 | 35 | private static final Logger logger = Loggers.MAIN; |
35 | 36 |
|
@@ -81,19 +82,17 @@ final class FPContext { |
81 | 82 | this.realtimeUri = config.realtimeUri; |
82 | 83 | } |
83 | 84 | } catch (MalformedURLException e) { |
84 | | - logger.error("construction context error", e); |
| 85 | + logger.error("construction context error MalformedURLException"); |
85 | 86 | } catch (URISyntaxException e) { |
86 | | - logger.error("construction context error", e); |
| 87 | + logger.error("construction context error URISyntaxException"); |
87 | 88 | } |
88 | 89 | this.serverSdkKey = serverSdkKey; |
89 | 90 | this.refreshInterval = config.refreshInterval; |
90 | 91 | this.location = config.location; |
91 | 92 | this.httpConfiguration = config.httpConfiguration; |
92 | 93 | String sdkVersion = getVersion(); |
93 | | - this.headers = config.httpConfiguration.headers.newBuilder() |
94 | | - .add(GET_SDK_KEY_HEADER, serverSdkKey) |
95 | | - .add(USER_AGENT_HEADER, SDK_FLAG_PREFIX + sdkVersion) |
96 | | - .build(); |
| 94 | + this.headers = config.httpConfiguration.headers.newBuilder().add(GET_SDK_KEY_HEADER, serverSdkKey) |
| 95 | + .add(USER_AGENT_HEADER, SDK_FLAG_PREFIX + sdkVersion).build(); |
97 | 96 | } |
98 | 97 |
|
99 | 98 | public URL getSynchronizerUrl() { |
@@ -124,32 +123,41 @@ public Headers getHeaders() { |
124 | 123 | return headers; |
125 | 124 | } |
126 | 125 |
|
127 | | - private synchronized String getVersion() { |
128 | | - String version = null; |
129 | | - try { |
130 | | - Properties p = new Properties(); |
131 | | - InputStream is = getClass() |
132 | | - .getResourceAsStream("/META-INF/maven/com.featureprobe/server-sdk-java/pom.properties"); |
| 126 | + public synchronized String getVersion() { |
| 127 | + try ( |
| 128 | + InputStream is = |
| 129 | + getResourceAsStream("/META-INF/maven/com.featureprobe/server-sdk-java/pom.properties")) { |
133 | 130 | if (is != null) { |
| 131 | + Properties p = new Properties(); |
134 | 132 | p.load(is); |
135 | | - version = p.getProperty("version", DEFAULT_SDK_VERSION); |
| 133 | + return p.getProperty("version", DEFAULT_SDK_VERSION); |
136 | 134 | } |
137 | | - } catch (Exception e) { |
| 135 | + } catch (IOException e) { |
| 136 | + logger.error("get version error", e); |
| 137 | + } |
138 | 138 |
|
| 139 | + Package aPackage = getPackage(); |
| 140 | + if (aPackage == null) { |
| 141 | + return DEFAULT_SDK_VERSION; |
139 | 142 | } |
140 | | - if (version == null) { |
141 | | - Package aPackage = getClass().getPackage(); |
142 | | - if (aPackage != null) { |
143 | | - version = aPackage.getImplementationVersion(); |
144 | | - if (version == null) { |
145 | | - version = aPackage.getSpecificationVersion(); |
146 | | - } |
147 | | - } |
| 143 | + |
| 144 | + String version = aPackage.getImplementationVersion(); |
| 145 | + if (version != null) { |
| 146 | + return version; |
148 | 147 | } |
149 | | - if (version == null) { |
150 | | - version = DEFAULT_SDK_VERSION; |
| 148 | + version = aPackage.getSpecificationVersion(); |
| 149 | + if (version != null) { |
| 150 | + return version; |
151 | 151 | } |
152 | | - return version; |
| 152 | + return DEFAULT_SDK_VERSION; |
| 153 | + } |
| 154 | + |
| 155 | + protected Package getPackage() { |
| 156 | + return getClass().getPackage(); |
| 157 | + } |
| 158 | + |
| 159 | + protected InputStream getResourceAsStream(String resource) { |
| 160 | + return getClass().getResourceAsStream(resource); |
153 | 161 | } |
154 | 162 |
|
155 | 163 | public URI getRealtimeUri() { |
|
0 commit comments