@Override
 public void initialize(
     String indexName,
     Properties properties,
     Similarity similarity,
     WorkerBuildContext buildContext) {
   super.initialize(indexName, properties, similarity, buildContext);
   ServiceManager serviceManager = buildContext.getServiceManager();
   ComponentRegistryService componentRegistryService =
       serviceManager.requestService(ComponentRegistryService.class);
   ComponentRegistry componentRegistry = componentRegistryService.getComponentRegistry();
   Cache cache = componentRegistry.getComponent(Cache.class);
   cache.addListener(new TopologyChangeListener());
   closeIndexWriter();
 }
  @Override
  public void close() {
    if (stopped.compareAndSet(false, true)) { // make sure we only stop once
      try {
        worker.close();
      } catch (Exception e) {
        log.workerException(e);
      }

      this.allIndexesManager.stop();
      this.timingSource.stop();

      serviceManager.releaseAllServices();

      for (Analyzer an : this.analyzers.values()) {
        an.close();
      }
      for (AbstractDocumentBuilder documentBuilder :
          this.documentBuildersContainedEntities.values()) {
        documentBuilder.close();
      }
      for (EntityIndexBinding entityIndexBinding : this.indexBindingForEntities.values()) {
        entityIndexBinding.getDocumentBuilder().close();
      }

      // unregister statistic mbean
      if (statisticsMBeanName != null) {
        JMXRegistrar.unRegisterMBean(statisticsMBeanName);
      }
    }
  }
  public BridgeFactory(ServiceManager serviceManager) {
    annotationBasedProviders = new HashSet<BridgeProvider>(5);
    annotationBasedProviders.add(new CalendarBridgeProvider());
    annotationBasedProviders.add(new DateBridgeProvider());
    annotationBasedProviders.add(new NumericBridgeProvider());
    annotationBasedProviders.add(new SpatialBridgeProvider());
    annotationBasedProviders.add(new TikaBridgeProvider());

    ClassLoaderService classLoaderService = serviceManager.requestService(ClassLoaderService.class);
    try {
      regularProviders = classLoaderService.loadJavaServices(BridgeProvider.class);
      regularProviders.add(new EnumBridgeProvider());
      regularProviders.add(new BasicJDKTypesBridgeProvider(serviceManager));
    } finally {
      serviceManager.releaseService(ClassLoaderService.class);
    }
  }
    @Override
    protected Set<String> loadInitialShardNames(Properties properties, BuildContext buildContext) {
      ServiceManager serviceManager = buildContext.getServiceManager();
      SessionFactory sessionFactory =
          serviceManager.requestService(HibernateSessionFactoryService.class).getSessionFactory();
      Session session = sessionFactory.openSession();
      try {
        Criteria initialShardsCriteria = session.createCriteria(Animal.class);
        initialShardsCriteria.setProjection(Projections.distinct(Property.forName("type")));

        @SuppressWarnings("unchecked")
        List<String> initialTypes = initialShardsCriteria.list();
        return new HashSet<String>(initialTypes);
      } finally {
        session.close();
      }
    }