Search in sources :

Example 16 with LineReader

use of com.google.common.io.LineReader in project incubator-gobblin by apache.

the class PasswordManager method getEncryptors.

private List<TextEncryptor> getEncryptors(CachedInstanceKey cacheKey) {
    List<TextEncryptor> encryptors = new ArrayList<>();
    int numOfEncryptionKeys = cacheKey.numOfEncryptionKeys;
    String suffix = "";
    int i = 1;
    if (cacheKey.masterPasswordFile == null || numOfEncryptionKeys < 1) {
        return encryptors;
    }
    Exception exception = null;
    do {
        Path currentMasterPasswordFile = new Path(cacheKey.masterPasswordFile + suffix);
        try (Closer closer = Closer.create()) {
            if (!fs.exists(currentMasterPasswordFile) || fs.getFileStatus(currentMasterPasswordFile).isDirectory()) {
                continue;
            }
            InputStream in = closer.register(fs.open(currentMasterPasswordFile));
            String masterPassword = new LineReader(new InputStreamReader(in, Charsets.UTF_8)).readLine();
            TextEncryptor encryptor = useStrongEncryptor ? new StrongTextEncryptor() : new BasicTextEncryptor();
            // setPassword() needs to be called via reflection since the TextEncryptor interface doesn't have this method.
            encryptor.getClass().getMethod("setPassword", String.class).invoke(encryptor, masterPassword);
            encryptors.add(encryptor);
            suffix = "." + String.valueOf(i);
        } catch (FileNotFoundException fnf) {
            // It is ok for password files not being present
            LOG.warn("Master password file " + currentMasterPasswordFile + " not found.");
        } catch (IOException ioe) {
            exception = ioe;
            LOG.warn("Master password could not be read from file " + currentMasterPasswordFile);
        } catch (Exception e) {
            LOG.warn("Encryptor could not be instantiated.");
        }
    } while (i++ < numOfEncryptionKeys);
    // Throw exception if could not read any existing password file
    if (encryptors.size() < 1 && exception != null) {
        throw new RuntimeException("Master Password could not be read from any master password file.", exception);
    }
    return encryptors;
}
Also used : Path(org.apache.hadoop.fs.Path) Closer(com.google.common.io.Closer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) TextEncryptor(org.jasypt.util.text.TextEncryptor) StrongTextEncryptor(org.jasypt.util.text.StrongTextEncryptor) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) StrongTextEncryptor(org.jasypt.util.text.StrongTextEncryptor) LineReader(com.google.common.io.LineReader) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor)

Example 17 with LineReader

use of com.google.common.io.LineReader in project Truck-Factor by aserg-ufmg.

the class Alias method getAliasFromFile.

public static List<Alias> getAliasFromFile(String fileName) throws IOException {
    List<Alias> fileAliases = new ArrayList<Alias>();
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    LineReader lineReader = new LineReader(br);
    String sCurrentLine;
    String[] values;
    int countcfs = 0;
    while ((sCurrentLine = lineReader.readLine()) != null) {
        values = sCurrentLine.split(";");
        if (values.length < 3)
            System.err.println("Erro na linha " + countcfs);
        String rep = values[0];
        String dev1 = values[1];
        String dev2 = values[2];
        fileAliases.add(new Alias(rep, dev1, dev2));
        countcfs++;
    }
    return fileAliases;
}
Also used : LineReader(com.google.common.io.LineReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 18 with LineReader

use of com.google.common.io.LineReader in project jirm by agentgt.

the class SqlPartialParser method _processFile.

private static FileDeclarationSql _processFile(String path, String sql) throws IOException {
    LineReader lr = new LineReader(new StringReader(sql));
    String line;
    ImmutableList.Builder<ReferenceSql> references = ImmutableList.builder();
    ImmutableMap.Builder<String, HashDeclarationSql> hashes = ImmutableMap.builder();
    ImmutableList.Builder<ReferenceSql> hashReferences = ImmutableList.builder();
    Map<String, HashDeclarationSql> nameToHash = newHashMap();
    ImmutableList.Builder<String> referenceContent = ImmutableList.builder();
    ImmutableList.Builder<String> hashContent = ImmutableList.builder();
    ImmutableList.Builder<String> fileContent = ImmutableList.builder();
    boolean first = true;
    PSTATE state = PSTATE.OTHER;
    PSTATE previousState = PSTATE.OTHER;
    String currentHash = null;
    String currentReference = null;
    Map<String, List<String>> currentReferenceParameters = ImmutableMap.of();
    int hashStartIndex = 0;
    int referenceStartIndex = 0;
    int lineIndex = 0;
    String PE = "For path: '{}', ";
    while ((line = lr.readLine()) != null) {
        if (first)
            first = false;
        Matcher m = tokenPattern.matcher(line);
        String tag;
        if (m.matches() && (tag = m.group(1)) != null && !(tag = tag.trim()).isEmpty()) {
            if (tag != null && tag.startsWith("#")) {
                check.state(state != PSTATE.HASH, PE + "Cannot hash within hash at line: {}.", path, lineIndex);
                state = PSTATE.HASH;
                hashContent = ImmutableList.builder();
                hashReferences = ImmutableList.builder();
                currentHash = tag.substring(1).trim();
                HashDeclarationSql existing = nameToHash.get(currentHash);
                if (existing != null) {
                    throw check.stateInvalid(PE + "Hash: '#{}' already defined at line: {}, new definition at line: {}", path, currentHash, existing.getStartIndex(), lineIndex);
                }
                hashContent.add(line);
                hashStartIndex = lineIndex;
            } else if (tag != null && tag.startsWith(">")) {
                check.state(state != PSTATE.REFERENCE, PE + "Cannot reference within reference at line: {}.", path, lineIndex);
                previousState = state;
                state = PSTATE.REFERENCE;
                referenceContent = ImmutableList.builder();
                ReferenceHeader h = ReferenceHeader.parse(tag);
                currentReference = h.getPath().getFullPath();
                currentReferenceParameters = h.getParameters();
                check.state(!currentReference.isEmpty(), PE + "No reference defined", path);
                referenceStartIndex = lineIndex;
                referenceContent.add(line);
                if (previousState == PSTATE.HASH) {
                    hashContent.add(line);
                }
            } else if (tag != null && tag.equals("<")) {
                check.state(state == PSTATE.REFERENCE, PE + "Invalid close of reference line: {}", path, lineIndex);
                state = previousState;
                int length = lineIndex - referenceStartIndex + 1;
                referenceContent.add(line);
                check.state(length > -1, "length should be greater than -1");
                check.state(length >= 0, PE + "Hash Line index incorrect. Index: {}, Reference start: {}", path, lineIndex, referenceStartIndex);
                ReferenceSql rsql = new ReferenceSql(currentReference, path, referenceContent.build(), referenceStartIndex, length, currentReferenceParameters);
                references.add(rsql);
                if (PSTATE.HASH == previousState) {
                    hashReferences.add(rsql);
                    hashContent.add(line);
                }
            } else if (tag != null && tag.startsWith("/")) {
                check.state(state == PSTATE.HASH, PE + "Hash not started or reference not finished line: {}", path, lineIndex);
                String t = tag.substring(1).trim();
                check.state(!t.isEmpty(), PE + "No close hash is defined at line: {}", path, lineIndex);
                check.state(t.equals(currentHash), PE + "Should be current hash tag: {} at line: {}", path, currentHash, lineIndex);
                state = PSTATE.OTHER;
                int length = lineIndex - hashStartIndex + 1;
                hashContent.add(line);
                check.state(length >= 0, PE + "Hash Line index incorrect. Index: {}, Hash start: {}", path, lineIndex, hashStartIndex);
                HashDeclarationSql hash = new HashDeclarationSql(path + "#" + currentHash, hashContent.build(), hashReferences.build(), hashStartIndex, length);
                nameToHash.put(currentHash, hash);
                hashes.put(currentHash, hash);
            } else {
                throw check.stateInvalid(PE + "Malformed hash or reference: {} at line: {}", path, tag, lineIndex);
            }
        } else {
            if (PSTATE.HASH == state || PSTATE.HASH == previousState) {
                hashContent.add(line);
            }
            if (PSTATE.REFERENCE == state) {
                referenceContent.add(line);
            }
        }
        fileContent.add(line);
        lineIndex++;
    }
    check.state(PSTATE.OTHER == state, "Reference or hash not closed");
    FileDeclarationSql f = new FileDeclarationSql(path, fileContent.build(), references.build(), hashes.build());
    return f;
}
Also used : Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) LineReader(com.google.common.io.LineReader) StringReader(java.io.StringReader) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 19 with LineReader

use of com.google.common.io.LineReader in project jirm by agentgt.

the class SqlPlaceholderParser method _parseSql.

private static ParsedSql _parseSql(String sql, SqlPlaceholderParserConfig config) throws IOException {
    StringBuilder sb = new StringBuilder(sql.length());
    LineReader lr = new LineReader(new StringReader(sql));
    String line;
    ImmutableList.Builder<PlaceHolder> placeHolders = ImmutableList.builder();
    int nameIndex = 0;
    int positionalIndex = 0;
    int position = 0;
    boolean first = true;
    while ((line = lr.readLine()) != null) {
        if (first)
            first = false;
        else if (!config.isStripNewLines())
            sb.append("\n");
        Matcher m = tokenPattern.matcher(line);
        if (m.matches()) {
            String leftHand = m.group(1);
            int start = m.start(1);
            check.state(start == 0, "start should be 0");
            int[] ind = parseForReplacement(leftHand);
            check.state(ind != null, "Problem parsing {}", line);
            String before = leftHand.substring(0, ind[0]);
            String after = leftHand.substring(ind[1], leftHand.length());
            sb.append(before);
            sb.append("?");
            sb.append(after);
            String name = null;
            final PlaceHolder ph;
            if (m.groupCount() == 2 && !(name = nullToEmpty(m.group(2))).isEmpty()) {
                ph = new NamePlaceHolder(position, name, nameIndex);
                ++nameIndex;
            } else {
                ph = new PositionPlaceHolder(position, positionalIndex);
                ++positionalIndex;
            }
            placeHolders.add(ph);
            ++position;
        } else {
            sb.append(line);
        }
    }
    if (sql.endsWith("\r\n") || sql.endsWith("\n") || sql.endsWith("\r")) {
        if (!config.isStripNewLines())
            sb.append("\n");
    }
    return new ParsedSql(config, sql, sb.toString(), placeHolders.build());
}
Also used : Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) LineReader(com.google.common.io.LineReader) StringReader(java.io.StringReader)

Example 20 with LineReader

use of com.google.common.io.LineReader in project stashbot by palantir.

the class CommandOutputHandlerFactory method getRevlistOutputHandler.

public CommandOutputHandler<Object> getRevlistOutputHandler() {
    return new CommandOutputHandler<Object>() {

        private ArrayList<String> changesets;

        private LineReader lr;

        private boolean processed = false;

        @Override
        public void complete() throws ProcessException {
        }

        @Override
        public void setWatchdog(Watchdog watchdog) {
        }

        @Override
        public Object getOutput() {
            if (processed == false) {
                throw new IllegalStateException("getOutput() called before process()");
            }
            return ImmutableList.copyOf(changesets).reverse();
        }

        @Override
        public void process(InputStream output) throws ProcessException {
            processed = true;
            if (changesets == null) {
                changesets = new ArrayList<String>();
                lr = new LineReader(new InputStreamReader(output));
            }
            String sha1 = "";
            while (sha1 != null) {
                try {
                    sha1 = lr.readLine();
                } catch (IOException e) {
                    // dear god, why is this happening?
                    throw new RuntimeException(e);
                }
                if (sha1 != null && sha1.matches("[0-9a-fA-F]{40}")) {
                    changesets.add(sha1);
                }
            }
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) Watchdog(com.atlassian.utils.process.Watchdog) InputStream(java.io.InputStream) LineReader(com.google.common.io.LineReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler)

Aggregations

LineReader (com.google.common.io.LineReader)20 InputStreamReader (java.io.InputStreamReader)10 IOException (java.io.IOException)8 StringReader (java.io.StringReader)6 ArrayList (java.util.ArrayList)6 OutputStreamWriter (java.io.OutputStreamWriter)5 Socket (java.net.Socket)5 Discoverable (com.continuuity.weave.discovery.Discoverable)4 BufferedReader (java.io.BufferedReader)4 FileReader (java.io.FileReader)4 InputStream (java.io.InputStream)4 PrintWriter (java.io.PrintWriter)4 CommandOutputHandler (com.atlassian.stash.scm.CommandOutputHandler)3 Watchdog (com.atlassian.utils.process.Watchdog)3 WeaveController (com.continuuity.weave.api.WeaveController)3 WeaveRunner (com.continuuity.weave.api.WeaveRunner)3 PrinterLogHandler (com.continuuity.weave.api.logging.PrinterLogHandler)3 Matcher (java.util.regex.Matcher)3 Test (org.junit.Test)3 ServiceListenerAdapter (com.continuuity.weave.common.ServiceListenerAdapter)2