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.util.Map; 28import java.util.function.Function; 29 30import jdk.tools.jlink.internal.ResourcePoolManager.ResourcePoolImpl; 31import jdk.tools.jlink.plugin.ResourcePool; 32import jdk.tools.jlink.plugin.ResourcePoolBuilder; 33import jdk.tools.jlink.plugin.Plugin; 34import jdk.tools.jlink.internal.ImagePluginStack; 35import jdk.tools.jlink.internal.ResourcePoolManager; 36import jdk.tools.jlink.internal.ResourcePrevisitor; 37import jdk.tools.jlink.internal.StringTable; 38 39/** 40 * 41 * ZIP and String Sharing compression plugin 42 */ 43public final class DefaultCompressPlugin implements Plugin, ResourcePrevisitor { 44 public static final String NAME = "compress"; 45 public static final String FILTER = "filter"; 46 public static final String LEVEL_0 = "0"; 47 public static final String LEVEL_1 = "1"; 48 public static final String LEVEL_2 = "2"; 49 50 private StringSharingPlugin ss; 51 private ZipPlugin zip; 52 53 @Override 54 public String getName() { 55 return NAME; 56 } 57 58 @Override 59 public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) { 60 if (ss != null && zip != null) { 61 ResourcePoolManager resMgr = new ImagePluginStack.OrderedResourcePoolManager( 62 in.byteOrder(), ((ResourcePoolImpl)in).getStringTable()); 63 return zip.transform(ss.transform(in, resMgr.resourcePoolBuilder()), out); 64 } else if (ss != null) { 65 return ss.transform(in, out); 66 } else if (zip != null) { 67 return zip.transform(in, out); 68 } else { 69 in.transformAndCopy(Function.identity(), out); 70 return out.build(); 71 } 72 } 73 74 @Override 75 public void previsit(ResourcePool resources, StringTable strings) { 76 if (ss != null) { 77 ss.previsit(resources, strings); 78 } 79 } 80 81 @Override 82 public Category getType() { 83 return Category.COMPRESSOR; 84 } 85 86 @Override 87 public String getDescription() { 88 return PluginsResourceBundle.getDescription(NAME); 89 } 90 91 @Override 92 public boolean hasArguments() { 93 return true; 94 } 95 96 @Override 97 public String getArgumentsDescription() { 98 return PluginsResourceBundle.getArgument(NAME); 99 } 100 101 @Override 102 public void configure(Map<String, String> config) { 103 ResourceFilter resFilter = ResourceFilter.includeFilter(config.get(FILTER)); 104 String level = config.get(NAME); 105 if (level != null) { 106 switch (level) { 107 case LEVEL_0: 108 ss = null; 109 zip = null; 110 break; 111 case LEVEL_1: 112 ss = new StringSharingPlugin(resFilter); 113 break; 114 case LEVEL_2: 115 zip = new ZipPlugin(resFilter); 116 break; 117 default: 118 throw new IllegalArgumentException("Invalid compression level " + level); 119 } 120 } else { 121 throw new IllegalArgumentException("Invalid compression level " + level); 122 } 123 } 124} 125