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;
}
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;
}
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;
}
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());
}
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);
}
}
}
};
}
Aggregations