Skip to content

Commit

Permalink
refine ConfigurationParser: 1. performance. 2. keyword from in packag…
Browse files Browse the repository at this point in the history
…e name issue. 3. add some missing closable resource close
  • Loading branch information
XenoAmess committed Sep 24, 2024
1 parent 4e683e9 commit d592d01
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ public class ConfigurationParser {
*/
public static final String OPTIONALLY_PREFIX = "optionally";

private ConfigurationHandler handler;
protected static final String FROM_SEPARATOR = " from ";

private Properties systemProperties;
protected static final String USING_SEPARATOR = " using ";

protected static final String DEFAULT_SEPARATOR = " default ";

private final ConfigurationHandler handler;

private final Properties systemProperties;

public ConfigurationParser(ConfigurationHandler handler, Properties systemProperties) {
this.handler = handler;
Expand All @@ -74,188 +80,219 @@ public ConfigurationParser(ConfigurationHandler handler, Properties systemProper
*/
public void parse(InputStream is)
throws IOException, ConfigurationException, DuplicateRealmException, NoSuchRealmException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
try (
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, StandardCharsets.UTF_8)
)
) {

String line;
String line;

int lineNo = 0;
int lineNo = 0;

boolean mainSet = false;
boolean mainSet = false;

String curRealm = null;
String curRealm = null;

while (true) {
line = reader.readLine();

if (line == null) {
break;
}

++lineNo;
line = line.trim();

if (canIgnore(line)) {
continue;
}
while (true) {
line = reader.readLine();

if (line.startsWith(MAIN_PREFIX)) {
if (mainSet) {
throw new ConfigurationException("Duplicate main configuration", lineNo, line);
if (line == null) {
break;
}

String conf = line.substring(MAIN_PREFIX.length()).trim();
++lineNo;
line = line.trim();

int fromLoc = conf.indexOf("from");

if (fromLoc < 0) {
throw new ConfigurationException("Missing from clause", lineNo, line);
if (canIgnore(line)) {
continue;
}

String mainClassName = filter(conf.substring(0, fromLoc).trim());
char lineFirstChar = line.charAt(0);
switch (lineFirstChar) {
case 'm': {
if (line.startsWith(MAIN_PREFIX)) {
if (mainSet) {
throw new ConfigurationException("Duplicate main configuration", lineNo, line);
}

String mainRealmName = filter(conf.substring(fromLoc + 4).trim());
int fromLoc = line.indexOf(FROM_SEPARATOR, MAIN_PREFIX.length());

this.handler.setAppMain(mainClassName, mainRealmName);
if (fromLoc < 0) {
throw new ConfigurationException("Missing from clause", lineNo, line);
}

mainSet = true;
} else if (line.startsWith(SET_PREFIX)) {
String conf = line.substring(SET_PREFIX.length()).trim();
String mainClassName = filter(line.substring(MAIN_PREFIX.length(), fromLoc).trim());

int usingLoc = conf.indexOf(" using") + 1;
String mainRealmName = filter(line.substring(fromLoc + FROM_SEPARATOR.length()).trim());

String property = null;
this.handler.setAppMain(mainClassName, mainRealmName);

String propertiesFileName = null;
mainSet = true;

if (usingLoc > 0) {
property = conf.substring(0, usingLoc).trim();
break;
}
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
case 's': {
if (line.startsWith(SET_PREFIX)) {
String conf = line.substring(SET_PREFIX.length()).trim();

propertiesFileName = filter(conf.substring(usingLoc + 5).trim());
int usingLoc = conf.indexOf(USING_SEPARATOR);

conf = propertiesFileName;
}
String property = null;

String defaultValue = null;
String propertiesFileName = null;

int defaultLoc = conf.indexOf(" default") + 1;
if (usingLoc >= 0) {
property = conf.substring(0, usingLoc).trim();

if (defaultLoc > 0) {
defaultValue = filter(conf.substring(defaultLoc + 7).trim());
propertiesFileName = filter(conf.substring(usingLoc + USING_SEPARATOR.length()).trim());

if (property == null) {
property = conf.substring(0, defaultLoc).trim();
} else {
propertiesFileName = conf.substring(0, defaultLoc).trim();
}
}
conf = propertiesFileName;
}

String value = systemProperties.getProperty(property);
String defaultValue = null;

if (value != null) {
continue;
}
int defaultLoc = conf.indexOf(DEFAULT_SEPARATOR);

if (propertiesFileName != null) {
File propertiesFile = new File(propertiesFileName);
if (defaultLoc >= 0) {
defaultValue = filter(conf.substring(defaultLoc + DEFAULT_SEPARATOR.length()).trim());

if (propertiesFile.exists()) {
Properties properties = new Properties();
if (property == null) {
property = conf.substring(0, defaultLoc).trim();
} else {
propertiesFileName = conf.substring(0, defaultLoc).trim();
}
}

try {
properties.load(Files.newInputStream(Paths.get(propertiesFileName)));
String value = systemProperties.getProperty(property);

value = properties.getProperty(property);
} catch (Exception e) {
// do nothing
}
}
}
if (value != null) {
continue;
}

if (value == null && defaultValue != null) {
value = defaultValue;
}
if (propertiesFileName != null) {
File propertiesFile = new File(propertiesFileName);

if (value != null) {
value = filter(value);
systemProperties.setProperty(property, value);
}
} else if (line.startsWith("[")) {
int rbrack = line.indexOf("]");
if (propertiesFile.exists()) {
Properties properties = new Properties();

if (rbrack < 0) {
throw new ConfigurationException("Invalid realm specifier", lineNo, line);
}
try (InputStream inputStream = Files.newInputStream(Paths.get(propertiesFileName))){
properties.load(inputStream);
value = properties.getProperty(property);
} catch (Exception e) {
// do nothing
}
}
}

String realmName = line.substring(1, rbrack);
if (value == null && defaultValue != null) {
value = defaultValue;
}

handler.addRealm(realmName);
if (value != null) {
value = filter(value);
systemProperties.setProperty(property, value);
}

curRealm = realmName;
} else if (line.startsWith(IMPORT_PREFIX)) {
if (curRealm == null) {
throw new ConfigurationException("Unhandled import", lineNo, line);
}
break;
}
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
case '[': {
int rbrack = line.indexOf("]");

String conf = line.substring(IMPORT_PREFIX.length()).trim();
if (rbrack < 0) {
throw new ConfigurationException("Invalid realm specifier", lineNo, line);
}

int fromLoc = conf.indexOf("from");
String realmName = line.substring(1, rbrack);

if (fromLoc < 0) {
throw new ConfigurationException("Missing from clause", lineNo, line);
}
handler.addRealm(realmName);

String importSpec = conf.substring(0, fromLoc).trim();
curRealm = realmName;

String relamName = conf.substring(fromLoc + 4).trim();
break;
}
case 'i': {
if (line.startsWith(IMPORT_PREFIX)) {
if (curRealm == null) {
throw new ConfigurationException("Unhandled import", lineNo, line);
}
int fromLoc = line.indexOf(FROM_SEPARATOR, IMPORT_PREFIX.length());

handler.addImportFrom(relamName, importSpec);
if (fromLoc < 0) {
throw new ConfigurationException("Missing from clause", lineNo, line);
}

} else if (line.startsWith(LOAD_PREFIX)) {
String constituent = line.substring(LOAD_PREFIX.length()).trim();
String importSpec = line.substring(IMPORT_PREFIX.length(), fromLoc).trim();

constituent = filter(constituent);
String relamName = line.substring(fromLoc + FROM_SEPARATOR.length()).trim();

if (constituent.contains("*")) {
loadGlob(constituent, false /*not optionally*/);
} else {
File file = new File(constituent);
handler.addImportFrom(relamName, importSpec);

if (file.exists()) {
handler.addLoadFile(file);
} else {
try {
handler.addLoadURL(new URL(constituent));
} catch (MalformedURLException e) {
throw new FileNotFoundException(constituent);
break;
}
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
}
} else if (line.startsWith(OPTIONALLY_PREFIX)) {
String constituent = line.substring(OPTIONALLY_PREFIX.length()).trim();

constituent = filter(constituent);

if (constituent.contains("*")) {
loadGlob(constituent, true /*optionally*/);
} else {
File file = new File(constituent);

if (file.exists()) {
handler.addLoadFile(file);
} else {
try {
handler.addLoadURL(new URL(constituent));
} catch (MalformedURLException e) {
// swallow
case 'l': {
if (line.startsWith(LOAD_PREFIX)) {
String constituent = line.substring(LOAD_PREFIX.length()).trim();

constituent = filter(constituent);

if (constituent.contains("*")) {
loadGlob(constituent, false /*not optionally*/);
} else {
File file = new File(constituent);

if (file.exists()) {
handler.addLoadFile(file);
} else {
try {
handler.addLoadURL(new URL(constituent));
} catch (MalformedURLException e) {
throw new FileNotFoundException(constituent);
}
}
}

break;
}
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
case 'o': {
if (line.startsWith(OPTIONALLY_PREFIX)) {
String constituent = line.substring(OPTIONALLY_PREFIX.length()).trim();

constituent = filter(constituent);

if (constituent.contains("*")) {
loadGlob(constituent, true /*optionally*/);
} else {
File file = new File(constituent);

if (file.exists()) {
handler.addLoadFile(file);
} else {
try {
handler.addLoadURL(new URL(constituent));
} catch (MalformedURLException e) {
// swallow
}
}
}

break;
}
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
default:
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
} else {
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
}

reader.close();
}

/**
Expand Down Expand Up @@ -373,6 +410,6 @@ protected String filter(String text) throws ConfigurationException {
* otherwise <code>false</code>.
*/
private boolean canIgnore(String line) {
return (line.length() == 0 || line.startsWith("#"));
return (line.isEmpty() || line.startsWith("#"));
}
}
Loading

0 comments on commit d592d01

Please sign in to comment.