[hg] main-silver: use fileobjects, not JarFile to find manifest ...

  • From: Milos Kleint < >
  • To: ,
  • Subject: [hg] main-silver: use fileobjects, not JarFile to find manifest ...
  • Date: Fri, 01 Feb 2013 04:29:00 -0800

changeset 0f910e24d21d in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/0f910e24d21d
description:
        use fileobjects, not JarFile to find manifest and all jar's packages

diffstat:

 
maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenWhiteListQueryImpl.java
 |  91 +++++----
 1 files changed, 54 insertions(+), 37 deletions(-)

diffs (205 lines):

diff --git 
a/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenWhiteListQueryImpl.java
 
b/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenWhiteListQueryImpl.java
--- 
a/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenWhiteListQueryImpl.java
+++ 
b/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenWhiteListQueryImpl.java
@@ -45,6 +45,7 @@
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.IOException;
+import java.io.InputStream;
 import java.lang.ref.SoftReference;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -56,8 +57,6 @@
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.jar.Attributes;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
 import java.util.jar.Manifest;
 import javax.lang.model.element.ElementKind;
 import javax.swing.event.ChangeEvent;
@@ -65,6 +64,7 @@
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.project.MavenProject;
 import 
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 import org.netbeans.api.annotations.common.NonNull;
@@ -78,6 +78,7 @@
 import org.netbeans.spi.project.ProjectServiceProvider;
 import org.netbeans.spi.whitelist.WhiteListQueryImplementation;
 import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
 import org.openide.util.RequestProcessor;
 import org.openide.util.WeakSet;
 
@@ -111,7 +112,7 @@
                 if 
(NbMavenProject.PROP_PROJECT.equals(evt.getPropertyName())) {
                     synchronized (LOCK) {
                         isCached = false;
-                        cacheOrLoad(project);
+                        cacheOrLoad();
                     }
                 }
             }
@@ -131,8 +132,8 @@
         }
         ProjectSourcesClassPathProvider prov = 
project.getLookup().lookup(ProjectSourcesClassPathProvider.class);
         assert prov != null;
-        ClassPath cp = prov.getProjectSourcesClassPath(ClassPath.SOURCE);
-        if (!cp.contains(file)) {
+        ClassPath sourceCp = 
prov.getProjectSourcesClassPath(ClassPath.SOURCE);
+        if (!sourceCp.contains(file)) {
             return null;
         }     
         
@@ -141,7 +142,7 @@
             mvn.addPropertyChangeListener(projectListener);
         }
         
-        Tuple res = cacheOrLoad(project);
+        Tuple res = cacheOrLoad();
         
         MavenWhiteListImplementation val = new 
MavenWhiteListImplementation(res.privatePackages, res.transitivePackages);
         results.add(val);
@@ -154,21 +155,30 @@
     private static final WhiteListQuery.Result OK = new 
WhiteListQuery.Result();
     
 
-    private Set<String> getAllPackages(JarFile jf) {
+    private Set<String> getAllPackages(FileObject root) {       
         Set<String> toRet = new HashSet<String>();
-        Enumeration<JarEntry> en = jf.entries();
-        while (en.hasMoreElements()) {
-            JarEntry je = en.nextElement();
-            String name = je.getName();
-            if (!je.isDirectory() && name.endsWith(".class") && 
name.lastIndexOf('/') > -1) {
-                name = name.substring(0, name.lastIndexOf('/'));
-                toRet.add(name.replace('/', '.'));
-            }
-        }
+        processFolder(root, root, toRet);
+        toRet.remove("");
         return toRet;
     }
 
-    private Tuple calculateLists(Project project) {
+    private void processFolder(FileObject root, FileObject folder, 
Set<String> foundPackages) {
+        Enumeration<? extends FileObject> it = folder.getData(false);
+        while (it.hasMoreElements()) {
+            FileObject fileObject = it.nextElement();
+            if (fileObject.hasExt("class")) {
+                foundPackages.add(folder.getPath().replace('/', '.'));
+                break;
+            }
+        }
+        it = folder.getFolders(false);
+        while (it.hasMoreElements()) {
+            FileObject fileObject = it.nextElement();
+            processFolder(root, fileObject, foundPackages);
+        }
+    }
+
+    private Tuple calculateLists() {
         //System.out.println("calculate for project=" + 
project.getProjectDirectory());
         String useOsgiString = 
PluginPropertyUtils.getPluginProperty(project, 
MavenNbModuleImpl.GROUPID_MOJO, MavenNbModuleImpl.NBM_PLUGIN, 
"useOSGiDependencies", null, null);
         boolean useOsgi = useOsgiString != null ? 
Boolean.parseBoolean(useOsgiString) : false;
@@ -184,27 +194,26 @@
                 
         for (Artifact a : mp.getCompileArtifacts()) {
             if (a.getFile() != null) {
-                JarFile jf = null;
-                try {
-                    jf = new JarFile(a.getFile(), false);
-                    Manifest mf = jf.getManifest();
+                FileObject fo = FileUtil.toFileObject(a.getFile());
+                if (fo != null && FileUtil.isArchiveFile(fo)) {
+                    FileObject root = FileUtil.getArchiveRoot(fo);
+                    Manifest mf = getManifest(root);
+
                     if (mf != null && mf.getMainAttributes() != null) {
                         Attributes attrs = mf.getMainAttributes();
                         String osgiexport = attrs.getValue("Export-Package");
                         String osgiprivate = 
attrs.getValue("Private-Package");
                         String nbmexport = 
attrs.getValue("OpenIDE-Module-Public-Packages");
-                        Set<String> allpackages = getAllPackages(jf);
+                        Set<String> allpackages = getAllPackages(root);
                         if (nbmexport != null) {
                             String nbmMaven = 
attrs.getValue("Maven-Class-Path"); //modules built with maven with external 
libs
                             String friends = 
attrs.getValue("OpenIDE-Module-Friends");
                             nbms.add(new NBMWrapper(a, allpackages, 
nbmexport.equals("-") ? null : StringUtils.split(nbmexport, ","),
                                     friends != null ? 
StringUtils.split(friends, ",") : null, 
                                     nbmMaven != null ? 
StringUtils.split(nbmMaven, " ") : null));
-                        }
-                        else if (useOsgi && osgiexport != null) {
+                        } else if (useOsgi && osgiexport != null) {
                             //TODO
-                        } 
-                        else {
+                        } else {
                             if (a.getDependencyTrail() != null && 
a.getDependencyTrail().size() > 2) {
                                 unknown.add(new Wrapper(a, allpackages));
                             } else {
@@ -213,19 +222,10 @@
                             }
                         }
                     }
-                } catch (IOException ex) {
-                } finally {
-                    if (jf != null) {
-                        try {
-                            jf.close();
-                        } catch (IOException ex) {
                         }
                     }
                 }
                 
-            }
-        }
-        
         List<ExplicitDependency> explicits = 
PluginPropertyUtils.getPluginPropertyBuildable(project, 
MavenNbModuleImpl.GROUPID_MOJO, MavenNbModuleImpl.NBM_PLUGIN, null, new 
ExplicitBuilder());
         String codenamebase = PluginPropertyUtils.getPluginProperty(project, 
MavenNbModuleImpl.GROUPID_MOJO, MavenNbModuleImpl.NBM_PLUGIN, "codeNameBase", 
null, null);
         
@@ -305,7 +305,7 @@
         });
     }
 
-    private Tuple cacheOrLoad(Project project) {
+    private Tuple cacheOrLoad() {
         //compute the effective, known "private" packages that should not be 
accessible from the file.
 
         synchronized (LOCK) {
@@ -316,7 +316,7 @@
                     return new Tuple(set1, set2);
                 }
             }
-            Tuple tup = calculateLists(project);
+            Tuple tup = calculateLists();
             cachePrivatePackages = new 
SoftReference<Set<String>>(tup.privatePackages);
             cacheTransitivePackages = new 
SoftReference<Set<String>>(tup.transitivePackages);
             isCached = true;
@@ -325,6 +325,23 @@
         }
     }
     
+    private Manifest getManifest(FileObject root) {
+        FileObject manifestFo = root.getFileObject("META-INF/MANIFEST.MF");
+        if (manifestFo != null) {
+            InputStream is = null;
+            try {
+                is = manifestFo.getInputStream();
+                return new Manifest(is);
+            } catch (IOException ex) {
+                //Exceptions.printStackTrace(ex);
+            } finally {
+                IOUtil.close(is);
+            }
+        }
+        return null;
+
+    }
+    
     private static class MavenWhiteListImplementation implements 
WhiteListImplementation {
         private final List<ChangeListener> listeners = new 
ArrayList<ChangeListener>();
         @NonNull

[hg] main-silver: use fileobjects, not JarFile to find manifest ...

Milos Kleint 02/01/2013

Project Features

About this Project

APIsupport was started in November 2009, is owned by Antonin Nebuzelsky, and has 43 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close