ReleaseInfoPlugin.java revision 13901:b2a69d66dc65
1/*
2 * Copyright (c) 2016, 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.lang.module.ModuleDescriptor;
28import java.io.FileInputStream;
29import java.io.IOException;
30import java.util.Collections;
31import java.util.EnumSet;
32import java.util.List;
33import java.util.Map;
34import java.util.Set;
35import java.util.Properties;
36
37import jdk.tools.jlink.internal.Utils;
38import jdk.tools.jlink.plugin.ExecutableImage;
39import jdk.tools.jlink.plugin.PluginContext;
40import jdk.tools.jlink.plugin.PluginException;
41import jdk.tools.jlink.plugin.Pool;
42import jdk.tools.jlink.plugin.PostProcessorPlugin;
43
44/**
45 * This plugin adds/deletes information for 'release' file.
46 */
47public final class ReleaseInfoPlugin implements PostProcessorPlugin {
48    // option name
49    public static final String NAME = "release-info";
50    public static final String KEYS = "keys";
51
52    @Override
53    public Set<PluginType> getType() {
54        return Collections.singleton(CATEGORY.PROCESSOR);
55    }
56
57    @Override
58    public String getName() {
59        return NAME;
60    }
61
62    @Override
63    public String getDescription() {
64        return PluginsResourceBundle.getDescription(NAME);
65    }
66
67    @Override
68    public Set<STATE> getState() {
69        return EnumSet.of(STATE.FUNCTIONAL);
70    }
71
72    @Override
73    public boolean hasArguments() {
74        return true;
75    }
76
77    @Override
78    public String getArgumentsDescription() {
79        return PluginsResourceBundle.getArgument(NAME);
80    }
81
82    @Override
83    public void configure(Map<String, String> config, PluginContext ctx) {
84        Properties release = ctx != null? ctx.getReleaseProperties() : null;
85        if (release != null) {
86            String operation = config.get(NAME);
87            switch (operation) {
88                case "add": {
89                    // leave it to open-ended! source, java_version, java_full_version
90                    // can be passed via this option like:
91                    //
92                    //     --release-info add:build_type=fastdebug,source=openjdk,java_version=9
93                    // and put whatever value that was passed in command line.
94
95                    config.keySet().stream().
96                        filter(s -> !NAME.equals(s)).
97                        forEach(s -> release.put(s, config.get(s)));
98                }
99                break;
100
101                case "del": {
102                    // --release-info del:keys=openjdk,java_version
103                    String[] keys = Utils.listParser.apply(config.get(KEYS));
104                    for (String k : keys) {
105                        release.remove(k);
106                    }
107                }
108                break;
109
110                default: {
111                    // --release-info <file>
112                    try (FileInputStream fis = new FileInputStream(operation)) {
113                        release.load(fis);
114                    } catch (IOException exp) {
115                        throw new RuntimeException(exp);
116                    }
117                }
118                break;
119            }
120        }
121    }
122
123    @Override
124    public List<String> process(ExecutableImage image) {
125        // Nothing to do! Release info copied already during configure!
126        return Collections.emptyList();
127    }
128}
129