< prev index next >

jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/ExcludeVMPlugin.java

Print this page


   1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.tools.jlink.internal.plugins;
  26 
  27 import java.io.BufferedReader;
  28 import java.io.IOException;
  29 import java.io.InputStreamReader;
  30 import java.io.UncheckedIOException;
  31 import java.nio.charset.StandardCharsets;
  32 import java.util.Comparator;
  33 import java.util.List;
  34 import java.util.Map;
  35 import java.util.TreeSet;
  36 import java.util.function.Predicate;
  37 import java.util.stream.Collectors;


  38 import jdk.tools.jlink.plugin.Plugin;
  39 import jdk.tools.jlink.plugin.ResourcePool;
  40 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
  41 import jdk.tools.jlink.plugin.ResourcePoolModule;
  42 import jdk.tools.jlink.plugin.ResourcePoolEntry;
  43 import jdk.tools.jlink.plugin.PluginException;
  44 
  45 /**
  46  *
  47  * Exclude VM plugin
  48  */
  49 public final class ExcludeVMPlugin implements Plugin {
  50 
  51     private static final class JvmComparator implements Comparator<Jvm> {
  52 
  53         @Override
  54         public int compare(Jvm o1, Jvm o2) {
  55             return o1.getEfficience() - o2.getEfficience();
  56         }
  57     }


  98      * /java.base/lib/{architecture}/{server|client|minimal}/{shared lib}
  99      * e.g.: /java.base/lib/server/libjvm.so
 100      * /java.base/lib/server/libjvm.dylib
 101      */
 102     private List<ResourcePoolEntry> getVMs(ResourcePoolModule javaBase, String[] jvmlibs) {
 103         List<ResourcePoolEntry> ret = javaBase.entries().filter((t) -> {
 104             String path = t.path();
 105             for (String jvmlib : jvmlibs) {
 106                 if (t.path().endsWith("/" + jvmlib)) {
 107                     return true;
 108                 }
 109             }
 110             return false;
 111         }).collect(Collectors.toList());
 112         return ret;
 113     }
 114 
 115     @Override
 116     public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
 117         ResourcePoolModule javaBase = in.moduleView().findModule("java.base").get();
 118         String[] jvmlibs = jvmlibs(javaBase.osName());
 119         TreeSet<Jvm> existing = new TreeSet<>(new JvmComparator());
 120         TreeSet<Jvm> removed = new TreeSet<>(new JvmComparator());
 121         if (!keepAll) {
 122             // First retrieve all available VM names and removed VM
 123             List<ResourcePoolEntry> jvms = getVMs(javaBase, jvmlibs);
 124             for (Jvm jvm : Jvm.values()) {
 125                 for (ResourcePoolEntry md : jvms) {
 126                     String mdPath = md.path();
 127                     for (String jvmlib : jvmlibs) {
 128                         if (mdPath.endsWith("/" + jvm.getName() + "/" + jvmlib)) {
 129                             existing.add(jvm);
 130                             if (isRemoved(md)) {
 131                                 removed.add(jvm);
 132                             }
 133                         }
 134                     }
 135                 }
 136             }
 137         }
 138         // Check that target exists


 240         for (Jvm platform : existing) {
 241             if (!removed.contains(platform)) {
 242                 remaining.add(platform);
 243                 builder.append("-").append(platform.getName()).append(" KNOWN\n");
 244             }
 245         }
 246 
 247         // removed JVM are aliased to the most efficient remaining JVM (last one).
 248         // The order in the file is from most to less efficient platform
 249         for (Jvm platform : removed.descendingSet()) {
 250             builder.append("-").append(platform.getName()).
 251                     append(" ALIASED_TO -").
 252                     append(remaining.last().getName()).append("\n");
 253         }
 254 
 255         byte[] content = builder.toString().getBytes(StandardCharsets.UTF_8);
 256 
 257         return orig.copyWithContent(content);
 258     }
 259 
 260     private static String[] jvmlibs(String osName) {
 261         if (isWindows(osName)) {


 262             return new String[] { "jvm.dll" };
 263         } else if (isMac(osName)) {
 264             return new String[] { "libjvm.dylib", "libjvm.a" };
 265         } else {
 266             return new String[] { "libjvm.so", "libjvm.a" };
 267         }
 268     }
 269 
 270     private static boolean isWindows(String osName) {
 271         return osName.startsWith("Windows");
 272     }
 273 
 274     private static boolean isMac(String osName) {
 275         return osName.startsWith("Mac OS") || osName.startsWith("Darwin");
 276     }
 277 }
   1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.tools.jlink.internal.plugins;
  26 
  27 import java.io.BufferedReader;
  28 import java.io.IOException;
  29 import java.io.InputStreamReader;
  30 import java.io.UncheckedIOException;
  31 import java.nio.charset.StandardCharsets;
  32 import java.util.Comparator;
  33 import java.util.List;
  34 import java.util.Map;
  35 import java.util.TreeSet;
  36 import java.util.function.Predicate;
  37 import java.util.stream.Collectors;
  38 
  39 import jdk.tools.jlink.internal.Platform;
  40 import jdk.tools.jlink.plugin.Plugin;
  41 import jdk.tools.jlink.plugin.ResourcePool;
  42 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
  43 import jdk.tools.jlink.plugin.ResourcePoolModule;
  44 import jdk.tools.jlink.plugin.ResourcePoolEntry;
  45 import jdk.tools.jlink.plugin.PluginException;
  46 
  47 /**
  48  *
  49  * Exclude VM plugin
  50  */
  51 public final class ExcludeVMPlugin implements Plugin {
  52 
  53     private static final class JvmComparator implements Comparator<Jvm> {
  54 
  55         @Override
  56         public int compare(Jvm o1, Jvm o2) {
  57             return o1.getEfficience() - o2.getEfficience();
  58         }
  59     }


 100      * /java.base/lib/{architecture}/{server|client|minimal}/{shared lib}
 101      * e.g.: /java.base/lib/server/libjvm.so
 102      * /java.base/lib/server/libjvm.dylib
 103      */
 104     private List<ResourcePoolEntry> getVMs(ResourcePoolModule javaBase, String[] jvmlibs) {
 105         List<ResourcePoolEntry> ret = javaBase.entries().filter((t) -> {
 106             String path = t.path();
 107             for (String jvmlib : jvmlibs) {
 108                 if (t.path().endsWith("/" + jvmlib)) {
 109                     return true;
 110                 }
 111             }
 112             return false;
 113         }).collect(Collectors.toList());
 114         return ret;
 115     }
 116 
 117     @Override
 118     public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
 119         ResourcePoolModule javaBase = in.moduleView().findModule("java.base").get();
 120         String[] jvmlibs = jvmlibs(javaBase);
 121         TreeSet<Jvm> existing = new TreeSet<>(new JvmComparator());
 122         TreeSet<Jvm> removed = new TreeSet<>(new JvmComparator());
 123         if (!keepAll) {
 124             // First retrieve all available VM names and removed VM
 125             List<ResourcePoolEntry> jvms = getVMs(javaBase, jvmlibs);
 126             for (Jvm jvm : Jvm.values()) {
 127                 for (ResourcePoolEntry md : jvms) {
 128                     String mdPath = md.path();
 129                     for (String jvmlib : jvmlibs) {
 130                         if (mdPath.endsWith("/" + jvm.getName() + "/" + jvmlib)) {
 131                             existing.add(jvm);
 132                             if (isRemoved(md)) {
 133                                 removed.add(jvm);
 134                             }
 135                         }
 136                     }
 137                 }
 138             }
 139         }
 140         // Check that target exists


 242         for (Jvm platform : existing) {
 243             if (!removed.contains(platform)) {
 244                 remaining.add(platform);
 245                 builder.append("-").append(platform.getName()).append(" KNOWN\n");
 246             }
 247         }
 248 
 249         // removed JVM are aliased to the most efficient remaining JVM (last one).
 250         // The order in the file is from most to less efficient platform
 251         for (Jvm platform : removed.descendingSet()) {
 252             builder.append("-").append(platform.getName()).
 253                     append(" ALIASED_TO -").
 254                     append(remaining.last().getName()).append("\n");
 255         }
 256 
 257         byte[] content = builder.toString().getBytes(StandardCharsets.UTF_8);
 258 
 259         return orig.copyWithContent(content);
 260     }
 261 
 262     private static String[] jvmlibs(ResourcePoolModule module) {
 263         Platform platform = Platform.getTargetPlatform(module);
 264         switch (platform) {
 265             case WINDOWS:
 266                 return new String[] { "jvm.dll" };
 267             case MACOS:
 268                 return new String[] { "libjvm.dylib", "libjvm.a" };
 269             default:
 270                 return new String[] { "libjvm.so", "libjvm.a" };
 271         }
 272     }








 273 }
< prev index next >