L2Properties.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. load(new FileInputStream(name));
  40. }
  41. public L2Properties(File file) throws IOException
  42. {
  43. load(new FileInputStream(file));
  44. }
  45. public L2Properties(InputStream inStream) throws IOException
  46. {
  47. load(inStream);
  48. }
  49. public L2Properties(Reader reader) throws IOException
  50. {
  51. load(reader);
  52. }
  53. public void load(String name) throws IOException
  54. {
  55. load(new FileInputStream(name));
  56. }
  57. public void load(File file) throws IOException
  58. {
  59. load(new FileInputStream(file));
  60. }
  61. @Override
  62. public void load(InputStream inStream) throws IOException
  63. {
  64. try (InputStreamReader isr = new InputStreamReader(inStream, Charset.defaultCharset());)
  65. {
  66. super.load(isr);
  67. }
  68. finally
  69. {
  70. inStream.close();
  71. }
  72. }
  73. @Override
  74. public void load(Reader reader) throws IOException
  75. {
  76. try
  77. {
  78. super.load(reader);
  79. }
  80. finally
  81. {
  82. reader.close();
  83. }
  84. }
  85. /**
  86. * @see Properties#getProperty(String)
  87. */
  88. @Override
  89. public String getProperty(String key)
  90. {
  91. String property = super.getProperty(key);
  92. if (property == null)
  93. {
  94. _log.info("L2Properties: Missing property for key - " + key);
  95. return null;
  96. }
  97. return property.trim();
  98. }
  99. /**
  100. * @see Properties#getProperty(String,String)
  101. */
  102. @Override
  103. public String getProperty(String key, String defaultValue)
  104. {
  105. String property = super.getProperty(key, defaultValue);
  106. if (property == null)
  107. {
  108. _log.warning("L2Properties: Missing defaultValue for key - " + key);
  109. return null;
  110. }
  111. return property.trim();
  112. }
  113. }