ConsoleCallbackHandler.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* ConsoleCallbackHandler.java --
  2. Copyright (C) 2005, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.security.auth.callback;
  32. import java.io.BufferedReader;
  33. import java.io.InputStreamReader;
  34. import java.io.IOException;
  35. import java.io.PrintStream;
  36. import java.util.Iterator;
  37. import java.util.Locale;
  38. import java.util.StringTokenizer;
  39. import java.util.TreeSet;
  40. import javax.security.auth.callback.ChoiceCallback;
  41. import javax.security.auth.callback.ConfirmationCallback;
  42. import javax.security.auth.callback.LanguageCallback;
  43. import javax.security.auth.callback.NameCallback;
  44. import javax.security.auth.callback.PasswordCallback;
  45. import javax.security.auth.callback.TextInputCallback;
  46. import javax.security.auth.callback.TextOutputCallback;
  47. /**
  48. * An implementation of {@link CallbackHandler} that reads and writes
  49. * information to and from <code>System.in</code> and <code>System.out</code>.
  50. */
  51. public class ConsoleCallbackHandler extends AbstractCallbackHandler
  52. {
  53. // Fields.
  54. // -------------------------------------------------------------------------
  55. private final PrintStream out;
  56. // Constructors.
  57. // -------------------------------------------------------------------------
  58. public ConsoleCallbackHandler()
  59. {
  60. this (System.out);
  61. }
  62. public ConsoleCallbackHandler (final PrintStream out)
  63. {
  64. super ("CONSOLE");
  65. this.out = out;
  66. }
  67. // Instance methods.
  68. // -------------------------------------------------------------------------
  69. protected void handleChoice(ChoiceCallback c) throws IOException
  70. {
  71. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  72. out.println(c.getPrompt());
  73. out.print('(');
  74. String[] choices = c.getChoices();
  75. for (int i = 0; i < choices.length; i++)
  76. {
  77. out.print(choices[i]);
  78. if (i != choices.length - 1)
  79. out.print(", ");
  80. }
  81. out.print(") ");
  82. if (c.getDefaultChoice() >= 0 && c.getDefaultChoice() < choices.length)
  83. {
  84. out.print('[');
  85. out.print(choices[c.getDefaultChoice()]);
  86. out.print("] ");
  87. }
  88. String reply = in.readLine();
  89. if (reply == null || reply.length() == 0)
  90. {
  91. c.setSelectedIndex(c.getDefaultChoice());
  92. return;
  93. }
  94. if (!c.allowMultipleSelections())
  95. {
  96. for (int i = 0; i < choices.length; i++)
  97. {
  98. if (reply.trim().equals(choices[i]))
  99. {
  100. c.setSelectedIndex(i);
  101. return;
  102. }
  103. }
  104. c.setSelectedIndex(c.getDefaultChoice());
  105. }
  106. else
  107. {
  108. TreeSet indices = new TreeSet();
  109. StringTokenizer tok = new StringTokenizer(reply, ",");
  110. String[] replies = new String[tok.countTokens()];
  111. int idx = 0;
  112. while (tok.hasMoreTokens())
  113. {
  114. replies[idx++] = tok.nextToken().trim();
  115. }
  116. for (int i = 0; i < choices.length; i++)
  117. for (int j = 0; j < replies.length; i++)
  118. {
  119. if (choices[i].equals(replies[j]))
  120. {
  121. indices.add(Integer.valueOf(i));
  122. }
  123. }
  124. if (indices.size() == 0)
  125. c.setSelectedIndex(c.getDefaultChoice());
  126. else
  127. {
  128. int[] ii = new int[indices.size()];
  129. int i = 0;
  130. for (Iterator it = indices.iterator(); it.hasNext(); )
  131. ii[i++] = ((Integer) it.next()).intValue();
  132. c.setSelectedIndexes(ii);
  133. }
  134. }
  135. }
  136. protected void handleConfirmation(ConfirmationCallback c) throws IOException
  137. {
  138. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  139. if (c.getPrompt() != null)
  140. out.print(c.getPrompt());
  141. String[] choices = null;
  142. int[] values = null;
  143. switch (c.getOptionType())
  144. {
  145. case ConfirmationCallback.OK_CANCEL_OPTION:
  146. out.print(messages.getString("callback.okCancel"));
  147. choices = new String[] {
  148. messages.getString("callback.ok"),
  149. messages.getString("callback.cancel"),
  150. messages.getString("callback.shortOk"),
  151. messages.getString("callback.shortCancel")
  152. };
  153. values = new int[] {
  154. ConfirmationCallback.OK, ConfirmationCallback.CANCEL,
  155. ConfirmationCallback.OK, ConfirmationCallback.CANCEL
  156. };
  157. break;
  158. case ConfirmationCallback.YES_NO_CANCEL_OPTION:
  159. out.print(messages.getString("callback.yesNoCancel"));
  160. choices = new String[] {
  161. messages.getString("callback.yes"),
  162. messages.getString("callback.no"),
  163. messages.getString("callback.cancel"),
  164. messages.getString("callback.shortYes"),
  165. messages.getString("callback.shortNo"),
  166. messages.getString("callback.shortCancel")
  167. };
  168. values = new int[] {
  169. ConfirmationCallback.YES, ConfirmationCallback.NO,
  170. ConfirmationCallback.CANCEL, ConfirmationCallback.YES,
  171. ConfirmationCallback.NO, ConfirmationCallback.CANCEL
  172. };
  173. break;
  174. case ConfirmationCallback.YES_NO_OPTION:
  175. out.print(messages.getString("callback.yesNo"));
  176. choices = new String[] { messages.getString("callback.yes"),
  177. messages.getString("callback.no"),
  178. messages.getString("callback.shortYes"),
  179. messages.getString("callback.shortNo") };
  180. values = new int[] { ConfirmationCallback.YES,
  181. ConfirmationCallback.NO,
  182. ConfirmationCallback.YES,
  183. ConfirmationCallback.NO };
  184. int defaultOption = c.getDefaultOption();
  185. if (defaultOption > -1 && defaultOption < choices.length)
  186. {
  187. out.print("[");
  188. out.print(choices[defaultOption]);
  189. out.print("] ");
  190. }
  191. break;
  192. case ConfirmationCallback.UNSPECIFIED_OPTION:
  193. choices = c.getOptions();
  194. values = new int[choices.length];
  195. for (int i = 0; i < values.length; i++)
  196. values[i] = i;
  197. out.print('(');
  198. for (int i = 0; i < choices.length; i++)
  199. {
  200. out.print(choices[i]);
  201. if (i != choices.length - 1)
  202. out.print(", ");
  203. }
  204. out.print(") [");
  205. out.print(choices[c.getDefaultOption()]);
  206. out.print("] ");
  207. break;
  208. default:
  209. throw new IllegalArgumentException();
  210. }
  211. String reply = in.readLine();
  212. if (reply == null)
  213. {
  214. c.setSelectedIndex(c.getDefaultOption());
  215. return;
  216. }
  217. reply = reply.trim();
  218. for (int i = 0; i < choices.length; i++)
  219. if (reply.equalsIgnoreCase(choices[i]))
  220. {
  221. c.setSelectedIndex(values[i]);
  222. return;
  223. }
  224. c.setSelectedIndex(c.getDefaultOption());
  225. }
  226. protected void handleLanguage(LanguageCallback c) throws IOException
  227. {
  228. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  229. out.print(messages.getString("callback.language"));
  230. String reply = null;
  231. reply = in.readLine();
  232. if (reply == null)
  233. {
  234. c.setLocale(Locale.getDefault());
  235. }
  236. else
  237. {
  238. c.setLocale(new Locale(reply.trim()));
  239. }
  240. }
  241. protected void handleName(NameCallback c) throws IOException
  242. {
  243. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  244. out.print(c.getPrompt());
  245. String name = in.readLine();
  246. if (name != null)
  247. c.setName(name.trim());
  248. }
  249. protected void handlePassword(PasswordCallback c) throws IOException
  250. {
  251. out.print(c.getPrompt());
  252. BufferedReader in =
  253. new BufferedReader(new InputStreamReader(System.in));
  254. String pass = in.readLine();
  255. c.setPassword(pass.toCharArray());
  256. }
  257. protected void handleTextInput(TextInputCallback c) throws IOException
  258. {
  259. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  260. out.print(c.getPrompt());
  261. String text = in.readLine();
  262. if (text != null)
  263. c.setText(text);
  264. }
  265. protected void handleTextOutput(TextOutputCallback c)
  266. {
  267. out.print(c.getMessage());
  268. }
  269. }