Exemplo n.º 1
0
  @SuppressWarnings({"rawtypes"})
  private Set<Class> getTableBeans() throws IOException, URISyntaxException {

    ClassLoader loader = getClass().getClassLoader();
    ClassPath cp;
    Set<ClassInfo> rss = null;
    try {
      cp = ClassPath.from(loader);
      rss = cp.getAllClasses();
    } catch (IOException e1) {
      e1.printStackTrace();
    }

    Set<Class> results = Sets.newHashSet();

    for (ClassInfo info : rss) {
      String className = info.getName();
      if (exclude(className)) continue;
      Class clazz;
      try {
        clazz = loader.loadClass(className);
        if (Storable.class.isAssignableFrom(clazz) && clazz != Storable.class) {
          results.add(clazz);
        }
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
    }
    return results;
  }
 private void makeSureIsNamed(ClassInfo ci) {
   logger.info("Checking class " + ci.getName());
   if (!ci.load().isAnnotationPresent(Named.class)) {
     throw new RuntimeException(
         "Configuration error: class "
             + ci.getName()
             + " has no @javax.inject.Named-annotation! "
             + "The contents of this class will not be loaded into the database!");
   }
 }
  /**
   * @param ci
   * @return
   */
  private boolean isSubclassOfAbstractDataContainer(ClassInfo ci) {
    final Class<?> otherClass = ci.load();

    boolean isNotAbstract = !Modifier.isAbstract(otherClass.getModifiers());
    boolean isAssignable = AbstractDataContainer.class.isAssignableFrom(otherClass);

    return isNotAbstract && isAssignable;
  }
  @SuppressWarnings("deprecation")
  @Test
  public void testExceptionsAreGood() throws Exception {
    ImmutableSet<ClassInfo> classes =
        ClassPath.from(Thread.currentThread().getContextClassLoader())
            .getTopLevelClasses(BaseServerResponseException.class.getPackage().getName());
    assertTrue(classes.size() > 5);

    for (ClassInfo classInfo : classes) {
      ourLog.info("Scanning {}", classInfo.getName());

      Class<?> next = Class.forName(classInfo.getName());
      assertNotNull(next);

      if (next == getClass()) {
        continue;
      }
      if (next == BaseServerResponseException.class) {
        continue;
      }
      if (next == UnclassifiedServerFailureException.class) {
        continue;
      }
      if (next == ResourceVersionNotSpecifiedException.class) {
        // This one is deprocated
        continue;
      }

      assertTrue(
          "Type " + next + " is not registered",
          BaseServerResponseException.isExceptionTypeRegistered(next));

      if (next == AuthenticationException.class) {
        continue;
      }

      try {
        next.getConstructor(String.class, IBaseOperationOutcome.class);
      } catch (NoSuchMethodException e) {
        fail(
            classInfo.getName()
                + " has no constructor with params: (String, IBaseOperationOutcome)");
      }
    }
  }
  private static synchronized void initialize() {
    if (whitelistedSerializableClassNames != null) return;

    ClassPath cp;
    try {
      cp = ClassPath.from(RIntermediateAggregationResultUtil.class.getClassLoader());
    } catch (IOException e) {
      throw new RuntimeException("Could not initialize classpath scanning!", e);
    }
    ImmutableSet<ClassInfo> classInfos = cp.getTopLevelClassesRecursive(ROOT_PKG);

    whitelistedSerializableClassNames = new HashSet<>();

    for (ClassInfo classInfo : classInfos) {
      Class<?> clazz = classInfo.load();
      if (clazz.getAnnotation(IntermediateResultSerialization.class) != null) {
        if (!IntermediateResultSerializationResolver.class.isAssignableFrom(clazz)) {
          logger.warn(
              "Class {} has {} annotation, but does not implement {}. Ignoring.",
              clazz.getName(),
              IntermediateResultSerialization.class.getSimpleName(),
              IntermediateResultSerializationResolver.class.getName());
          continue;
        }

        try {
          IntermediateResultSerializationResolver resolver =
              (IntermediateResultSerializationResolver) clazz.newInstance();

          resolver.resolve(
              cls -> {
                whitelistedSerializableClassNames.add(cls.getName());
                logger.debug(
                    "Whitelisted class {} for being de-/serialized for intermediate aggregation results",
                    cls);
              });
        } catch (InstantiationException | IllegalAccessException e) {
          logger.warn("Could not instantiate {}. Ignoring.", clazz.getName(), e);
        }
      }
    }
  }