Ejemplo n.º 1
0
  /**
   * Loads any custom operators and annotation accessors into the ConstraintFactory.
   *
   * @throws ResourceInstantiationException
   */
  protected void initCustomConstraints() throws ResourceInstantiationException {
    // Load operators
    if (operators != null) {
      for (String opName : operators) {
        Class<? extends ConstraintPredicate> clazz = null;
        try {
          clazz =
              Class.forName(opName, true, Gate.getClassLoader())
                  .asSubclass(ConstraintPredicate.class);
        } catch (ClassNotFoundException e) {
          // if couldn't find it that way, try with current thread class loader
          try {
            clazz =
                Class.forName(opName, true, Thread.currentThread().getContextClassLoader())
                    .asSubclass(ConstraintPredicate.class);
          } catch (ClassNotFoundException e1) {
            throw new ResourceInstantiationException(
                "Cannot load class for operator: " + opName, e1);
          }
        } catch (ClassCastException cce) {
          throw new ResourceInstantiationException(
              "Operator class '" + opName + "' must implement ConstraintPredicate");
        }

        // instantiate an instance of the class so can get the operator string
        try {
          ConstraintPredicate predicate = clazz.newInstance();
          String opSymbol = predicate.getOperator();
          // now store it in ConstraintFactory
          Factory.getConstraintFactory().addOperator(opSymbol, clazz);
        } catch (Exception e) {
          throw new ResourceInstantiationException(
              "Cannot instantiate class for operator: " + opName, e);
        }
      }
    }

    // Load annotationAccessors
    if (annotationAccessors != null) {
      for (String accessorName : annotationAccessors) {
        Class<? extends AnnotationAccessor> clazz = null;
        try {
          clazz =
              Class.forName(accessorName, true, Gate.getClassLoader())
                  .asSubclass(AnnotationAccessor.class);
        } catch (ClassNotFoundException e) {
          // if couldn't find it that way, try with current thread class loader
          try {
            clazz =
                Class.forName(accessorName, true, Thread.currentThread().getContextClassLoader())
                    .asSubclass(AnnotationAccessor.class);
          } catch (ClassNotFoundException e1) {
            throw new ResourceInstantiationException(
                "Cannot load class for accessor: " + accessorName, e1);
          }
        } catch (ClassCastException cce) {
          throw new ResourceInstantiationException(
              "Operator class '" + accessorName + "' must implement AnnotationAccessor");
        }

        // instantiate an instance of the class so can get the meta-property name string
        try {
          AnnotationAccessor aa = clazz.newInstance();
          String accSymbol = (String) aa.getKey();
          // now store it in ConstraintFactory
          Factory.getConstraintFactory().addMetaProperty(accSymbol, clazz);
        } catch (Exception e) {
          throw new ResourceInstantiationException(
              "Cannot instantiate class for accessor: " + accessorName, e);
        }
      }
    }
  }