Search in sources :

Example 11 with LineNumberReader

use of java.io.LineNumberReader in project OpenAM by OpenRock.

the class FileUtils method appendLinesToFile.

/**
     * Appends the given set of lines to the specified file.
     * 
     * @param filePath
     * @param linesToAppend
     * @param create should the file be created if it does not exist
     * @return true for success, false otherwise
     */
public static boolean appendLinesToFile(String filePath, String[] linesToAppend, boolean create) {
    boolean result = false;
    try {
        if (linesToAppend != null && linesToAppend.length > 0) {
            LineNumberReader reader = getLineNumReader(filePath, create);
            StringWriter writer = new StringWriter();
            String line = null;
            while ((line = reader.readLine()) != null) {
                writeLine(writer, line);
            }
            for (int i = 0; i < linesToAppend.length; i++) {
                writeLine(writer, linesToAppend[i]);
            }
            String tempFilePath = filePath + ".tmp";
            result = writeToFile(tempFilePath, writer);
            if (result) {
                File tempFile = new File(tempFilePath);
                if (tempFile.exists() && tempFile.isFile()) {
                    result = copyFile(tempFilePath, filePath);
                    if (result) {
                        result = tempFile.delete();
                    }
                }
            }
        }
    } catch (Exception ex) {
        result = false;
        Debug.log("FileUtils.appendLinesToFile() threw exception : ", ex);
    }
    return result;
}
Also used : StringWriter(java.io.StringWriter) File(java.io.File) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 12 with LineNumberReader

use of java.io.LineNumberReader in project OpenAM by OpenRock.

the class FileUtils method getLineByNumber.

/**
     * Method getLineByNumber
     * 
     * 
     * @param filePath
     * @param lineNum
     * 
     * @return
     * 
     */
public static String getLineByNumber(String filePath, int lineNum) {
    String retval = null;
    int curLine = -1;
    try {
        LineNumberReader reader = getLineNumReader(filePath);
        if ((reader != null) && (lineNum >= 1)) {
            retval = reader.readLine();
            curLine = reader.getLineNumber();
            while ((retval != null) && (curLine < lineNum)) {
                retval = reader.readLine();
                curLine = reader.getLineNumber();
            }
            reader.close();
        }
    } catch (Exception ex) {
        Debug.log("FileUtils.getLineByNumber() threw exception : ", ex);
        retval = null;
    }
    return retval;
}
Also used : IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 13 with LineNumberReader

use of java.io.LineNumberReader in project OpenAM by OpenRock.

the class BEAWeblogicPoliciesValidator method validate.

public void validate(Properties containerProp, String instancePath, String domainPath) throws DTException {
    toolOutWriter = WebContainerService.getToolWriter();
    if (domainPath != null) {
        File instanceDir = new File(instancePath);
        if (instanceDir.exists() && instanceDir.isDirectory()) {
            toolOutWriter.printStatusMsg(true, "webcontainer-instance-dir-check");
            String instanceLib_dir = containerProp.getProperty(WebContainerConstant.POLICIES_DIR);
            File instanceLibDir = new File(instanceDir, instanceLib_dir);
            System.out.println();
            if (instanceLibDir.exists() && instanceLibDir.isDirectory()) {
                toolOutWriter.printStatusMsg(true, "webcontainer-instance-lib-dir-check");
                String policiesFileName = containerProp.getProperty(WebContainerConstant.POLICIES_FILE);
                File policiesPatternFile = new File(containerProp.getProperty(WebContainerConstant.POLICIES_PATTERN_FILE));
                FileInputStream fin = null;
                Properties policiesPatternProp = new Properties();
                if (policiesPatternFile.exists() && policiesPatternFile.isFile()) {
                    try {
                        fin = new FileInputStream(policiesPatternFile);
                        policiesPatternProp.load(fin);
                    } catch (IOException ex) {
                        throw new DTException(ex.getMessage());
                    } finally {
                        if (fin != null) {
                            try {
                                fin.close();
                            } catch (IOException ignored) {
                            }
                        }
                    }
                }
                LineNumberReader lnRdr = null;
                try {
                    lnRdr = new LineNumberReader(new FileReader(new File(instanceLibDir, policiesFileName)));
                    String line = null;
                    StringBuffer buffer = null;
                    boolean policiesStarted = false;
                    while ((line = lnRdr.readLine()) != null) {
                        if (!policiesStarted) {
                            String[] validStart = line.split(WebContainerConstant.POLICY_GRANT_START_PATTERN);
                            if ((validStart.length > 1) || (!validStart[0].equals(line))) {
                                buffer = new StringBuffer();
                                buffer.append(line).append("\n");
                                policiesStarted = true;
                            }
                        } else {
                            buffer.append(line).append("\n");
                            String[] validEnd = line.split(WebContainerConstant.POLICY_GRANT_END_PATTERN);
                            if ((validEnd.length > 1) || (!validEnd[0].equals(line))) {
                                policiesStarted = false;
                                String policies = buffer.toString();
                                String[] validPolicies = policies.split(WebContainerConstant.POLICY_GRANT_START_PATTERN + "[{]");
                                if ((validPolicies.length > 1) || (!validPolicies[0].equals(policies))) {
                                    for (Enumeration e = policiesPatternProp.propertyNames(); e.hasMoreElements(); ) {
                                        String name = (String) e.nextElement();
                                        if (name.startsWith(WebContainerConstant.POLICIES_PATTERN)) {
                                            String value = policiesPatternProp.getProperty(name);
                                            String[] answers = policies.split(value);
                                            if ((answers.length > 1) || (!answers[0].equals(policies))) {
                                                policiesPatternProp.remove(name);
                                                int num = Integer.parseInt(name.substring(WebContainerConstant.POLICIES_PATTERN.length(), name.length()));
                                                policiesPatternProp.remove(WebContainerConstant.POLICIES_CLEARTEXT + String.valueOf(num));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    boolean passed = true;
                    for (Enumeration e = policiesPatternProp.propertyNames(); e.hasMoreElements(); ) {
                        String name = (String) e.nextElement();
                        if (name.startsWith(WebContainerConstant.POLICIES_CLEARTEXT)) {
                            if (passed) {
                                toolOutWriter.printError("webcontainer-policies-invalid");
                            }
                            toolOutWriter.printError(policiesPatternProp.getProperty(name));
                            passed = false;
                        }
                    }
                    if (passed) {
                        toolOutWriter.printStatusMsg(true, "webcontainer-policies-check");
                    } else {
                        toolOutWriter.printStatusMsg(false, "webcontainer-policies-check");
                    }
                } catch (IOException ex) {
                    throw new DTException(ex.getMessage());
                } finally {
                    if (lnRdr != null) {
                        try {
                            lnRdr.close();
                        } catch (IOException ignored) {
                        }
                    }
                }
            } else {
                toolOutWriter.printError("webcontainer-instance-lib-dir-invalid");
                toolOutWriter.printStatusMsg(false, "webcontainer-instance-lib-dir-check");
            }
        } else {
            toolOutWriter.printError("webcontainer-instance-dir-invalid");
            toolOutWriter.printStatusMsg(false, "webcontainer-instance-dir-check");
        }
    }
}
Also used : Enumeration(java.util.Enumeration) DTException(com.sun.identity.diagnostic.base.core.common.DTException) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) LineNumberReader(java.io.LineNumberReader) FileReader(java.io.FileReader) File(java.io.File)

Example 14 with LineNumberReader

use of java.io.LineNumberReader in project OpenAM by OpenRock.

the class IBMWebSphereJVMOptionsValidator method validate.

public void validate(Properties containerProp, String profilePath, String serverPath) throws DTException {
    toolOutWriter = WebContainerService.getToolWriter();
    if (serverPath != null) {
        File serverDir = new File(serverPath);
        if (serverDir.exists() && serverDir.isDirectory()) {
            toolOutWriter.printStatusMsg(true, "webcontainer-server-dir-check");
            String serverFileName = containerProp.getProperty(WebContainerConstant.JVMOPTIONS_FILE);
            File jvmoptionsFile = new File(containerProp.getProperty(WebContainerConstant.JVMOPTIONS_PATTERN_FILE));
            FileInputStream fin = null;
            Properties jvmoptionsProp = new Properties();
            if (jvmoptionsFile.exists() && jvmoptionsFile.isFile()) {
                try {
                    fin = new FileInputStream(jvmoptionsFile);
                    jvmoptionsProp.load(fin);
                } catch (IOException ex) {
                    throw new DTException(ex.getMessage());
                } finally {
                    if (fin != null) {
                        try {
                            fin.close();
                        } catch (IOException ignored) {
                        }
                    }
                }
            }
            LineNumberReader lnRdr = null;
            String javahomePath = null;
            try {
                lnRdr = new LineNumberReader(new FileReader(new File(serverDir, serverFileName)));
                String buffer = null;
                while ((buffer = lnRdr.readLine()) != null) {
                    for (Enumeration e = jvmoptionsProp.propertyNames(); e.hasMoreElements(); ) {
                        String name = (String) e.nextElement();
                        if (name.startsWith(WebContainerConstant.JVMOPTIONS_PATTERN)) {
                            String value = jvmoptionsProp.getProperty(name);
                            String[] answers = buffer.split(value);
                            if ((answers.length > 1) || (!answers[0].equals(buffer))) {
                                jvmoptionsProp.remove(name);
                                int num = Integer.parseInt(name.substring(WebContainerConstant.JVMOPTIONS_PATTERN.length(), name.length()));
                                jvmoptionsProp.remove(WebContainerConstant.JVMOPTIONS_CLEARTEXT + String.valueOf(num));
                            }
                        }
                    }
                }
                boolean passed = true;
                for (Enumeration e = jvmoptionsProp.propertyNames(); e.hasMoreElements(); ) {
                    String name = (String) e.nextElement();
                    if (name.startsWith(WebContainerConstant.JVMOPTIONS_CLEARTEXT)) {
                        if (passed) {
                            toolOutWriter.printError("webcontainer-jvm-options-invalid");
                        }
                        toolOutWriter.printError(jvmoptionsProp.getProperty(name));
                        passed = false;
                    }
                }
                if (passed) {
                    toolOutWriter.printStatusMsg(true, "webcontainer-jvm-options-check");
                } else {
                    toolOutWriter.printStatusMsg(false, "webcontainer-jvm-options-check");
                }
            } catch (IOException ex) {
                throw new DTException(ex.getMessage());
            } finally {
                if (lnRdr != null) {
                    try {
                        lnRdr.close();
                    } catch (IOException ignored) {
                    }
                }
            }
        } else {
            toolOutWriter.printError("webcontainer-server-dir-invalid");
            toolOutWriter.printStatusMsg(false, "webcontainer-server-dir-check");
        }
    } else {
        toolOutWriter.printError("webcontainer-server-dir-invalid");
        toolOutWriter.printStatusMsg(false, "webcontainer-server-dir-check");
    }
}
Also used : Enumeration(java.util.Enumeration) DTException(com.sun.identity.diagnostic.base.core.common.DTException) FileReader(java.io.FileReader) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) LineNumberReader(java.io.LineNumberReader)

Example 15 with LineNumberReader

use of java.io.LineNumberReader in project OpenAM by OpenRock.

the class IBMWebSphereJVMVersionValidator method validate.

public void validate(Properties containerProp, String profilePath, String serverPath) throws DTException {
    toolOutWriter = WebContainerService.getToolWriter();
    if (profilePath != null) {
        File profileDir = new File(profilePath);
        if (profileDir.exists() && profileDir.isDirectory()) {
            toolOutWriter.printStatusMsg(true, "webcontainer-profile-dir-check");
            String javahome_dir = containerProp.getProperty(WebContainerConstant.JAVAHOME_DIR);
            File javahomeDir = new File(profileDir, javahome_dir);
            if (javahomeDir.exists() && javahomeDir.isDirectory()) {
                toolOutWriter.printStatusMsg(true, "webcontainer-properties-dir-check");
                String javahomeFileName = containerProp.getProperty(WebContainerConstant.JAVAHOME_FILE);
                if (WebContainerConstant.OS_NAME.toLowerCase().indexOf(WebContainerConstant.WINDOWS) >= 0) {
                    javahomeFileName = javahomeFileName + ".bat";
                } else {
                    javahomeFileName = javahomeFileName + ".sh";
                }
                LineNumberReader lnRdr = null;
                String javahomePath = null;
                try {
                    lnRdr = new LineNumberReader(new FileReader(new File(javahomeDir, javahomeFileName)));
                    String buffer = null;
                    String javahomeTag = containerProp.getProperty(WebContainerConstant.JAVAHOME_TAG);
                    while ((buffer = lnRdr.readLine()) != null) {
                        String[] s = buffer.split("\\s*" + javahomeTag + "\\s*=");
                        if ((s.length > 1) || (!s[0].equals(buffer))) {
                            javahomePath = buffer.substring(buffer.indexOf("=") + 1, buffer.length()).trim();
                            break;
                        }
                    }
                } catch (IOException ex) {
                    throw new DTException(ex.getMessage());
                } finally {
                    if (lnRdr != null) {
                        try {
                            lnRdr.close();
                        } catch (IOException ignored) {
                        }
                    }
                }
                LineNumberReader processLnRdr = null;
                try {
                    toolOutWriter = WebContainerService.getToolWriter();
                    if (javahomePath.startsWith("\"")) {
                        javahomePath = javahomePath.substring(1, javahomePath.length());
                    }
                    if (javahomePath.endsWith("\"")) {
                        javahomePath = javahomePath.substring(0, javahomePath.length() - 1);
                    }
                    if (javahomePath.endsWith("/") || (javahomePath.endsWith("\\"))) {
                        javahomePath = javahomePath.substring(0, javahomePath.length() - 1);
                    }
                    Process pro = Runtime.getRuntime().exec(javahomePath + "/bin/java -version");
                    processLnRdr = new LineNumberReader(new InputStreamReader(pro.getErrorStream()));
                    String buffer = null;
                    String jvmVersion = null;
                    while ((buffer = processLnRdr.readLine()) != null) {
                        int startIndex = buffer.indexOf("\"");
                        int endIndex = buffer.indexOf("\"", startIndex + 1);
                        if ((startIndex >= 0) && (endIndex >= 0) && (endIndex > startIndex)) {
                            jvmVersion = buffer.substring(startIndex + 1, endIndex);
                            break;
                        }
                    }
                    String startWithMinor = jvmVersion.substring(jvmVersion.indexOf(".") + 1, jvmVersion.length());
                    int index = -1;
                    if ((index = startWithMinor.indexOf(".")) >= 0) {
                        startWithMinor = startWithMinor.substring(0, index);
                    }
                    if ((index = startWithMinor.indexOf("_")) >= 0) {
                        startWithMinor = startWithMinor.substring(0, index + 1);
                    }
                    if (Integer.valueOf(startWithMinor) >= WebContainerConstant.SUPPORTED_JVM_MINOR_VERSION) {
                        toolOutWriter.printResult(jvmVersion);
                        toolOutWriter.printStatusMsg(true, "webcontainer-container-version");
                    } else {
                        toolOutWriter.printError(jvmVersion);
                        toolOutWriter.printStatusMsg(false, "webcontainer-container-version");
                    }
                } catch (IOException ex) {
                    throw new DTException(ex.getMessage());
                } finally {
                    if (processLnRdr != null) {
                        try {
                            processLnRdr.close();
                        } catch (Exception ignored) {
                        }
                    }
                }
            } else {
                toolOutWriter.printError("webcontainer-properties-dir-invalid");
                toolOutWriter.printStatusMsg(false, "webcontainer-properties-dir-check");
            }
        } else {
            toolOutWriter.printError("webcontainer-profile-dir-invalid");
            toolOutWriter.printStatusMsg(false, "webcontainer-profile-dir-check");
        }
    } else {
        toolOutWriter.printError("webcontainer-profile-dir-invalid");
        toolOutWriter.printStatusMsg(false, "webcontainer-profile-dir-check");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DTException(com.sun.identity.diagnostic.base.core.common.DTException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) DTException(com.sun.identity.diagnostic.base.core.common.DTException) LineNumberReader(java.io.LineNumberReader)

Aggregations

LineNumberReader (java.io.LineNumberReader)401 IOException (java.io.IOException)203 InputStreamReader (java.io.InputStreamReader)167 FileReader (java.io.FileReader)102 File (java.io.File)79 InputStream (java.io.InputStream)64 StringReader (java.io.StringReader)64 ArrayList (java.util.ArrayList)54 FileInputStream (java.io.FileInputStream)33 BufferedReader (java.io.BufferedReader)27 PrintWriter (java.io.PrintWriter)25 HashMap (java.util.HashMap)22 FileNotFoundException (java.io.FileNotFoundException)20 BufferedWriter (java.io.BufferedWriter)16 FileWriter (java.io.FileWriter)16 Pattern (java.util.regex.Pattern)16 Test (org.junit.jupiter.api.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Reader (java.io.Reader)14