/**
   * Create an audit reader associated with an open session.
   *
   * @param session An open session.
   * @return An audit reader associated with the given sesison. It shouldn't be used after the
   *     session is closed.
   * @throws AuditException When the given required listeners aren't installed.
   */
  public static AuditReader get(Session session) throws AuditException {
    SessionImplementor sessionImpl;
    if (!(session instanceof SessionImplementor)) {
      sessionImpl = (SessionImplementor) session.getSessionFactory().getCurrentSession();
    } else {
      sessionImpl = (SessionImplementor) session;
    }

    // todo : I wonder if there is a better means to do this via "named lookup" based on the session
    // factory name/uuid
    final EventListenerRegistry listenerRegistry =
        sessionImpl.getFactory().getServiceRegistry().getService(EventListenerRegistry.class);

    for (PostInsertEventListener listener :
        listenerRegistry.getEventListenerGroup(EventType.POST_INSERT).listeners()) {
      if (listener instanceof EnversListener) {
        // todo : slightly different from original code in that I am not checking the other listener
        // groups...
        return new AuditReaderImpl(
            ((EnversListener) listener).getAuditConfiguration(), session, sessionImpl);
      }
    }

    throw new AuditException("Envers listeners were not properly registered");
  }
  public void setUp() throws Exception {
    Configuration conf =
        new Configuration()
            .configure("/org/compass/gps/device/hibernate/cascade/one/hibernate.cfg.xml")
            .setProperty(Environment.HBM2DDL_AUTO, "create");
    sessionFactory = conf.buildSessionFactory();

    // set the session factory for the Hibernate transcation factory BEFORE we construct the compass
    // instnace
    HibernateSyncTransactionFactory.setSessionFactory(sessionFactory);

    final ObjectContext theObjectContext = ObjectContextFactory.create();

    theObjectContext.add(Foundations.class);
    theObjectContext.add(House.class);
    theObjectContext.add(Roof.class);

    //        CompassConfiguration cpConf = new CompassConfiguration()
    //                .configure(getCompassConfigLocation());
    //        cpConf.getSettings().setBooleanSetting(CompassEnvironment.DEBUG, true);
    compass =
        ElasticSearchTests.mockSimpleCompass(
            "10.10.10.101", theObjectContext); // cpConf.buildCompass();

    fileHandlerMonitor = FileHandlerMonitor.getFileHandlerMonitor(compass);
    fileHandlerMonitor.verifyNoHandlers();

    ElasticSearchTests.deleteAllIndexes(compass);
    ElasticSearchTests.verifyAllIndexes(compass);

    //        HibernateEntityLifecycleInjector lifecycleInjector = new
    // org.compass.gps.device.hibernate.lifecycle.DefaultHibernateEntityCollectionLifecycleInjector(true);

    HibernateGpsDevice compassGpsDevice = new HibernateGpsDevice();
    compassGpsDevice.setName("hibernate");
    compassGpsDevice.setSessionFactory(sessionFactory);
    compassGpsDevice.setFetchCount(5000);
    //        compassGpsDevice.setLifecycleInjector(lifecycleInjector);

    SingleCompassGps compassGps = new SingleCompassGps();
    compassGps.setCompass(compass);
    compassGps.setGpsDevices(new CompassGpsDevice[] {compassGpsDevice});

    compassGps.start();

    SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;

    EventListenerRegistry theRegistry = null; // (AGR_OSEM) Hib4

    EventListenerGroup<PostInsertEventListener> theGroup =
        theRegistry.getEventListenerGroup(EventType.POST_INSERT);

    for (PostInsertEventListener listener : theGroup.listeners()) {
      if (listener instanceof HibernateEventListener) {
        hibernateEventListener = (HibernateEventListener) listener;
        break;
      }
    }
  }
Ejemplo n.º 3
0
  @Test
  public void testListeners() throws Exception {
    File testPackage = buildExplicitPar();
    addPackageToClasspath(testPackage);

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1", new HashMap());
    EntityManager em = emf.createEntityManager();
    EventListenerRegistry listenerRegistry =
        em.unwrap(SessionImplementor.class)
            .getFactory()
            .getServiceRegistry()
            .getService(EventListenerRegistry.class);
    assertEquals(
        "Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work",
        listenerRegistry.getEventListenerGroup(EventType.PRE_INSERT).count(),
        listenerRegistry.getEventListenerGroup(EventType.PRE_UPDATE).count() + 1);

    em.close();
    emf.close();
  }