Java Class.getProtectionDomain Examples

Java Class.getProtectionDomain - 30 examples found. These are the top rated real world Java examples of Class.getProtectionDomain extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  public static ClassReader readClass(Class<?> theClass)
      throws IOException, BenignClassReadException {
    if (theClass.isArray()) {
      throw new BenignClassReadException(theClass.getName() + " is an array");
    }
    if (Proxy.isProxyClass(theClass)) {
      throw new BenignClassReadException(theClass.getName() + " is a Proxy class");
    }
    if (isRMIStubOrProxy(theClass)) {
      throw new BenignClassReadException(theClass.getName() + " is an RMI Stub or Proxy class");
    }
    if (theClass.getName().startsWith("sun.reflect.")) {
      throw new BenignClassReadException(theClass.getName() + " is a reflection class");
    }
    if (isJAXBClass(theClass)) {
      throw new BenignClassReadException(theClass.getName() + " is a JAXB accessor class");
    }
    if ((theClass.getProtectionDomain().getCodeSource() != null)
        && (theClass.getProtectionDomain().getCodeSource().getLocation() == null)) {
      throw new BenignClassReadException(theClass.getName() + " is a generated class");
    }
    URL resource = getClassResource(theClass.getClassLoader(), Type.getInternalName(theClass));
    if (resource == null) {
      ClassReader reader =
          ServiceFactory.getClassTransformerService()
              .getContextManager()
              .getClassWeaverService()
              .getClassReader(theClass);

      if (reader != null) {
        return reader;
      }
    }
    return getClassReaderFromResource(theClass.getName(), resource);
  }
Example #2
0
  public static LinkedHashSet<String> findJars(LogicalPlan dag, Class<?>[] defaultClasses) {
    List<Class<?>> jarClasses = new ArrayList<Class<?>>();

    for (String className : dag.getClassNames()) {
      try {
        Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        jarClasses.add(clazz);
      } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException("Failed to load class " + className, e);
      }
    }

    for (Class<?> clazz : Lists.newArrayList(jarClasses)) {
      // process class and super classes (super does not require deploy annotation)
      for (Class<?> c = clazz; c != Object.class && c != null; c = c.getSuperclass()) {
        // LOG.debug("checking " + c);
        jarClasses.add(c);
        jarClasses.addAll(Arrays.asList(c.getInterfaces()));
      }
    }

    jarClasses.addAll(Arrays.asList(defaultClasses));

    if (dag.isDebug()) {
      LOG.debug("Deploy dependencies: {}", jarClasses);
    }

    LinkedHashSet<String> localJarFiles = new LinkedHashSet<String>(); // avoid duplicates
    HashMap<String, String> sourceToJar = new HashMap<String, String>();

    for (Class<?> jarClass : jarClasses) {
      if (jarClass.getProtectionDomain().getCodeSource() == null) {
        // system class
        continue;
      }
      String sourceLocation =
          jarClass.getProtectionDomain().getCodeSource().getLocation().toString();
      String jar = sourceToJar.get(sourceLocation);
      if (jar == null) {
        // don't create jar file from folders multiple times
        jar = JarFinder.getJar(jarClass);
        sourceToJar.put(sourceLocation, jar);
        LOG.debug("added sourceLocation {} as {}", sourceLocation, jar);
      }
      if (jar == null) {
        throw new AssertionError("Cannot resolve jar file for " + jarClass);
      }
      localJarFiles.add(jar);
    }

    String libJarsPath = dag.getValue(LogicalPlan.LIBRARY_JARS);
    if (!StringUtils.isEmpty(libJarsPath)) {
      String[] libJars = StringUtils.splitByWholeSeparator(libJarsPath, LIB_JARS_SEP);
      localJarFiles.addAll(Arrays.asList(libJars));
    }

    LOG.info("Local jar file dependencies: " + localJarFiles);

    return localJarFiles;
  }
Example #3
0
 /**
  * From http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a- running-jar-file
  *
  * <p>Used to get all classes in the specified package.
  *
  * @param caller The class that requests
  * @param pckgname The package name.
  * @return The classes contained by the specified package.
  * @throws ClassNotFoundException
  */
 private static Class<?>[] getClasses(Class<?> caller, String pckgname)
     throws ClassNotFoundException {
   String source = caller.getProtectionDomain().getCodeSource().getLocation().getPath();
   if (source.toLowerCase().endsWith(".jar"))
     try {
       return getClassesFromJARFile(
           caller.getProtectionDomain().getCodeSource().getLocation().toURI(), pckgname);
     } catch (URISyntaxException e) {
       throw new AssertionError(e);
     }
   else return getClassesLocal(pckgname);
 }
  public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
    log.debug("calling loadClass boolean with class " + name + ".  I am " + this);

    // reverse the logic. try myself first, then delgate:
    Class c = findLoadedClass(name);
    if (c == null) {
      try {
        c = findClass(name);
        log.debug("Yes!  classloader" + this + " did have " + name);
      } catch (ClassNotFoundException cnfe) {
        log.debug("No!  classloader" + this + " didn't have " + name + " delegating to parent!");
        c = super.loadClass(name, resolve);
      }
    }
    if (resolve) {
      resolveClass(c);
    }
    log.debug(
        "found class "
            + name
            + ".  I am "
            + this
            + " it's classloader is "
            + c.getClassLoader()
            + " it's codesource it "
            + c.getProtectionDomain().getCodeSource());

    return c;
  }
  public ClassMeta getClassInfo(Class<?> cls) {
    final Package pkg = cls.getPackage();

    URL loadedFrom = null;

    try {
      loadedFrom = cls.getProtectionDomain().getCodeSource().getLocation();
    } catch (Throwable t) {
      Log.warn(t, "Failed to get source for %s", cls);
    }

    final API apiAnnotation = pkg.getAnnotation(API.class);
    final ApiInfo apiInfo = apiAnnotation != null ? new ApiInfo(apiAnnotation) : null;

    Map<File, Set<String>> mods = Maps.newHashMap();
    for (ModCandidate candidate : table.getCandidatesFor(pkg.getName())) {
      if (!candidate.getClassList().contains(cls.getName().replace('.', '/'))) continue;

      final File candidateFile = candidate.getModContainer();

      Set<String> modIds = Sets.newHashSet();
      mods.put(candidateFile, modIds);
      for (ModContainer mod : candidate.getContainedMods()) modIds.add(mod.getModId());
    }

    return new ClassMeta(cls, loadedFrom, apiInfo, mods);
  }
Example #6
0
 /**
  * 获取版本号
  *
  * @param clazz
  * @param defaultVersion
  * @return
  */
 public static String getVersion(Class clazz, String defaultVersion) {
   try {
     // 首先查找MANIFEST.MF规范中的版本号
     String version = clazz.getPackage().getImplementationVersion();
     if (version == null || version.length() == 0) {
       version = clazz.getPackage().getSpecificationVersion();
     }
     if (version == null || version.length() == 0) {
       // 如果MANIFEST.MF规范中没有版本号,基于jar包名获取版本号
       String file = clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
       if (file != null && file.length() > 0 && file.endsWith(".jar")) {
         Matcher matcher = VERSION_PATTERN.matcher(file);
         while (matcher.find() && matcher.groupCount() > 0) {
           version = matcher.group(1);
         }
       }
     }
     // 返回版本号,如果为空返回缺省版本号
     return version == null || version.length() == 0 ? defaultVersion : version;
   } catch (Throwable e) { // 防御性容错
     // 忽略异常,返回缺省版本号
     logger.error(e.getMessage(), e);
     return defaultVersion;
   }
 }