Frames | No Frames |
1: /* AbstractCallbackHandler.java -- 2: Copyright (C) 2005, 2006 Free Software Foundation, Inc. 3: 4: This file is a part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2 of the License, or (at 9: your option) any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; if not, write to the Free Software 18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19: USA 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.javax.security.auth.callback; 40: 41: import gnu.java.security.Engine; 42: 43: import java.io.IOException; 44: import java.lang.reflect.InvocationTargetException; 45: import java.util.PropertyResourceBundle; 46: import java.util.ResourceBundle; 47: 48: import java.security.NoSuchAlgorithmException; 49: import java.security.NoSuchProviderException; 50: import java.security.Provider; 51: import java.security.Security; 52: 53: import javax.security.auth.callback.Callback; 54: import javax.security.auth.callback.CallbackHandler; 55: import javax.security.auth.callback.ChoiceCallback; 56: import javax.security.auth.callback.ConfirmationCallback; 57: import javax.security.auth.callback.LanguageCallback; 58: import javax.security.auth.callback.NameCallback; 59: import javax.security.auth.callback.PasswordCallback; 60: import javax.security.auth.callback.TextInputCallback; 61: import javax.security.auth.callback.TextOutputCallback; 62: import javax.security.auth.callback.UnsupportedCallbackException; 63: 64: public abstract class AbstractCallbackHandler implements CallbackHandler 65: { 66: 67: // Fields. 68: // ------------------------------------------------------------------------- 69: 70: private static final String SERVICE = "CallbackHandler"; 71: 72: protected final ResourceBundle messages; 73: 74: private final String name; 75: 76: // Constructors. 77: // ------------------------------------------------------------------------- 78: 79: protected AbstractCallbackHandler (final String name) 80: { 81: super(); 82: messages = PropertyResourceBundle.getBundle("gnu/javax/security/auth/callback/MessagesBundle"); 83: this.name = name; 84: } 85: 86: // Class methods. 87: // ------------------------------------------------------------------------- 88: 89: public static CallbackHandler getInstance(String type) 90: throws NoSuchAlgorithmException 91: { 92: Provider[] p = Security.getProviders(); 93: for (int i = 0; i < p.length; i++) 94: { 95: try 96: { 97: return getInstance(type, p[i]); 98: } 99: catch (NoSuchAlgorithmException ignored) 100: { 101: } 102: } 103: throw new NoSuchAlgorithmException(type); 104: } 105: 106: public static CallbackHandler getInstance(String type, String provider) 107: throws NoSuchAlgorithmException, NoSuchProviderException 108: { 109: Provider p = Security.getProvider(provider); 110: if (p == null) 111: { 112: throw new NoSuchProviderException(provider); 113: } 114: return getInstance(type, p); 115: } 116: 117: public static CallbackHandler getInstance(String type, Provider provider) 118: throws NoSuchAlgorithmException 119: { 120: try 121: { 122: return (CallbackHandler) Engine.getInstance(SERVICE, type, provider); 123: } 124: catch (InvocationTargetException ite) 125: { 126: Throwable cause = ite.getCause(); 127: if (cause instanceof NoSuchAlgorithmException) 128: throw (NoSuchAlgorithmException) cause; 129: NoSuchAlgorithmException nsae = new NoSuchAlgorithmException(type); 130: if (cause != null) 131: nsae.initCause (cause); 132: throw nsae; 133: } 134: catch (ClassCastException cce) 135: { 136: NoSuchAlgorithmException nsae = new NoSuchAlgorithmException(type); 137: nsae.initCause (cce); 138: throw nsae; 139: } 140: } 141: 142: // Instance methods. 143: // ------------------------------------------------------------------------- 144: 145: public void handle(Callback[] callbacks) 146: throws IOException, UnsupportedCallbackException 147: { 148: if (callbacks == null) 149: throw new NullPointerException(); 150: for (int i = 0; i < callbacks.length; i++) 151: { 152: if (callbacks[i] == null) 153: continue; 154: if (callbacks[i] instanceof ChoiceCallback) 155: handleChoice((ChoiceCallback) callbacks[i]); 156: else if (callbacks[i] instanceof ConfirmationCallback) 157: handleConfirmation((ConfirmationCallback) callbacks[i]); 158: else if (callbacks[i] instanceof LanguageCallback) 159: handleLanguage((LanguageCallback) callbacks[i]); 160: else if (callbacks[i] instanceof NameCallback) 161: handleName((NameCallback) callbacks[i]); 162: else if (callbacks[i] instanceof PasswordCallback) 163: handlePassword((PasswordCallback) callbacks[i]); 164: else if (callbacks[i] instanceof TextInputCallback) 165: handleTextInput((TextInputCallback) callbacks[i]); 166: else if (callbacks[i] instanceof TextOutputCallback) 167: handleTextOutput((TextOutputCallback) callbacks[i]); 168: else 169: handleOther(callbacks[i]); 170: } 171: } 172: 173: public final String getName () 174: { 175: return name; 176: } 177: 178: // Abstract methods. 179: // ------------------------------------------------------------------------- 180: 181: /** 182: * Handles a {@link ChoiceCallback}. 183: * 184: * @param callback The choice callback. 185: * @throws IOException If an I/O error occurs. 186: */ 187: protected abstract void handleChoice(ChoiceCallback callback) 188: throws IOException; 189: 190: /** 191: * Handles a {@link ConfirmationCallback}. 192: * 193: * @param callback The confirmation callback. 194: * @throws IOException If an I/O error occurs. 195: */ 196: protected abstract void handleConfirmation(ConfirmationCallback callback) 197: throws IOException; 198: 199: /** 200: * Handles a {@link LanguageCallback}. 201: * 202: * @param callback The language callback. 203: * @throws IOException If an I/O error occurs. 204: */ 205: protected abstract void handleLanguage(LanguageCallback callback) 206: throws IOException; 207: 208: /** 209: * Handles a {@link NameCallback}. 210: * 211: * @param callback The name callback. 212: * @throws IOException If an I/O error occurs. 213: */ 214: protected abstract void handleName(NameCallback callback) 215: throws IOException; 216: 217: /** 218: * Handles a {@link PasswordCallback}. 219: * 220: * @param callback The password callback. 221: * @throws IOException If an I/O error occurs. 222: */ 223: protected abstract void handlePassword(PasswordCallback callback) 224: throws IOException; 225: 226: /** 227: * Handles a {@link TextInputCallback}. 228: * 229: * @param callback The text input callback. 230: * @throws IOException If an I/O error occurs. 231: */ 232: protected abstract void handleTextInput(TextInputCallback callback) 233: throws IOException; 234: 235: /** 236: * Handles a {@link TextOutputCallback}. 237: * 238: * @param callback The text output callback. 239: * @throws IOException If an I/O error occurs. 240: */ 241: protected abstract void handleTextOutput(TextOutputCallback callback) 242: throws IOException; 243: 244: /** 245: * Handles an unknown callback. The default implementation simply throws 246: * an {@link UnsupportedCallbackException}. 247: * 248: * @param callback The callback to handle. 249: * @throws IOException If an I/O error occurs. 250: * @throws UnsupportedCallbackException If the specified callback is not 251: * supported. 252: */ 253: protected void handleOther(Callback callback) 254: throws IOException, UnsupportedCallbackException 255: { 256: throw new UnsupportedCallbackException(callback); 257: } 258: }