Просмотр исходного кода

BETA: HtmCache refactor:
* Unhardcoded HTML filter, accepts .htm and .html files only.
* Class cleanup.
* Using [http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html Automatic Resource Management]

Zoey76 13 лет назад
Родитель
Сommit
9308b727e6

+ 33 - 64
L2J_Server_BETA/java/com/l2jserver/gameserver/cache/HtmCache.java

@@ -18,7 +18,6 @@ import gnu.trove.map.hash.TIntObjectHashMap;
 
 import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileFilter;
 import java.io.FileInputStream;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -26,15 +25,17 @@ import java.util.logging.Logger;
 import com.l2jserver.Config;
 import com.l2jserver.gameserver.util.L2TIntObjectHashMap;
 import com.l2jserver.gameserver.util.Util;
+import com.l2jserver.util.file.filter.HTMLFilter;
 
 /**
  * @author Layane
- *
  */
 public class HtmCache
 {
 	private static Logger _log = Logger.getLogger(HtmCache.class.getName());
 	
+	private static final HTMLFilter htmlFilter = new HTMLFilter();
+	
 	private final TIntObjectHashMap<String> _cache;
 	
 	private int _loadedFiles;
@@ -48,9 +49,13 @@ public class HtmCache
 	private HtmCache()
 	{
 		if (Config.LAZY_CACHE)
+		{
 			_cache = new L2TIntObjectHashMap<String>();
+		}
 		else
+		{
 			_cache = new TIntObjectHashMap<String>();
+		}
 		reload();
 	}
 	
@@ -92,30 +97,19 @@ public class HtmCache
 		return _loadedFiles;
 	}
 	
-	private static class HtmFilter implements FileFilter
-	{
-		@Override
-		public boolean accept(File file)
-		{
-			if (!file.isDirectory())
-			{
-				return (file.getName().endsWith(".htm") || file.getName().endsWith(".html"));
-			}
-			return true;
-		}
-	}
-	
 	private void parseDir(File dir)
 	{
-		FileFilter filter = new HtmFilter();
-		File[] files = dir.listFiles(filter);
-		
+		final File[] files = dir.listFiles(htmlFilter);
 		for (File file : files)
 		{
 			if (!file.isDirectory())
+			{
 				loadFile(file);
+			}
 			else
+			{
 				parseDir(file);
+			}
 		}
 	}
 	
@@ -123,19 +117,12 @@ public class HtmCache
 	{
 		final String relpath = Util.getRelativePath(Config.DATAPACK_ROOT, file);
 		final int hashcode = relpath.hashCode();
-		
-		final HtmFilter filter = new HtmFilter();
-		
-		if (file.exists() && filter.accept(file) && !file.isDirectory())
+		String content = null;
+		if (htmlFilter.accept(file))
 		{
-			String content;
-			FileInputStream fis = null;
-			
-			try
+			try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis);)
 			{
-				fis = new FileInputStream(file);
-				BufferedInputStream bis = new BufferedInputStream(fis);
-				int bytes = bis.available();
+				final int bytes = bis.available();
 				byte[] raw = new byte[bytes];
 				
 				bis.read(raw);
@@ -143,7 +130,6 @@ public class HtmCache
 				content = content.replaceAll("\r\n", "\n");
 				
 				String oldContent = _cache.get(hashcode);
-				
 				if (oldContent == null)
 				{
 					_bytesBuffLen += bytes;
@@ -151,42 +137,26 @@ public class HtmCache
 				}
 				else
 				{
-					_bytesBuffLen = _bytesBuffLen - oldContent.length() + bytes;
+					_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
 				}
-				
 				_cache.put(hashcode, content);
-				
-				return content;
 			}
 			catch (Exception e)
 			{
 				_log.log(Level.WARNING, "Problem with htm file " + e.getMessage(), e);
 			}
-			finally
-			{
-				try
-				{
-					fis.close();
-				}
-				catch (Exception e1)
-				{
-				}
-			}
 		}
-		
-		return null;
+		return content;
 	}
 	
 	public String getHtmForce(String prefix, String path)
 	{
 		String content = getHtm(prefix, path);
-		
 		if (content == null)
 		{
 			content = "<html><body>My text is missing:<br>" + path + "</body></html>";
 			_log.warning("Cache[HTML]: Missing HTML page: " + path);
 		}
-		
 		return content;
 	}
 	
@@ -194,32 +164,38 @@ public class HtmCache
 	{
 		String newPath = null;
 		String content;
-		if (prefix != null && !prefix.isEmpty())
+		if ((prefix != null) && !prefix.isEmpty())
 		{
 			newPath = prefix + path;
 			content = getHtm(newPath);
 			if (content != null)
+			{
 				return content;
+			}
 		}
 		
 		content = getHtm(path);
-		if (content != null && newPath != null)
+		if ((content != null) && (newPath != null))
+		{
 			_cache.put(newPath.hashCode(), content);
+		}
 		
 		return content;
 	}
 	
 	private String getHtm(String path)
 	{
-		if (path == null || path.isEmpty())
+		if ((path == null) || path.isEmpty())
+		{
 			return ""; // avoid possible NPE
+		}
 		
 		final int hashCode = path.hashCode();
 		String content = _cache.get(hashCode);
-		
-		if (Config.LAZY_CACHE && content == null)
+		if (Config.LAZY_CACHE && (content == null))
+		{
 			content = loadFile(new File(Config.DATAPACK_ROOT, path));
-		
+		}
 		return content;
 	}
 	
@@ -229,19 +205,12 @@ public class HtmCache
 	}
 	
 	/**
-	 * Check if an HTM exists and can be loaded
 	 * @param path The path to the HTM
-	 * @return 
-	 * */
+	 * @return {@code true} if the path targets a HTM or HTML file, {@code false} otherwise.
+	 */
 	public boolean isLoadable(String path)
 	{
-		File file = new File(path);
-		HtmFilter filter = new HtmFilter();
-		
-		if (file.exists() && filter.accept(file) && !file.isDirectory())
-			return true;
-		
-		return false;
+		return htmlFilter.accept(new File(path));
 	}
 	
 	@SuppressWarnings("synthetic-access")

+ 37 - 0
L2J_Server_BETA/java/com/l2jserver/util/file/filter/HTMLFilter.java

@@ -0,0 +1,37 @@
+/*
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ * 
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.l2jserver.util.file.filter;
+
+import java.io.File;
+import java.io.FileFilter;
+
+/**
+ * Specialized {@link FileFilter} class.<br>
+ * Accepts <b>files</b> ending with ".htm" and ".html" only.
+ * @author Zoey76
+ */
+public class HTMLFilter implements FileFilter
+{
+	@Override
+	public boolean accept(File f)
+	{
+		if (!f.exists() || f.isDirectory())
+		{
+			return false;
+		}
+		final String name = f.getName().toLowerCase();
+		return name.endsWith(".htm") || name.endsWith(".html");
+	}
+}