Example usage for org.hibernate.boot.registry StandardServiceRegistry getService

List of usage examples for org.hibernate.boot.registry StandardServiceRegistry getService

Introduction

In this page you can find the example usage for org.hibernate.boot.registry StandardServiceRegistry getService.

Prototype

<R extends Service> R getService(Class<R> serviceRole);

Source Link

Document

Retrieve a service by role.

Usage

From source file:edu.jhuapl.dorset.components.HibernateServiceTest.java

License:Open Source License

@Test
public void testCreationOfSessionFactory() {
    Properties props = getProperties();
    Config conf = ConfigFactory.parseProperties(props);

    hs = new HibernateService(conf);
    SessionFactory sf = hs.getSessionFactory();
    assertNotNull(sf);/*from  www.j  a  v a  2s . co  m*/
    assertFalse(sf.isClosed());

    // traverse through the session factory to get at configuration values
    SessionFactoryOptions sfo = sf.getSessionFactoryOptions();
    StandardServiceRegistry ssr = sfo.getServiceRegistry();
    ConfigurationService cs = ssr.getService(ConfigurationService.class);
    assertEquals(props.getProperty("hibernate.connection.driver_class"),
            cs.getSetting("hibernate.connection.driver_class", StandardConverters.STRING));
    assertEquals(props.getProperty("hibernate.connection.url"),
            cs.getSetting("hibernate.connection.url", StandardConverters.STRING));
    assertEquals(props.getProperty("hibernate.dialect"),
            cs.getSetting("hibernate.dialect", StandardConverters.STRING));
    assertEquals(props.getProperty("hibernate.hbm2ddl.auto"),
            cs.getSetting("hibernate.hbm2ddl.auto", StandardConverters.STRING));

    // check mapping
    ClassMetadata cm = sf.getClassMetadata(TestObject.class);
    String[] names = cm.getPropertyNames();
    assertEquals(1, names.length);
    assertEquals("name", names[0]);
    assertEquals("string", cm.getPropertyType("name").getName());
}

From source file:org.infinispan.test.hibernate.cache.AbstractGeneralDataRegionTest.java

License:LGPL

protected void withSessionFactoriesAndRegions(int num, SFRConsumer consumer) throws Exception {
    StandardServiceRegistryBuilder ssrb = createStandardServiceRegistryBuilder()
            .applySetting(AvailableSettings.CACHE_REGION_FACTORY, TestInfinispanRegionFactory.class.getName());
    Properties properties = CacheTestUtil.toProperties(ssrb.getSettings());
    List<StandardServiceRegistry> registries = new ArrayList<>();
    List<SessionFactory> sessionFactories = new ArrayList<>();
    List<GeneralDataRegion> regions = new ArrayList<>();
    for (int i = 0; i < num; ++i) {
        StandardServiceRegistry registry = ssrb.build();
        registries.add(registry);//from  ww  w.  j  a v  a2 s . c  o  m

        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        sessionFactories.add(sessionFactory);

        InfinispanRegionFactory regionFactory = (InfinispanRegionFactory) registry
                .getService(RegionFactory.class);
        GeneralDataRegion region = (GeneralDataRegion) createRegion(regionFactory,
                getStandardRegionName(REGION_PREFIX), properties, null);
        regions.add(region);
    }
    waitForClusterToForm(regions);
    try {
        consumer.accept(sessionFactories, regions);
    } finally {
        for (SessionFactory sessionFactory : sessionFactories) {
            sessionFactory.close();
        }
        for (StandardServiceRegistry registry : registries) {
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }
}

From source file:org.infinispan.test.hibernate.cache.commons.AbstractGeneralDataRegionTest.java

License:LGPL

protected void withSessionFactoriesAndRegions(int num, SFRConsumer consumer) throws Exception {
    StandardServiceRegistryBuilder ssrb = createStandardServiceRegistryBuilder().applySetting(
            AvailableSettings.CACHE_REGION_FACTORY,
            TestRegionFactoryProvider.load().getRegionFactoryClass().getName());
    Properties properties = CacheTestUtil.toProperties(ssrb.getSettings());
    List<StandardServiceRegistry> registries = new ArrayList<>();
    List<SessionFactory> sessionFactories = new ArrayList<>();
    List<InfinispanBaseRegion> regions = new ArrayList<>();
    for (int i = 0; i < num; ++i) {
        StandardServiceRegistry registry = ssrb.build();
        registries.add(registry);//from   www.j  ava2 s. c  o m

        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        sessionFactories.add(sessionFactory);

        TestRegionFactory regionFactory = TestRegionFactoryProvider.load()
                .wrap(registry.getService(RegionFactory.class));
        InfinispanBaseRegion region = createRegion(regionFactory, REGION_PREFIX + "/who-cares");
        regions.add(region);
    }
    waitForClusterToForm(regions);
    try {
        consumer.accept(sessionFactories, regions);
    } finally {
        for (SessionFactory sessionFactory : sessionFactories) {
            sessionFactory.close();
        }
        for (StandardServiceRegistry registry : registries) {
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.JndiRegionFactoryTest.java

License:LGPL

@Override
protected void afterStandardServiceRegistryBuilt(StandardServiceRegistry ssr) {
    if (bindToJndi) {
        try {/*from w w w . java  2 s  .c  om*/
            // Create an in-memory jndi
            namingServer = new SingletonNamingServer();
            namingMain = new Main();
            namingMain.setInstallGlobalService(true);
            namingMain.setPort(-1);
            namingMain.start();
            props = new Properties();
            props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
            props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

            final String cfgFileName = (String) ssr.getService(ConfigurationService.class).getSettings()
                    .get(InfinispanProperties.INFINISPAN_CONFIG_RESOURCE_PROP);
            manager = new DefaultCacheManager(
                    cfgFileName == null ? InfinispanProperties.DEF_INFINISPAN_CONFIG_RESOURCE : cfgFileName,
                    false);
            Context ctx = new InitialContext(props);
            bind(JNDI_NAME, manager, EmbeddedCacheManager.class, ctx);
        } catch (Exception e) {
            throw new RuntimeException("Failure to set up JNDI", e);
        }
    }
}

From source file:org.infinispan.test.hibernate.cache.commons.JndiInfinispanRegionFactoryTestCase.java

License:LGPL

@Test
public void testConstruction() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
            .applySetting(AvailableSettings.CACHE_REGION_FACTORY, JndiInfinispanRegionFactory.class.getName())
            .build();//from  w w  w . ja va2  s  . co  m
    try {
        RegionFactory regionFactory = ssr.getService(RegionFactory.class);
        assertTyping(JndiInfinispanRegionFactory.class, regionFactory);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}

From source file:org.infinispan.test.hibernate.cache.functional.JndiRegionFactoryTest.java

License:LGPL

@Override
protected void afterStandardServiceRegistryBuilt(StandardServiceRegistry ssr) {
    if (bindToJndi) {
        try {//from   w  w  w . j  a  va2 s  .  c  om
            // Create an in-memory jndi
            namingServer = new SingletonNamingServer();
            namingMain = new Main();
            namingMain.setInstallGlobalService(true);
            namingMain.setPort(-1);
            namingMain.start();
            props = new Properties();
            props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
            props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

            final String cfgFileName = (String) ssr.getService(ConfigurationService.class).getSettings()
                    .get(InfinispanRegionFactory.INFINISPAN_CONFIG_RESOURCE_PROP);
            manager = new DefaultCacheManager(
                    cfgFileName == null ? InfinispanRegionFactory.DEF_INFINISPAN_CONFIG_RESOURCE : cfgFileName,
                    false);
            Context ctx = new InitialContext(props);
            bind(JNDI_NAME, manager, EmbeddedCacheManager.class, ctx);
        } catch (Exception e) {
            throw new RuntimeException("Failure to set up JNDI", e);
        }
    }
}