Search in sources :

Example 1 with DistributedAttachmentManager

use of jgnash.engine.attachment.DistributedAttachmentManager in project jgnash by ccavanaugh.

the class JpaNetworkServer method createEngine.

private Engine createEngine(final DataStoreType dataStoreType, final String fileName, final int port, final char[] password) {
    final Properties properties = JpaConfiguration.getClientProperties(dataStoreType, fileName, EngineFactory.LOCALHOST, port, password);
    logger.log(Level.INFO, "Local connection url is: {0}", properties.getProperty(JpaConfiguration.JAVAX_PERSISTENCE_JDBC_URL));
    Engine engine = null;
    try {
        // An exception will be thrown if the password is not correct, or the database did not have a password
        if (SqlUtils.isConnectionValid(properties.getProperty(JpaConfiguration.JAVAX_PERSISTENCE_JDBC_URL))) {
            if (SqlUtils.useOldPersistenceUnit(fileName, password)) {
                System.out.println("Using old persistence unit");
                factory = Persistence.createEntityManagerFactory(JpaConfiguration.OLD_UNIT_NAME, properties);
            } else {
                factory = Persistence.createEntityManagerFactory(JpaConfiguration.UNIT_NAME, properties);
            }
            em = factory.createEntityManager();
            distributedLockManager = new DistributedLockManager(EngineFactory.LOCALHOST, port + LOCK_SERVER_INCREMENT);
            distributedLockManager.connectToServer(password);
            distributedAttachmentManager = new DistributedAttachmentManager(EngineFactory.LOCALHOST, port + TRANSFER_SERVER_INCREMENT);
            distributedAttachmentManager.connectToServer(password);
            logger.info("Created local JPA container and engine");
            engine = new Engine(new JpaEngineDAO(em, true), distributedLockManager, distributedAttachmentManager, // treat as a remote engine
            SERVER_ENGINE);
        }
    } catch (final Exception e) {
        logger.log(Level.SEVERE, e.toString(), e);
    }
    return engine;
}
Also used : DistributedAttachmentManager(jgnash.engine.attachment.DistributedAttachmentManager) DistributedLockManager(jgnash.engine.concurrent.DistributedLockManager) Properties(java.util.Properties) Engine(jgnash.engine.Engine) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 2 with DistributedAttachmentManager

use of jgnash.engine.attachment.DistributedAttachmentManager in project jgnash by ccavanaugh.

the class AbstractJpaDataStore method getClientEngine.

@Override
public Engine getClientEngine(final String host, final int port, final char[] password, final String dataBasePath) {
    final Properties properties = JpaConfiguration.getClientProperties(getType(), dataBasePath, host, port, password);
    Engine engine = null;
    try {
        if (SqlUtils.isConnectionValid(properties.getProperty(JpaConfiguration.JAVAX_PERSISTENCE_JDBC_URL))) {
            factory = Persistence.createEntityManagerFactory(JpaConfiguration.UNIT_NAME, properties);
            em = factory.createEntityManager();
            if (em != null) {
                distributedLockManager = new DistributedLockManager(host, port + JpaNetworkServer.LOCK_SERVER_INCREMENT);
                boolean lockManagerResult = distributedLockManager.connectToServer(password);
                distributedAttachmentManager = new DistributedAttachmentManager(host, port + JpaNetworkServer.TRANSFER_SERVER_INCREMENT);
                boolean attachmentManagerResult = distributedAttachmentManager.connectToServer(password);
                if (attachmentManagerResult && lockManagerResult) {
                    engine = new Engine(new JpaEngineDAO(em, true), distributedLockManager, distributedAttachmentManager, EngineFactory.DEFAULT);
                    logger.info("Created local JPA container and engine");
                    fileName = null;
                    remote = true;
                } else {
                    distributedLockManager.disconnectFromServer();
                    distributedAttachmentManager.disconnectFromServer();
                    em.close();
                    factory.close();
                    em = null;
                    factory = null;
                }
            }
        }
    } catch (final Exception e) {
        logger.log(Level.SEVERE, e.toString(), e);
    }
    return engine;
}
Also used : DistributedAttachmentManager(jgnash.engine.attachment.DistributedAttachmentManager) DistributedLockManager(jgnash.engine.concurrent.DistributedLockManager) Properties(java.util.Properties) Engine(jgnash.engine.Engine) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Aggregations

IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Properties (java.util.Properties)2 Engine (jgnash.engine.Engine)2 DistributedAttachmentManager (jgnash.engine.attachment.DistributedAttachmentManager)2 DistributedLockManager (jgnash.engine.concurrent.DistributedLockManager)2