1 /* AbstractCallbackHandler.java --
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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
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. */
39 package gnu
.javax
.security
.auth
.callback
;
41 import gnu
.java
.security
.Engine
;
43 import java
.io
.IOException
;
44 import java
.lang
.reflect
.InvocationTargetException
;
45 import java
.util
.PropertyResourceBundle
;
46 import java
.util
.ResourceBundle
;
48 import java
.security
.NoSuchAlgorithmException
;
49 import java
.security
.NoSuchProviderException
;
50 import java
.security
.Provider
;
51 import java
.security
.Security
;
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
;
64 public abstract class AbstractCallbackHandler
implements CallbackHandler
68 // -------------------------------------------------------------------------
70 private static final String SERVICE
= "CallbackHandler";
72 protected final ResourceBundle messages
;
74 private final String name
;
77 // -------------------------------------------------------------------------
79 protected AbstractCallbackHandler (final String name
)
82 messages
= PropertyResourceBundle
.getBundle("gnu/javax/security/auth/callback/MessagesBundle");
87 // -------------------------------------------------------------------------
89 public static CallbackHandler
getInstance(String type
)
90 throws NoSuchAlgorithmException
92 Provider
[] p
= Security
.getProviders();
93 for (int i
= 0; i
< p
.length
; i
++)
97 return getInstance(type
, p
[i
]);
99 catch (NoSuchAlgorithmException ignored
)
103 throw new NoSuchAlgorithmException(type
);
106 public static CallbackHandler
getInstance(String type
, String provider
)
107 throws NoSuchAlgorithmException
, NoSuchProviderException
109 Provider p
= Security
.getProvider(provider
);
112 throw new NoSuchProviderException(provider
);
114 return getInstance(type
, p
);
117 public static CallbackHandler
getInstance(String type
, Provider provider
)
118 throws NoSuchAlgorithmException
122 return (CallbackHandler
) Engine
.getInstance(SERVICE
, type
, provider
);
124 catch (InvocationTargetException ite
)
126 Throwable cause
= ite
.getCause();
127 if (cause
instanceof NoSuchAlgorithmException
)
128 throw (NoSuchAlgorithmException
) cause
;
129 NoSuchAlgorithmException nsae
= new NoSuchAlgorithmException(type
);
131 nsae
.initCause (cause
);
134 catch (ClassCastException cce
)
136 NoSuchAlgorithmException nsae
= new NoSuchAlgorithmException(type
);
137 nsae
.initCause (cce
);
143 // -------------------------------------------------------------------------
145 public void handle(Callback
[] callbacks
)
146 throws IOException
, UnsupportedCallbackException
148 if (callbacks
== null)
149 throw new NullPointerException();
150 for (int i
= 0; i
< callbacks
.length
; i
++)
152 if (callbacks
[i
] == null)
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
]);
169 handleOther(callbacks
[i
]);
173 public final String
getName ()
179 // -------------------------------------------------------------------------
182 * Handles a {@link ChoiceCallback}.
184 * @param callback The choice callback.
185 * @throws IOException If an I/O error occurs.
187 protected abstract void handleChoice(ChoiceCallback callback
)
191 * Handles a {@link ConfirmationCallback}.
193 * @param callback The confirmation callback.
194 * @throws IOException If an I/O error occurs.
196 protected abstract void handleConfirmation(ConfirmationCallback callback
)
200 * Handles a {@link LanguageCallback}.
202 * @param callback The language callback.
203 * @throws IOException If an I/O error occurs.
205 protected abstract void handleLanguage(LanguageCallback callback
)
209 * Handles a {@link NameCallback}.
211 * @param callback The name callback.
212 * @throws IOException If an I/O error occurs.
214 protected abstract void handleName(NameCallback callback
)
218 * Handles a {@link PasswordCallback}.
220 * @param callback The password callback.
221 * @throws IOException If an I/O error occurs.
223 protected abstract void handlePassword(PasswordCallback callback
)
227 * Handles a {@link TextInputCallback}.
229 * @param callback The text input callback.
230 * @throws IOException If an I/O error occurs.
232 protected abstract void handleTextInput(TextInputCallback callback
)
236 * Handles a {@link TextOutputCallback}.
238 * @param callback The text output callback.
239 * @throws IOException If an I/O error occurs.
241 protected abstract void handleTextOutput(TextOutputCallback callback
)
245 * Handles an unknown callback. The default implementation simply throws
246 * an {@link UnsupportedCallbackException}.
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
253 protected void handleOther(Callback callback
)
254 throws IOException
, UnsupportedCallbackException
256 throw new UnsupportedCallbackException(callback
);