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();
              }
            });
  }
 @Override
 protected void db4oSetupAfterStore() throws Exception {
   EventRegistryFactory.forObjectContainer(db())
       .updated()
       .addListener(
           new EventListener4<ObjectInfoEventArgs>() {
             @Override
             public void onEvent(Event4<ObjectInfoEventArgs> e, ObjectInfoEventArgs args) {
               if (doThrow) {
                 if (args.info().getObject().getClass().equals(Item.class)) {
                   throw new RuntimeException();
                 }
               }
             }
           });
 }
  /** internal method, public for implementation reasons. */
  public void apply(final InternalObjectContainer objectContainer) {

    if (objectContainer.isClient()) {
      throw new IllegalStateException(
          getClass().getName() + " should be configured on the server.");
    }

    EventRegistryFactory.forObjectContainer(objectContainer)
        .committing()
        .addListener(
            new EventListener4() {

              private FieldMetadata _fieldMetaData;

              private void ensureSingleOccurence(Transaction trans, ObjectInfoCollection col) {
                final Iterator4 i = col.iterator();
                while (i.moveNext()) {
                  final ObjectInfo objectInfo = (ObjectInfo) i.current();

                  if (reflectClass() != reflectorFor(trans, objectInfo.getObject())) continue;

                  final Object obj = objectFor(trans, objectInfo);
                  Object fieldValue = fieldMetadata().getOn(trans, obj);
                  if (fieldValue == null) {
                    continue;
                  }
                  BTreeRange range = fieldMetadata().search(trans, fieldValue);
                  if (range.size() > 1) {
                    throw new UniqueFieldValueConstraintViolationException(
                        classMetadata().getName(), fieldMetadata().getName());
                  }
                }
              }

              private boolean isClassMetadataAvailable() {
                return null != classMetadata();
              }

              private FieldMetadata fieldMetadata() {
                if (_fieldMetaData != null) {
                  return _fieldMetaData;
                }
                _fieldMetaData = classMetadata().fieldMetadataForName(_fieldName);
                return _fieldMetaData;
              }

              private ClassMetadata classMetadata() {
                return objectContainer.classMetadataForReflectClass(reflectClass());
              }

              private ReflectClass reflectClass() {
                return ReflectorUtils.reflectClassFor(objectContainer.reflector(), _clazz);
              }

              public void onEvent(Event4 e, EventArgs args) {
                if (!isClassMetadataAvailable()) {
                  return;
                }
                CommitEventArgs commitEventArgs = (CommitEventArgs) args;
                Transaction trans = (Transaction) commitEventArgs.transaction();
                ensureSingleOccurence(trans, commitEventArgs.added());
                ensureSingleOccurence(trans, commitEventArgs.updated());
              }

              private Object objectFor(Transaction trans, ObjectInfo info) {
                int id = (int) info.getInternalID();
                HardObjectReference ref = HardObjectReference.peekPersisted(trans, id, 1);
                return ref._object;
              }
            });
  }