public void apply(InternalObjectContainer container) {
    final Queries queries = Db4oMBeans.newQueriesMBean(container);
    final CommonConfiguration config =
        Db4oLegacyConfigurationBridge.asCommonConfiguration(container.configure());
    config
        .diagnostic()
        .addListener(
            new DiagnosticListener() {
              public void onDiagnostic(Diagnostic d) {
                if (d instanceof LoadedFromClassIndex) {
                  queries.notifyClassIndexScan((LoadedFromClassIndex) d);
                }
              }
            });

    final EventRegistry events = EventRegistryFactory.forObjectContainer(container);
    events
        .queryStarted()
        .addListener(
            new EventListener4<QueryEventArgs>() {
              public void onEvent(Event4<QueryEventArgs> e, QueryEventArgs args) {
                queries.notifyQueryStarted();
              }
            });

    events
        .queryFinished()
        .addListener(
            new EventListener4<QueryEventArgs>() {
              public void onEvent(Event4<QueryEventArgs> e, QueryEventArgs args) {
                queries.notifyQueryFinished();
              }
            });
  }
Ejemplo n.º 2
0
  public void testQueryEvents() {

    EventRegistry registry = EventRegistryFactory.forObjectContainer(db());

    EventRecorder recorder = new EventRecorder(fileSession().lock());

    registry.queryStarted().addListener(recorder);
    registry.queryFinished().addListener(recorder);

    Assert.areEqual(0, recorder.size());

    Query q = db().query();
    q.execute();

    Assert.areEqual(2, recorder.size());
    EventRecord e1 = recorder.get(0);
    Assert.areSame(registry.queryStarted(), e1.e);
    Assert.areSame(q, ((QueryEventArgs) e1.args).query());

    EventRecord e2 = recorder.get(1);
    Assert.areSame(registry.queryFinished(), e2.e);
    Assert.areSame(q, ((QueryEventArgs) e2.args).query());

    recorder.clear();

    registry.queryStarted().removeListener(recorder);
    registry.queryFinished().removeListener(recorder);

    db().query().execute();

    Assert.areEqual(0, recorder.size());
  }
Ejemplo n.º 3
0
 private static void referentialIntegrity() {
   ObjectContainer container = Db4oEmbedded.openFile(DATABASE_FILE);
   // #example: Referential integrity
   final EventRegistry events = EventRegistryFactory.forObjectContainer(container);
   events
       .deleting()
       .addListener(
           new EventListener4<CancellableObjectEventArgs>() {
             @Override
             public void onEvent(
                 Event4<CancellableObjectEventArgs> events, CancellableObjectEventArgs eventArgs) {
               final Object toDelete = eventArgs.object();
               if (toDelete instanceof Pilot) {
                 final ObjectContainer container = eventArgs.objectContainer();
                 final ObjectSet<Car> cars =
                     container.query(
                         new Predicate<Car>() {
                           @Override
                           public boolean match(Car car) {
                             return car.getPilot() == toDelete;
                           }
                         });
                 if (cars.size() > 0) {
                   eventArgs.cancel();
                 }
               }
             }
           });
   // #end example
   try {
     Pilot pilot = container.query(Pilot.class).get(0);
     container.delete(pilot);
   } finally {
     container.close();
   }
 }