Search in sources :

Example 16 with LineNumberReader

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

the class SunAppSvrPoliciesValidator method validate.

public void validate(Properties containerProp, String containerPath, String domainPath) throws DTException {
    toolOutWriter = WebContainerService.getToolWriter();
    if (domainPath != null) {
        File domainDir = new File(domainPath);
        if (domainDir.exists() && domainDir.isDirectory()) {
            toolOutWriter.printStatusMsg(true, "webcontainer-domain-dir-check");
            String domainConfig_dir = containerProp.getProperty(WebContainerConstant.POLICIES_DIR);
            File domainConfigDir = new File(domainDir, domainConfig_dir);
            if (domainConfigDir.exists() && domainConfigDir.isDirectory()) {
                toolOutWriter.printStatusMsg(true, "webcontainer-domain-config-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(domainConfigDir, 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-domain-config-dir-invalid");
                toolOutWriter.printStatusMsg(false, "webcontainer-domain-config-dir-check");
            }
        } else {
            toolOutWriter.printError("webcontainer-domain-dir-invalid");
            toolOutWriter.printStatusMsg(false, "webcontainer-domain-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 17 with LineNumberReader

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

the class SunWebServerJVMOptionsValidator method validate.

public void validate(Properties containerProp, String containerPath, String domainPath) throws DTException {
    toolOutWriter = WebContainerService.getToolWriter();
    if (domainPath != null) {
        File domainDir = new File(domainPath);
        if (domainDir.exists() && domainDir.isDirectory()) {
            toolOutWriter.printStatusMsg(true, "webcontainer-domain-dir-check");
            String domainConfig_dir = containerProp.getProperty(WebContainerConstant.JVMOPTIONS_DIR);
            File domainConfigDir = new File(domainDir, domainConfig_dir);
            if (domainConfigDir.exists() && domainConfigDir.isDirectory()) {
                toolOutWriter.printStatusMsg(true, "webcontainer-domain-config-dir-check");
                String domainFileName = 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(domainConfigDir, domainFileName)));
                    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-domain-config-dir-invalid");
                toolOutWriter.printStatusMsg(false, "webcontainer-domain-config-dir-check");
            }
        } else {
            toolOutWriter.printError("webcontainer-domain-dir-invalid");
            toolOutWriter.printStatusMsg(false, "webcontainer-domain-dir-check");
        }
    } else {
        toolOutWriter.printError("webcontainer-domain-dir-invalid");
        toolOutWriter.printStatusMsg(false, "webcontainer-domain-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 18 with LineNumberReader

use of java.io.LineNumberReader in project ProPPR by TeamCohen.

the class ParsedFile method init.

private void init(String filename) {
    this.filename = filename;
    this.dataLine = -2;
    try {
        reader = new LineNumberReader(new FileReader(filename));
        closed = false;
        this.next();
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : FileReader(java.io.FileReader) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 19 with LineNumberReader

use of java.io.LineNumberReader in project tdi-studio-se by Talend.

the class JavaProcessor method getLineNumbers.

/**
     * Find line numbers of the beginning of the code of process nodes.
     *
     * @param file Code file where we are searching node's code.
     * @param nodes List of nodes searched.
     * @return Line numbers where code of nodes appears.
     * @throws CoreException Search failed.
     */
private static int[] getLineNumbers(IFile file, String[] nodes) throws CoreException {
    List<Integer> lineNumbers = new ArrayList<Integer>();
    // List of code's lines searched in the file
    List<String> searchedLines = new ArrayList<String>();
    for (String node : nodes) {
        searchedLines.add(node);
    }
    LineNumberReader lineReader = new LineNumberReader(new InputStreamReader(file.getContents()));
    try {
        String line = lineReader.readLine();
        while (!searchedLines.isEmpty() && line != null) {
            boolean nodeFound = false;
            for (Iterator<String> i = searchedLines.iterator(); !nodeFound && i.hasNext(); ) {
                String nodeMain = i.next();
                if (line.indexOf(nodeMain) != -1) {
                    nodeFound = true;
                    i.remove();
                    // Search the first valid code line
                    boolean lineCodeFound = false;
                    line = lineReader.readLine();
                    while (line != null && !lineCodeFound) {
                        if (isCodeLine(line)) {
                            lineCodeFound = true;
                            lineNumbers.add(new Integer(lineReader.getLineNumber() + 1));
                        }
                        line = lineReader.readLine();
                    }
                }
            }
            line = lineReader.readLine();
        }
    } catch (IOException ioe) {
        //$NON-NLS-1$ //$NON-NLS-2$
        IStatus status = new Status(IStatus.ERROR, "", IStatus.OK, "Source code read failure.", ioe);
        throw new CoreException(status);
    }
    int[] res = new int[lineNumbers.size()];
    int pos = 0;
    for (Integer i : lineNumbers) {
        res[pos++] = i.intValue();
    }
    return res;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IStatus(org.eclipse.core.runtime.IStatus) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IJavaBreakpoint(org.eclipse.jdt.debug.core.IJavaBreakpoint) IJavaLineBreakpoint(org.eclipse.jdt.debug.core.IJavaLineBreakpoint) LineNumberReader(java.io.LineNumberReader) CoreException(org.eclipse.core.runtime.CoreException)

Example 20 with LineNumberReader

use of java.io.LineNumberReader in project voltdb by VoltDB.

the class TestInMemoryJarfile method testReadFileFromJarfile.

/**
     *
     */
public void testReadFileFromJarfile() throws IOException {
    String catalog0 = this.m_catalog.serialize();
    assertTrue(catalog0.length() > 0);
    InMemoryJarfile jarfile = new InMemoryJarfile(m_jarPath.getAbsolutePath());
    byte[] catalogBytes = jarfile.get(CatalogUtil.CATALOG_FILENAME);
    String catalog1 = new String(catalogBytes, "UTF-8");
    assertTrue(catalog1.length() > 0);
    assertEquals(catalog0.length(), catalog1.length());
    LineNumberReader reader0 = new LineNumberReader(new CharArrayReader(catalog0.toCharArray()));
    LineNumberReader reader1 = new LineNumberReader(new CharArrayReader(catalog1.toCharArray()));
    try {
        int lines = 0;
        while (reader0.ready()) {
            assertEquals(reader0.ready(), reader1.ready());
            assertEquals(reader0.readLine(), reader1.readLine());
            lines++;
        }
        assertTrue(lines > 0);
        reader0.close();
        reader1.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        assertTrue(false);
    }
}
Also used : CharArrayReader(java.io.CharArrayReader) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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