Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/krb5/SCDynamicStoreConfig.java
38830 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.krb5;2627import java.io.IOException;28import java.util.Collection;29import java.util.Hashtable;30import java.util.Vector;313233public class SCDynamicStoreConfig {34private static native void installNotificationCallback();35private static native Hashtable<String, Object> getKerberosConfig();36private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;3738static {39boolean isMac = java.security.AccessController.doPrivileged(40new java.security.PrivilegedAction<Boolean>() {41public Boolean run() {42String osname = System.getProperty("os.name");43if (osname.contains("OS X")) {44System.loadLibrary("osx");45return true;46}47return false;48}49});50if (isMac) installNotificationCallback();51}5253private static Vector<String> unwrapHost(54Collection<Hashtable<String, String>> c) {55Vector<String> vector = new Vector<String>();56for (Hashtable<String, String> m : c) {57vector.add(m.get("host"));58}59return vector;60}6162/**63* convertRealmConfigs: Maps the Object graph that we get from JNI to the64* object graph that Config expects. Also the items inside the kdc array65* are wrapped inside Hashtables66*/67@SuppressWarnings("unchecked")68private static Hashtable<String, Object>69convertRealmConfigs(Hashtable<String, ?> configs) {70Hashtable<String, Object> realmsTable = new Hashtable<String, Object>();7172for (String realm : configs.keySet()) {73// get the kdc74Hashtable<String, Collection<?>> map =75(Hashtable<String, Collection<?>>) configs.get(realm);76Hashtable<String, Vector<String>> realmMap =77new Hashtable<String, Vector<String>>();7879// put the kdc into the realmMap80Collection<Hashtable<String, String>> kdc =81(Collection<Hashtable<String, String>>) map.get("kdc");82if (kdc != null) realmMap.put("kdc", unwrapHost(kdc));8384// put the admin server into the realmMap85Collection<Hashtable<String, String>> kadmin =86(Collection<Hashtable<String, String>>) map.get("kadmin");87if (kadmin != null) realmMap.put("admin_server", unwrapHost(kadmin));8889// add the full entry to the realmTable90realmsTable.put(realm, realmMap);91}9293return realmsTable;94}9596/**97* Calls down to JNI to get the raw Kerberos Config and maps the object98* graph to the one that Kerberos Config in Java expects99*100* @return101* @throws IOException102*/103public static Hashtable<String, Object> getConfig() throws IOException {104Hashtable<String, Object> stanzaTable = getKerberosConfig();105if (stanzaTable == null) {106throw new IOException(107"Could not load configuration from SCDynamicStore");108}109if (DEBUG) System.out.println("Raw map from JNI: " + stanzaTable);110return convertNativeConfig(stanzaTable);111}112113@SuppressWarnings("unchecked")114private static Hashtable<String, Object> convertNativeConfig(115Hashtable<String, Object> stanzaTable) {116// convert SCDynamicStore realm structure to Java realm structure117Hashtable<String, ?> realms =118(Hashtable<String, ?>) stanzaTable.get("realms");119if (realms != null) {120stanzaTable.remove("realms");121Hashtable<String, Object> realmsTable = convertRealmConfigs(realms);122stanzaTable.put("realms", realmsTable);123}124WrapAllStringInVector(stanzaTable);125if (DEBUG) System.out.println("stanzaTable : " + stanzaTable);126return stanzaTable;127}128129@SuppressWarnings("unchecked")130private static void WrapAllStringInVector(131Hashtable<String, Object> stanzaTable) {132for (String s: stanzaTable.keySet()) {133Object v = stanzaTable.get(s);134if (v instanceof Hashtable) {135WrapAllStringInVector((Hashtable<String,Object>)v);136} else if (v instanceof String) {137Vector<String> vec = new Vector<>();138vec.add((String)v);139stanzaTable.put(s, vec);140}141}142}143}144145146