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  */
  25 package jdk.tools.jlink.internal.plugins;
  26 
  27 import java.io.FileInputStream;
  28 import java.io.IOException;
  29 import java.util.EnumSet;
  30 import java.util.HashMap;
  31 import java.util.Map;
  32 import java.util.Properties;
  33 import java.util.Set;
  34 import java.util.function.Function;
  35 import jdk.tools.jlink.internal.Utils;
  36 import jdk.tools.jlink.plugin.ResourcePool;
  37 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
  38 import jdk.tools.jlink.plugin.Plugin.Category;
  39 import jdk.tools.jlink.plugin.Plugin.State;
  40 import jdk.tools.jlink.plugin.Plugin;
  41 
  42 /**
  43  * This plugin adds/deletes information for 'release' file.
  44  */
  45 public final class ReleaseInfoPlugin implements Plugin {
  46     // option name
  47     public static final String NAME = "release-info";
  48     public static final String KEYS = "keys";
  49     private final Map<String, String> release = new HashMap<>();
  50 
  51     @Override
  52     public Category getType() {
  53         return Category.METAINFO_ADDER;
  54     }
  55 
  56     @Override
  57     public String getName() {
  58         return NAME;
  59     }
  60 
  61     @Override
  62     public String getDescription() {
  63         return PluginsResourceBundle.getDescription(NAME);
  64     }
  65 
  66     @Override
  67     public Set<State> getState() {
  68         return EnumSet.of(State.FUNCTIONAL);
  69     }
  70 
  71     @Override
  72     public boolean hasArguments() {
  73         return true;
  74     }
  75 
  76     @Override
  77     public String getArgumentsDescription() {
  78         return PluginsResourceBundle.getArgument(NAME);
  79     }
  80 
  81     @Override
  82     public void configure(Map<String, String> config) {
  83         String operation = config.get(NAME);
  84         switch (operation) {
  85             case "add": {
  86                 // leave it to open-ended! source, java_version, java_full_version
  87                 // can be passed via this option like:
  88                 //
  89                 //     --release-info add:build_type=fastdebug,source=openjdk,java_version=9
  90                 // and put whatever value that was passed in command line.
  91 
  92                 config.keySet().stream().
  93                     filter(s -> !NAME.equals(s)).
  94                     forEach(s -> release.put(s, config.get(s)));
  95             }
  96             break;
  97 
  98             case "del": {
  99                 // --release-info del:keys=openjdk,java_version
 100                 Utils.parseList(config.get(KEYS)).stream().forEach((k) -> {
 101                     release.remove(k);
 102                 });
 103             }
 104             break;
 105 
 106             default: {
 107                 // --release-info <file>
 108                 Properties props = new Properties();
 109                 try (FileInputStream fis = new FileInputStream(operation)) {
 110                     props.load(fis);
 111                 } catch (IOException exp) {
 112                     throw new RuntimeException(exp);
 113                 }
 114                 props.forEach((k, v) -> release.put(k.toString(), v.toString()));
 115             }
 116             break;
 117         }
 118     }
 119 
 120     @Override
 121     public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
 122         in.releaseProperties().putAll(release);
 123         return in;
 124     }
 125 }