L2Properties.java 3.1 KB

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