L2Properties.java 3.0 KB

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