org.codehaus.mojo.versions.NetRc Maven / Gradle / Ivy
package org.codehaus.mojo.versions;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
class NetRc {
private static final Pattern NETRC_TOKEN = Pattern.compile("(\\S+)");
public static class UsernamePasswordCredentials {
public String login;
public String password;
public UsernamePasswordCredentials(String l, String p) {
this.login = l;
this.password = p;
}
}
private enum ParseState {
START, REQ_KEY, REQ_VALUE, MACHINE, LOGIN, PASSWORD, MACDEF, END;
};
private File netrc;
private long lastModified;
private Map hosts = new HashMap();
public static NetRc getInstance() {
File netrc = getDefaultFile();
return netrc.exists() ? getInstance(netrc) : null;
}
public static NetRc getInstance(String netrcPath) {
File netrc = new File(netrcPath);
return netrc.exists() ? getInstance(new File(netrcPath)) : null;
}
public static NetRc getInstance(File netrc) {
return new NetRc(netrc).parse();
}
private static File getDefaultFile() {
File home = new File(System.getProperty("user.home"));
File netrc = new File(home, ".netrc");
if (!netrc.exists())
netrc = new File(home, "_netrc"); // windows variant
return netrc;
}
public UsernamePasswordCredentials getCredentials(String host) {
if (!this.netrc.exists())
return null;
if (this.lastModified != this.netrc.lastModified())
parse();
return this.hosts.get(host);
}
private NetRc(File netrc) {
this.netrc = netrc;
}
synchronized private NetRc parse() {
if (!netrc.exists())
return null;
this.hosts.clear();
this.lastModified = this.netrc.lastModified();
BufferedReader r = null;
try {
r = new BufferedReader(new FileReader(netrc));
String line = null;
String machine = null;
String login = null;
String password = null;
ParseState state = ParseState.START;
Matcher matcher = NETRC_TOKEN.matcher("");
while ((line = r.readLine()) != null) {
line = line.trim();
if (StringUtils.isEmpty(line)) {
if (state == ParseState.MACDEF) {
state = ParseState.REQ_KEY;
}
continue;
}
matcher.reset(line);
while (matcher.find()) {
String match = matcher.group();
switch (state) {
case START:
if ("machine".equals(match)) {
state = ParseState.MACHINE;
}
break;
case REQ_KEY:
if ("login".equals(match)) {
state = ParseState.LOGIN;
} else if ("password".equals(match)) {
state = ParseState.PASSWORD;
} else if ("macdef".equals(match)) {
state = ParseState.MACDEF;
} else if ("machine".equals(match)) {
state = ParseState.MACHINE;
} else {
state = ParseState.REQ_VALUE;
}
break;
case REQ_VALUE:
state = ParseState.REQ_KEY;
break;
case MACHINE:
if (machine != null) {
if (login != null && password != null) {
this.hosts.put(machine, new UsernamePasswordCredentials(login, password));
}
}
machine = match;
login = null;
password = null;
state = ParseState.REQ_KEY;
break;
case LOGIN:
login = match;
state = ParseState.REQ_KEY;
break;
case PASSWORD:
password = match;
state = ParseState.REQ_KEY;
break;
case MACDEF:
// Only way out is an empty line, handled before the find() loop.
break;
}
}
}
if (machine != null) {
if (login != null && password != null) {
this.hosts.put(machine, new UsernamePasswordCredentials(login, password));
}
}
} catch (IOException e) {
throw new RuntimeException("Invalid netrc file: '" + this.netrc.getAbsolutePath() + "'", e);
} finally {
try {
r.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return this;
}
}