8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: SetupUtils.java,v 1.7 2009/10/30 21:10:21 weisun2 Exp $
dfc4e0fc3052835b2a069aa9d869fa1161c33fe6Peter Major * Portions Copyrighted 2011-2013 ForgeRock AS
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class SetupUtils implements SetupConstants{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Loads properties from file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param fileName The path to the properties file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return The Properties object of the specified propertoes file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Properties loadProperties(String fileName)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster InputStream propIn = new FileInputStream(fileName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Prompts user for input from system in.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param message The text message to prompt the user.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return The user input.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String getUserInput(String message)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String getUserInput(String message, String def)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String ret = getUserInput(MessageFormat.format(message, def));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets the map for text replacement.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param bundle The ResourceBundle which contains the tokens' name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param confProp The properties which may contain the tokens value.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return The properties object as a map for text replacement.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Properties getTokens(ResourceBundle bundle,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String tokenString = bundle.getString(currentOS + TOKEN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int commaIndex = tokenString.indexOf(",", tokensOffset);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tempVarName = tokenString.substring(tokensOffset, commaIndex).
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tempVarValue = bundle.getString(currentOS + "." +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tempVarValue = evaluatePropertiesValue(tempVarValue, confProp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tempVarName = tokenString.substring(tokensOffset,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tempVarValue = bundle.getString(currentOS + "." +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tempVarValue = evaluatePropertiesValue(tempVarValue,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster commaIndex = tokenString.indexOf(",", tokensOffset);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } while (true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Lookups and replaces the variables in the string
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param value The string may contain variables which need to be replaced.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param lookupProp The properties may contain the real values.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return The String with variables replaced by correct value.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String evaluatePropertiesValue(String value,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (((refPrefix = returnValue.indexOf(VAR_PREFIX)) >= 0)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster && ((refSuffix = returnValue.indexOf(VAR_SUFFIX)) >
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster key = returnValue.substring(refPrefix + VAR_PREFIX.length(),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((lookupProp != null) && (lookupProp.containsKey(key))) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster returnValue = returnValue.replaceAll(REX_VAR_PREFIX + key +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //offset = refSuffix + 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Get the from files list and the to file list.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param fromDir The directory of the source files located
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param toDir The directory of the destinated files located
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param fromFilePattern The pattern of the name of source files.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param toFilePattern The pattern of the name of destinated files.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param fromFilesList (as return) List of files from the source directory.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param toFilesList (as return) List of files of the destinated directory.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void getFiles(File fromDir, File toDir,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster LinkedList fromFilesList, LinkedList toFilesList) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster toFilesList.addLast(new File(toDir, fromFiles[ i ].getName()));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getFiles(fromFiles[ i ], new File(toDir, fromFiles[ i ]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getName()), fromFilePattern, toFilePattern, fromFilesList,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Determines the current operating system.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return The string to represent the current operating system.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((OS_ARCH.toLowerCase().indexOf(X86) >= 0) ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (OS_NAME.toLowerCase().indexOf(WINDOWS) >= 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (OS_NAME.toLowerCase().indexOf(SUNOS) >= 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Transform the file name by using string patterns.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param from The pattern of the source file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param to The pattern of the destinated file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param file The file to be transform.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return The transformed name of the file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String transformFileName(String from, String to, File file) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return transformFileName(from, to, file, DEFAULT_WILD_CARD);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Transform the file name by using string patterns.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param from The pattern of the source file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param to The pattern of the destinated file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param file The file to be transform.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param wildCard The wildcard character is used in the string pattern.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return The transformed name of the file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String transformFileName(String from, String to, File file,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder nameToReturn = new StringBuilder();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean matched = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int matchedIndex = fileName.indexOf(buffer.toString(),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster fileNameOffset = matchedIndex + buffer.length();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster nameToReturn.append(tokensToKeep.removeFirst());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Ungzip the gzip archive from source file to destinated directory.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param srcFile The path to the source gzip archive.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param destDir The destinated directory for the decompression.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void ungzip(String srcFile, String destDir) throws
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tempDestFileName = tempDestFileName.substring(srcFile.lastIndexOf(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster FileOutputStream fout = new FileOutputStream(new File(destDir,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Prints the usage through system out.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param bundle The ResourceBundle which contains the message.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void printUsage(ResourceBundle bundle){
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString("message.info.usage"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Lookups and set the resource bundle variables to the Properties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param bundle The ResourceBundle is going to be evaluated.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param lookupProp The properties may contain the real values and the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * storage of the results.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void evaluateBundleValues(ResourceBundle bundle,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String name = (String) propNames.nextElement();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String value = (String) SetupUtils.evaluatePropertiesValue(bundle
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copy and replace the variables in the scripts.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param bundle The ResourceBundle which contains the prompt messages.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param lookupProp The properties which contains the variables map, file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * patterns, source directory, and destinated directory.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void copyAndFilterScripts(ResourceBundle bundle,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String fromFilePattern = lookupProp.getProperty(currentOS + FROM_FILE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String toFilePattern = lookupProp.getProperty(currentOS + TO_FILE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String tempFromDir = lookupProp.getProperty(currentOS + FROM_DIR);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String tempToDir = lookupProp.getProperty(currentOS + TO_DIR);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Properties tokens = SetupUtils.getTokens(bundle, lookupProp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SetupUtils.getFiles(fromDir, toDir, fromFilePattern, toFilePattern,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ListIterator srcIter = fromFilesList.listIterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ListIterator destIter = toFilesList.listIterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ((srcIter.hasNext()) && (destIter.hasNext())) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster CopyUtils.copyFile(srcFile, destFile, tokens, true, false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Process proc = Runtime.getRuntime().exec("/bin/chmod -R +x " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString("message.info." +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "permission.scripts"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString("message.info." +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "permission.scripts"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //ex.printStackTrace();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString("message.info.success") + " " +