/**
  * Create a new SpringSessionContext for the given Hibernate SessionFactory.
  *
  * @param sessionFactory the SessionFactory to provide current Sessions for
  */
 public SpringSessionContext(SessionFactoryImplementor sessionFactory) {
   this.sessionFactory = sessionFactory;
   JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class);
   TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
   this.jtaSessionContext =
       (transactionManager != null ? new SpringJtaSessionContext(sessionFactory) : null);
 }
  @Test
  public void testServiceContributorDiscovery() throws Exception {
    final ServiceReference sr = bundleContext.getServiceReference(SessionFactory.class.getName());
    final SessionFactoryImplementor sfi = (SessionFactoryImplementor) bundleContext.getService(sr);

    assertNotNull(sfi.getServiceRegistry().getService(SomeService.class));
  }
 @Override
 public void sessionFactoryCreated(SessionFactory factory) {
   SessionFactoryImplementor factoryImplementor = (SessionFactoryImplementor) factory;
   GridDialect gridDialect = factoryImplementor.getServiceRegistry().getService(GridDialect.class);
   SessionFactoryLifecycleAwareDialect sessionFactoryAwareDialect =
       GridDialects.getDialectFacetOrNull(gridDialect, SessionFactoryLifecycleAwareDialect.class);
   if (sessionFactoryAwareDialect != null) {
     sessionFactoryAwareDialect.sessionFactoryCreated(factoryImplementor);
   }
 }
 @Override
 public MassIndexer createMassIndexer(
     SearchFactoryImplementor searchFactory,
     SessionFactoryImplementor sessionFactory,
     Class<?>... entities) {
   return new OgmMassIndexer(
       sessionFactory.getServiceRegistry().getService(GridDialect.class),
       searchFactory,
       sessionFactory,
       entities);
 }
  @Test
  public void testExtensionPoints() throws Exception {
    final ServiceReference sr = bundleContext.getServiceReference(SessionFactory.class.getName());
    final SessionFactoryImplementor sfi = (SessionFactoryImplementor) bundleContext.getService(sr);

    assertTrue(TestIntegrator.passed());

    Class impl =
        sfi.getServiceRegistry()
            .getService(StrategySelector.class)
            .selectStrategyImplementor(Calendar.class, TestStrategyRegistrationProvider.GREGORIAN);
    assertNotNull(impl);

    BasicType basicType = sfi.getTypeResolver().basic(TestTypeContributor.NAME);
    assertNotNull(basicType);
  }
  @BeforeClassOnce
  @SuppressWarnings({"UnusedDeclaration"})
  public void buildEntityManagerFactory() throws Exception {
    log.trace("Building EntityManagerFactory");

    entityManagerFactoryBuilder =
        (EntityManagerFactoryBuilderImpl)
            Bootstrap.getEntityManagerFactoryBuilder(
                buildPersistenceUnitDescriptor(), buildSettings());
    entityManagerFactory =
        entityManagerFactoryBuilder.build().unwrap(SessionFactoryImplementor.class);

    serviceRegistry =
        (StandardServiceRegistryImpl)
            entityManagerFactory.getServiceRegistry().getParentServiceRegistry();

    afterEntityManagerFactoryBuilt();
  }
  OgmEntityPersister(
      final PersistentClass persistentClass,
      final EntityRegionAccessStrategy cacheAccessStrategy,
      final NaturalIdRegionAccessStrategy naturalIdRegionAccessStrategy,
      final SessionFactoryImplementor factory,
      final Mapping mapping,
      final EntityDiscriminator discriminator)
      throws HibernateException {
    super(persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, factory);
    if (log.isTraceEnabled()) {
      log.tracef("Creating OgmEntityPersister for %s", persistentClass.getClassName());
    }
    ServiceRegistryImplementor serviceRegistry = factory.getServiceRegistry();
    this.gridDialect = serviceRegistry.getService(GridDialect.class);
    this.optionsService = serviceRegistry.getService(OptionsService.class);

    tableName =
        persistentClass
            .getTable()
            .getQualifiedName(
                factory.getDialect(),
                factory.getSettings().getDefaultCatalogName(),
                factory.getSettings().getDefaultSchemaName());

    this.discriminator = discriminator;

    // SPACES
    // TODO: i'm not sure, but perhaps we should exclude
    //      abstract denormalized tables?

    int spacesSize = 1 + persistentClass.getSynchronizedTables().size();
    spaces = new String[spacesSize];
    spaces[0] = tableName;
    @SuppressWarnings("unchecked")
    Iterator<String> syncTablesIter = persistentClass.getSynchronizedTables().iterator();
    for (int i = 1; i < spacesSize; i++) {
      spaces[i] = syncTablesIter.next();
    }

    HashSet<String> subclassTables = new HashSet<String>();
    Iterator<Table> tableIter = persistentClass.getSubclassTableClosureIterator();
    while (tableIter.hasNext()) {
      Table table = tableIter.next();
      subclassTables.add(
          table.getQualifiedName(
              factory.getDialect(),
              factory.getSettings().getDefaultCatalogName(),
              factory.getSettings().getDefaultSchemaName()));
    }
    subclassSpaces = ArrayHelper.toStringArray(subclassTables);

    if (isMultiTable()) {
      int idColumnSpan = getIdentifierColumnSpan();
      ArrayList<String> tableNames = new ArrayList<String>();
      ArrayList<String[]> keyColumns = new ArrayList<String[]>();
      if (!isAbstract()) {
        tableNames.add(tableName);
        keyColumns.add(getIdentifierColumnNames());
      }
      @SuppressWarnings("unchecked")
      Iterator<Table> iter = persistentClass.getSubclassTableClosureIterator();
      while (iter.hasNext()) {
        Table tab = iter.next();
        if (!tab.isAbstractUnionTable()) {
          String tableName =
              tab.getQualifiedName(
                  factory.getDialect(),
                  factory.getSettings().getDefaultCatalogName(),
                  factory.getSettings().getDefaultSchemaName());
          tableNames.add(tableName);
          String[] key = new String[idColumnSpan];
          @SuppressWarnings("unchecked")
          Iterator<Column> citer = tab.getPrimaryKey().getColumnIterator();
          for (int k = 0; k < idColumnSpan; k++) {
            key[k] = citer.next().getQuotedName(factory.getDialect());
          }
          keyColumns.add(key);
        }
      }

      constraintOrderedTableNames = ArrayHelper.toStringArray(tableNames);
      constraintOrderedKeyColumnNames = ArrayHelper.to2DStringArray(keyColumns);
    } else {
      constraintOrderedTableNames = new String[] {tableName};
      constraintOrderedKeyColumnNames = new String[][] {getIdentifierColumnNames()};
    }

    initPropertyPaths(mapping);

    // Grid related metadata
    TypeTranslator typeTranslator = serviceRegistry.getService(TypeTranslator.class);
    final Type[] types = getPropertyTypes();
    final int length = types.length;
    gridPropertyTypes = new GridType[length];
    for (int index = 0; index < length; index++) {
      gridPropertyTypes[index] = typeTranslator.getType(types[index]);
    }
    gridVersionType = typeTranslator.getType(getVersionType());
    gridIdentifierType = typeTranslator.getType(getIdentifierType());
    List<String> columnNames = new ArrayList<String>();
    for (int propertyCount = 0; propertyCount < this.getPropertySpan(); propertyCount++) {
      String[] property = this.getPropertyColumnNames(propertyCount);
      for (int columnCount = 0; columnCount < property.length; columnCount++) {
        columnNames.add(property[columnCount]);
      }
    }
    if (discriminator.getColumnName() != null) {
      columnNames.add(discriminator.getColumnName());
    }
    this.tupleContext =
        new TupleContext(columnNames, optionsService.context().getEntityOptions(getMappedClass()));
    jpaEntityName = persistentClass.getJpaEntityName();
    entityKeyMetadata = new EntityKeyMetadata(getTableName(), getIdentifierColumnNames());
    // load unique key association key metadata
    associationKeyMetadataPerPropertyName = new HashMap<String, AssociationKeyMetadata>();
    initAssociationKeyMetadata();
    initCustomSQLStrings();
  }
 @Override
 public ServiceRegistryImplementor getServiceRegistry() {
   return delegate.getServiceRegistry();
 }