2121import java .io .File ;
2222import java .io .FileInputStream ;
2323import java .io .IOException ;
24+ import java .lang .reflect .Constructor ;
2425import java .nio .file .Path ;
26+ import java .nio .file .Paths ;
2527import java .util .HashMap ;
2628import java .util .Map ;
2729import java .util .Map .Entry ;
2830import java .util .Properties ;
2931import org .apache .zookeeper .Environment ;
30- import org .apache .zookeeper .server .quorum .QuorumPeerConfig ;
3132import org .apache .zookeeper .server .util .VerifyingFileFactory ;
3233import org .slf4j .Logger ;
3334import org .slf4j .LoggerFactory ;
@@ -65,35 +66,33 @@ public ZKConfig() {
6566 /**
6667 * <p><b>Use {@link ZKConfig#ZKConfig(Path configPath)} instead.</b>
6768 *
68- * <p><b>The signature of this method will be changed to throw {@link ConfigException}
69- * instead of {@link QuorumPeerConfig.ConfigException} in future release .</b>
69+ * <p><b>The signature of this method has been changed to throw {@link ConfigException}
70+ * instead of {@code QuorumPeerConfig.ConfigException}.</b>
7071 *
7172 * @param configPath
7273 * Configuration file path
7374 * @throws ConfigException
7475 * if failed to load configuration properties
7576 */
7677 @ Deprecated
77- public ZKConfig (String configPath ) throws QuorumPeerConfig . ConfigException {
78- this (new File (configPath ));
78+ public ZKConfig (String configPath ) throws ConfigException {
79+ this (Paths . get (configPath ));
7980 }
8081
8182 /**
8283 * <p><b>Use {@link ZKConfig#ZKConfig(Path configPath)} instead.</b>
8384 *
84- * <p><b>The signature of this method will be changed to throw {@link ConfigException}
85- * instead of {@link QuorumPeerConfig.ConfigException} in future release .</b>
85+ * <p><b>The signature of this method has been changed to throw {@link ConfigException}
86+ * instead of {@code QuorumPeerConfig.ConfigException}.</b>
8687 *
8788 * @param configFile
8889 * Configuration file
8990 * @throws ConfigException
9091 * if failed to load configuration properties
9192 */
9293 @ Deprecated
93- public ZKConfig (File configFile ) throws QuorumPeerConfig .ConfigException {
94- this ();
95- addConfiguration (configFile );
96- LOG .info ("ZK Config {}" , this .properties );
94+ public ZKConfig (File configFile ) throws ConfigException {
95+ this (configFile .toPath ());
9796 }
9897
9998 /**
@@ -102,9 +101,10 @@ public ZKConfig(File configFile) throws QuorumPeerConfig.ConfigException {
102101 * @param configPath path to configuration file
103102 * @throws ConfigException
104103 */
105- @ SuppressWarnings ("deprecation" )
106104 public ZKConfig (Path configPath ) throws ConfigException {
107- this (configPath .toFile ());
105+ this ();
106+ addConfiguration (configPath );
107+ LOG .info ("ZK Config {}" , this .properties );
108108 }
109109
110110 private void init () {
@@ -216,16 +216,39 @@ public void setProperty(String key, String value) {
216216 *
217217 * @param configPath path to Configuration file.
218218 */
219- @ SuppressWarnings ("deprecation" )
220219 public void addConfiguration (Path configPath ) throws ConfigException {
221- addConfiguration (configPath .toFile ());
220+ Path absoluteConfigPath = configPath .toAbsolutePath ();
221+ LOG .info ("Reading configuration from: {}" , absoluteConfigPath );
222+ try {
223+ File configFile = (new VerifyingFileFactory .Builder (LOG ).warnForRelativePath ()
224+ .failForNonExistingPath ()
225+ .build ()).validate (configPath .toFile ());
226+ Properties cfg = new Properties ();
227+ try (FileInputStream in = new FileInputStream (configFile )) {
228+ cfg .load (in );
229+ }
230+ parseProperties (cfg );
231+ } catch (IOException | IllegalArgumentException e ) {
232+ LOG .error ("Error while configuration from: {}" , absoluteConfigPath , e );
233+ String msg = "Error while processing " + absoluteConfigPath ;
234+ try {
235+ Class <?> clazz = Class .forName ("org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException" );
236+ Class <? extends ConfigException > exceptionClass = clazz .asSubclass (ConfigException .class );
237+ Constructor <? extends ConfigException > constructor = exceptionClass .getDeclaredConstructor (String .class , Exception .class );
238+ throw constructor .newInstance (msg , e );
239+ } catch (ClassNotFoundException ignored ) {
240+ } catch (Exception ignored ) {
241+ LOG .warn ("Fail to construct QuorumPeerConfig.ConfigException" , e );
242+ }
243+ throw new ConfigException (msg , e );
244+ }
222245 }
223246
224247 /**
225248 * <p><b>Use {@link #addConfiguration(Path)} instead.</b></p>
226249 *
227- * <p><b>The signature of this method will be changed to throw {@link ConfigException}
228- * instead of {@link QuorumPeerConfig.ConfigException} in future release .</b>
250+ * <p><b>The signature of this method has been changed to throw {@link ConfigException}
251+ * instead of {@code QuorumPeerConfig.ConfigException}.</b>
229252 *
230253 * <p>Add a configuration resource. The properties form this configuration will
231254 * overwrite corresponding already loaded property and system property
@@ -234,31 +257,15 @@ public void addConfiguration(Path configPath) throws ConfigException {
234257 * Configuration file.
235258 */
236259 @ Deprecated
237- public void addConfiguration (File configFile ) throws QuorumPeerConfig .ConfigException {
238- LOG .info ("Reading configuration from: {}" , configFile .getAbsolutePath ());
239- try {
240- configFile = (new VerifyingFileFactory .Builder (LOG ).warnForRelativePath ()
241- .failForNonExistingPath ()
242- .build ()).validate (configFile );
243- Properties cfg = new Properties ();
244- FileInputStream in = new FileInputStream (configFile );
245- try {
246- cfg .load (in );
247- } finally {
248- in .close ();
249- }
250- parseProperties (cfg );
251- } catch (IOException | IllegalArgumentException e ) {
252- LOG .error ("Error while configuration from: {}" , configFile .getAbsolutePath (), e );
253- throw new QuorumPeerConfig .ConfigException ("Error while processing " + configFile .getAbsolutePath (), e );
254- }
260+ public void addConfiguration (File configFile ) throws ConfigException {
261+ addConfiguration (configFile .toPath ());
255262 }
256263
257264 /**
258265 * <p><b>Use {@link #addConfiguration(Path)} instead.</b></p>
259266 *
260- * <p><b>The signature of this method will be changed to throw {@link ConfigException}
261- * instead of {@link QuorumPeerConfig.ConfigException} in future release .</b>
267+ * <p><b>The signature of this method has been changed to throw {@link ConfigException}
268+ * instead of {@code QuorumPeerConfig.ConfigException}.</b>
262269 *
263270 * <p>Add a configuration resource. The properties form this configuration will
264271 * overwrite corresponding already loaded property and system property
@@ -267,8 +274,8 @@ public void addConfiguration(File configFile) throws QuorumPeerConfig.ConfigExce
267274 * Configuration file path.
268275 */
269276 @ Deprecated
270- public void addConfiguration (String configPath ) throws QuorumPeerConfig . ConfigException {
271- addConfiguration (new File (configPath ));
277+ public void addConfiguration (String configPath ) throws ConfigException {
278+ addConfiguration (Paths . get (configPath ));
272279 }
273280
274281 private void parseProperties (Properties cfg ) {
0 commit comments