1818package org .apache .dolphinscheduler .plugin .task .java ;
1919
2020import 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
2322import org .apache .dolphinscheduler .common .utils .JSONUtils ;
2423import org .apache .dolphinscheduler .plugin .task .api .AbstractTask ;
2726import org .apache .dolphinscheduler .plugin .task .api .TaskConstants ;
2827import org .apache .dolphinscheduler .plugin .task .api .TaskException ;
2928import org .apache .dolphinscheduler .plugin .task .api .TaskExecutionContext ;
30- import org .apache .dolphinscheduler .plugin .task .api .model .Property ;
3129import org .apache .dolphinscheduler .plugin .task .api .model .ResourceInfo ;
3230import org .apache .dolphinscheduler .plugin .task .api .model .TaskResponse ;
3331import org .apache .dolphinscheduler .plugin .task .api .parameters .AbstractParameters ;
3432import org .apache .dolphinscheduler .plugin .task .api .resource .ResourceContext ;
3533import org .apache .dolphinscheduler .plugin .task .api .shell .IShellInterceptorBuilder ;
3634import 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 ;
4135import org .apache .dolphinscheduler .plugin .task .java .exception .RunTypeNotFoundException ;
4236
43- import org .apache .commons .io .FileUtils ;
44-
4537import 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-
5538import lombok .extern .slf4j .Slf4j ;
56-
5739import com .google .common .base .Preconditions ;
5840
41+
5942@ Slf4j
6043public 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}
0 commit comments