renamed packages to sqlhawk
[sqlHawk.git] / src / uk / co / timwise / sqlhawk / util / ConsolePasswordReader.java
blob6eff0353888015bab9d7769250c4a62d385841f5
1 /*
2 * This file is a part of the SchemaSpy project (http://schemaspy.sourceforge.net).
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 John Currier
5 * SchemaSpy is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * SchemaSpy is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 package uk.co.timwise.sqlhawk.util;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
24 /**
25 * Implementation of {@link PasswordReader} that takes advantage of the
26 * built-in password reading abilities of Java6 (or higher).
28 * Use {@link PasswordReader#getInstance()} to get an instance of
29 * PasswordReader that's appropriate for your JVM
30 * (this one requires a Java6 or higher JVM).
32 * @author John Currier
34 public class ConsolePasswordReader extends PasswordReader {
35 private final Object console;
36 private final Method readPassword;
38 /**
39 * Attempt to resolve the Console methods that were introduced in Java6.
41 * @throws SecurityException
42 * @throws NoSuchMethodException
43 * @throws InvocationTargetException
44 * @throws IllegalAccessException
45 * @throws IllegalArgumentException
47 protected ConsolePasswordReader() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
48 // get the console by calling System.console() (Java6+ method)
49 Method consoleGetter = System.class.getMethod("console", (Class[])null);
50 console = consoleGetter.invoke(null, (Object[])null);
52 // get Console.readPassword(String, Object[]) method
53 Class<?>[] paramTypes = new Class<?>[] {String.class, Object[].class};
54 readPassword = console.getClass().getMethod("readPassword", paramTypes);
57 /**
58 * Attempt to use the previously resolved Console.
59 * If unable to use it then revert to the one implemented in the base class.
61 @Override
62 public char[] readPassword(String fmt, Object... args) {
63 try {
64 return (char[])readPassword.invoke(console, fmt, args);
65 } catch (Throwable exc) {
66 return super.readPassword(fmt, args);