All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.randombits.confluence.metadata.indexing.graph.DefaultIndexDatabase Maven / Gradle / Ivy

There is a newer version: 7.4.1
Show newest version
package org.randombits.confluence.metadata.indexing.graph;

import com.atlassian.confluence.setup.BootstrapManager;
import com.orientechnologies.orient.core.Orient;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static java.io.File.separator;

/**
 * Manage the lifecycle of database connections.
 *
 * @author LongYC
 * @since 7.0.0.20150209
 */
@Component
public class DefaultIndexDatabase implements IndexDatabase, InitializingBean, DisposableBean {

    private static final Logger log = LoggerFactory.getLogger(DefaultIndexDatabase.class);

    public static final String DATABASE_NAME = "metadataIndexDB";

    private BootstrapManager bootstrapManager;
    private OrientGraphFactory orientGraphFactory;

    @Override
    public void afterPropertiesSet() {
        orientGraphFactory = initializeFactory();
    }

    @Override
    public void destroy() {
        shutdownFactory();
    }

    @Override
    public OrientGraphFactory initializeFactory() {
        if (orientGraphFactory == null) {
            return new OrientGraphFactory(getDatabaseDirectory()).setupPool(1, 30);
        }
        return orientGraphFactory;
    }

    @Override
    public void shutdownFactory() {
        if (orientGraphFactory != null) {
            Orient.instance().shutdown();
            orientGraphFactory.close();
        }
    }

    public String getDatabaseDirectory() {
        return "plocal:" + bootstrapManager.getLocalHome() + separator + "com.servicerocket.confluence.metadata" + separator + DATABASE_NAME;
    }

    @Override
    public  T executeTransaction(Transaction transaction) {
        OrientGraph graph = orientGraphFactory.getTx();
        try {
            T result = transaction.execute(graph);
            graph.commit();
            return result;
        } catch (Exception e) {
            graph.rollback();
            log.debug("Error in executing Metadata index database transaction.", e);
        } finally {
            graph.shutdown();
        }
        return null;
    }

    @Autowired
    public void setBootstrapManager(BootstrapManager bootstrapManager) {
        this.bootstrapManager = bootstrapManager;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy