Пример #1
0
  private Engine createEngine(
      final DataStoreType database, final String fileName, final int port, final char[] password) {

    Properties properties =
        JpaConfiguration.getClientProperties(database, fileName, "localhost", port, password);

    Logger.getLogger(JpaNetworkServer.class.getName())
        .log(
            Level.INFO,
            "Local connection url is: {0}",
            properties.getProperty(JpaConfiguration.JAVAX_PERSISTENCE_JDBC_URL));

    Engine engine = null;

    try {
      factory = Persistence.createEntityManagerFactory("jgnash", properties);

      em = factory.createEntityManager();

      distributedLockManager = new DistributedLockManager("localhost", port + 2);
      distributedLockManager.connectToServer(password);

      distributedAttachmentManager = new DistributedAttachmentManager("localhost", port + 3);
      distributedAttachmentManager.connectToServer(password);

      Logger.getLogger(JpaNetworkServer.class.getName())
          .info("Created local JPA container and engine");
      engine =
          new Engine(
              new JpaEngineDAO(em, true),
              distributedLockManager,
              distributedAttachmentManager,
              SERVER_ENGINE); // treat as a remote engine
    } catch (final Exception e) {
      Logger.getLogger(JpaNetworkServer.class.getName()).log(Level.SEVERE, e.toString(), e);
    }

    return engine;
  }
Пример #2
0
  private boolean run(
      final DataStoreType dataStoreType,
      final String fileName,
      final int port,
      final char[] password) {
    boolean result = false;

    final DistributedLockServer distributedLockServer = new DistributedLockServer(port + 2);
    final boolean lockServerStarted = distributedLockServer.startServer(password);

    final AttachmentTransferServer attachmentTransferServer =
        new AttachmentTransferServer(
            port + 3, 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 + 1);
        result = messageBusServer.startServer(dataStoreType, fileName, password);

        if (result) { // don't continue if the server is not started successfully
          // Start the backup thread that ensures an XML backup is created at set intervals
          final ScheduledExecutorService backupExecutor =
              Executors.newSingleThreadScheduledExecutor(new DefaultDaemonThreadFactory());

          // 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.getLogger(JpaNetworkServer.class.getName())
                      .info("Remote shutdown request was received");
                  stopServer();
                }

                dirty = true;
              };

          messageBusServer.addLocalListener(listener);

          // wait here forever
          try {
            while (!stop) { // check for condition, handle a spurious wake up
              wait(); // wait forever for notify() from stopServer()
            }
          } catch (final InterruptedException ex) {
            Logger.getLogger(JpaNetworkServer.class.getName()).log(Level.SEVERE, null, ex);
          }

          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 {
      if (lockServerStarted) {
        distributedLockServer.stopServer();
      }

      if (attachmentServerStarted) {
        attachmentTransferServer.stopServer();
      }
    }
    return result;
  }