1919package org .tron .core .config ;
2020
2121import static org .apache .commons .lang3 .StringUtils .isBlank ;
22+ import static org .apache .commons .lang3 .StringUtils .isNoneBlank ;
2223
2324import com .typesafe .config .ConfigFactory ;
25+
2426import java .io .File ;
27+ import java .io .FileNotFoundException ;
28+
2529import lombok .extern .slf4j .Slf4j ;
30+ import org .springframework .util .ResourceUtils ;
2631
2732@ Slf4j
2833public class Configuration {
@@ -32,29 +37,36 @@ public class Configuration {
3237 /**
3338 * Get configuration by a given path.
3439 *
35- * @param configurationPath path to configuration file
40+ * @param confFileName path to configuration file
3641 * @return loaded configuration
3742 */
38- public static com .typesafe .config .Config getByPath (final String configurationPath ) {
39- if (isBlank (configurationPath )) {
40- throw new IllegalArgumentException ("Configuration path is required!" );
43+ public static com .typesafe .config .Config getByFileName (final String shellConfFileName , final String confFileName ) {
44+ if (isNoneBlank (shellConfFileName )) {
45+ File shellConfFile = new File (shellConfFileName );
46+ resolveConfigFile (shellConfFileName , shellConfFile );
47+ return config ;
4148 }
4249
43- File confFile = new File (configurationPath );
44- if (confFile .exists ()) {
45- config = ConfigFactory .parseFile (new File (configurationPath ));
50+ if (isBlank (confFileName )) {
51+ throw new IllegalArgumentException ("Configuration path is required!" );
4652 } else {
47- config = ConfigFactory .load (configurationPath );
53+ File confFile = new File (confFileName );
54+ resolveConfigFile (confFileName , confFile );
55+ return config ;
4856 }
49- return config ;
5057 }
5158
52- public static com .typesafe .config .Config getByFile (final File confFile ) {
53- if (!confFile .exists ()) {
54- throw new IllegalArgumentException ("Configuration path is required!" );
59+ private static void resolveConfigFile (String fileName , File confFile ) {
60+ if (confFile .exists ()) {
61+ config = ConfigFactory .parseFile (confFile );
62+ } else {
63+ try {
64+ ResourceUtils .getFile ("classpath:" + fileName );
65+ } catch (FileNotFoundException e ) {
66+ throw new IllegalArgumentException ("Configuration path is required! No Such file " + fileName );
67+ }
68+ config = ConfigFactory .load (fileName );
5569 }
56- config = ConfigFactory .parseFile (confFile );
57- return config ;
5870 }
5971}
6072
0 commit comments