L2Properties.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.Reader;
  22. import java.nio.charset.Charset;
  23. import java.util.Properties;
  24. import java.util.logging.Logger;
  25. /**
  26. * Specialized {@link java.util.Properties} class.<br>
  27. * Simplifies loading of property files and adds logging if a non existing property is requested.<br>
  28. * @author Noctarius
  29. */
  30. public final class L2Properties extends Properties
  31. {
  32. private static final long serialVersionUID = 1L;
  33. private static Logger _log = Logger.getLogger(L2Properties.class.getName());
  34. public L2Properties()
  35. {
  36. }
  37. public L2Properties(String name) throws IOException
  38. {
  39. try (FileInputStream fis = new FileInputStream(name))
  40. {
  41. load(fis);
  42. }
  43. }
  44. public L2Properties(File file) throws IOException
  45. {
  46. try (FileInputStream fis = new FileInputStream(file))
  47. {
  48. load(fis);
  49. }
  50. }
  51. public L2Properties(InputStream inStream) throws IOException
  52. {
  53. load(inStream);
  54. }
  55. public L2Properties(Reader reader) throws IOException
  56. {
  57. load(reader);
  58. }
  59. public void load(String name) throws IOException
  60. {
  61. try (FileInputStream fis = new FileInputStream(name))
  62. {
  63. load(fis);
  64. }
  65. }
  66. public void load(File file) throws IOException
  67. {
  68. try (FileInputStream fis = new FileInputStream(file))
  69. {
  70. load(fis);
  71. }
  72. }
  73. @Override
  74. public void load(InputStream inStream) throws IOException
  75. {
  76. try (InputStreamReader isr = new InputStreamReader(inStream, Charset.defaultCharset()))
  77. {
  78. super.load(isr);
  79. }
  80. finally
  81. {
  82. inStream.close();
  83. }
  84. }
  85. @Override
  86. public void load(Reader reader) throws IOException
  87. {
  88. try
  89. {
  90. super.load(reader);
  91. }
  92. finally
  93. {
  94. reader.close();
  95. }
  96. }
  97. @Override
  98. public String getProperty(String key)
  99. {
  100. String property = super.getProperty(key);
  101. if (property == null)
  102. {
  103. _log.info("L2Properties: Missing property for key - " + key);
  104. return null;
  105. }
  106. return property.trim();
  107. }
  108. @Override
  109. public String getProperty(String key, String defaultValue)
  110. {
  111. String property = super.getProperty(key, defaultValue);
  112. if (property == null)
  113. {
  114. _log.warning("L2Properties: Missing defaultValue for key - " + key);
  115. return null;
  116. }
  117. return property.trim();
  118. }
  119. }