< prev index next >

src/java.base/share/classes/com/sun/java/util/jar/pack/PropMap.java

Print this page




  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 
  26 package com.sun.java.util.jar.pack;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.PrintStream;
  31 import java.io.PrintWriter;


  32 import java.util.ArrayList;
  33 import java.util.Collection;
  34 import java.util.Comparator;
  35 import java.util.HashMap;
  36 import java.util.List;
  37 import java.util.Map;
  38 import java.util.Properties;
  39 import java.util.Set;
  40 import java.util.SortedMap;
  41 import java.util.TreeMap;
  42 import java.util.jar.Pack200;
  43 
  44 /**
  45  * Control block for publishing Pack200 options to the other classes.
  46  */
  47 
  48 final class PropMap implements SortedMap<String, String>  {
  49     private final TreeMap<String, String> theMap = new TreeMap<>();;
  50 
  51     // Override:
  52     public String put(String key, String value) {
  53         String oldValue = theMap.put(key, value);
  54         return oldValue;
  55     }
  56 
  57     // All this other stuff is private to the current package.
  58     // Outide clients of Pack200 do not need to use it; they can
  59     // get by with generic SortedMap functionality.
  60     private static Map<String, String> defaultProps;
  61     static {
  62         Properties props = new Properties();
  63 
  64         // Allow implementation selected via -Dpack.disable.native=true

  65         props.put(Utils.DEBUG_DISABLE_NATIVE,
  66                   String.valueOf(Boolean.getBoolean(Utils.DEBUG_DISABLE_NATIVE)));
  67 
  68         // Set the DEBUG_VERBOSE from system
  69         props.put(Utils.DEBUG_VERBOSE,
  70                   String.valueOf(Integer.getInteger(Utils.DEBUG_VERBOSE,0)));




  71 
  72         // The segment size is unlimited
  73         props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
  74 
  75         // Preserve file ordering by default.
  76         props.put(Pack200.Packer.KEEP_FILE_ORDER, Pack200.Packer.TRUE);
  77 
  78         // Preserve all modification times by default.
  79         props.put(Pack200.Packer.MODIFICATION_TIME, Pack200.Packer.KEEP);
  80 
  81         // Preserve deflation hints by default.
  82         props.put(Pack200.Packer.DEFLATE_HINT, Pack200.Packer.KEEP);
  83 
  84         // Pass through files with unrecognized attributes by default.
  85         props.put(Pack200.Packer.UNKNOWN_ATTRIBUTE, Pack200.Packer.PASS);
  86 
  87         // Pass through files with unrecognized format by default, also
  88         // allow system property to be set
  89         props.put(Utils.CLASS_FORMAT_ERROR,
  90                 System.getProperty(Utils.CLASS_FORMAT_ERROR, Pack200.Packer.PASS));
  91 
  92         // Default effort is 5, midway between 1 and 9.
  93         props.put(Pack200.Packer.EFFORT, "5");
  94 
  95         // Define certain attribute layouts by default.
  96         // Do this after the previous props are put in place,
  97         // to allow override if necessary.
  98         String propFile = "intrinsic.properties";
  99 
 100         try (InputStream propStr = PackerImpl.class.getResourceAsStream(propFile)) {


 101             if (propStr == null) {
 102                 throw new RuntimeException(propFile + " cannot be loaded");
 103             }
 104             props.load(propStr);
 105         } catch (IOException ee) {
 106             throw new RuntimeException(ee);
 107         }
 108 
 109         for (Map.Entry<Object, Object> e : props.entrySet()) {
 110             String key = (String) e.getKey();
 111             String val = (String) e.getValue();
 112             if (key.startsWith("attribute.")) {
 113                 e.setValue(Attribute.normalizeLayoutString(val));
 114             }
 115         }
 116 
 117         @SuppressWarnings({"unchecked", "rawtypes"})
 118         HashMap<String, String> temp = new HashMap(props);  // shrink to fit
 119         defaultProps = temp;
 120     }
 121 






 122     PropMap() {
 123         theMap.putAll(defaultProps);
 124     }
 125 
 126     // Return a view of this map which includes only properties
 127     // that begin with the given prefix.  This is easy because
 128     // the map is sorted, and has a subMap accessor.
 129     SortedMap<String, String> prefixMap(String prefix) {
 130         int len = prefix.length();
 131         if (len == 0)
 132             return this;
 133         char nextch = (char)(prefix.charAt(len-1) + 1);
 134         String limit = prefix.substring(0, len-1)+nextch;
 135         //System.out.println(prefix+" => "+subMap(prefix, limit));
 136         return subMap(prefix, limit);
 137     }
 138 
 139     String getProperty(String s) {
 140         return get(s);
 141     }




  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 
  26 package com.sun.java.util.jar.pack;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.PrintStream;
  31 import java.io.PrintWriter;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;
  34 import java.util.ArrayList;
  35 import java.util.Collection;
  36 import java.util.Comparator;
  37 import java.util.HashMap;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.Properties;
  41 import java.util.Set;
  42 import java.util.SortedMap;
  43 import java.util.TreeMap;
  44 import java.util.jar.Pack200;
  45 
  46 /**
  47  * Control block for publishing Pack200 options to the other classes.
  48  */
  49 
  50 final class PropMap implements SortedMap<String, String>  {
  51     private final TreeMap<String, String> theMap = new TreeMap<>();;
  52 
  53     // Override:
  54     public String put(String key, String value) {
  55         String oldValue = theMap.put(key, value);
  56         return oldValue;
  57     }
  58 
  59     // All this other stuff is private to the current package.
  60     // Outide clients of Pack200 do not need to use it; they can
  61     // get by with generic SortedMap functionality.
  62     private static Map<String, String> defaultProps;
  63     static {
  64         Properties props = new Properties();
  65 
  66         // Allow implementation selected via -Dpack.disable.native=true
  67         String propValue = getPropertyValue(Utils.DEBUG_DISABLE_NATIVE, "false");
  68         props.put(Utils.DEBUG_DISABLE_NATIVE,
  69                   String.valueOf(Boolean.parseBoolean(propValue)));
  70 
  71         // Set the DEBUG_VERBOSE from system
  72         int verbose = 0;
  73         try {
  74             verbose = Integer.decode(getPropertyValue(Utils.DEBUG_VERBOSE, "0"));
  75         } catch (NumberFormatException e) {
  76         }
  77         props.put(Utils.DEBUG_VERBOSE, String.valueOf(verbose));
  78 
  79         // The segment size is unlimited
  80         props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
  81 
  82         // Preserve file ordering by default.
  83         props.put(Pack200.Packer.KEEP_FILE_ORDER, Pack200.Packer.TRUE);
  84 
  85         // Preserve all modification times by default.
  86         props.put(Pack200.Packer.MODIFICATION_TIME, Pack200.Packer.KEEP);
  87 
  88         // Preserve deflation hints by default.
  89         props.put(Pack200.Packer.DEFLATE_HINT, Pack200.Packer.KEEP);
  90 
  91         // Pass through files with unrecognized attributes by default.
  92         props.put(Pack200.Packer.UNKNOWN_ATTRIBUTE, Pack200.Packer.PASS);
  93 
  94         // Pass through files with unrecognized format by default, also
  95         // allow system property to be set
  96         props.put(Utils.CLASS_FORMAT_ERROR,
  97                   getPropertyValue(Utils.CLASS_FORMAT_ERROR, Pack200.Packer.PASS));
  98 
  99         // Default effort is 5, midway between 1 and 9.
 100         props.put(Pack200.Packer.EFFORT, "5");
 101 
 102         // Define certain attribute layouts by default.
 103         // Do this after the previous props are put in place,
 104         // to allow override if necessary.
 105         String propFile = "intrinsic.properties";
 106 
 107         PrivilegedAction<InputStream> pa =
 108             () -> PackerImpl.class.getResourceAsStream(propFile);
 109         try (InputStream propStr = AccessController.doPrivileged(pa)) {
 110             if (propStr == null) {
 111                 throw new RuntimeException(propFile + " cannot be loaded");
 112             }
 113             props.load(propStr);
 114         } catch (IOException ee) {
 115             throw new RuntimeException(ee);
 116         }
 117 
 118         for (Map.Entry<Object, Object> e : props.entrySet()) {
 119             String key = (String) e.getKey();
 120             String val = (String) e.getValue();
 121             if (key.startsWith("attribute.")) {
 122                 e.setValue(Attribute.normalizeLayoutString(val));
 123             }
 124         }
 125 
 126         @SuppressWarnings({"unchecked", "rawtypes"})
 127         HashMap<String, String> temp = new HashMap(props);  // shrink to fit
 128         defaultProps = temp;
 129     }
 130 
 131     private static String getPropertyValue(String key, String defaultValue) {
 132         PrivilegedAction<String> pa = () -> System.getProperty(key);
 133         String s = AccessController.doPrivileged(pa);
 134         return s != null ? s : defaultValue;
 135     }
 136 
 137     PropMap() {
 138         theMap.putAll(defaultProps);
 139     }
 140 
 141     // Return a view of this map which includes only properties
 142     // that begin with the given prefix.  This is easy because
 143     // the map is sorted, and has a subMap accessor.
 144     SortedMap<String, String> prefixMap(String prefix) {
 145         int len = prefix.length();
 146         if (len == 0)
 147             return this;
 148         char nextch = (char)(prefix.charAt(len-1) + 1);
 149         String limit = prefix.substring(0, len-1)+nextch;
 150         //System.out.println(prefix+" => "+subMap(prefix, limit));
 151         return subMap(prefix, limit);
 152     }
 153 
 154     String getProperty(String s) {
 155         return get(s);
 156     }


< prev index next >