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