Example usage for org.hibernate.boot SessionFactoryBuilder unwrap

List of usage examples for org.hibernate.boot SessionFactoryBuilder unwrap

Introduction

In this page you can find the example usage for org.hibernate.boot SessionFactoryBuilder unwrap.

Prototype

<T extends SessionFactoryBuilder> T unwrap(Class<T> type);

Source Link

Document

Allows unwrapping this builder as another, more specific type.

Usage

From source file:com.booleanworks.peacockmantisshrimp.modules.hibernate.mongodbogm.HibernateOgmBootstapper.java

public boolean bootstrap() throws OgmHelperException {

    this.checkConfigurationAndUpdateStatus();

    if (this.getStatus() != Status.goodConfiguration) {
        Logger.getLogger(this.getClass().getCanonicalName()).log(Level.SEVERE,
                "Bootstrapper seems to be in a bad configuration... aborting.");
        return false;
    }//from  w w  w .  j a  v  a  2s  . c  om

    if (this.getStatus() == Status.bootstrapping) {
        throw new OgmHelperException("already bootstrapping  !!");
    }

    this.setStatus(Status.bootstrapping);

    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();

    //And specific config
    ssrb.applySetting(OgmProperties.ENABLED, true);

    //assuming you are using JTA in a non container environment
    //ssrb.applySetting(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta");
    //assuming JBoss TransactionManager in standalone mode
    //ssrb.applySetting(AvailableSettings.JTA_PLATFORM, "JBossTS");
    //Using provided settings
    ssrb.applySettings(this.getSettings());

    if (this.getSettings().containsKey(OgmProperties.DATASTORE_PROVIDER)) {
        String providerValue = (String) this.getSettings().get(OgmProperties.DATASTORE_PROVIDER);

        if (providerValue.toLowerCase().equals("mongodb")) {
            Logger.getLogger(this.getClass().getCanonicalName()).log(Level.INFO,
                    "Detecting MongoDb provider.... doing additional checks");
            if (!this.getSettings().containsKey(MongoDBProperties.AUTHENTICATION_MECHANISM)) {
                AuthenticationMechanismType authenticationMechanismType = AuthenticationMechanismType.SCRAM_SHA_1;

                Logger.getLogger(this.getClass().getCanonicalName()).log(Level.WARNING,
                        "No MongoDb authentication mecanism set, patching configuration, setting to "
                                + authenticationMechanismType.name());
                this.getSettings().put(MongoDBProperties.AUTHENTICATION_MECHANISM, authenticationMechanismType);

                Logger.getLogger(this.getClass().getCanonicalName()).log(Level.WARNING,
                        "Current setings are: " + this.getSettings().toString());
            }
        }

    }

    //saving ssrb
    this.setStandardServiceRegistryBuilder(ssrb);

    //building registry
    StandardServiceRegistry registry = ssrb.build();
    if (registry == null) {
        throw new OgmHelperException("Failed to get a registry !!");
    }

    //saving registry
    this.setStandardServiceRegistry(registry);

    MetadataSources ms = new MetadataSources(registry);

    for (Class cEntity : this.getDeclaredEntities()) {
        ms.addAnnotatedClass(cEntity);
        Logger.getLogger(this.getClass().getCanonicalName()).log(Level.INFO,
                "Added entity: " + cEntity.getCanonicalName());
    }

    Metadata md = ms.buildMetadata(registry);

    SessionFactoryBuilder sfb = md.getSessionFactoryBuilder();

    OgmSessionFactory osf = sfb.unwrap(OgmSessionFactoryBuilder.class).build();

    //Saving the MetadataSources
    this.setMetadataSources(ms);

    //Saving SessionFactoryBuilder
    this.setSessionFactoryBuilder(sfb);

    //Savinf the OgmSessionFactory 
    this.setOgmSessionFactory(osf);

    this.setStatus(Status.ready);

    return true;
}