Skip to content

Commit 9c45ca0

Browse files
committed
[Feature-15819][Task Plugin]Update the JAVA task
1 parent a3ce4a0 commit 9c45ca0

File tree

8 files changed

+216
-335
lines changed

8 files changed

+216
-335
lines changed

dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaConstants.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ private JavaConstants() {
2929
public static final String JAVA_HOME_VAR = "${JAVA_HOME}";
3030

3131
/**
32-
* this constant represents the use of the java command to run a task
32+
* This constant represents the use of the java -jar command to run a task
3333
**/
34-
public static final String RUN_TYPE_JAVA = "JAVA";
34+
public static final String RUN_TYPE_FAT_JAR = "FATJAR";
3535

3636
/**
37-
* this constant represents the use of the java -jar command to run a task
37+
* This constant represents the use of the java -cp command to run a task
3838
**/
39-
public static final String RUN_TYPE_JAR = "JAR";
39+
public static final String RUN_TYPE_NORMAL_JAR = "NORMALJAR";
4040

4141
/**
4242
* This constant is the Classpath or module path delimiter for different operating systems
@@ -48,13 +48,4 @@ private JavaConstants() {
4848
**/
4949
public static final String CLASSPATH_CURRENT_DIR = ".";
5050

51-
/**
52-
* This constant is used to construct the pre-pathname of the Java source file
53-
**/
54-
public static final String JAVA_SOURCE_CODE_NAME_TEMPLATE = "%s/%s.java";
55-
56-
/**
57-
* This constant is the regular expression to get the class name of the source file
58-
**/
59-
public static final String PUBLIC_CLASS_NAME_REGEX = "(.*\\s*public\\s+class\\s+)([a-zA-Z_]+[//w_]*)([.\\s\\S]*)";
6051
}

dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaParameters.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,13 @@
2020
import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo;
2121
import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
2222

23-
import org.apache.commons.lang3.StringUtils;
24-
2523
import java.util.List;
2624

2725
import lombok.Data;
2826

2927
@Data
3028
public class JavaParameters extends AbstractParameters {
3129

32-
/**
33-
* origin java script
34-
*/
35-
private String rawScript;
36-
3730
/**
3831
* run in jar file
3932
*/
@@ -67,11 +60,12 @@ public class JavaParameters extends AbstractParameters {
6760
/**
6861
* Check that the parameters are valid
6962
*
70-
* @returnboolean
63+
* @return boolean
7164
*/
7265
@Override
7366
public boolean checkParameters() {
74-
return runType != null && (StringUtils.isNotBlank(rawScript) || mainJar != null);
67+
return runType != null && (runType.equals(JavaConstants.RUN_TYPE_FAT_JAR) ||
68+
runType.equals(JavaConstants.RUN_TYPE_NORMAL_JAR));
7569
}
7670

7771
/**

dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaTask.java

Lines changed: 35 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.apache.dolphinscheduler.plugin.task.java;
1919

2020
import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.JAVA_HOME_VAR;
21-
import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.PUBLIC_CLASS_NAME_REGEX;
2221

2322
import org.apache.dolphinscheduler.common.utils.JSONUtils;
2423
import org.apache.dolphinscheduler.plugin.task.api.AbstractTask;
@@ -27,35 +26,19 @@
2726
import org.apache.dolphinscheduler.plugin.task.api.TaskConstants;
2827
import org.apache.dolphinscheduler.plugin.task.api.TaskException;
2928
import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
30-
import org.apache.dolphinscheduler.plugin.task.api.model.Property;
3129
import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo;
3230
import org.apache.dolphinscheduler.plugin.task.api.model.TaskResponse;
3331
import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
3432
import org.apache.dolphinscheduler.plugin.task.api.resource.ResourceContext;
3533
import org.apache.dolphinscheduler.plugin.task.api.shell.IShellInterceptorBuilder;
3634
import org.apache.dolphinscheduler.plugin.task.api.shell.ShellInterceptorBuilderFactory;
37-
import org.apache.dolphinscheduler.plugin.task.api.utils.MapUtils;
38-
import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils;
39-
import org.apache.dolphinscheduler.plugin.task.java.exception.JavaSourceFileExistException;
40-
import org.apache.dolphinscheduler.plugin.task.java.exception.PublicClassNotFoundException;
4135
import org.apache.dolphinscheduler.plugin.task.java.exception.RunTypeNotFoundException;
4236

43-
import org.apache.commons.io.FileUtils;
44-
4537
import java.io.File;
46-
import java.io.IOException;
47-
import java.nio.charset.StandardCharsets;
48-
import java.nio.file.Files;
49-
import java.nio.file.Paths;
50-
import java.util.HashMap;
51-
import java.util.Map;
52-
import java.util.regex.Matcher;
53-
import java.util.regex.Pattern;
54-
5538
import lombok.extern.slf4j.Slf4j;
56-
5739
import com.google.common.base.Preconditions;
5840

41+
5942
@Slf4j
6043
public class JavaTask extends AbstractTask {
6144

@@ -74,10 +57,6 @@ public class JavaTask extends AbstractTask {
7457
*/
7558
private TaskExecutionContext taskRequest;
7659

77-
/**
78-
* class name regex pattern
79-
*/
80-
private static final Pattern classNamePattern = Pattern.compile(PUBLIC_CLASS_NAME_REGEX);
8160

8261
public JavaTask(TaskExecutionContext taskRequest) {
8362
super(taskRequest);
@@ -109,19 +88,20 @@ public void init() {
10988
public void handle(TaskCallBack taskCallBack) throws TaskException {
11089
try {
11190
// Step 1: judge if is java or jar run type.
112-
// Step 2 case1: the jar run type builds the command directly, adding resource to the java -jar class when
91+
// Step 2 case1: the fat jar run type builds the command directly, adding resource to the java -jar class when
92+
// building the command
93+
// Step 2 case2: the normal jar run type builds the command directly, adding resource to the java -cp class when
11394
// building the command
114-
// Step 2 case2: the java run type, first replace the custom parameters, then compile the code, and then
115-
// build the command will add resource
11695
// Step 3: to run the command
11796
String command = null;
11897
switch (javaParameters.getRunType()) {
119-
case JavaConstants.RUN_TYPE_JAVA:
120-
command = buildJavaCommand();
121-
break;
122-
case JavaConstants.RUN_TYPE_JAR:
98+
99+
case JavaConstants.RUN_TYPE_FAT_JAR:
123100
command = buildJarCommand();
124101
break;
102+
case JavaConstants.RUN_TYPE_NORMAL_JAR:
103+
command = buildNormalJarCommand();
104+
break;
125105
default:
126106
throw new RunTypeNotFoundException("run type is required, but it is null now.");
127107
}
@@ -149,44 +129,52 @@ public void handle(TaskCallBack taskCallBack) throws TaskException {
149129
}
150130
}
151131

132+
152133
/**
153-
* Construct a shell command for the java Run mode
134+
* Construct a shell command for the java -jar Run mode
154135
*
155136
* @return String
156-
* @throws Exception
157137
**/
158-
protected String buildJavaCommand() throws Exception {
138+
protected String buildJarCommand() {
139+
ResourceContext resourceContext = taskRequest.getResourceContext();
140+
String mainJarAbsolutePathInLocal = resourceContext
141+
.getResourceItem(javaParameters.getMainJar().getResourceName())
142+
.getResourceAbsolutePathInLocal();
159143
StringBuilder builder = new StringBuilder();
160-
String sourceCode = buildJavaSourceContent();
161-
builder.append(buildJavaCompileCommand(sourceCode))
162-
.append(";")
163-
.append(getJavaCommandPath())
144+
builder.append(getJavaCommandPath())
164145
.append("java").append(" ")
165-
.append(buildResourcePath())
166-
.append(" ")
167-
.append(getPublicClassName(sourceCode))
168-
.append(" ")
146+
.append(buildResourcePath()).append(" ")
147+
.append("-jar").append(" ")
148+
.append(mainJarAbsolutePathInLocal).append(" ")
169149
.append(javaParameters.getMainArgs().trim()).append(" ")
170150
.append(javaParameters.getJvmArgs().trim());
171151
return builder.toString();
172152
}
173153

154+
174155
/**
175-
* Construct a shell command for the java -jar Run mode
156+
* Construct a shell command for the java -cp run mode
176157
*
177158
* @return String
178159
**/
179-
protected String buildJarCommand() {
160+
protected String buildNormalJarCommand() {
180161
ResourceContext resourceContext = taskRequest.getResourceContext();
181-
String mainJarAbsolutePathInLocal = resourceContext
182-
.getResourceItem(javaParameters.getMainJar().getResourceName())
162+
String mainJarAbsolutePathInLocal = resourceContext.getResourceItem(
163+
javaParameters.getMainJar()
164+
.getResourceName())
183165
.getResourceAbsolutePathInLocal();
166+
String mainJarName=null;
167+
try{
168+
mainJarName = MainClassExtractor.getMainClassName(mainJarAbsolutePathInLocal);
169+
}
170+
catch (Exception e){
171+
e.printStackTrace();
172+
}
184173
StringBuilder builder = new StringBuilder();
185174
builder.append(getJavaCommandPath())
186175
.append("java").append(" ")
187176
.append(buildResourcePath()).append(" ")
188-
.append("-jar").append(" ")
189-
.append(mainJarAbsolutePathInLocal).append(" ")
177+
.append(mainJarName).append(" ")
190178
.append(javaParameters.getMainArgs().trim()).append(" ")
191179
.append(javaParameters.getJvmArgs().trim());
192180
return builder.toString();
@@ -207,35 +195,6 @@ public AbstractParameters getParameters() {
207195
return javaParameters;
208196
}
209197

210-
/**
211-
* Creates a Java source file when it does not exist
212-
*
213-
* @param sourceCode
214-
* @param fileName
215-
* @return String
216-
**/
217-
protected void createJavaSourceFileIfNotExists(String sourceCode, String fileName) throws IOException {
218-
log.info("tenantCode: {}, task dir:{}", taskRequest.getTenantCode(), taskRequest.getExecutePath());
219-
if (!Files.exists(Paths.get(fileName))) {
220-
log.info("the java source code:{}, will be write to the file: {}", fileName, sourceCode);
221-
// write data to file
222-
FileUtils.writeStringToFile(new File(fileName),
223-
sourceCode,
224-
StandardCharsets.UTF_8);
225-
} else {
226-
throw new JavaSourceFileExistException("java source file exists, please report an issue on official.");
227-
}
228-
}
229-
230-
/**
231-
* Construct the full path name of the Java source file from the temporary execution path of the task
232-
*
233-
* @return String
234-
**/
235-
protected String buildJavaSourceCodeFileFullName(String publicClassName) {
236-
return String.format(JavaConstants.JAVA_SOURCE_CODE_NAME_TEMPLATE, taskRequest.getExecutePath(),
237-
publicClassName);
238-
}
239198

240199
/**
241200
* Construct a Classpath or module path based on isModulePath
@@ -262,46 +221,6 @@ protected String buildResourcePath() {
262221
return builder.toString();
263222
}
264223

265-
/**
266-
* Constructs a shell command compiled from a Java source file
267-
*
268-
* @param sourceCode
269-
* @return String
270-
* @throws IOException
271-
**/
272-
protected String buildJavaCompileCommand(String sourceCode) throws IOException {
273-
String publicClassName = getPublicClassName(sourceCode);
274-
String fileName = buildJavaSourceCodeFileFullName(publicClassName);
275-
createJavaSourceFileIfNotExists(sourceCode, fileName);
276-
277-
StringBuilder compilerCommand = new StringBuilder()
278-
.append(getJavaCommandPath())
279-
.append("javac").append(" ")
280-
.append(buildResourcePath()).append(" ")
281-
.append(fileName);
282-
return compilerCommand.toString();
283-
}
284-
285-
/**
286-
* Work with Java source file content, such as replacing local variables
287-
*
288-
* @return String
289-
**/
290-
protected String buildJavaSourceContent() {
291-
String rawJavaScript = javaParameters.getRawScript().replaceAll("\\r\\n", "\n");
292-
// replace placeholder
293-
294-
Map<String, Property> paramsMap = taskRequest.getPrepareParamsMap();
295-
if (MapUtils.isEmpty(paramsMap)) {
296-
paramsMap = new HashMap<>();
297-
}
298-
if (MapUtils.isNotEmpty(taskRequest.getParamsMap())) {
299-
paramsMap.putAll(taskRequest.getParamsMap());
300-
}
301-
log.info("The current java source code will begin to replace the placeholder: {}", rawJavaScript);
302-
rawJavaScript = ParameterUtils.convertParameterPlaceholders(rawJavaScript, ParameterUtils.convert(paramsMap));
303-
return rawJavaScript;
304-
}
305224

306225
/**
307226
* Gets the operating system absolute path to the Java command
@@ -312,17 +231,5 @@ private String getJavaCommandPath() {
312231
return JAVA_HOME_VAR + File.separator + "bin" + File.separator;
313232
}
314233

315-
/**
316-
* Gets the public class name from the Java source file
317-
*
318-
* @param sourceCode
319-
* @return String
320-
**/
321-
public String getPublicClassName(String sourceCode) {
322-
Matcher matcher = classNamePattern.matcher(sourceCode);
323-
if (!matcher.find()) {
324-
throw new PublicClassNotFoundException("public class is not be found in source code : " + sourceCode);
325-
}
326-
return matcher.group(2).trim();
327-
}
234+
328235
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dolphinscheduler.plugin.task.java;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.util.jar.JarFile;
22+
import java.util.jar.Manifest;
23+
24+
public class MainClassExtractor {
25+
26+
public static String getMainClassName(String jarFilePath) throws IOException {
27+
JarFile jarFile = new JarFile(new File(jarFilePath));
28+
Manifest manifest = jarFile.getManifest();
29+
String mainClassName = manifest.getMainAttributes().getValue("Main-Class");
30+
jarFile.close();
31+
return mainClassName;
32+
}
33+
}

0 commit comments

Comments
 (0)