Skip to content

Commit 716cbe9

Browse files
committed
[bugfix] implemented try-with-resources for instances of Closeable
1 parent 4af60cc commit 716cbe9

File tree

3 files changed

+94
-91
lines changed
  • dss-appconn/appconns/dss-datachecker-appconn/src/main/java/com/webank/wedatasphere/dss/appconn/datachecker/utils
  • dss-orchestrator/orchestrators/dss-workflow/dss-workflow-server/src/main/java/com/webank/wedatasphere/dss/workflow/io/input/impl

3 files changed

+94
-91
lines changed

dss-appconn/appconns/dss-datachecker-appconn/src/main/java/com/webank/wedatasphere/dss/appconn/datachecker/utils/HttpUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public static Response httpClientHandleBase(String actionUrl, RequestBody reques
5959

6060
public static String httpClientHandle(String actionUrl, RequestBody requestBody, Map<String, String> urlMap) throws Exception{
6161
String returnData = "";
62-
try {
63-
Response response = httpClientHandleBase(actionUrl, requestBody, urlMap);
62+
try (Response response = httpClientHandleBase(actionUrl, requestBody, urlMap)) {
6463
returnData = response.body().string();
6564
logger.info("mask interface return message:" + returnData);
6665
} catch (IOException e) {
@@ -80,8 +79,7 @@ public static String httpClientHandle(String actionUrl) throws Exception{
8079
.build();
8180
Call call = okHttpClient.newCall(request);
8281
String returnData = "";
83-
try {
84-
Response response = call.execute();
82+
try (Response response = call.execute()) {
8583
returnData = response.body().string();
8684
logger.info("interface return message:" + returnData);
8785
} catch (IOException e) {

dss-appconn/appconns/dss-datachecker-appconn/src/main/java/com/webank/wedatasphere/dss/appconn/datachecker/utils/QualitisUtil.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,24 @@ public String submitTask(long groupId, String createUser, String executionUser)
9494
.build();
9595

9696
Call call = okHttpClient.newCall(request);
97-
Response response = call.execute();
98-
String resultJson = response.body().string();
99-
logger.info("Response Json: " + resultJson);
100-
if (StringUtils.isNotEmpty(resultJson)) {
101-
Map<String, Object> resultMap = DSSCommonUtils.COMMON_GSON.fromJson(resultJson,Map.class);
102-
logger.info(String.valueOf(resultMap));
103-
String code = (String) resultMap.get("code");
104-
if ("200".equals(code)) {
105-
applicationId = (String) ((Map<String, Object>) resultMap.get("data")).get(
106-
"application_id");
107-
logger.info(resultMap.get("message").toString());
108-
} else {
109-
throw new RuntimeException(resultMap.get("message").toString());
97+
try (Response response = call.execute()) {
98+
String resultJson = response.body().string();
99+
logger.info("Response Json: " + resultJson);
100+
if (StringUtils.isNotEmpty(resultJson)) {
101+
Map<String, Object> resultMap = DSSCommonUtils.COMMON_GSON.fromJson(resultJson, Map.class);
102+
logger.info(String.valueOf(resultMap));
103+
String code = (String) resultMap.get("code");
104+
if ("200".equals(code)) {
105+
applicationId = (String) ((Map<String, Object>) resultMap.get("data")).get(
106+
"application_id");
107+
logger.info(resultMap.get("message").toString());
108+
return applicationId;
109+
} else {
110+
throw new RuntimeException(resultMap.get("message").toString());
111+
}
110112
}
111113
}
112-
return applicationId;
114+
return null;
113115
}
114116

115117
public String createAndSubmitRule(CheckDataObject dataObject,String projectName,String ruleName,String user) throws IOException {
@@ -161,24 +163,26 @@ public String createAndSubmitRule(CheckDataObject dataObject,String projectName,
161163
.build();
162164

163165
Call call = okHttpClient.newCall(request);
164-
Response response = call.execute();
165-
String resultJson = response.body().string();
166-
logger.info("call qualitis end,Response Json:{} " , resultJson);
167-
if (StringUtils.isNotEmpty(resultJson)) {
168-
Map<String, Object> resultMap = DSSCommonUtils.COMMON_GSON.fromJson(resultJson,Map.class);
169-
String code = (String) resultMap.get("code");
170-
if ("200".equals(code)) {
171-
Map<String, Object> applicationDetail = (Map<String, Object>) ((Map<String, Object>) (resultMap.get(
172-
"data"))).get("application_detail");
173-
Map<String, Object> project_application = ((List<Map<String, Object>>) (applicationDetail.get(
174-
"project_applications"))).get(0);
175-
applicationId = (String) project_application.get("application_id");
176-
logger.info(resultMap.get("message").toString());
177-
} else {
178-
throw new RuntimeException(resultMap.get("message").toString());
166+
try (Response response = call.execute()) {
167+
String resultJson = response.body().string();
168+
logger.info("call qualitis end,Response Json:{} ", resultJson);
169+
if (StringUtils.isNotEmpty(resultJson)) {
170+
Map<String, Object> resultMap = DSSCommonUtils.COMMON_GSON.fromJson(resultJson, Map.class);
171+
String code = (String) resultMap.get("code");
172+
if ("200".equals(code)) {
173+
Map<String, Object> applicationDetail = (Map<String, Object>) ((Map<String, Object>) (resultMap.get(
174+
"data"))).get("application_detail");
175+
Map<String, Object> project_application = ((List<Map<String, Object>>) (applicationDetail.get(
176+
"project_applications"))).get(0);
177+
applicationId = (String) project_application.get("application_id");
178+
logger.info(resultMap.get("message").toString());
179+
return applicationId;
180+
} else {
181+
throw new RuntimeException(resultMap.get("message").toString());
182+
}
179183
}
180184
}
181-
return applicationId;
185+
return null;
182186
}
183187

184188
/**
@@ -215,11 +219,9 @@ public String getTaskStatus(String applicationId){
215219
.build();
216220

217221
Call call = okHttpClient.newCall(request);
218-
Response response = null;
219222
String resultJson = "";
220223
String status = "";
221-
try {
222-
response = call.execute();
224+
try (Response response = call.execute()) {
223225
resultJson = response.body().string();
224226
logger.info("Response Json: " + resultJson);
225227
if (StringUtils.isNotEmpty(resultJson)) {

dss-orchestrator/orchestrators/dss-workflow/dss-workflow-server/src/main/java/com/webank/wedatasphere/dss/workflow/io/input/impl/NodeInputServiceImpl.java

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -144,34 +144,36 @@ public String uploadAppConnResourceNew(String userName, String projectName, DSSF
144144
logger.info("nodeResourcePath:{}", nodeResourcePath);
145145
File file = new File(nodeResourcePath);
146146
if (file.exists()) {
147-
InputStream resourceInputStream = bmlService.readLocalResourceFile(userName, nodeResourcePath);
148-
Supplier<Map<String, Object>> bmlResourceMap = () -> {
149-
BmlResource resource = bmlService.upload(userName, resourceInputStream, UUID.randomUUID().toString() + ".json",
150-
projectName);
151-
return ImmutableMap.of(
152-
"resourceId", resource.getResourceId(),
153-
"version", resource.getVersion()
154-
);
155-
};
156-
Supplier<Map<String, Object>> streamResourceMap = () -> MapUtils.newCommonMap(ImportRequestRef.INPUT_STREAM_KEY, resourceInputStream);
157-
try {
158-
nodeExportContent = nodeService.importNode(userName, appConnNode, bmlResourceMap, streamResourceMap, orcVersion);
159-
} catch (ExternalOperationFailedException e) {
160-
logger.error("failed to import node.", e);
161-
throw new DSSRuntimeException(e.getErrCode(), e.getMessage());
162-
} catch (Exception e) {
163-
logger.error("failed to import node.", e);
164-
throw new DSSRuntimeException(90011, e.getMessage());
165-
}
166-
if (nodeExportContent != null) {
167-
if (nodeExportContent.get("project_id") != null) {
168-
Long newProjectId = Long.parseLong(nodeExportContent.get("project_id").toString());
169-
logger.warn(String.format("new appConn node add into dss,dssProjectId: %s,newProjectId: %s", appConnNode.getProjectId(), newProjectId));
170-
nodeExportContent.remove("project_id");
147+
try (InputStream resourceInputStream = bmlService.readLocalResourceFile(userName, nodeResourcePath)) {
148+
Supplier<Map<String, Object>> bmlResourceMap = () -> {
149+
BmlResource resource = bmlService.upload(userName, resourceInputStream, UUID.randomUUID().toString() + ".json",
150+
projectName);
151+
return ImmutableMap.of(
152+
"resourceId", resource.getResourceId(),
153+
"version", resource.getVersion()
154+
);
155+
};
156+
157+
Supplier<Map<String, Object>> streamResourceMap = () -> MapUtils.newCommonMap(ImportRequestRef.INPUT_STREAM_KEY, resourceInputStream);
158+
try {
159+
nodeExportContent = nodeService.importNode(userName, appConnNode, bmlResourceMap, streamResourceMap, orcVersion);
160+
} catch (ExternalOperationFailedException e) {
161+
logger.error("failed to import node.", e);
162+
throw new DSSRuntimeException(e.getErrCode(), e.getMessage());
163+
} catch (Exception e) {
164+
logger.error("failed to import node.", e);
165+
throw new DSSRuntimeException(90011, e.getMessage());
166+
}
167+
if (nodeExportContent != null) {
168+
if (nodeExportContent.get("project_id") != null) {
169+
Long newProjectId = Long.parseLong(nodeExportContent.get("project_id").toString());
170+
logger.warn(String.format("new appConn node add into dss,dssProjectId: %s,newProjectId: %s", appConnNode.getProjectId(), newProjectId));
171+
nodeExportContent.remove("project_id");
172+
}
173+
nodeJsonMap.replace("jobContent", nodeExportContent);
174+
appConnNode.setJobContent(nodeExportContent);
175+
return BDPJettyServerHelper.jacksonJson().writeValueAsString(nodeJsonMap);
171176
}
172-
nodeJsonMap.replace("jobContent", nodeExportContent);
173-
appConnNode.setJobContent(nodeExportContent);
174-
return BDPJettyServerHelper.jacksonJson().writeValueAsString(nodeJsonMap);
175177
}
176178
} else {
177179
logger.warn("appConn node resource file does not exists. nodeId: {}" + nodeId);
@@ -209,34 +211,35 @@ public String uploadAppConnResource(String userName, String projectName, DSSFlow
209211
logger.info("nodeResourcePath:{}", nodeResourcePath);
210212
File file = new File(nodeResourcePath);
211213
if (file.exists()) {
212-
InputStream resourceInputStream = bmlService.readLocalResourceFile(userName, nodeResourcePath);
213-
Supplier<Map<String, Object>> bmlResourceMap = () -> {
214-
BmlResource resource = bmlService.upload(userName, resourceInputStream, UUID.randomUUID().toString() + ".json",
215-
projectName);
216-
return ImmutableMap.of(
217-
"resourceId", resource.getResourceId(),
218-
"version", resource.getVersion()
219-
);
220-
};
221-
Supplier<Map<String, Object>> streamResourceMap = () -> MapUtils.newCommonMap(ImportRequestRef.INPUT_STREAM_KEY, resourceInputStream);
222-
try {
223-
nodeExportContent = nodeService.importNode(userName, appConnNode, bmlResourceMap, streamResourceMap, orcVersion);
224-
} catch (ExternalOperationFailedException e) {
225-
logger.error("failed to import node.", e);
226-
throw new DSSRuntimeException(e.getErrCode(), e.getMessage());
227-
} catch (Exception e) {
228-
logger.error("failed to import node.", e);
229-
throw new DSSRuntimeException(90011, e.getMessage());
230-
}
231-
if (nodeExportContent != null) {
232-
if (nodeExportContent.get("project_id") != null) {
233-
Long newProjectId = Long.parseLong(nodeExportContent.get("project_id").toString());
234-
logger.warn(String.format("new appConn node add into dss,dssProjectId: %s,newProjectId: %s", appConnNode.getProjectId(), newProjectId));
235-
nodeExportContent.remove("project_id");
214+
try (InputStream resourceInputStream = bmlService.readLocalResourceFile(userName, nodeResourcePath)) {
215+
Supplier<Map<String, Object>> bmlResourceMap = () -> {
216+
BmlResource resource = bmlService.upload(userName, resourceInputStream, UUID.randomUUID().toString() + ".json",
217+
projectName);
218+
return ImmutableMap.of(
219+
"resourceId", resource.getResourceId(),
220+
"version", resource.getVersion()
221+
);
222+
};
223+
Supplier<Map<String, Object>> streamResourceMap = () -> MapUtils.newCommonMap(ImportRequestRef.INPUT_STREAM_KEY, resourceInputStream);
224+
try {
225+
nodeExportContent = nodeService.importNode(userName, appConnNode, bmlResourceMap, streamResourceMap, orcVersion);
226+
} catch (ExternalOperationFailedException e) {
227+
logger.error("failed to import node.", e);
228+
throw new DSSRuntimeException(e.getErrCode(), e.getMessage());
229+
} catch (Exception e) {
230+
logger.error("failed to import node.", e);
231+
throw new DSSRuntimeException(90011, e.getMessage());
232+
}
233+
if (nodeExportContent != null) {
234+
if (nodeExportContent.get("project_id") != null) {
235+
Long newProjectId = Long.parseLong(nodeExportContent.get("project_id").toString());
236+
logger.warn(String.format("new appConn node add into dss,dssProjectId: %s,newProjectId: %s", appConnNode.getProjectId(), newProjectId));
237+
nodeExportContent.remove("project_id");
238+
}
239+
nodeJsonMap.replace("jobContent", nodeExportContent);
240+
appConnNode.setJobContent(nodeExportContent);
241+
return BDPJettyServerHelper.jacksonJson().writeValueAsString(nodeJsonMap);
236242
}
237-
nodeJsonMap.replace("jobContent", nodeExportContent);
238-
appConnNode.setJobContent(nodeExportContent);
239-
return BDPJettyServerHelper.jacksonJson().writeValueAsString(nodeJsonMap);
240243
}
241244
} else {
242245
logger.warn("appConn node resource file does not exists. nodeId: {}" + nodeId);

0 commit comments

Comments
 (0)