You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2018/06/21 20:35:54 UTC

[GitHub] sormuras closed pull request #27: [WIP] Add module descriptor information to ArtifactInfo

sormuras closed pull request #27: [WIP] Add module descriptor information to ArtifactInfo
URL: https://github.com/apache/maven-indexer/pull/27
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/indexer-core/pom.xml b/indexer-core/pom.xml
index c13e046..16b54d4 100644
--- a/indexer-core/pom.xml
+++ b/indexer-core/pom.xml
@@ -287,6 +287,14 @@ under the License.
           </excludes>
         </configuration>
       </plugin>
+        <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-compiler-plugin</artifactId>
+            <configuration>
+                <source>9</source>
+                <target>9</target>
+            </configuration>
+        </plugin>
     </plugins>
   </build>
 
diff --git a/indexer-core/src/main/java/org/apache/maven/index/ArtifactInfo.java b/indexer-core/src/main/java/org/apache/maven/index/ArtifactInfo.java
index 7f75838..99a3e93 100644
--- a/indexer-core/src/main/java/org/apache/maven/index/ArtifactInfo.java
+++ b/indexer-core/src/main/java/org/apache/maven/index/ArtifactInfo.java
@@ -338,6 +338,21 @@
 
     private String uinfo = null;
 
+    /**
+     * Java module system name declared by or derived for this artifact.
+     * @since 6.0.1
+     */
+    private String moduleName = null;
+
+    /**
+     * @since 6.0.1
+     */
+    private boolean moduleIsAutomatic = false;
+
+    /**
+     * @since 6.0.1
+     */
+    private boolean moduleNameIsSetViaMetaInf = false;
 
     public ArtifactInfo()
     {
@@ -1113,4 +1128,36 @@ public void setBundleRequiredExecutionEnvironment( String bundleRequiredExecutio
         this.bundleRequiredExecutionEnvironment = bundleRequiredExecutionEnvironment;
     }
 
+    public String getModuleName()
+    {
+        return moduleName;
+    }
+
+    public ArtifactInfo setModuleName( String moduleName )
+    {
+        this.moduleName = moduleName;
+        return this;
+    }
+
+    public boolean isModuleIsAutomatic()
+    {
+        return moduleIsAutomatic;
+    }
+
+    public ArtifactInfo setModuleIsAutomatic( boolean moduleIsAutomatic )
+    {
+        this.moduleIsAutomatic = moduleIsAutomatic;
+        return this;
+    }
+
+    public boolean isModuleNameIsSetViaMetaInf()
+    {
+        return moduleNameIsSetViaMetaInf;
+    }
+
+    public ArtifactInfo setModuleNameIsSetViaMetaInf( boolean moduleNameIsSetViaMetaInf )
+    {
+        this.moduleNameIsSetViaMetaInf = moduleNameIsSetViaMetaInf;
+        return this;
+    }
 }
diff --git a/indexer-core/src/main/java/org/apache/maven/index/creator/JarFileContentsIndexCreator.java b/indexer-core/src/main/java/org/apache/maven/index/creator/JarFileContentsIndexCreator.java
index 9318fbb..41d93ef 100644
--- a/indexer-core/src/main/java/org/apache/maven/index/creator/JarFileContentsIndexCreator.java
+++ b/indexer-core/src/main/java/org/apache/maven/index/creator/JarFileContentsIndexCreator.java
@@ -9,7 +9,7 @@
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0    
+ *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
@@ -23,9 +23,11 @@
 import javax.inject.Singleton;
 import java.io.File;
 import java.io.IOException;
+import java.lang.module.ModuleDescriptor;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
+import java.util.Optional;
 
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field.Index;
@@ -148,6 +150,7 @@ private void updateArtifactInfo( final ArtifactInfo ai, final File f )
         if ( f.getName().endsWith( ".jar" ) )
         {
             updateArtifactInfo( ai, f, null );
+            updateArtifactInfoWithModuleDescriptorProperties( ai, f );
         }
         else if ( f.getName().endsWith( ".war" ) )
         {
@@ -223,6 +226,20 @@ else if ( name.startsWith( strippedPrefix )
         }
     }
 
+    private void updateArtifactInfoWithModuleDescriptorProperties( final ArtifactInfo ai, final File f )
+    {
+        ModuleHelper helper = new ModuleHelper();
+        Optional<ModuleDescriptor> od = helper.describeModule( f.toPath(), true );
+        if ( od.isPresent() )
+        {
+            ModuleDescriptor d = od.get();
+            System.out.println( d );
+            ai.setModuleName( d.name() );
+            ai.setModuleIsAutomatic( d.isAutomatic() );
+            ai.setModuleNameIsSetViaMetaInf( true );
+        }
+    }
+
     @Override
     public String toString()
     {
diff --git a/indexer-core/src/main/java/org/apache/maven/index/creator/ModuleHelper.java b/indexer-core/src/main/java/org/apache/maven/index/creator/ModuleHelper.java
new file mode 100644
index 0000000..59eb3db
--- /dev/null
+++ b/indexer-core/src/main/java/org/apache/maven/index/creator/ModuleHelper.java
@@ -0,0 +1,86 @@
+package org.apache.maven.index.creator;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.lang.module.FindException;
+import java.lang.module.ModuleDescriptor;
+import java.lang.module.ModuleFinder;
+import java.lang.module.ModuleReader;
+import java.lang.module.ModuleReference;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Path;
+import java.util.Optional;
+import java.util.Set;
+
+class ModuleHelper
+{
+    Optional<ModuleDescriptor> describeModule( Path path, boolean reportFileNameBasedModuleAsEmpty )
+    {
+        try
+        {
+            Set<ModuleReference> allModules = ModuleFinder.of( path ).findAll();
+            if ( allModules.size() != 1 )
+            {
+                throw new IllegalStateException( "expected to find single module, but got: " + allModules );
+            }
+            ModuleReference reference = allModules.iterator().next();
+            ModuleDescriptor descriptor = reference.descriptor();
+            // TODO info("describeModule({0} -> {1})", path, descriptor);
+            if ( reportFileNameBasedModuleAsEmpty )
+            {
+                if ( descriptor.isAutomatic() )
+                {
+                    if ( !isAutomaticModuleNameAttributeAvailable( reference ) )
+                    {
+                        return Optional.empty();
+                    }
+                }
+            }
+            return Optional.of( descriptor );
+        }
+        catch ( FindException e )
+        {
+            // TODO debug("finding module(s) failed: {0}", e);
+            return Optional.empty();
+        }
+    }
+
+    private boolean isAutomaticModuleNameAttributeAvailable( ModuleReference moduleReference )
+    {
+        try ( ModuleReader moduleReader = moduleReference.open() )
+        {
+            String manifestString =
+                            moduleReader
+                                            .read( "META-INF/MANIFEST.MF" )
+                                            .map( StandardCharsets.UTF_8::decode )
+                                            .map( Object::toString )
+                                            .orElse( "" );
+            if ( manifestString.contains( "Automatic-Module-Name" ) )
+            {
+                return true;
+            }
+        }
+        catch ( Exception e )
+        {
+            // TODO debug("reading manifest failed: {0}", e);
+        }
+        return false;
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services