public static String getPassword(String prompt) throws IOException { String password = null; String javaVers = System.getProperty("java.runtime.version"); if (LOG.isDebugEnabled()) { LOG.debug("About to attempt to read password under Java " + "version: "+javaVers); } // Split version string into components. String is of the form // MAJOR_VERSION_NUMBER . MINOR_VERSION_NUMBR . REVISION_STRING // e.g. 1.6.0_17-b04-248-10M3025 String[] versionMajAndMin = javaVers.split("\\."); int majorVersion = Integer.parseInt(versionMajAndMin[0]); int minorVersion = Integer.parseInt(versionMajAndMin[1]); if (majorVersion > 1 || minorVersion >= 6) { password = getPasswordJDK16OrLater(prompt); } else { password = getPasswordBeforeJDK16(prompt); } return password; }
private static String getPasswordJDK16OrLater(String prompt) throws IOException { String password = null; try { Class consoleClass = Class.forName("java.io.Console"); Method consoleReadPassword = consoleClass.getDeclaredMethod("readPassword", new Class[] {}); Method consoleMethod = System.class.getDeclaredMethod("console", new Class[] {});; Object consoleObject = consoleMethod.invoke(null, new Object[] {}); if (consoleObject == null) { if (LOG.isDebugEnabled()) { LOG.debug("java.io.Console using reflection returned null, " + "so that password will be fetched using the pre-JDK 1.6 version"); } password = getPasswordBeforeJDK16(prompt); } else { System.out.println(prompt + " (typing will be hidden):"); Object passwordObj = consoleReadPassword.invoke(consoleObject, new Object[] {}); char[] passwordChars = (char[]) passwordObj; password = String.valueOf(passwordChars); } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new IOException("Encountered trouble using relection to generate java.io.Console class"); } return password; }
String credentialsPassword = PasswordInput.getPassword( "Password for credentials file '" + credentialsFile + "'"); try { String password2 = "password2"; while (!password1.equals(password2)) { password1 = PasswordInput.getPassword("Encryption password"); password2 = PasswordInput.getPassword("Confirm password"); if (!password1.equals(password2)) { System.out.println("The original and confirmation passwords do not match, try again.");
private static String getPasswordJDK16OrLater(String prompt) throws IOException { String password = null; try { Class consoleClass = Class.forName("java.io.Console"); Method consoleReadPassword = consoleClass.getDeclaredMethod("readPassword", new Class[] {}); Method consoleMethod = System.class.getDeclaredMethod("console", new Class[] {});; Object consoleObject = consoleMethod.invoke(null, new Object[] {}); if (consoleObject == null) { if (LOG.isDebugEnabled()) { LOG.debug("java.io.Console using reflection returned null, " + "so that password will be fetched using the pre-JDK 1.6 version"); } password = getPasswordBeforeJDK16(prompt); } else { System.out.println(prompt + " (typing will be hidden):"); Object passwordObj = consoleReadPassword.invoke(consoleObject, new Object[] {}); char[] passwordChars = (char[]) passwordObj; password = String.valueOf(passwordChars); } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new IOException("Encountered trouble using relection to generate java.io.Console class"); } return password; }
String credentialsPassword = PasswordInput.getPassword( "Password for credentials file '" + credentialsFile + "'"); try { String password2 = "password2"; while (!password1.equals(password2)) { password1 = PasswordInput.getPassword("Encryption password"); password2 = PasswordInput.getPassword("Confirm password"); if (!password1.equals(password2)) { System.out.println("The original and confirmation passwords do not match, try again.");
public static String getPassword(String prompt) throws IOException { String password = null; String javaVers = System.getProperty("java.runtime.version"); if (LOG.isDebugEnabled()) { LOG.debug("About to attempt to read password under Java " + "version: "+javaVers); } // Split version string into components. String is of the form // MAJOR_VERSION_NUMBER . MINOR_VERSION_NUMBR . REVISION_STRING // e.g. 1.6.0_17-b04-248-10M3025 String[] versionMajAndMin = javaVers.split("\\."); int majorVersion = Integer.parseInt(versionMajAndMin[0]); int minorVersion = Integer.parseInt(versionMajAndMin[1]); if (majorVersion > 1 || minorVersion >= 6) { password = getPasswordJDK16OrLater(prompt); } else { password = getPasswordBeforeJDK16(prompt); } return password; }