DefaultCompressPlugin.java revision 14357:5c412978857d
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 */ 25package jdk.tools.jlink.internal.plugins; 26 27import java.io.IOException; 28import java.io.UncheckedIOException; 29import java.util.ArrayList; 30import java.util.Collections; 31import java.util.HashSet; 32import java.util.List; 33import java.util.Map; 34import java.util.Set; 35 36import jdk.tools.jlink.plugin.PluginException; 37import jdk.tools.jlink.internal.PoolImpl; 38import jdk.tools.jlink.plugin.Pool; 39import jdk.tools.jlink.plugin.TransformerPlugin; 40import jdk.tools.jlink.internal.ImagePluginStack; 41import jdk.tools.jlink.internal.ResourcePrevisitor; 42import jdk.tools.jlink.internal.StringTable; 43import jdk.tools.jlink.internal.Utils; 44 45/** 46 * 47 * ZIP and String Sharing compression plugin 48 */ 49public final class DefaultCompressPlugin implements TransformerPlugin, ResourcePrevisitor { 50 public static final String NAME = "compress"; 51 public static final String FILTER = "filter"; 52 public static final String LEVEL_0 = "0"; 53 public static final String LEVEL_1 = "1"; 54 public static final String LEVEL_2 = "2"; 55 56 private StringSharingPlugin ss; 57 private ZipPlugin zip; 58 59 @Override 60 public String getName() { 61 return NAME; 62 } 63 64 @Override 65 public void visit(Pool in, Pool out) { 66 if (ss != null && zip != null) { 67 Pool output = new ImagePluginStack.OrderedResourcePool(in.getByteOrder(), 68 ((PoolImpl) in).getStringTable()); 69 ss.visit(in, output); 70 zip.visit(output, out); 71 } else if (ss != null) { 72 ss.visit(in, out); 73 } else if (zip != null) { 74 zip.visit(in, out); 75 } 76 } 77 78 @Override 79 public void previsit(Pool resources, StringTable strings) { 80 if (ss != null) { 81 ss.previsit(resources, strings); 82 } 83 } 84 85 @Override 86 public Set<PluginType> getType() { 87 Set<PluginType> set = new HashSet<>(); 88 set.add(CATEGORY.COMPRESSOR); 89 return Collections.unmodifiableSet(set); 90 } 91 92 @Override 93 public String getDescription() { 94 return PluginsResourceBundle.getDescription(NAME); 95 } 96 97 @Override 98 public boolean hasArguments() { 99 return true; 100 } 101 102 @Override 103 public String getArgumentsDescription() { 104 return PluginsResourceBundle.getArgument(NAME); 105 } 106 107 @Override 108 public void configure(Map<String, String> config) { 109 try { 110 String filter = config.get(FILTER); 111 String[] patterns = filter == null ? null 112 : Utils.listParser.apply(filter); 113 ResourceFilter resFilter = new ResourceFilter(patterns); 114 String level = config.get(NAME); 115 if (level != null) { 116 switch (level) { 117 case LEVEL_0: 118 ss = new StringSharingPlugin(resFilter); 119 break; 120 case LEVEL_1: 121 zip = new ZipPlugin(resFilter); 122 break; 123 case LEVEL_2: 124 ss = new StringSharingPlugin(resFilter); 125 zip = new ZipPlugin(resFilter); 126 break; 127 default: 128 throw new IllegalArgumentException("Invalid compression level " + level); 129 } 130 } else { 131 ss = new StringSharingPlugin(resFilter); 132 zip = new ZipPlugin(resFilter); 133 } 134 } catch (IOException ex) { 135 throw new UncheckedIOException(ex); 136 } 137 } 138} 139