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 26package sun.util.resources; 27 28import java.io.InputStream; 29import java.security.AccessController; 30import java.security.PrivilegedActionException; 31import java.security.PrivilegedExceptionAction; 32import java.util.Collections; 33import java.util.Enumeration; 34import java.util.ResourceBundle; 35import java.util.Set; 36 37/** 38 * BreakIteratorResourceBundle is an abstract class for loading BreakIterator 39 * data (rules or dictionary) from each module. An implementation class must 40 * implement getBreakIteratorInfo() that returns an instance of the 41 * corresponding BreakIteratorInfo (basename). The data name is taken from the 42 * BreakIteratorInfo instance. 43 * 44 * <p>For example, if the given key is "WordDictionary" and Locale is "th", the 45 * data name is taken from a BreakIteratorInfo_th and the key's value is 46 * "thai_dict". Its data thai_dict is loaded from the Module of the 47 * implementation class of this class. 48 */ 49 50public abstract class BreakIteratorResourceBundle extends ResourceBundle { 51 // If any keys that are not for data names are added to BreakIteratorInfo*, 52 // those keys must be added to NON_DATA_KEYS. 53 private static final Set<String> NON_DATA_KEYS = Set.of("BreakIteratorClasses"); 54 55 private volatile Set<String> keys; 56 57 /** 58 * Returns an instance of the corresponding {@code BreakIteratorInfo} (basename). 59 * The instance shouldn't have its parent. 60 */ 61 protected abstract ResourceBundle getBreakIteratorInfo(); 62 63 @Override 64 protected Object handleGetObject(String key) { 65 if (NON_DATA_KEYS.contains(key)) { 66 return null; 67 } 68 ResourceBundle info = getBreakIteratorInfo(); 69 if (!info.containsKey(key)) { 70 return null; 71 } 72 String path = getClass().getPackage().getName().replace('.', '/') 73 + '/' + info.getString(key); 74 byte[] data; 75 try (InputStream is = getResourceAsStream(path)) { 76 data = is.readAllBytes(); 77 } catch (Exception e) { 78 throw new InternalError("Can't load " + path, e); 79 } 80 return data; 81 } 82 83 private InputStream getResourceAsStream(String path) throws Exception { 84 PrivilegedExceptionAction<InputStream> pa; 85 pa = () -> getClass().getModule().getResourceAsStream(path); 86 InputStream is; 87 try { 88 is = AccessController.doPrivileged(pa); 89 } catch (PrivilegedActionException e) { 90 throw e.getException(); 91 } 92 return is; 93 } 94 95 @Override 96 public Enumeration<String> getKeys() { 97 return Collections.enumeration(keySet()); 98 } 99 100 @Override 101 protected Set<String> handleKeySet() { 102 if (keys == null) { 103 ResourceBundle info = getBreakIteratorInfo(); 104 Set<String> k = info.keySet(); 105 k.removeAll(NON_DATA_KEYS); 106 synchronized (this) { 107 if (keys == null) { 108 keys = k; 109 } 110 } 111 } 112 return keys; 113 } 114} 115