View Javadoc
1   /*
2    * Copyright (C) 2025 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.gmavenplus.util;
18  
19  import org.apache.maven.plugin.logging.Log;
20  import org.apache.maven.plugin.logging.SystemStreamLog;
21  import org.codehaus.gmavenplus.model.GroovyCompileConfiguration;
22  import org.codehaus.gmavenplus.model.GroovyDocConfiguration;
23  import org.codehaus.gmavenplus.model.GroovyStubConfiguration;
24  
25  import java.io.IOException;
26  import java.io.ObjectInputStream;
27  import java.nio.file.Files;
28  import java.nio.file.Paths;
29  import java.util.Collections;
30  import java.util.List;
31  
32  /**
33   * Main class to be executed in a forked process for Groovy compilation.
34   *
35   * @author Keegan Witt
36   */
37  public class ForkedGroovyCompiler {
38  
39      public static void main(String[] args) {
40          if (args.length != 1) {
41              System.err.println("Usage: java " + ForkedGroovyCompiler.class.getName() + " <configuration-file>");
42              System.exit(1);
43          }
44  
45          String configFilePath = args[0];
46          try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Paths.get(configFilePath)))) {
47              Object configuration = ois.readObject();
48              Log log = new SystemStreamLog();
49  
50              List<?> classpath = Collections.emptyList();
51              org.codehaus.gmavenplus.model.IncludeClasspath includeClasspath = null;
52  
53              if (configuration instanceof GroovyCompileConfiguration) {
54                  classpath = ((GroovyCompileConfiguration) configuration).getClasspath();
55                  includeClasspath = ((GroovyCompileConfiguration) configuration).getIncludeClasspath();
56              } else if (configuration instanceof GroovyStubConfiguration) {
57                  classpath = ((GroovyStubConfiguration) configuration).getClasspath();
58                  includeClasspath = ((GroovyStubConfiguration) configuration).getIncludeClasspath();
59              } else if (configuration instanceof GroovyDocConfiguration) {
60                  classpath = ((GroovyDocConfiguration) configuration).getClasspath();
61                  includeClasspath = ((GroovyDocConfiguration) configuration).getIncludeClasspath();
62              }
63  
64              ClassLoader parent = ClassLoader.getSystemClassLoader();
65              if (includeClasspath != null && includeClasspath.name().equals("PROJECT_ONLY")) {
66                  try {
67                      java.lang.reflect.Method getPlatformClassLoader = ClassLoader.class.getMethod("getPlatformClassLoader");
68                      parent = (ClassLoader) getPlatformClassLoader.invoke(null);
69                  } catch (Exception e) {
70                      parent = null;
71                  }
72              }
73  
74              List<?> finalClasspath = classpath != null ? classpath : Collections.emptyList();
75              if (includeClasspath != null && includeClasspath.name().equals("PLUGIN_ONLY")) {
76                  finalClasspath = Collections.emptyList();
77              }
78  
79              ClassWrangler classWrangler = new ClassWrangler(finalClasspath, parent, log);
80              GroovyCompiler compiler = new GroovyCompiler(classWrangler, log);
81  
82              if (configuration instanceof GroovyCompileConfiguration) {
83                  compiler.compile((GroovyCompileConfiguration) configuration);
84              } else if (configuration instanceof GroovyStubConfiguration) {
85                  compiler.generateStubs((GroovyStubConfiguration) configuration);
86              } else if (configuration instanceof GroovyDocConfiguration) {
87                  compiler.generateGroovyDoc((GroovyDocConfiguration) configuration);
88              } else {
89                  throw new IllegalArgumentException("Unknown configuration type: " + configuration.getClass().getName());
90              }
91          } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException | java.lang.reflect.InvocationTargetException e) {
92              e.printStackTrace();
93              System.exit(1);
94          }
95      }
96  }