jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/SortResourcesPlugin.java
changeset 37947 f69560487686
parent 37946 e420b9f05aaf
parent 37936 428ebc487445
child 37948 caf97b37ebec
equal deleted inserted replaced
37946:e420b9f05aaf 37947:f69560487686
     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.File;
       
    29 import java.io.FileInputStream;
       
    30 import java.io.IOException;
       
    31 import java.io.InputStreamReader;
       
    32 import java.nio.charset.StandardCharsets;
       
    33 import java.util.ArrayList;
       
    34 import java.util.Collections;
       
    35 import java.util.HashSet;
       
    36 import java.util.List;
       
    37 import java.util.Map;
       
    38 import java.util.Set;
       
    39 import java.util.regex.Matcher;
       
    40 import java.util.regex.Pattern;
       
    41 import java.util.stream.Collectors;
       
    42 import jdk.tools.jlink.plugin.PluginException;
       
    43 import jdk.tools.jlink.plugin.Pool;
       
    44 import jdk.tools.jlink.plugin.Pool.ModuleData;
       
    45 import jdk.tools.jlink.plugin.Pool.ModuleDataType;
       
    46 import jdk.tools.jlink.plugin.TransformerPlugin;
       
    47 import jdk.tools.jlink.internal.Utils;
       
    48 
       
    49 /**
       
    50  *
       
    51  * Sort Resources plugin
       
    52  */
       
    53 public final class SortResourcesPlugin implements TransformerPlugin {
       
    54 
       
    55     public static final String NAME = "sort-resources";
       
    56     private final List<Pattern> filters = new ArrayList<>();
       
    57     private List<String> orderedPaths;
       
    58     private boolean isFile;
       
    59 
       
    60     @Override
       
    61     public String getName() {
       
    62         return NAME;
       
    63     }
       
    64 
       
    65     static class SortWrapper {
       
    66 
       
    67         private final ModuleData resource;
       
    68         private final int ordinal;
       
    69 
       
    70         SortWrapper(ModuleData resource, int ordinal) {
       
    71             this.resource = resource;
       
    72             this.ordinal = ordinal;
       
    73         }
       
    74 
       
    75         ModuleData getResource() {
       
    76             return resource;
       
    77         }
       
    78 
       
    79         String getPath() {
       
    80             return resource.getPath();
       
    81         }
       
    82 
       
    83         int getOrdinal() {
       
    84             return ordinal;
       
    85         }
       
    86     }
       
    87 
       
    88     private int getPatternOrdinal(String path) {
       
    89         int ordinal = -1;
       
    90         for (int i = 0; i < filters.size(); i++) {
       
    91             Matcher m = filters.get(i).matcher(path);
       
    92             if (m.matches()) {
       
    93                 ordinal = i;
       
    94                 break;
       
    95             }
       
    96         }
       
    97         return ordinal;
       
    98     }
       
    99 
       
   100     private int getFileOrdinal(String path) {
       
   101         return orderedPaths.indexOf(path);
       
   102     }
       
   103 
       
   104     @Override
       
   105     public void visit(Pool in, Pool out) {
       
   106         in.getContent().stream()
       
   107                 .filter(w -> w.getType().equals(ModuleDataType.CLASS_OR_RESOURCE))
       
   108                 .map((r) -> new SortWrapper(r, isFile
       
   109                         ? getFileOrdinal(r.getPath())
       
   110                         : getPatternOrdinal(r.getPath())))
       
   111                 .sorted((sw1, sw2) -> {
       
   112                     int ordinal1 = sw1.getOrdinal();
       
   113                     int ordinal2 = sw2.getOrdinal();
       
   114 
       
   115                     if (ordinal1 >= 0) {
       
   116                         if (ordinal2 >= 0) {
       
   117                             return ordinal1 - ordinal2;
       
   118                         } else {
       
   119                             return -1;
       
   120                         }
       
   121                     } else if (ordinal2 >= 0) {
       
   122                         return 1;
       
   123                     }
       
   124 
       
   125                     return sw1.getPath().compareTo(sw2.getPath());
       
   126                 }).forEach((sw) -> {
       
   127             try {
       
   128                 out.add(sw.getResource());
       
   129             } catch (Exception ex) {
       
   130                 throw new RuntimeException(ex);
       
   131             }
       
   132         });
       
   133         in.getContent().stream()
       
   134                 .filter(m -> !m.getType().equals(ModuleDataType.CLASS_OR_RESOURCE))
       
   135                 .forEach((m) -> {
       
   136                     try {
       
   137                         out.add(m);
       
   138                     } catch (Exception ex) {
       
   139                         throw new RuntimeException(ex);
       
   140                     }
       
   141                 });
       
   142     }
       
   143 
       
   144     @Override
       
   145     public Set<PluginType> getType() {
       
   146         Set<PluginType> set = new HashSet<>();
       
   147         set.add(CATEGORY.SORTER);
       
   148         return Collections.unmodifiableSet(set);
       
   149     }
       
   150 
       
   151     @Override
       
   152     public String getDescription() {
       
   153         return PluginsResourceBundle.getDescription(NAME);
       
   154     }
       
   155 
       
   156     @Override
       
   157     public boolean hasArguments() {
       
   158         return true;
       
   159     }
       
   160 
       
   161     @Override
       
   162     public String getArgumentsDescription() {
       
   163        return PluginsResourceBundle.getArgument(NAME);
       
   164     }
       
   165 
       
   166     @Override
       
   167     public void configure(Map<String, String> config) {
       
   168         String val = config.get(NAME);
       
   169         try {
       
   170             String[] patterns = Utils.listParser.apply(val);
       
   171             boolean isf = false;
       
   172             List<String> paths = null;
       
   173             if (patterns != null) {
       
   174                 if (patterns.length == 1) {
       
   175                     String filePath = patterns[0];
       
   176                     File f = new File(filePath);
       
   177                     if (f.exists()) {
       
   178                         isf = true;
       
   179                         try (FileInputStream fis = new FileInputStream(f);
       
   180                                 InputStreamReader ins
       
   181                                 = new InputStreamReader(fis, StandardCharsets.UTF_8);
       
   182                                 BufferedReader reader = new BufferedReader(ins)) {
       
   183                             paths = reader.lines().collect(Collectors.toList());
       
   184                         }
       
   185                     }
       
   186                 }
       
   187                 if (!isf) {
       
   188                     for (String p : patterns) {
       
   189                         p = p.replaceAll(" ", "");
       
   190                         Pattern pattern = Pattern.compile(ResourceFilter.escape(p));
       
   191                         filters.add(pattern);
       
   192                     }
       
   193                 }
       
   194             }
       
   195             orderedPaths = paths;
       
   196             isFile = isf;
       
   197         } catch (IOException ex) {
       
   198             throw new PluginException(ex);
       
   199         }
       
   200     }
       
   201 }