Search in sources :

Example 1 with JpaNetworkServer

use of jgnash.engine.jpa.JpaNetworkServer in project jgnash by ccavanaugh.

the class jGnashFx method startServer.

private static void startServer() {
    try {
        if (!FileUtils.isFileLocked(serverFile.getAbsolutePath())) {
            JpaNetworkServer networkServer = new JpaNetworkServer();
            networkServer.startServer(serverFile.getAbsolutePath(), port, password);
            // clear the password to protect against malicious code
            Arrays.fill(password, (char) 0);
        } else {
            System.err.println(ResourceUtils.getString("Message.FileIsLocked"));
        }
    } catch (final FileNotFoundException e) {
        logSevere(jGnashFx.class, e);
        System.err.println("File " + serverFile.getAbsolutePath() + " was not found");
    } catch (final Exception e) {
        logSevere(jGnashFx.class, e);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) JpaNetworkServer(jgnash.engine.jpa.JpaNetworkServer) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with JpaNetworkServer

use of jgnash.engine.jpa.JpaNetworkServer in project jgnash by ccavanaugh.

the class FileTransferTest method networkedTest.

@Test
public void networkedTest() throws Exception {
    final int port = JpaNetworkServer.DEFAULT_PORT + 100;
    //System.setProperty(EncryptionManager.ENCRYPTION_FLAG, "false");
    //System.setProperty("ssl", "false");
    ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID);
    String testFile = null;
    try {
        Path temp = Files.createTempFile("jpa-test", JpaHsqlDataStore.FILE_EXT);
        Files.delete(temp);
        testFile = temp.toString();
        assertNotNull(testFile);
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                Files.delete(temp);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }));
    } catch (final IOException e) {
        Logger.getLogger(FileTransferTest.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
        fail();
    }
    // Start an engine and close so we have a populated file
    EngineFactory.bootLocalEngine(testFile, EngineFactory.DEFAULT, EngineFactory.EMPTY_PASSWORD, DataStoreType.HSQL_DATABASE);
    EngineFactory.closeEngine(EngineFactory.DEFAULT);
    final JpaNetworkServer networkServer = new JpaNetworkServer();
    final String serverFile = testFile;
    Logger.getLogger(FileTransferTest.class.getName()).info("Starting Server");
    new StartServerThread(networkServer, serverFile, port, EngineFactory.EMPTY_PASSWORD).start();
    Thread.sleep(4000);
    try {
        Engine e = EngineFactory.bootClientEngine(EngineFactory.LOCALHOST, port, EngineFactory.EMPTY_PASSWORD, EngineFactory.DEFAULT);
        Account account = new Account(AccountType.CASH, e.getDefaultCurrency());
        account.setName("test");
        e.addAccount(e.getRootAccount(), account);
        Path tempAttachment = Paths.get(Object.class.getResource("/jgnash-logo.png").toURI());
        assertTrue(Files.exists(tempAttachment));
        // push a copy of the attachment
        assertTrue(e.addAttachment(tempAttachment, true));
        // wait for transfer to finish
        Thread.sleep(4000);
        Path newPath = Paths.get(AttachmentUtils.getAttachmentDirectory(Paths.get(testFile)) + FileUtils.separator + tempAttachment.getFileName());
        newPath.toFile().deleteOnExit();
        // Verify copy has occurred
        // same length?
        assertEquals(tempAttachment.toFile().length(), newPath.toFile().length());
        // different files?
        assertNotEquals(tempAttachment.toString(), newPath.toString());
        // Test that move is working
        Path moveFile = Files.createTempFile("jgnash", "test");
        try (final BufferedWriter bw = Files.newBufferedWriter(moveFile, Charset.defaultCharset())) {
            bw.write("This is the temporary file content 3.");
        }
        assertTrue(e.addAttachment(moveFile, false));
        assertFalse(Files.exists(moveFile));
        final Path attachmentPath = AttachmentUtils.getAttachmentDirectory(Paths.get(testFile));
        assertNotNull(attachmentPath);
        // Create a new temp file in the directory
        tempAttachment = Files.createTempFile(attachmentPath, "tempfile2-", ".txt");
        tempAttachment.toFile().deleteOnExit();
        try (final BufferedWriter bw = Files.newBufferedWriter(tempAttachment, Charset.defaultCharset())) {
            bw.write("This is the temporary file content 2.");
        }
        Future<Path> pathFuture = e.getAttachment(tempAttachment.getFileName().toString());
        Path remoteTemp = pathFuture.get();
        assertTrue(Files.exists(remoteTemp));
        assertNotEquals(remoteTemp.toString(), tempAttachment.toString());
        EngineFactory.closeEngine(EngineFactory.DEFAULT);
    } catch (Exception e) {
        Logger.getLogger(FileTransferTest.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
        fail();
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) JpaNetworkServer(jgnash.engine.jpa.JpaNetworkServer) Test(org.junit.Test)

Example 3 with JpaNetworkServer

use of jgnash.engine.jpa.JpaNetworkServer in project jgnash by ccavanaugh.

the class Main method init.

private void init(final String[] args) {
    configureLogging();
    final OptionParser parser = buildParser();
    try {
        final OptionSet options = parser.parse(args);
        /* handle a file name passed in as an argument without use of the -file argument
               assumed behavior for windows users */
        if (!options.nonOptionArguments().isEmpty()) {
            // Check for no-option version of a file load
            for (final Object object : options.nonOptionArguments()) {
                if (object instanceof String) {
                    if (Files.exists(Paths.get((String) object))) {
                        file = new File((String) object);
                        break;
                    } else {
                        System.err.println(object + " was not a valid file");
                    }
                }
            }
        }
        if (options.has(EDT_OPTION)) {
            enableEDT = true;
        }
        if (options.has(VERBOSE_OPTION_SHORT)) {
            verbose = true;
        }
        if (options.has(HANG_DETECT_OPTION)) {
            hangDetect = true;
        }
        if (options.has(PORT_OPTION)) {
            port = (Integer) options.valueOf(PORT_OPTION);
        }
        if (options.has(PASSWORD_OPTION)) {
            password = ((String) options.valueOf(PASSWORD_OPTION)).toCharArray();
        }
        if (options.has(HOST_OPTION)) {
            hostName = (String) options.valueOf(HOST_OPTION);
        }
        if (options.has(FILE_OPTION_SHORT) && file == null) {
            file = (File) options.valueOf(FILE_OPTION_SHORT);
            if (!file.exists()) {
                file = null;
            }
        }
        if (options.has(SERVER_OPTION)) {
            final File file = (File) options.valueOf(SERVER_OPTION);
            if (file.exists()) {
                serverFile = file;
            }
        }
        // Check to see if portable preferences are being used
        if (options.has(PORTABLE_FILE_OPTION)) {
            final File file = (File) options.valueOf(PORTABLE_FILE_OPTION);
            if (file.exists()) {
                portableFile = file;
            }
        } else if (options.has(PORTABLE_OPTION_SHORT)) {
            // simple use of portable preferences
            portable = true;
        }
        /* If a shutdown request is found, it trumps any other commandline options */
        if (options.has(SHUTDOWN_OPTION)) {
            if (hostName == null) {
                hostName = EngineFactory.LOCALHOST;
            }
            MessageBus.getInstance().shutDownRemoteServer(hostName, port + 1, password);
        } else if (options.has(UNINSTALL_OPTION_SHORT)) {
            /* Dump the registry settings if requested */
            PortablePreferences.deleteUserPreferences();
        } else if (serverFile != null) {
            try {
                if (!FileUtils.isFileLocked(serverFile.getAbsolutePath())) {
                    JpaNetworkServer networkServer = new JpaNetworkServer();
                    networkServer.startServer(serverFile.getAbsolutePath(), port, password);
                } else {
                    System.err.println(ResourceUtils.getString("Message.FileIsLocked"));
                }
            } catch (FileNotFoundException e) {
                logSevere(Main.class, e);
                System.err.println("File " + serverFile.getAbsolutePath() + " was not found");
            } catch (Exception e) {
                logSevere(Main.class, e);
            }
        } else {
            // start the UI
            if (portable || portableFile != null) {
                // must hook in the preferences implementation first
                // for best operation
                PortablePreferences.initPortablePreferences(portableFile.getAbsolutePath());
            }
            enableAntialiasing();
            if (options.has(OPEN_GL_OPTION)) {
                System.setProperty("sun.java2d.opengl", "True");
            }
            if (options.has(XRENDER_OPTION)) {
                System.setProperty("sun.java2d.xrender", "True");
            }
            if (OS.isSystemOSX()) {
                System.setProperty("apple.laf.useScreenMenuBar", "true");
            }
            setupNetworking();
            if (hostName != null) {
                new UIApplication(hostName, port, password);
            } else if (file != null && file.exists()) {
                new UIApplication(file.toPath(), password);
            } else {
                new UIApplication(null, null);
            }
        }
    } catch (final Exception e) {
        try {
            parser.printHelpOn(System.err);
        } catch (final IOException ioe) {
            logSevere(Main.class, ioe);
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) JpaNetworkServer(jgnash.engine.jpa.JpaNetworkServer) UIApplication(jgnash.ui.UIApplication) IOException(java.io.IOException) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with JpaNetworkServer

use of jgnash.engine.jpa.JpaNetworkServer in project jgnash by ccavanaugh.

the class FileTransferTest method encryptedNetworkedTest.

@Test
public void encryptedNetworkedTest() throws Exception {
    final char[] password = new char[] { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' };
    //System.setProperty(EncryptionManager.ENCRYPTION_FLAG, "true");
    //System.setProperty("ssl", "true");
    ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID);
    String testFile = null;
    try {
        final Path temp = Files.createTempFile("jpa-test-e", JpaH2DataStore.FILE_EXT);
        Files.delete(temp);
        testFile = temp.toString();
        assertNotNull(testFile);
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                Files.delete(temp);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }));
    } catch (final IOException e) {
        Logger.getLogger(FileTransferTest.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
        fail();
    }
    // Start an engine and close so we have a populated file
    EngineFactory.bootLocalEngine(testFile, EngineFactory.DEFAULT, EngineFactory.EMPTY_PASSWORD, DataStoreType.H2_DATABASE);
    EngineFactory.closeEngine(EngineFactory.DEFAULT);
    // Change the password
    SqlUtils.changePassword(testFile, EngineFactory.EMPTY_PASSWORD, password);
    final JpaNetworkServer networkServer = new JpaNetworkServer();
    final String serverFile = testFile;
    System.out.println("starting server");
    new StartServerThread(networkServer, serverFile, JpaNetworkServer.DEFAULT_PORT, password).start();
    // give the server enough time to start on slower or virtualized systems
    Thread.sleep(20000);
    try {
        Engine e = EngineFactory.bootClientEngine(EngineFactory.LOCALHOST, JpaNetworkServer.DEFAULT_PORT, password, EngineFactory.DEFAULT);
        Account account = new Account(AccountType.CASH, e.getDefaultCurrency());
        account.setName("test");
        e.addAccount(e.getRootAccount(), account);
        Path tempAttachment = Paths.get(Object.class.getResource("/jgnash-logo.png").toURI());
        assertTrue(Files.exists(tempAttachment));
        // push a copy of the attachment
        assertTrue(e.addAttachment(tempAttachment, true));
        // wait for the transfer to finish, it may have been pushed into the background
        Thread.sleep(4000);
        Path newPath = Paths.get(AttachmentUtils.getAttachmentDirectory(Paths.get(testFile)) + FileUtils.separator + tempAttachment.getFileName());
        newPath.toFile().deleteOnExit();
        // Verify copy has occurred
        // same length?
        assertEquals(tempAttachment.toFile().length(), newPath.toFile().length());
        // different files?
        assertNotEquals(tempAttachment.toString(), newPath.toString());
        final Path attachmentPath = AttachmentUtils.getAttachmentDirectory(Paths.get(testFile));
        assertNotNull(attachmentPath);
        // Create a new temp file in the directory
        tempAttachment = Files.createTempFile(attachmentPath, "tempfile2-", ".txt");
        tempAttachment.toFile().deleteOnExit();
        //write it
        try (BufferedWriter bw = Files.newBufferedWriter(tempAttachment, Charset.defaultCharset())) {
            bw.write("This is the temporary file content 2.");
        }
        Future<Path> pathFuture = e.getAttachment(tempAttachment.getFileName().toString());
        Path remoteTemp = pathFuture.get();
        assertTrue(Files.exists(remoteTemp));
        assertNotEquals(remoteTemp.toString(), tempAttachment.toString());
        EngineFactory.closeEngine(EngineFactory.DEFAULT);
    } catch (final Exception e) {
        Logger.getLogger(FileTransferTest.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
        fail();
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) JpaNetworkServer(jgnash.engine.jpa.JpaNetworkServer) Test(org.junit.Test)

Aggregations

JpaNetworkServer (jgnash.engine.jpa.JpaNetworkServer)4 IOException (java.io.IOException)3 BufferedWriter (java.io.BufferedWriter)2 FileNotFoundException (java.io.FileNotFoundException)2 Path (java.nio.file.Path)2 Test (org.junit.Test)2 File (java.io.File)1 UIApplication (jgnash.ui.UIApplication)1 OptionParser (joptsimple.OptionParser)1 OptionSet (joptsimple.OptionSet)1