Search in sources :

Example 1 with EngineException

use of jgnash.engine.EngineException in project jgnash by ccavanaugh.

the class JpaNetworkServer method startServer.

public synchronized void startServer(final String fileName, final int port, final char[] password) throws EngineException {
    final Path file = Paths.get(fileName);
    // create the base directory if needed
    if (!Files.exists(file)) {
        final Path parent = file.getParent();
        if (parent != null && !Files.exists(parent)) {
            try {
                Files.createDirectories(parent);
            } catch (IOException e) {
                throw new EngineException("Could not create directory for file: " + parent);
            }
        }
    }
    final FileMagic.FileType type = FileMagic.magic(Paths.get(fileName));
    switch(type) {
        case h2:
        case h2mv:
            runH2Server(fileName, port, password);
            break;
        case hsql:
            runHsqldbServer(fileName, port, password);
            break;
        default:
            logger.severe("Not a valid file type for server usage");
    }
    // force exit
    System.exit(0);
}
Also used : Path(java.nio.file.Path) FileMagic(jgnash.util.FileMagic) EngineException(jgnash.engine.EngineException) IOException(java.io.IOException)

Example 2 with EngineException

use of jgnash.engine.EngineException in project jgnash by ccavanaugh.

the class ApiTest method createEngine.

@Override
protected Engine createEngine() throws IOException {
    database = testFolder.createFile("api-test.bxds").getAbsolutePath();
    EngineFactory.deleteDatabase(database);
    try {
        final Engine engine = EngineFactory.bootLocalEngine(database, EngineFactory.DEFAULT, EngineFactory.EMPTY_PASSWORD, DataStoreType.BINARY_XSTREAM);
        // disable for test
        engine.setCreateBackups(false);
        return engine;
    } catch (final EngineException e) {
        fail("Fatal error occurred");
        return null;
    }
}
Also used : EngineException(jgnash.engine.EngineException) Engine(jgnash.engine.Engine)

Example 3 with EngineException

use of jgnash.engine.EngineException in project jgnash by ccavanaugh.

the class JpaNetworkServer method run.

/**
 * Starts the server and blocks until it is stopped
 *
 * @param dataStoreType datastore type
 * @param fileName      database file name
 * @param port          port
 * @param password      password*
 * @throws EngineException thrown if engine or buss cannot be created
 */
private synchronized void run(final DataStoreType dataStoreType, final String fileName, final int port, final char[] password) throws EngineException {
    final DistributedLockServer distributedLockServer = new DistributedLockServer(port + LOCK_SERVER_INCREMENT);
    final boolean lockServerStarted = distributedLockServer.startServer(password);
    final AttachmentTransferServer attachmentTransferServer = new AttachmentTransferServer(port + TRANSFER_SERVER_INCREMENT, AttachmentUtils.getAttachmentDirectory(Paths.get(fileName)));
    final boolean attachmentServerStarted = attachmentTransferServer.startServer(password);
    if (attachmentServerStarted && lockServerStarted) {
        final Engine engine = createEngine(dataStoreType, fileName, port, password);
        if (engine != null) {
            // Start the message bus and pass the file name so it can be reported to the client
            final MessageBusServer messageBusServer = new MessageBusServer(port + MESSAGE_SERVER_INCREMENT);
            // don't continue if the server is not started successfully
            if (messageBusServer.startServer(dataStoreType, fileName, password)) {
                // Start the backup thread that ensures an XML backup is created at set intervals
                final ScheduledExecutorService backupExecutor = Executors.newSingleThreadScheduledExecutor(new DefaultDaemonThreadFactory("JPA Network Server Executor"));
                // run commit every backup period after startup
                backupExecutor.scheduleWithFixedDelay(() -> {
                    if (dirty) {
                        exportXML(engine, fileName);
                        EngineFactory.removeOldCompressedXML(fileName, engine.getRetainedBackupLimit());
                        dirty = false;
                    }
                }, BACKUP_PERIOD, BACKUP_PERIOD, TimeUnit.HOURS);
                final LocalServerListener listener = event -> {
                    // look for a remote request to stop the server
                    if (event.startsWith(STOP_SERVER_MESSAGE)) {
                        logger.info("Remote shutdown request was received");
                        stopServer();
                    }
                    dirty = true;
                };
                messageBusServer.addLocalListener(listener);
                // if a callback has been registered, call it
                if (runningCallback != null) {
                    runningCallback.run();
                }
                // wait here forever
                try {
                    while (stopLatch.getCount() != 0) {
                        // check for condition, handle a spurious wake up
                        // wait forever from stopServer()
                        stopLatch.await();
                    }
                } catch (final InterruptedException ex) {
                    logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                    Thread.currentThread().interrupt();
                }
                messageBusServer.removeLocalListener(listener);
                backupExecutor.shutdown();
                exportXML(engine, fileName);
                messageBusServer.stopServer();
                EngineFactory.closeEngine(SERVER_ENGINE);
                EngineFactory.removeOldCompressedXML(fileName, engine.getRetainedBackupLimit());
                distributedLockManager.disconnectFromServer();
                distributedAttachmentManager.disconnectFromServer();
                distributedLockServer.stopServer();
                attachmentTransferServer.stopServer();
                em.close();
                factory.close();
            } else {
                throw new EngineException("Failed to start the Message Bus");
            }
        } else {
            throw new EngineException("Failed to create the engine");
        }
    } else {
        if (lockServerStarted) {
            distributedLockServer.stopServer();
        }
        if (attachmentServerStarted) {
            attachmentTransferServer.stopServer();
        }
    }
}
Also used : Engine(jgnash.engine.Engine) Persistence(javax.persistence.Persistence) EngineFactory(jgnash.engine.EngineFactory) AttachmentTransferServer(jgnash.engine.attachment.AttachmentTransferServer) LocalServerListener(jgnash.engine.message.LocalServerListener) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) SQLException(java.sql.SQLException) DefaultDaemonThreadFactory(jgnash.util.DefaultDaemonThreadFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MessageBusServer(jgnash.engine.message.MessageBusServer) Path(java.nio.file.Path) Properties(java.util.Properties) AttachmentUtils(jgnash.engine.AttachmentUtils) DistributedAttachmentManager(jgnash.engine.attachment.DistributedAttachmentManager) Files(java.nio.file.Files) FileMagic(jgnash.util.FileMagic) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) DistributedLockManager(jgnash.engine.concurrent.DistributedLockManager) Logger(java.util.logging.Logger) Executors(java.util.concurrent.Executors) EngineException(jgnash.engine.EngineException) StoredObject(jgnash.engine.StoredObject) TimeUnit(java.util.concurrent.TimeUnit) LogUtil(jgnash.util.LogUtil) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Paths(java.nio.file.Paths) EntityManagerFactory(javax.persistence.EntityManagerFactory) FileUtils(jgnash.util.FileUtils) DistributedLockServer(jgnash.engine.concurrent.DistributedLockServer) DataStoreType(jgnash.engine.DataStoreType) MessageBusServer(jgnash.engine.message.MessageBusServer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LocalServerListener(jgnash.engine.message.LocalServerListener) DistributedLockServer(jgnash.engine.concurrent.DistributedLockServer) EngineException(jgnash.engine.EngineException) DefaultDaemonThreadFactory(jgnash.util.DefaultDaemonThreadFactory) AttachmentTransferServer(jgnash.engine.attachment.AttachmentTransferServer) Engine(jgnash.engine.Engine)

Aggregations

EngineException (jgnash.engine.EngineException)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Engine (jgnash.engine.Engine)2 FileMagic (jgnash.util.FileMagic)2 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 Persistence (javax.persistence.Persistence)1