View Javadoc
1   package org.codehaus.plexus.languages.java.jpms;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.BufferedWriter;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.lang.reflect.Method;
26  import java.nio.charset.Charset;
27  import java.nio.file.FileVisitResult;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.nio.file.SimpleFileVisitor;
32  import java.nio.file.attribute.BasicFileAttributes;
33  import java.util.HashMap;
34  import java.util.Map;
35  import java.util.Properties;
36  import java.util.Set;
37  
38  /**
39   * Extract the module name by calling the main method with an external JVM
40   * 
41   * @author Robert Scholte
42   * @since 1.0.0
43   */
44  public class MainClassModuleNameExtractor
45  {
46      private final Path jdkHome;
47  
48      public MainClassModuleNameExtractor( Path jdkHome )
49      {
50          this.jdkHome = jdkHome;
51      }
52  
53      public <T> Map<T, String> extract( Map<T, Path> files )
54          throws IOException
55      {
56          Path workDir = Files.createTempDirectory( "plexus-java_jpms-" );
57  
58          try (InputStream is =
59              MainClassModuleNameExtractor.class.getResourceAsStream( this.getClass().getSimpleName() + ".class" ))
60          {
61              Path pckg = workDir.resolve( this.getClass().getPackage().getName().replace( '.', '/' ) );
62  
63              Files.createDirectories( pckg );
64  
65              Files.copy( is, pckg.resolve( this.getClass().getSimpleName() + ".class" ) );
66          }
67  
68          try (BufferedWriter argsWriter = Files.newBufferedWriter( workDir.resolve( "args" ), Charset.defaultCharset() ))
69          {
70              argsWriter.append( "--class-path" );
71              argsWriter.newLine();
72  
73              argsWriter.append( "." );
74              argsWriter.newLine();
75  
76              argsWriter.append( this.getClass().getName() );
77              argsWriter.newLine();
78  
79              for ( Path p : files.values() )
80              {
81                  argsWriter.append( p.toAbsolutePath().toString() );
82                  argsWriter.newLine();
83              }
84          }
85  
86          ProcessBuilder builder = new ProcessBuilder( jdkHome.resolve( "bin/java" ).toAbsolutePath().toString(),
87                                                       "@args" ).directory( workDir.toFile() );
88  
89          Process p = builder.start();
90  
91          Properties output = new Properties();
92          try (InputStream is = p.getInputStream())
93          {
94              output.load( is );
95          }
96  
97          Map<T, String> moduleNames = new HashMap<>( files.size() );
98          for ( Map.Entry<T, Path> entry : files.entrySet() )
99          {
100             moduleNames.put( entry.getKey(), output.getProperty( entry.getValue().toAbsolutePath().toString(), null ) );
101         }
102 
103         try
104         {
105             Files.walkFileTree( workDir, new SimpleFileVisitor<Path>()
106             {
107                 @Override
108                 public FileVisitResult visitFile( Path file, BasicFileAttributes attrs )
109                     throws IOException
110                 {
111                     Files.delete( file );
112                     return FileVisitResult.CONTINUE;
113                 }
114 
115                 @Override
116                 public FileVisitResult postVisitDirectory( Path dir, IOException exc )
117                     throws IOException
118                 {
119                     Files.delete( dir );
120                     return FileVisitResult.CONTINUE;
121                 }
122             } );
123         }
124         catch ( IOException e )
125         {
126             // noop, we did our best to clean it up
127         }
128 
129         return moduleNames;
130     }
131 
132     public static void main( String[] args )
133     {
134         Properties properties = new Properties();
135 
136         for ( String path : args )
137         {
138             String moduleName = getModuleName( Paths.get( path ) );
139             if ( moduleName != null )
140             {
141                 properties.setProperty( path, moduleName );
142             }
143         }
144 
145         try
146         {
147             properties.store( System.out, "" );
148         }
149         catch ( IOException e )
150         {
151             System.exit( 1 );
152         }
153     }
154 
155     public static String getModuleName( Path modulePath )
156     {
157         String name = null;
158         try
159         {
160             // Use Java9 code to get moduleName, don't try to do it better with own implementation
161             Class<?> moduleFinderClass = Class.forName( "java.lang.module.ModuleFinder" );
162 
163             Method ofMethod = moduleFinderClass.getMethod( "of", java.nio.file.Path[].class );
164             Object moduleFinderInstance =
165                 ofMethod.invoke( null, new Object[] { new java.nio.file.Path[] { modulePath } } );
166 
167             Method findAllMethod = moduleFinderClass.getMethod( "findAll" );
168 
169             @SuppressWarnings( "unchecked" )
170             Set<Object> moduleReferences = (Set<Object>) findAllMethod.invoke( moduleFinderInstance );
171 
172             if ( moduleReferences.isEmpty() )
173             {
174                 return null;
175             }
176 
177             Object moduleReference = moduleReferences.iterator().next();
178             Method descriptorMethod = moduleReference.getClass().getMethod( "descriptor" );
179             Object moduleDescriptorInstance = descriptorMethod.invoke( moduleReference );
180 
181             Method nameMethod = moduleDescriptorInstance.getClass().getMethod( "name" );
182             name = (String) nameMethod.invoke( moduleDescriptorInstance );
183         }
184         catch ( ReflectiveOperationException e )
185         {
186             // noop
187         }
188         catch ( SecurityException e )
189         {
190             // noop
191         }
192         catch ( IllegalArgumentException e )
193         {
194             // noop
195         }
196         return name;
197     }
198 }