Skip to content

Commit d592d01

Browse files
committed
refine ConfigurationParser: 1. performance. 2. keyword from in package name issue. 3. add some missing closable resource close
1 parent 4e683e9 commit d592d01

File tree

4 files changed

+227
-132
lines changed

4 files changed

+227
-132
lines changed

src/main/java/org/codehaus/plexus/classworlds/launcher/ConfigurationParser.java

Lines changed: 169 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ public class ConfigurationParser {
5454
*/
5555
public static final String OPTIONALLY_PREFIX = "optionally";
5656

57-
private ConfigurationHandler handler;
57+
protected static final String FROM_SEPARATOR = " from ";
5858

59-
private Properties systemProperties;
59+
protected static final String USING_SEPARATOR = " using ";
60+
61+
protected static final String DEFAULT_SEPARATOR = " default ";
62+
63+
private final ConfigurationHandler handler;
64+
65+
private final Properties systemProperties;
6066

6167
public ConfigurationParser(ConfigurationHandler handler, Properties systemProperties) {
6268
this.handler = handler;
@@ -74,188 +80,219 @@ public ConfigurationParser(ConfigurationHandler handler, Properties systemProper
7480
*/
7581
public void parse(InputStream is)
7682
throws IOException, ConfigurationException, DuplicateRealmException, NoSuchRealmException {
77-
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
83+
try (
84+
BufferedReader reader = new BufferedReader(
85+
new InputStreamReader(is, StandardCharsets.UTF_8)
86+
)
87+
) {
7888

79-
String line;
89+
String line;
8090

81-
int lineNo = 0;
91+
int lineNo = 0;
8292

83-
boolean mainSet = false;
93+
boolean mainSet = false;
8494

85-
String curRealm = null;
95+
String curRealm = null;
8696

87-
while (true) {
88-
line = reader.readLine();
89-
90-
if (line == null) {
91-
break;
92-
}
93-
94-
++lineNo;
95-
line = line.trim();
96-
97-
if (canIgnore(line)) {
98-
continue;
99-
}
97+
while (true) {
98+
line = reader.readLine();
10099

101-
if (line.startsWith(MAIN_PREFIX)) {
102-
if (mainSet) {
103-
throw new ConfigurationException("Duplicate main configuration", lineNo, line);
100+
if (line == null) {
101+
break;
104102
}
105103

106-
String conf = line.substring(MAIN_PREFIX.length()).trim();
104+
++lineNo;
105+
line = line.trim();
107106

108-
int fromLoc = conf.indexOf("from");
109-
110-
if (fromLoc < 0) {
111-
throw new ConfigurationException("Missing from clause", lineNo, line);
107+
if (canIgnore(line)) {
108+
continue;
112109
}
113110

114-
String mainClassName = filter(conf.substring(0, fromLoc).trim());
111+
char lineFirstChar = line.charAt(0);
112+
switch (lineFirstChar) {
113+
case 'm': {
114+
if (line.startsWith(MAIN_PREFIX)) {
115+
if (mainSet) {
116+
throw new ConfigurationException("Duplicate main configuration", lineNo, line);
117+
}
115118

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

118-
this.handler.setAppMain(mainClassName, mainRealmName);
121+
if (fromLoc < 0) {
122+
throw new ConfigurationException("Missing from clause", lineNo, line);
123+
}
119124

120-
mainSet = true;
121-
} else if (line.startsWith(SET_PREFIX)) {
122-
String conf = line.substring(SET_PREFIX.length()).trim();
125+
String mainClassName = filter(line.substring(MAIN_PREFIX.length(), fromLoc).trim());
123126

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

126-
String property = null;
129+
this.handler.setAppMain(mainClassName, mainRealmName);
127130

128-
String propertiesFileName = null;
131+
mainSet = true;
129132

130-
if (usingLoc > 0) {
131-
property = conf.substring(0, usingLoc).trim();
133+
break;
134+
}
135+
throw new ConfigurationException("Unhandled configuration", lineNo, line);
136+
}
137+
case 's': {
138+
if (line.startsWith(SET_PREFIX)) {
139+
String conf = line.substring(SET_PREFIX.length()).trim();
132140

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

135-
conf = propertiesFileName;
136-
}
143+
String property = null;
137144

138-
String defaultValue = null;
145+
String propertiesFileName = null;
139146

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

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

145-
if (property == null) {
146-
property = conf.substring(0, defaultLoc).trim();
147-
} else {
148-
propertiesFileName = conf.substring(0, defaultLoc).trim();
149-
}
150-
}
152+
conf = propertiesFileName;
153+
}
151154

152-
String value = systemProperties.getProperty(property);
155+
String defaultValue = null;
153156

154-
if (value != null) {
155-
continue;
156-
}
157+
int defaultLoc = conf.indexOf(DEFAULT_SEPARATOR);
157158

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

161-
if (propertiesFile.exists()) {
162-
Properties properties = new Properties();
162+
if (property == null) {
163+
property = conf.substring(0, defaultLoc).trim();
164+
} else {
165+
propertiesFileName = conf.substring(0, defaultLoc).trim();
166+
}
167+
}
163168

164-
try {
165-
properties.load(Files.newInputStream(Paths.get(propertiesFileName)));
169+
String value = systemProperties.getProperty(property);
166170

167-
value = properties.getProperty(property);
168-
} catch (Exception e) {
169-
// do nothing
170-
}
171-
}
172-
}
171+
if (value != null) {
172+
continue;
173+
}
173174

174-
if (value == null && defaultValue != null) {
175-
value = defaultValue;
176-
}
175+
if (propertiesFileName != null) {
176+
File propertiesFile = new File(propertiesFileName);
177177

178-
if (value != null) {
179-
value = filter(value);
180-
systemProperties.setProperty(property, value);
181-
}
182-
} else if (line.startsWith("[")) {
183-
int rbrack = line.indexOf("]");
178+
if (propertiesFile.exists()) {
179+
Properties properties = new Properties();
184180

185-
if (rbrack < 0) {
186-
throw new ConfigurationException("Invalid realm specifier", lineNo, line);
187-
}
181+
try (InputStream inputStream = Files.newInputStream(Paths.get(propertiesFileName))){
182+
properties.load(inputStream);
183+
value = properties.getProperty(property);
184+
} catch (Exception e) {
185+
// do nothing
186+
}
187+
}
188+
}
188189

189-
String realmName = line.substring(1, rbrack);
190+
if (value == null && defaultValue != null) {
191+
value = defaultValue;
192+
}
190193

191-
handler.addRealm(realmName);
194+
if (value != null) {
195+
value = filter(value);
196+
systemProperties.setProperty(property, value);
197+
}
192198

193-
curRealm = realmName;
194-
} else if (line.startsWith(IMPORT_PREFIX)) {
195-
if (curRealm == null) {
196-
throw new ConfigurationException("Unhandled import", lineNo, line);
197-
}
199+
break;
200+
}
201+
throw new ConfigurationException("Unhandled configuration", lineNo, line);
202+
}
203+
case '[': {
204+
int rbrack = line.indexOf("]");
198205

199-
String conf = line.substring(IMPORT_PREFIX.length()).trim();
206+
if (rbrack < 0) {
207+
throw new ConfigurationException("Invalid realm specifier", lineNo, line);
208+
}
200209

201-
int fromLoc = conf.indexOf("from");
210+
String realmName = line.substring(1, rbrack);
202211

203-
if (fromLoc < 0) {
204-
throw new ConfigurationException("Missing from clause", lineNo, line);
205-
}
212+
handler.addRealm(realmName);
206213

207-
String importSpec = conf.substring(0, fromLoc).trim();
214+
curRealm = realmName;
208215

209-
String relamName = conf.substring(fromLoc + 4).trim();
216+
break;
217+
}
218+
case 'i': {
219+
if (line.startsWith(IMPORT_PREFIX)) {
220+
if (curRealm == null) {
221+
throw new ConfigurationException("Unhandled import", lineNo, line);
222+
}
223+
int fromLoc = line.indexOf(FROM_SEPARATOR, IMPORT_PREFIX.length());
210224

211-
handler.addImportFrom(relamName, importSpec);
225+
if (fromLoc < 0) {
226+
throw new ConfigurationException("Missing from clause", lineNo, line);
227+
}
212228

213-
} else if (line.startsWith(LOAD_PREFIX)) {
214-
String constituent = line.substring(LOAD_PREFIX.length()).trim();
229+
String importSpec = line.substring(IMPORT_PREFIX.length(), fromLoc).trim();
215230

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

218-
if (constituent.contains("*")) {
219-
loadGlob(constituent, false /*not optionally*/);
220-
} else {
221-
File file = new File(constituent);
233+
handler.addImportFrom(relamName, importSpec);
222234

223-
if (file.exists()) {
224-
handler.addLoadFile(file);
225-
} else {
226-
try {
227-
handler.addLoadURL(new URL(constituent));
228-
} catch (MalformedURLException e) {
229-
throw new FileNotFoundException(constituent);
235+
break;
230236
}
237+
throw new ConfigurationException("Unhandled configuration", lineNo, line);
231238
}
232-
}
233-
} else if (line.startsWith(OPTIONALLY_PREFIX)) {
234-
String constituent = line.substring(OPTIONALLY_PREFIX.length()).trim();
235-
236-
constituent = filter(constituent);
237-
238-
if (constituent.contains("*")) {
239-
loadGlob(constituent, true /*optionally*/);
240-
} else {
241-
File file = new File(constituent);
242-
243-
if (file.exists()) {
244-
handler.addLoadFile(file);
245-
} else {
246-
try {
247-
handler.addLoadURL(new URL(constituent));
248-
} catch (MalformedURLException e) {
249-
// swallow
239+
case 'l': {
240+
if (line.startsWith(LOAD_PREFIX)) {
241+
String constituent = line.substring(LOAD_PREFIX.length()).trim();
242+
243+
constituent = filter(constituent);
244+
245+
if (constituent.contains("*")) {
246+
loadGlob(constituent, false /*not optionally*/);
247+
} else {
248+
File file = new File(constituent);
249+
250+
if (file.exists()) {
251+
handler.addLoadFile(file);
252+
} else {
253+
try {
254+
handler.addLoadURL(new URL(constituent));
255+
} catch (MalformedURLException e) {
256+
throw new FileNotFoundException(constituent);
257+
}
258+
}
259+
}
260+
261+
break;
250262
}
263+
throw new ConfigurationException("Unhandled configuration", lineNo, line);
251264
}
265+
case 'o': {
266+
if (line.startsWith(OPTIONALLY_PREFIX)) {
267+
String constituent = line.substring(OPTIONALLY_PREFIX.length()).trim();
268+
269+
constituent = filter(constituent);
270+
271+
if (constituent.contains("*")) {
272+
loadGlob(constituent, true /*optionally*/);
273+
} else {
274+
File file = new File(constituent);
275+
276+
if (file.exists()) {
277+
handler.addLoadFile(file);
278+
} else {
279+
try {
280+
handler.addLoadURL(new URL(constituent));
281+
} catch (MalformedURLException e) {
282+
// swallow
283+
}
284+
}
285+
}
286+
287+
break;
288+
}
289+
throw new ConfigurationException("Unhandled configuration", lineNo, line);
290+
}
291+
default:
292+
throw new ConfigurationException("Unhandled configuration", lineNo, line);
252293
}
253-
} else {
254-
throw new ConfigurationException("Unhandled configuration", lineNo, line);
255294
}
256295
}
257-
258-
reader.close();
259296
}
260297

261298
/**
@@ -373,6 +410,6 @@ protected String filter(String text) throws ConfigurationException {
373410
* otherwise <code>false</code>.
374411
*/
375412
private boolean canIgnore(String line) {
376-
return (line.length() == 0 || line.startsWith("#"));
413+
return (line.isEmpty() || line.startsWith("#"));
377414
}
378415
}

0 commit comments

Comments
 (0)