Browse Source

BETA: Addressing the problem with resource leaks.
* Using [http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 encapsulation] to avoid the System.in input stream to get closed.
* build.xml changes to include it in tools by UnAfraid, thanks.
* Closing #6273


Tested by: UnAfraid

Zoey76 12 years ago
parent
commit
11ea1de9ec

+ 3 - 3
L2J_Server_BETA/build.xml

@@ -149,7 +149,7 @@
 			<fileset dir="${build.bin}">
 				<include name="**/dbinstaller/**" />
 				<include name="**/images/**" />
-				<include name="**/util/swing/**" />
+				<include name="**/util/**" />
 				<include name="**/SQLFilter**" />
 				<exclude name="**/LauncherGS*" />
 				<exclude name="**/LauncherLS*" />
@@ -167,7 +167,7 @@
 			<fileset dir="${build.bin}">
 				<include name="**/dbinstaller/**" />
 				<include name="**/images/**" />
-				<include name="**/util/swing/**" />
+				<include name="**/util/**" />
 				<include name="**/SQLFilter**" />
 				<exclude name="**/LauncherCS*" />
 				<exclude name="**/LauncherLS*" />
@@ -185,7 +185,7 @@
 			<fileset dir="${build.bin}">
 				<include name="**/dbinstaller/**" />
 				<include name="**/images/**" />
-				<include name="**/util/swing/**" />
+				<include name="**/util/**" />
 				<include name="**/SQLFilter**" />
 				<exclude name="**/LauncherCS*" />
 				<exclude name="**/LauncherGS*" />

+ 40 - 41
L2J_Server_BETA/java/com/l2jserver/tools/dbinstaller/console/DBInstallerConsole.java

@@ -21,6 +21,7 @@ import java.util.prefs.Preferences;
 import com.l2jserver.tools.dbinstaller.DBOutputInterface;
 import com.l2jserver.tools.dbinstaller.RunTasks;
 import com.l2jserver.tools.dbinstaller.util.mysql.MySqlConnect;
+import com.l2jserver.util.CloseShieldedInputStream;
 
 /**
  * @author mrTJO
@@ -34,44 +35,45 @@ public class DBInstallerConsole implements DBOutputInterface
 		System.out.println("Welcome to L2J DataBase installer");
 		Preferences prop = Preferences.userRoot();
 		RunTasks rt = null;
-		Scanner scn = new Scanner(System.in);
-		
-		while (_con == null)
+		try (Scanner scn = new Scanner(new CloseShieldedInputStream(System.in)))
 		{
-			System.out.printf("%s (%s): ", "Host", prop.get("dbHost_" + db, "localhost"));
-			String dbHost = scn.nextLine();
-			System.out.printf("%s (%s): ", "Port", prop.get("dbPort_" + db, "3306"));
-			String dbPort = scn.nextLine();
-			System.out.printf("%s (%s): ", "Username", prop.get("dbUser_" + db, "root"));
-			String dbUser = scn.nextLine();
-			System.out.printf("%s (%s): ", "Password", "");
-			String dbPass = scn.nextLine();
-			System.out.printf("%s (%s): ", "Database", prop.get("dbDbse_" + db, db));
-			String dbDbse = scn.nextLine();
-			
-			dbHost = dbHost.isEmpty() ? prop.get("dbHost_" + db, "localhost") : dbHost;
-			dbPort = dbPort.isEmpty() ? prop.get("dbPort_" + db, "3306") : dbPort;
-			dbUser = dbUser.isEmpty() ? prop.get("dbUser_" + db, "root") : dbUser;
-			dbDbse = dbDbse.isEmpty() ? prop.get("dbDbse_" + db, db) : dbDbse;
-			
-			MySqlConnect connector = new MySqlConnect(dbHost, dbPort, dbUser, dbPass, dbDbse, true);
+			while (_con == null)
+			{
+				System.out.printf("%s (%s): ", "Host", prop.get("dbHost_" + db, "localhost"));
+				String dbHost = scn.nextLine();
+				System.out.printf("%s (%s): ", "Port", prop.get("dbPort_" + db, "3306"));
+				String dbPort = scn.nextLine();
+				System.out.printf("%s (%s): ", "Username", prop.get("dbUser_" + db, "root"));
+				String dbUser = scn.nextLine();
+				System.out.printf("%s (%s): ", "Password", "");
+				String dbPass = scn.nextLine();
+				System.out.printf("%s (%s): ", "Database", prop.get("dbDbse_" + db, db));
+				String dbDbse = scn.nextLine();
+				
+				dbHost = dbHost.isEmpty() ? prop.get("dbHost_" + db, "localhost") : dbHost;
+				dbPort = dbPort.isEmpty() ? prop.get("dbPort_" + db, "3306") : dbPort;
+				dbUser = dbUser.isEmpty() ? prop.get("dbUser_" + db, "root") : dbUser;
+				dbDbse = dbDbse.isEmpty() ? prop.get("dbDbse_" + db, db) : dbDbse;
+				
+				MySqlConnect connector = new MySqlConnect(dbHost, dbPort, dbUser, dbPass, dbDbse, true);
+				
+				_con = connector.getConnection();
+			}
 			
-			_con = connector.getConnection();
-		}
-		
-		System.out.print("(C)lean install, (U)pdate or (E)xit? ");
-		String resp = scn.next();
-		if (resp.equalsIgnoreCase("c"))
-		{
-			System.out.print("Do you really want to destroy your db (Y/N)?");
-			if (scn.next().equalsIgnoreCase("y"))
+			System.out.print("(C)lean install, (U)pdate or (E)xit? ");
+			String resp = scn.next();
+			if (resp.equalsIgnoreCase("c"))
 			{
-				rt = new RunTasks(this, db, dir, cleanUp, true);
+				System.out.print("Do you really want to destroy your db (Y/N)?");
+				if (scn.next().equalsIgnoreCase("y"))
+				{
+					rt = new RunTasks(this, db, dir, cleanUp, true);
+				}
+			}
+			else if (resp.equalsIgnoreCase("u"))
+			{
+				rt = new RunTasks(this, db, dir, cleanUp, false);
 			}
-		}
-		else if (resp.equalsIgnoreCase("u"))
-		{
-			rt = new RunTasks(this, db, dir, cleanUp, false);
 		}
 		
 		if (rt != null)
@@ -120,15 +122,12 @@ public class DBInstallerConsole implements DBOutputInterface
 	public int requestConfirm(String title, String message, int type)
 	{
 		System.out.print(message);
-		Scanner scn = new Scanner(System.in);
-		
-		String res = scn.next();
-		if (res.equalsIgnoreCase("y"))
+		String res = "";
+		try (Scanner scn = new Scanner(new CloseShieldedInputStream(System.in)))
 		{
-			return 0;
+			res = scn.next();
 		}
-		
-		return 1;
+		return res.equalsIgnoreCase("y") ? 0 : 1;
 	}
 	
 	@Override

+ 120 - 0
L2J_Server_BETA/java/com/l2jserver/util/CloseShieldedInputStream.java

@@ -0,0 +1,120 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Prevent the underlying input stream to close.
+ * @author Joe Cheng, Zoey76
+ */
+public class CloseShieldedInputStream extends InputStream
+{
+	private InputStream _in = null;
+	
+	/**
+	 * Instantiates a new close shielded input stream.
+	 * @param in the in
+	 */
+	public CloseShieldedInputStream(InputStream in)
+	{
+		_in = in;
+	}
+	
+	@Override
+	public void close()
+	{
+		_in = null;
+	}
+	
+	@Override
+	public int read() throws IOException
+	{
+		if (_in == null)
+		{
+			throw new IOException("Stream is null!");
+		}
+		return _in.read();
+	}
+	
+	@Override
+	public int read(byte b[]) throws IOException
+	{
+		if (_in == null)
+		{
+			throw new IOException("Stream is null!");
+		}
+		return _in.read(b);
+	}
+	
+	@Override
+	public int read(byte b[], int off, int len) throws IOException
+	{
+		if (_in == null)
+		{
+			throw new IOException("Stream is null!");
+		}
+		return _in.read(b, off, len);
+	}
+	
+	@Override
+	public long skip(long n) throws IOException
+	{
+		if (_in == null)
+		{
+			throw new IOException("Stream is null!");
+		}
+		return _in.skip(n);
+	}
+	
+	@Override
+	public synchronized void mark(int readlimit)
+	{
+		if (_in != null)
+		{
+			_in.mark(readlimit);
+		}
+	}
+	
+	@Override
+	public boolean markSupported()
+	{
+		if (_in == null)
+		{
+			return false;
+		}
+		return _in.markSupported();
+	}
+	
+	@Override
+	public synchronized void reset() throws IOException
+	{
+		if (_in == null)
+		{
+			throw new IOException("Stream is null!");
+		}
+		_in.reset();
+	}
+	
+	/**
+	 * Gets the underlying stream.
+	 * @return the underlying stream
+	 */
+	public InputStream getUnderlyingStream()
+	{
+		return _in;
+	}
+}