Skip to content

Commit d9b32d3

Browse files
authored
Merge pull request #2 from xelloss00x/main
[feature] 1.0.1.0 1. 优化了配置文件读取方式,支持读取jar包内配置文件 2. 提供厂商接口单线程执行的配置
2 parents c11fba6 + 148c401 commit d9b32d3

File tree

8 files changed

+197
-52
lines changed

8 files changed

+197
-52
lines changed

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/
34+
35+
*.DS_Store

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22

33
### 新增
44

5-
* OPPO、XM icon和图片上传接口实现
5+
* OPPO、XM icon和图片上传接口实现
6+
7+
[1.0.1.0] - 2021-01-04
8+
9+
* 优化了配置文件读取方式,支持读取jar包内配置文件
10+
* 提供厂商接口单线程执行的配置

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ GtSDK.moduleSet=oppo,xm
3434
## 判断文件是否相同的方式,支持sha1和fileName。如不配置默认使用fileName。(为了避免文件重复上传,文件上传后sdk会将厂商返回的链接缓存起来,下次从缓存里取。
3535
## 这里就是决定使用什么方式来判断文件是否重复。缓存的时效使用各厂商icon最大存储时效,由于目前使用的本地缓存,应用重启也会导致缓存清空)
3636
GtSDK.judgeFile=sha1
37+
## 多厂商接口调用是否默认使用多线程
38+
GtSDK.mThread=true
3739
## 接口调用超时等待时间,单位毫秒,默认500毫秒
3840
GtSDK.callTimeout=500
3941
## 以下是各厂商配置参数

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.getui.push</groupId>
88
<artifactId>getui-3rd-push-utils</artifactId>
9-
<version>1.0.0.0</version>
9+
<version>1.0.1.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>${project.groupId}:${project.artifactId}</name>

src/main/java/com/getui/gtps/GtSDKStarter.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9+
import java.io.FileNotFoundException;
910
import java.io.IOException;
11+
import java.io.InputStream;
1012

1113
/**
1214
* @author wangxu
@@ -31,13 +33,17 @@ public static GtSDKStarter getInstance() {
3133
/**
3234
* 加载配置文件
3335
*
34-
* @param fileName 配置文件名,用户目录下的相对路径
36+
* @param fileName 配置文件名,当前方法调用者类的ClassLoader所在路径下的配置文件
3537
* @return GtSDKStarter
3638
* @throws IOException 加载配置文件时可能会有IOException
3739
*/
38-
public GtSDKStarter loadPropertyFile(String fileName) throws IOException {
39-
CommonConfig.manufacturerProperties.load(new java.io.FileInputStream(System.getProperty("user.dir") + fileName));
40-
return this;
40+
public GtSDKStarter loadPropertyFile(String fileName) throws ClassNotFoundException, IOException {
41+
InputStream inputStream = Class.forName(Thread.currentThread().getStackTrace()[2].getClassName()).getClassLoader().getResourceAsStream(fileName);
42+
if (inputStream != null) {
43+
CommonConfig.manufacturerProperties.load(Class.forName(Thread.currentThread().getStackTrace()[2].getClassName()).getClassLoader().getResourceAsStream(fileName));
44+
return this;
45+
}
46+
throw new FileNotFoundException(fileName);
4147
}
4248

4349
/**
@@ -54,5 +60,4 @@ public void init() {
5460
LOGGER.info("GT SDKInit finish.");
5561
}
5662

57-
5863
}

src/main/java/com/getui/gtps/config/CommonConfig.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Collections;
44
import java.util.HashSet;
5+
import java.util.Objects;
56
import java.util.Properties;
67
import java.util.Set;
78
import java.util.stream.Collectors;
@@ -37,6 +38,11 @@ public class CommonConfig {
3738
*/
3839
public static String sameFileJudgePattern = ByName;
3940

41+
/**
42+
* 多厂商接口调用默认是否多线程
43+
*/
44+
public static boolean mThread = true;
45+
4046
/**
4147
* 接口调用超时等待时间,单位毫秒,默认500毫秒
4248
*/
@@ -50,6 +56,10 @@ public class CommonConfig {
5056
public static Properties manufacturerProperties = new Properties();
5157

5258
/* 以下是sdk参数的初始化 */
59+
60+
/**
61+
* 以下是sdk参数的初始化
62+
*/
5363
public static void init() {
5464
initPropertiesByFile();
5565
}
@@ -58,15 +68,16 @@ public static void init() {
5868
* 按照配置文件进行参数初始化
5969
*/
6070
private static void initPropertiesByFile() {
61-
CommonConfig.manufacturerInitSwitch = Boolean.parseBoolean(manufacturerProperties.getProperty(ManufacturerInitSwitch));
71+
CommonConfig.manufacturerInitSwitch = !Objects.equals(false, Boolean.parseBoolean(manufacturerProperties.getProperty(ManufacturerInitSwitch, "true")));
6272
String moduleString = manufacturerProperties.getProperty(ModuleSet);
6373
if (moduleString != null) {
6474
String[] modules = moduleString.split(",");
6575
CommonConfig.moduleSet.remove(AllModule);
6676
Collections.addAll(CommonConfig.moduleSet, modules);
6777
}
6878
CommonConfig.sameFileJudgePattern = BySha1.equalsIgnoreCase(manufacturerProperties.getProperty(JudgeFile)) ? BySha1 : ByName;
69-
CommonConfig.callTimeout = Integer.valueOf((String) manufacturerProperties.getOrDefault(CallTimeout, String.valueOf(CommonConfig.callTimeout)));
79+
CommonConfig.mThread = !Objects.equals(false, Boolean.parseBoolean(manufacturerProperties.getProperty(MThread, "true")));
80+
CommonConfig.callTimeout = Integer.parseInt((String) manufacturerProperties.getOrDefault(CallTimeout, String.valueOf(CommonConfig.callTimeout)));
7081
}
7182

7283
}

src/main/java/com/getui/gtps/config/GtSDKConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static class FilePropertyName {
2424
public final static String ManufacturerInitSwitch = Prefix.GtSDK + "manufacturerInitSwitch";
2525
public final static String ModuleSet = Prefix.GtSDK + "moduleSet";
2626
public final static String JudgeFile = Prefix.GtSDK + "judgeFile";
27+
public final static String MThread = Prefix.GtSDK + "mThread";
2728
public final static String CallTimeout = Prefix.GtSDK + "callTimeout";
2829
}
2930

src/main/java/com/getui/gtps/manufacturer/ManufacturerFactory.java

Lines changed: 129 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -115,105 +115,191 @@ private static void initAuth() {
115115
}
116116

117117
/**
118-
* 多厂商icon上传,多厂商上传同一个icon文件
118+
* 多厂商icon上传,多厂商上传同一个icon文件,多线程执行
119119
*
120120
* @param file 本地icon文件
121121
* @return 多厂商icon上传结果
122122
* @throws FileNotFoundException 本地icon文件找不到
123123
*/
124124
public static Map<String, Result> uploadIcon(File file) throws FileNotFoundException {
125+
return uploadIcon(CommonConfig.mThread, file);
126+
}
127+
128+
/**
129+
* 多厂商icon上传,多厂商上传同一个icon文件
130+
*
131+
* @param mThread 是否使用多线程
132+
* @param file 本地icon文件
133+
* @return 多厂商icon上传结果
134+
* @throws FileNotFoundException 本地icon文件找不到
135+
*/
136+
public static Map<String, Result> uploadIcon(boolean mThread, File file) throws FileNotFoundException {
125137
if (!file.exists()) {
126138
throw new FileNotFoundException(file.getAbsolutePath());
127139
}
128140
init();
129-
Map<String, Result> result = new HashMap<>();
141+
Map<String, Result> result = new HashMap<>(factory.size());
130142
if (factory.size() > 0) {
131-
Map<String, CompletableFuture<Result>> futures = factory.entrySet().stream()
132-
.collect(Collectors.toMap(
133-
Map.Entry::getKey,
134-
e -> CompletableFuture.supplyAsync(() -> e.getValue().uploadIcon(file), myExecutor)));
143+
if (mThread) {
144+
Map<String, CompletableFuture<Result>> futures = factory.entrySet().stream()
145+
.collect(Collectors.toMap(
146+
Map.Entry::getKey,
147+
e -> CompletableFuture.supplyAsync(() -> e.getValue().uploadIcon(file), myExecutor)));
135148

136-
getFutureResult(result, futures, Thread.currentThread().getStackTrace()[1].getMethodName());
149+
getFutureResult(result, futures, Thread.currentThread().getStackTrace()[1].getMethodName());
150+
} else {
151+
factory.forEach((k, v) -> {
152+
try {
153+
result.put(k, v.uploadIcon(file));
154+
} catch (AuthFailedException e) {
155+
result.put(k, Result.authFail());
156+
}
157+
});
158+
}
137159
}
138160
return result;
139161
}
140162

141163
/**
142-
* 多厂商icon上传,指定每个厂商上传的icon文件
164+
* 多厂商icon上传,指定每个厂商上传的icon文件,多线程执行
143165
*
144166
* @param manufacturerFile icon文件
145167
* @return 多厂商icon上传结果
146168
*/
147169
public static Map<String, Result> uploadIcon(ManufacturerFile... manufacturerFile) {
170+
return uploadIcon(CommonConfig.mThread, manufacturerFile);
171+
}
172+
173+
/**
174+
* 多厂商icon上传,指定每个厂商上传的icon文件
175+
*
176+
* @param mThread 是否使用多线程
177+
* @param manufacturerFile icon文件
178+
* @return 多厂商icon上传结果
179+
*/
180+
public static Map<String, Result> uploadIcon(boolean mThread, ManufacturerFile... manufacturerFile) {
148181
init();
149-
Map<String, Result> result = new HashMap<>();
182+
Map<String, Result> result = new HashMap<>(manufacturerFile.length);
150183
if (factory.size() > 0) {
184+
if (mThread) {
185+
Function<ManufacturerFile, CompletableFuture<Result>> valueMapper = file -> {
186+
if (!file.exists()) {
187+
return CompletableFuture.supplyAsync(() -> Result.fail(String.format("file %s not found", file.getAbsolutePath())), myExecutor);
188+
}
189+
Optional<BaseManufacturer> optional = Optional.ofNullable(factory.get(file.getManufacturerName()));
190+
return optional.map(baseManufacturer -> CompletableFuture.supplyAsync(() -> baseManufacturer.uploadIcon(file), myExecutor))
191+
.orElseGet(() -> CompletableFuture.supplyAsync(Result::noInstance, myExecutor));
192+
};
151193

152-
Function<ManufacturerFile, CompletableFuture<Result>> valueMapper = file -> {
153-
if (!file.exists()) {
154-
return CompletableFuture.supplyAsync(() -> Result.fail(String.format("file %s not found", file.getAbsolutePath())), myExecutor);
155-
}
156-
Optional<BaseManufacturer> optional = Optional.ofNullable(factory.get(file.getManufacturerName()));
157-
return optional.map(baseManufacturer -> CompletableFuture.supplyAsync(() -> baseManufacturer.uploadIcon(file), myExecutor))
158-
.orElseGet(() -> CompletableFuture.supplyAsync(Result::noInstance, myExecutor));
159-
};
194+
Map<String, CompletableFuture<Result>> futures = Arrays.stream(manufacturerFile)
195+
.collect(Collectors.toMap(ManufacturerFile::getManufacturerName, valueMapper));
160196

161-
Map<String, CompletableFuture<Result>> futures = Arrays.stream(manufacturerFile)
162-
.collect(Collectors.toMap(ManufacturerFile::getManufacturerName, valueMapper));
163-
164-
getFutureResult(result, futures, Thread.currentThread().getStackTrace()[1].getMethodName());
197+
getFutureResult(result, futures, Thread.currentThread().getStackTrace()[1].getMethodName());
198+
} else {
199+
Arrays.stream(manufacturerFile).forEach(file -> {
200+
BaseManufacturer manufacturer = factory.get(file.getManufacturerName());
201+
try {
202+
result.put(file.getManufacturerName(), manufacturer.uploadIcon(file));
203+
} catch (AuthFailedException e) {
204+
result.put(file.getManufacturerName(), Result.authFail());
205+
}
206+
});
207+
}
165208
}
166209
return result;
167210
}
168211

169212
/**
170-
* 多厂商图片上传,多厂商上传同一个图片文件
213+
* 多厂商图片上传,多厂商上传同一个图片文件,多线程执行
171214
*
172215
* @param file 本地图片文件
173216
* @return 多厂商图片上传结果
174-
* @throws FileNotFoundException 本地icon文件找不到
217+
* @throws FileNotFoundException 本地图片文件找不到
175218
*/
176219
public static Map<String, Result> uploadPic(File file) throws FileNotFoundException {
220+
return uploadPic(CommonConfig.mThread, file);
221+
}
222+
223+
/**
224+
* 多厂商图片上传,多厂商上传同一个图片文件
225+
*
226+
* @param mThread 是否使用多线程
227+
* @param file 本地图片文件
228+
* @return 多厂商图片上传结果
229+
* @throws FileNotFoundException 本地图片文件找不到
230+
*/
231+
public static Map<String, Result> uploadPic(boolean mThread, File file) throws FileNotFoundException {
177232
if (!file.exists()) {
178233
throw new FileNotFoundException(file.getAbsolutePath());
179234
}
180235
init();
181-
Map<String, Result> result = new HashMap<>();
236+
Map<String, Result> result = new HashMap<>(factory.size());
182237
if (factory.size() > 0) {
183-
Map<String, CompletableFuture<Result>> futures = factory.entrySet().stream()
184-
.collect(Collectors.toMap(
185-
Map.Entry::getKey,
186-
e -> CompletableFuture.supplyAsync(() -> e.getValue().uploadPic(file), myExecutor)));
238+
if (mThread) {
239+
Map<String, CompletableFuture<Result>> futures = factory.entrySet().stream()
240+
.collect(Collectors.toMap(
241+
Map.Entry::getKey,
242+
e -> CompletableFuture.supplyAsync(() -> e.getValue().uploadPic(file), myExecutor)));
187243

188-
getFutureResult(result, futures, Thread.currentThread().getStackTrace()[1].getMethodName());
244+
getFutureResult(result, futures, Thread.currentThread().getStackTrace()[1].getMethodName());
245+
} else {
246+
factory.forEach((k, v) -> {
247+
try {
248+
result.put(k, v.uploadPic(file));
249+
} catch (AuthFailedException e) {
250+
result.put(k, Result.authFail());
251+
}
252+
});
253+
}
189254
}
190255
return result;
191256
}
192257

193258
/**
194-
* 多厂商图片上传,指定每个厂商上传的图片文件
259+
* 多厂商图片上传,指定每个厂商上传的图片文件,多线程执行
195260
*
196261
* @param manufacturerFile 图片文件
197262
* @return 多厂商图片上传结果
198263
*/
199264
public static Map<String, Result> uploadPic(ManufacturerFile... manufacturerFile) {
265+
return uploadPic(CommonConfig.mThread, manufacturerFile);
266+
}
267+
268+
/**
269+
* 多厂商图片上传,指定每个厂商上传的图片文件
270+
*
271+
* @param mThread 是否使用多线程
272+
* @param manufacturerFile 图片文件
273+
* @return 多厂商图片上传结果
274+
*/
275+
public static Map<String, Result> uploadPic(boolean mThread, ManufacturerFile... manufacturerFile) {
200276
init();
201-
Map<String, Result> result = new HashMap<>();
277+
Map<String, Result> result = new HashMap<>(manufacturerFile.length);
202278
if (factory.size() > 0) {
279+
if (mThread) {
280+
Function<ManufacturerFile, CompletableFuture<Result>> valueMapper = file -> {
281+
if (!file.exists()) {
282+
return CompletableFuture.supplyAsync(() -> Result.fail(String.format("file %s not found", file.getAbsolutePath())), myExecutor);
283+
}
284+
Optional<BaseManufacturer> optional = Optional.ofNullable(factory.get(file.getManufacturerName()));
285+
return optional.map(baseManufacturer -> CompletableFuture.supplyAsync(() -> baseManufacturer.uploadPic(file), myExecutor))
286+
.orElseGet(() -> CompletableFuture.supplyAsync(Result::noInstance, myExecutor));
287+
};
203288

204-
Function<ManufacturerFile, CompletableFuture<Result>> valueMapper = file -> {
205-
if (!file.exists()) {
206-
return CompletableFuture.supplyAsync(() -> Result.fail(String.format("file %s not found", file.getAbsolutePath())), myExecutor);
207-
}
208-
Optional<BaseManufacturer> optional = Optional.ofNullable(factory.get(file.getManufacturerName()));
209-
return optional.map(baseManufacturer -> CompletableFuture.supplyAsync(() -> baseManufacturer.uploadPic(file), myExecutor))
210-
.orElseGet(() -> CompletableFuture.supplyAsync(Result::noInstance, myExecutor));
211-
};
289+
Map<String, CompletableFuture<Result>> futures = Arrays.stream(manufacturerFile)
290+
.collect(Collectors.toMap(ManufacturerFile::getManufacturerName, valueMapper));
212291

213-
Map<String, CompletableFuture<Result>> futures = Arrays.stream(manufacturerFile)
214-
.collect(Collectors.toMap(ManufacturerFile::getManufacturerName, valueMapper));
215-
216-
getFutureResult(result, futures, Thread.currentThread().getStackTrace()[1].getMethodName());
292+
getFutureResult(result, futures, Thread.currentThread().getStackTrace()[1].getMethodName());
293+
} else {
294+
Arrays.stream(manufacturerFile).forEach(file -> {
295+
BaseManufacturer manufacturer = factory.get(file.getManufacturerName());
296+
try {
297+
result.put(file.getManufacturerName(), manufacturer.uploadPic(file));
298+
} catch (AuthFailedException e) {
299+
result.put(file.getManufacturerName(), Result.authFail());
300+
}
301+
});
302+
}
217303
}
218304
return result;
219305
}

0 commit comments

Comments
 (0)