Пример #1
0
  private void addFilterDef(FullTextFilterDef defAnn) {
    FilterDef filterDef = new FilterDef(defAnn);
    if (filterDef.getImpl().equals(ShardSensitiveOnlyFilter.class)) {
      // this is a placeholder don't process regularly
      filterDefs.put(defAnn.name(), filterDef);
      return;
    }
    try {
      filterDef.getImpl().newInstance();
    } catch (IllegalAccessException e) {
      throw new SearchException(
          "Unable to create Filter class: " + filterDef.getImpl().getName(), e);
    } catch (InstantiationException e) {
      throw new SearchException(
          "Unable to create Filter class: " + filterDef.getImpl().getName(), e);
    }
    for (Method method : filterDef.getImpl().getMethods()) {
      if (method.isAnnotationPresent(Factory.class)) {
        if (filterDef.getFactoryMethod() != null) {
          throw new SearchException(
              "Multiple @Factory methods found"
                  + defAnn.name()
                  + ": "
                  + filterDef.getImpl().getName()
                  + "."
                  + method.getName());
        }
        ReflectionHelper.setAccessible(method);
        filterDef.setFactoryMethod(method);
      }
      if (method.isAnnotationPresent(Key.class)) {
        if (filterDef.getKeyMethod() != null) {
          throw new SearchException(
              "Multiple @Key methods found"
                  + defAnn.name()
                  + ": "
                  + filterDef.getImpl().getName()
                  + "."
                  + method.getName());
        }
        ReflectionHelper.setAccessible(method);
        filterDef.setKeyMethod(method);
      }

      String name = method.getName();
      if (name.startsWith("set") && method.getParameterTypes().length == 1) {
        filterDef.addSetter(Introspector.decapitalize(name.substring(3)), method);
      }
    }
    filterDefs.put(defAnn.name(), filterDef);
  }
  /**
   * If we have a work instance we have to check whether the instance to be indexed is contained in
   * any other indexed entities for a tenant.
   *
   * @param instance the instance to be indexed
   * @param workPlan the current work plan
   * @param currentRecursionContext the current {@link
   *     org.hibernate.search.engine.spi.ContainedInRecursionContext} object used to check the graph
   *     traversal
   * @param tenantIdentifier the identifier of the tenant or null, if there isn't one
   * @see #appendContainedInWorkForInstance(Object, WorkPlan, ContainedInRecursionContext)
   */
  public void appendContainedInWorkForInstance(
      Object instance,
      WorkPlan workPlan,
      ContainedInRecursionContext currentRecursionContext,
      String tenantIdentifier) {
    for (ContainedInMetadata containedInMetadata : typeMetadata.getContainedInMetadata()) {
      XMember member = containedInMetadata.getContainedInMember();
      Object unproxiedInstance = instanceInitializer.unproxy(instance);

      ContainedInRecursionContext recursionContext =
          updateContainedInRecursionContext(
              unproxiedInstance, containedInMetadata, currentRecursionContext);

      if (recursionContext.isTerminal()) {
        continue;
      }

      Object value = ReflectionHelper.getMemberValue(unproxiedInstance, member);

      if (value == null) {
        continue;
      }

      if (member.isArray()) {
        Object[] array = (Object[]) value;
        for (Object arrayValue : array) {
          processSingleContainedInInstance(
              workPlan, arrayValue, recursionContext, tenantIdentifier);
        }
      } else if (member.isCollection()) {
        Collection<?> collection = null;
        try {
          collection = getActualCollection(member, value);
          collection.size(); // load it
        } catch (Exception e) {
          if (e.getClass().getName().contains("org.hibernate.LazyInitializationException")) {
            /* A deleted entity not having its collection initialized
             * leads to a LIE because the collection is no longer attached to the session
             *
             * But that's ok as the collection update event has been processed before
             * or the fk would have been cleared and thus triggering the cleaning
             */
            collection = null;
          }
        }
        if (collection != null) {
          for (Object collectionValue : collection) {
            processSingleContainedInInstance(
                workPlan, collectionValue, recursionContext, tenantIdentifier);
          }
        }
      } else {
        processSingleContainedInInstance(workPlan, value, recursionContext, tenantIdentifier);
      }
    }
  }