Example #1
0
 private static PrintWriter getPrintWriter(String saveLocation, String fileName) {
   CodeSource codeSource = GrepolisBot.class.getProtectionDomain().getCodeSource();
   File jarFile = null;
   try {
     jarFile = new File(codeSource.getLocation().toURI().getPath());
   } catch (URISyntaxException e) {
     e.printStackTrace();
   }
   String jarDir;
   if (jarFile != null) {
     jarDir = jarFile.getParentFile().getPath();
     File directory = new File(jarDir + File.separator + saveLocation + File.separator);
     // System.out.println("Directory: " + jarDir + File.separator + saveLocation +
     // File.separator);
     if (!directory.exists()) {
       directory.mkdirs();
     }
     try {
       return new PrintWriter(jarDir + File.separator + saveLocation + File.separator + fileName);
     } catch (FileNotFoundException e) {
       System.out.println("Error saving " + fileName);
       return null;
     }
   }
   return null;
 }
  public static ClassLoader newClassLoader(final Class... userClasses) throws Exception {

    Set<URL> userClassUrls = new HashSet<>();
    for (Class anyUserClass : userClasses) {
      ProtectionDomain protectionDomain = anyUserClass.getProtectionDomain();
      CodeSource codeSource = protectionDomain.getCodeSource();
      URL classLocation = codeSource.getLocation();
      userClassUrls.add(classLocation);
    }
    StringTokenizer tokenString =
        new StringTokenizer(System.getProperty("java.class.path"), File.pathSeparator);
    String pathIgnore = System.getProperty("java.home");
    if (pathIgnore == null) {
      pathIgnore = userClassUrls.iterator().next().toString();
    }

    List<URL> urls = new ArrayList<>();
    while (tokenString.hasMoreElements()) {
      String value = tokenString.nextToken();
      URL itemLocation = new File(value).toURI().toURL();
      if (!userClassUrls.contains(itemLocation)
          && itemLocation.toString().indexOf(pathIgnore) >= 0) {
        urls.add(itemLocation);
      }
    }
    URL[] urlArray = urls.toArray(new URL[urls.size()]);

    ClassLoader masterClassLoader = URLClassLoader.newInstance(urlArray, null);
    ClassLoader appClassLoader =
        URLClassLoader.newInstance(userClassUrls.toArray(new URL[0]), masterClassLoader);
    return appClassLoader;
  }
Example #3
0
  private static List<File> createPossibleHomeDirList() {
    List<File> homeDirCheckList = new ArrayList<File>(4);

    // include codeSource dir in check list
    CodeSource lib = OCSSWRuntimeConfig.class.getProtectionDomain().getCodeSource();
    if (lib != null) {
      URL libUrl = lib.getLocation();
      if (libUrl.getProtocol().equals("file")) {
        String libPath = libUrl.getPath();
        File libParentDir = new File(libPath).getParentFile();
        if (libParentDir != null) {
          // include one above libParentDir
          if (libParentDir.getParentFile() != null) {
            homeDirCheckList.add(libParentDir.getParentFile());
          }
          // include libParentDir
          homeDirCheckList.add(libParentDir);
        }
      }
    }
    // include CWD in check list
    homeDirCheckList.add(new File(".").getAbsoluteFile());
    // include one above CWD in check list
    homeDirCheckList.add(new File("src/test").getAbsoluteFile());
    return homeDirCheckList;
  }
  boolean isSelected(String className, ProtectionDomain protectionDomain) {
    CodeSource codeSource = protectionDomain.getCodeSource();

    if (codeSource == null
        || className.charAt(0) == '['
        || className.startsWith("mockit.")
        || className.startsWith("org.junit.")
        || className.startsWith("junit.")
        || className.startsWith("org.testng.")) {
      return false;
    }

    if (classesToExclude != null && classesToExclude.reset(className).matches()) {
      return false;
    } else if (classesToInclude != null && classesToInclude.reset(className).matches()) {
      return true;
    } else if (testCode != null && testCode.reset(className).matches()) {
      return false;
    }

    String location = codeSource.getLocation().getPath();

    return !location.endsWith(".jar")
        && !location.endsWith("/.cp/")
        && (testCode == null
            || !location.endsWith("/test-classes/")
                && !location.endsWith("/jmockit/main/classes/"));
  }
Example #5
0
  public static String getSourceLocation(final Class<?> cls) {

    String exMsg = null;
    java.security.CodeSource codeSource = null;

    try {
      codeSource = cls.getProtectionDomain().getCodeSource();

    } catch (final Exception e) {
      exMsg = e.toString();
    }

    final URL classURL = codeSource == null ? getSourceURL(cls) : codeSource.getLocation();

    if (classURL == null) {
      return cls.getName() + ": (missing codeSource and classLoader)";
    }

    final String path = classURL.getPath();
    final File file = new File(path);

    if (!file.isFile()) {
      return cls.getName() + ": " + path + " (not a file)" + (exMsg == null ? "" : "; " + exMsg);
    }

    return String.format(
        "%s (SN: %s): %s (MD5: %s)%s",
        cls.getName(),
        getSerialVersionUID(cls),
        path,
        MD5Kit.md5sum(file),
        exMsg == null ? "" : "; " + exMsg);
  }
Example #6
0
  @Override
  public void init(SpoutApplication args) {
    boolean inJar = false;

    try {
      CodeSource cs = SpoutClient.class.getProtectionDomain().getCodeSource();
      inJar = cs.getLocation().toURI().getPath().endsWith(".jar");
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }

    if (inJar) {
      unpackLwjgl();
    }
    ExecutorService executorBoss =
        Executors.newCachedThreadPool(new NamedThreadFactory("SpoutServer - Boss", true));
    ExecutorService executorWorker =
        Executors.newCachedThreadPool(new NamedThreadFactory("SpoutServer - Worker", true));
    ChannelFactory factory = new NioClientSocketChannelFactory(executorBoss, executorWorker);
    bootstrap.setFactory(factory);

    ChannelPipelineFactory pipelineFactory = new CommonPipelineFactory(this, true);
    bootstrap.setPipelineFactory(pipelineFactory);
    super.init(args);

    getScheduler().startRenderThread();
  }
Example #7
0
  public MoquiStart(ClassLoader parent, boolean loadWebInf) {
    super(parent);
    this.loadWebInf = loadWebInf;

    URL wrapperWarUrl = null;
    try {
      // get outer file (the war file)
      pd = getClass().getProtectionDomain();
      CodeSource cs = pd.getCodeSource();
      wrapperWarUrl = cs.getLocation();
      outerFile = new JarFile(new File(wrapperWarUrl.toURI()));

      // allow for classes in the outerFile as well
      jarFileList.add(outerFile);

      Enumeration<JarEntry> jarEntries = outerFile.entries();
      while (jarEntries.hasMoreElements()) {
        JarEntry je = jarEntries.nextElement();
        if (je.isDirectory()) continue;
        // if we aren't loading the WEB-INF files and it is one, skip it
        if (!loadWebInf && je.getName().startsWith("WEB-INF")) continue;
        // get jars, can be anywhere in the file
        String jeName = je.getName().toLowerCase();
        if (jeName.lastIndexOf(".jar") == jeName.length() - 4) {
          File file = createTempFile(je);
          jarFileList.add(new JarFile(file));
        }
      }
    } catch (Exception e) {
      System.out.println("Error loading jars in war file [" + wrapperWarUrl + "]: " + e.toString());
    }
  }
Example #8
0
 /**
  * 获取类的class文件位置的URL
  *
  * @param cls
  * @return
  */
 private static URL getClassLocationURL(final Class cls) {
   if (cls == null) throw new IllegalArgumentException("null input: cls");
   URL result = null;
   final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
   final ProtectionDomain pd = cls.getProtectionDomain();
   if (pd != null) {
     final CodeSource cs = pd.getCodeSource();
     if (cs != null) result = cs.getLocation();
     if (result != null) {
       if ("file".equals(result.getProtocol())) {
         try {
           if (result.toExternalForm().endsWith(".jar")
               || result.toExternalForm().endsWith(".zip"))
             result =
                 new URL(
                     "jar:".concat(result.toExternalForm()).concat("!/").concat(clsAsResource));
           else if (new File(result.getFile()).isDirectory())
             result = new URL(result, clsAsResource);
         } catch (MalformedURLException ignore) {
         }
       }
     }
   }
   if (result == null) {
     final ClassLoader clsLoader = cls.getClassLoader();
     result =
         clsLoader != null
             ? clsLoader.getResource(clsAsResource)
             : ClassLoader.getSystemResource(clsAsResource);
   }
   return result;
 }
 private String getPathOfJar() {
   String jarDir = null;
   try {
     CodeSource codeSource = ClientController.class.getProtectionDomain().getCodeSource();
     File jarFile = new File(codeSource.getLocation().toURI().getPath());
     jarDir = jarFile.getParentFile().getPath();
   } catch (URISyntaxException e1) {
     e1.printStackTrace();
   }
   return jarDir;
 }
 public void addClassDependency(Class<?> clazz) {
   CodeSource source = clazz.getProtectionDomain().getCodeSource();
   if (source == null) return;
   Path absolutePath = null;
   try {
     absolutePath = Paths.get(source.getLocation().toURI()).toAbsolutePath();
   } catch (URISyntaxException e) {
     e.printStackTrace();
   }
   globalDependencies.add(absolutePath);
 }
 /**
  * Retrieve the FML signing certificates, if any. Validate these against the published FML
  * certificates in your mod, if you wish.
  *
  * <p>Deprecated because mods should <b>NOT</b> trust this code. Rather they should copy this, or
  * something like this, into their own mods.
  *
  * @return Certificates used to sign FML and Forge
  */
 @Deprecated
 public Certificate[] getFMLSigningCertificates() {
   CodeSource codeSource =
       getClass().getClassLoader().getParent().getClass().getProtectionDomain().getCodeSource();
   Certificate[] certs = codeSource.getCertificates();
   if (certs == null) {
     return new Certificate[0];
   } else {
     return certs;
   }
 }
Example #12
0
 /**
  * Allow extended classes to add permissions before the permissions collection is set to
  * read-only.
  *
  * @param codeSource
  * @param perms
  */
 protected void addPermissions(CodeSource codeSource, PermissionCollection perms) {
   for (Map.Entry<CodeSource, PermissionCollection> e : codeSource2Permissions.entrySet()) {
     final CodeSource cs = e.getKey();
     if (cs.implies(codeSource)) {
       // BootLogInstance.get().info(cs + " -> " + codeSource);
       final PermissionCollection pc = e.getValue();
       for (Enumeration<?> ee = pc.elements(); ee.hasMoreElements(); ) {
         perms.add((Permission) ee.nextElement());
       }
     }
   }
 }
Example #13
0
  /**
   * Returns JAR archive structure.
   *
   * @param jarClass any class within the JAR
   * @param allowedExtensions list of extension filters
   * @param allowedPackgages list of allowed packages
   * @param listener jar download listener
   * @return JAR archive structure
   */
  public static JarStructure getJarStructure(
      final Class jarClass,
      final List<String> allowedExtensions,
      final List<String> allowedPackgages,
      final FileDownloadListener listener) {
    try {
      final CodeSource src = jarClass.getProtectionDomain().getCodeSource();
      if (src != null) {
        // Creating structure

        // Source url
        final URL jarUrl = src.getLocation();
        final URI uri = jarUrl.toURI();

        // Source file
        final File jarFile;
        final String scheme = uri.getScheme();
        if (scheme != null && scheme.equalsIgnoreCase("file")) {
          // Local jar-file
          jarFile = new File(uri);
        } else {
          // Remote jar-file
          jarFile =
              FileUtils.downloadFile(
                  jarUrl.toString(), File.createTempFile("jar_file", ".tmp"), listener);
        }

        // Creating
        final JarEntry jarEntry = new JarEntry(JarEntryType.jarEntry, jarFile.getName());
        final JarStructure jarStructure = new JarStructure(jarEntry);
        jarStructure.setJarLocation(jarFile.getAbsolutePath());

        // Reading all entries and parsing them into structure
        final ZipInputStream zip = new ZipInputStream(jarUrl.openStream());
        ZipEntry zipEntry;
        while ((zipEntry = zip.getNextEntry()) != null) {
          final String entryName = zipEntry.getName();
          if (isAllowedPackage(entryName, allowedPackgages)
              && (zipEntry.isDirectory() || isAllowedExtension(entryName, allowedExtensions))) {
            parseElement(jarEntry, entryName, zipEntry);
          }
        }
        zip.close();

        return jarStructure;
      }
    } catch (final IOException e) {
      FlatLafLogger.error(ReflectUtils.class, e);
    } catch (final URISyntaxException e) {
      FlatLafLogger.error(ReflectUtils.class, e);
    }
    return null;
  }
  private static URI getJarURI() throws URISyntaxException {
    final ProtectionDomain domain;
    final CodeSource source;
    final URL url;
    final URI uri;

    domain = ExtractExeToLocalMachine.class.getProtectionDomain();
    source = domain.getCodeSource();
    url = source.getLocation();
    uri = url.toURI();
    return (uri);
  }
 String getJarFromClass(Class<?> clz) {
   CodeSource source = clz.getProtectionDomain().getCodeSource();
   if (null == source) {
     throw new RuntimeException("Could not get CodeSource for class");
   }
   URL jarUrl = source.getLocation();
   String jar = jarUrl.getPath();
   if (!jar.endsWith(".jar")) {
     throw new RuntimeException("Need to have a jar to run mapreduce: " + jar);
   }
   return jar;
 }
Example #16
0
 static {
   CodeSource codeSrc = SikuliX.class.getProtectionDomain().getCodeSource();
   if (codeSrc != null && codeSrc.getLocation() != null) {
     URL jarURL = codeSrc.getLocation();
     jarPath = FileManager.slashify(new File(jarURL.getPath()).getAbsolutePath(), false);
     jarParentPath = (new File(jarPath)).getParent();
     if (jarPath.endsWith(".jar")) {
       runningFromJar = true;
     } else {
       jarPath += "/";
     }
   }
 }
  /*--------------------------------------------------------------------------*/
  private String getNativePath(String name, NativeLibraryClient client) {
    ProtectionDomain domain = client.getClass().getProtectionDomain();
    CodeSource codeSource = domain.getCodeSource();
    URL url = codeSource.getLocation();
    String path = url.getPath();
    path = path + nativeDirectory + '/' + name + extension;
    path = path.replace('/', File.separatorChar);
    // Revise the URI-path to a file path; needed in uninstaller because it
    // writes the jar contents into a sandbox; may be with blanks in the
    // path.
    path = revisePath(path);

    return (path);
  }
 public static String getVersion(Class<?> cls, String defaultVersion) {
   try {
     // 首先查找MANIFEST.MF规范中的版本号
     String version = cls.getPackage().getImplementationVersion();
     if (version == null || version.length() == 0) {
       version = cls.getPackage().getSpecificationVersion();
     }
     if (version == null || version.length() == 0) {
       // 如果规范中没有版本号,基于jar包名获取版本号
       CodeSource codeSource = cls.getProtectionDomain().getCodeSource();
       if (codeSource == null) {
         LOGGER.info(
             "No codeSource for class "
                 + cls.getName()
                 + " when getVersion, use default version "
                 + defaultVersion);
       } else {
         String file = codeSource.getLocation().getFile();
         if (file != null && file.length() > 0 && file.endsWith(".jar")) {
           file = file.substring(0, file.length() - 4);
           int i = file.lastIndexOf('/');
           if (i >= 0) {
             file = file.substring(i + 1);
           }
           i = file.indexOf("-");
           if (i >= 0) {
             file = file.substring(i + 1);
           }
           while (file.length() > 0 && !Character.isDigit(file.charAt(0))) {
             i = file.indexOf("-");
             if (i >= 0) {
               file = file.substring(i + 1);
             } else {
               break;
             }
           }
           version = file;
         }
       }
     }
     // 返回版本号,如果为空返回缺省版本号
     return version == null || version.length() == 0 ? defaultVersion : version;
   } catch (Throwable e) { // 防御性容错
     // 忽略异常,返回缺省版本号
     LOGGER.error("return default version, ignore exception " + e.getMessage(), e);
     return defaultVersion;
   }
 }
Example #19
0
  /** 获取类的class文件位置的URL。这个方法是本类最基础的方法,供其它方法调用。 */
  private static URL getClassLocationURL(final Class cls) {
    if (cls == null) throw new IllegalArgumentException("null input: cls");
    URL result = null;
    final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
    final ProtectionDomain pd = cls.getProtectionDomain();
    // java.lang.Class contract does not specify
    // if 'pd' can ever be null;
    // it is not the case for Sun's implementations,
    // but guard against null
    // just in case:
    if (pd != null) {
      final CodeSource cs = pd.getCodeSource();
      // 'cs' can be null depending on
      // the classloader behavior:
      if (cs != null) result = cs.getLocation();

      if (result != null) {
        // Convert a code source location into
        // a full class file location
        // for some common cases:
        if ("file".equals(result.getProtocol())) {
          try {
            if (result.toExternalForm().endsWith(".jar")
                || result.toExternalForm().endsWith(".zip"))
              result =
                  new URL(
                      "jar:".concat(result.toExternalForm()).concat("!/").concat(clsAsResource));
            else if (new File(result.getFile()).isDirectory())
              result = new URL(result, clsAsResource);
          } catch (MalformedURLException ignore) {
          }
        }
      }
    }

    if (result == null) {
      // Try to find 'cls' definition as a resource;
      // this is not
      // document.d to be legal, but Sun's
      // implementations seem to //allow this:
      final ClassLoader clsLoader = cls.getClassLoader();
      result =
          clsLoader != null
              ? clsLoader.getResource(clsAsResource)
              : ClassLoader.getSystemResource(clsAsResource);
    }
    return result;
  }
Example #20
0
  public StoredDataManager() {
    // для получения пути к исполняемому файлу
    src = this.getClass().getProtectionDomain().getCodeSource();
    if (src != null) {
      try {
        url = new URL(src.getLocation(), PropFileName);
      } catch (MalformedURLException e) {
        // С таким способом получения URL ошибка не должна возникать.
        e.printStackTrace();
      }
      // URL файла
      String path = url.getPath();
      try {
        // декодирование URL для устранения проблемы с пробелами
        decodedPath = URLDecoder.decode(path, "UTF-8");
        // окончательное получение файла настроек
        propFile = new File(decodedPath);
      } catch (UnsupportedEncodingException e) {
        // Не вижу причин возникновения ошибки тут.
        e.printStackTrace();
      }
    }

    // загрузка сохраненных настроек (если возможно)
    if (propFile.canRead()) {
      try {
        props.load(new FileInputStream(propFile));
        loaded = true;
      } catch (IOException e) {
        // Тут ничего не надо делать. Нет файла настроек - и ладно,
        //   будут значения по умолчанию.
        e.printStackTrace();
      }
    }
  }
Example #21
0
 /**
  * Loads protection domain for specified jar. The domain is not loaded from the jar itself but it
  * is derived from parent protection domain.
  *
  * @param aURL jar url
  * @return
  */
 protected ProtectionDomain getProtectionDomain(URL aURL) {
   ProtectionDomain parentDomain = getClass().getProtectionDomain();
   CodeSource csParent = parentDomain.getCodeSource();
   Certificate[] certParent = csParent.getCertificates();
   CodeSource csChild =
       (certParent == null
           ? new CodeSource(aURL, csParent.getCodeSigners())
           : new CodeSource(aURL, certParent));
   ProtectionDomain pdChild =
       new ProtectionDomain(
           csChild,
           parentDomain.getPermissions(),
           parentDomain.getClassLoader(),
           parentDomain.getPrincipals());
   return pdChild;
 }
Example #22
0
 /**
  * Returns JAR location File for the specified class.
  *
  * @param jarClass any class from that JAR
  * @return JAR location File
  */
 public static File getJarLocationFile(final Class jarClass) {
   try {
     final CodeSource src = jarClass.getProtectionDomain().getCodeSource();
     if (src != null) {
       final URL jarUrl = src.getLocation();
       final URI uri = jarUrl.toURI();
       final String scheme = uri.getScheme();
       if (scheme != null && scheme.equalsIgnoreCase("file")) {
         return new File(uri);
       }
     }
   } catch (final URISyntaxException e) {
     FlatLafLogger.error(ReflectUtils.class, e);
   }
   return null;
 }
  /*
   * Match CodeSource to a CodeSigner[] in the signer cache.
   */
  private CodeSigner[] findMatchingSigners(CodeSource cs) {
    if (cs instanceof VerifierCodeSource) {
      VerifierCodeSource vcs = (VerifierCodeSource) cs;
      if (vcs.isSameDomain(csdomain)) {
        return ((VerifierCodeSource) cs).getPrivateSigners();
      }
    }

    /*
     * In practice signers should always be optimized above
     * but this handles a CodeSource of any type, just in case.
     */
    CodeSource[] sources = mapSignersToCodeSources(cs.getLocation(), getJarCodeSigners(), true);
    List sourceList = new ArrayList();
    for (int i = 0; i < sources.length; i++) {
      sourceList.add(sources[i]);
    }
    int j = sourceList.indexOf(cs);
    if (j != -1) {
      CodeSigner[] match;
      match = ((VerifierCodeSource) sourceList.get(j)).getPrivateSigners();
      if (match == null) {
        match = emptySigner;
      }
      return match;
    }
    return null;
  }
  public ChunkerFeatureExtractor() throws ClassNotFoundException, IOException {

    @SuppressWarnings("unchecked") // req for deserialize
    CodeSource src = this.getClass().getProtectionDomain().getCodeSource();
    String loc = src.getLocation().toString();
    File hmmFile =
        new File(
            loc.substring(5, loc.length() - 10)
                + "/objects/pos-en-general-brown.HiddenMarkovModel");
    // File hmmFile = new
    // File("C:/Users/D059348/dev/HU/MaschinelleSprachverarbeitung/objects/pos-en-general-brown.HiddenMarkovModel");
    HiddenMarkovModel posHmm = (HiddenMarkovModel) AbstractExternalizable.readObject(hmmFile);

    FastCache<String, double[]> emissionCache = new FastCache<String, double[]>(100000);
    mPosTagger = new HmmDecoder(posHmm, null, emissionCache);
  }
  private static List<TFM_CommandInfo> getCommands() {
    List<TFM_CommandInfo> commandList = new ArrayList<TFM_CommandInfo>();

    try {
      CodeSource codeSource = TotalFreedomMod.class.getProtectionDomain().getCodeSource();
      if (codeSource != null) {
        ZipInputStream zip = new ZipInputStream(codeSource.getLocation().openStream());
        ZipEntry zipEntry;
        while ((zipEntry = zip.getNextEntry()) != null) {
          String entryName = zipEntry.getName();
          Matcher matcher = COMMAND_PATTERN.matcher(entryName);
          if (matcher.find()) {
            try {
              Class<?> commandClass =
                  Class.forName(TFM_CommandHandler.COMMAND_PATH + "." + matcher.group(1));

              CommandPermissions commandPermissions =
                  commandClass.getAnnotation(CommandPermissions.class);
              CommandParameters commandParameters =
                  commandClass.getAnnotation(CommandParameters.class);

              if (commandPermissions != null && commandParameters != null) {
                TFM_CommandInfo commandInfo =
                    new TFM_CommandInfo(
                        commandClass,
                        matcher.group(1).split("_")[1],
                        commandPermissions.level(),
                        commandPermissions.source(),
                        commandPermissions.blockHostConsole(),
                        commandParameters.description(),
                        commandParameters.usage(),
                        commandParameters.aliases());

                commandList.add(commandInfo);
              }
            } catch (ClassNotFoundException ex) {
              TFM_Log.severe(ex);
            }
          }
        }
      }
    } catch (IOException ex) {
      TFM_Log.severe(ex);
    }

    return commandList;
  }
Example #26
0
 public PermissionCollection getPermissions(CodeSource codeSource) {
   Permissions perms = new Permissions();
   for (Iterator it = cs2pc.entrySet().iterator(); it.hasNext(); ) {
     Map.Entry e = (Map.Entry) it.next();
     CodeSource cs = (CodeSource) e.getKey();
     if (cs.implies(codeSource)) {
       logger.log(Component.POLICY, "{0} -> {1}", new Object[] {cs, codeSource});
       PermissionCollection pc = (PermissionCollection) e.getValue();
       for (Enumeration ee = pc.elements(); ee.hasMoreElements(); ) {
         perms.add((Permission) ee.nextElement());
       }
     } else logger.log(Component.POLICY, "{0} !-> {1}", new Object[] {cs, codeSource});
   }
   logger.log(
       Component.POLICY, "returning permissions {0} for {1}", new Object[] {perms, codeSource});
   return perms;
 }
  /* this code is workaround for subtle bug/feature in JDK1.3.1 and 1.4,
  related to loading applets behind proxy */
  protected PermissionCollection getPermissions(CodeSource codesource) {
    PermissionCollection sysPerms = null;
    Policy policy =
        (Policy)
            AccessController.doPrivileged(
                new PrivilegedAction() {
                  public Object run() {
                    return Policy.getPolicy();
                  }
                });
    if (policy != null) sysPerms = policy.getPermissions(new CodeSource(null, null));
    else sysPerms = new Permissions();

    final PermissionCollection perms = sysPerms;
    if (base != null && base.getHost() != null)
      perms.add(new SocketPermission(base.getHost() + ":1-", "accept,connect,resolve"));
    URL url = codesource.getLocation();

    if (url.getProtocol().equals("file")) {

      String path = url.getFile().replace('/', File.separatorChar);

      if (!path.endsWith(File.separator)) {
        int endIndex = path.lastIndexOf(File.separatorChar);
        if (endIndex != -1) {
          path = path.substring(0, endIndex + 1) + "-";
          perms.add(new FilePermission(path, "read"));
        }
      }
      perms.add(new SocketPermission("localhost", "connect,accept"));
      AccessController.doPrivileged(
          new PrivilegedAction() {
            public Object run() {
              try {
                String host = InetAddress.getLocalHost().getHostName();
                perms.add(new SocketPermission(host, "connect,accept"));
              } catch (UnknownHostException uhe) {
              }
              return null;
            }
          });

      if (base.getProtocol().equals("file")) {
        String bpath = base.getFile().replace('/', File.separatorChar);
        if (bpath.endsWith(File.separator)) {
          bpath += "-";
        }
        perms.add(new FilePermission(bpath, "read"));
      }
    }
    // for (Enumeration e=perms.elements();e.hasMoreElements();)
    // System.err.println("p="+e.nextElement());
    return perms;
  }
 private void findAgent() {
   try {
     if (this.agent == null || this.agent.length == 0) {
       Class<?> loaded = Class.forName(SPRING_LOADED_AGENT_CLASSNAME);
       if (loaded != null) {
         if (this.noverify == null) {
           this.noverify = true;
         }
         CodeSource source = loaded.getProtectionDomain().getCodeSource();
         if (source != null) {
           this.agent = new File[] {new File(source.getLocation().getFile())};
         }
       }
     }
   } catch (ClassNotFoundException ex) {
     // ignore;
   }
   if (this.noverify == null) {
     this.noverify = false;
   }
 }
Example #29
0
 private URL getClassLocation(final Class<XmlTransformServer> classToFind) {
   URL result = null;
   if (classToFind == null) {
     throw new IllegalArgumentException("Class is null");
   }
   final String classAsResource = classToFind.getName().replace('.', '/').concat(".class");
   final ProtectionDomain pd = classToFind.getProtectionDomain();
   if (pd != null) {
     final CodeSource cs = pd.getCodeSource();
     if (cs != null) {
       result = cs.getLocation();
     }
     if (result != null) {
       // Convert a code source location into a full class file location
       if (result.getProtocol().equals("file")) {
         try {
           if (result.toExternalForm().endsWith(".jar")
               || result.toExternalForm().endsWith(".zip")) {
             result =
                 new URL(
                     "jar:".concat(result.toExternalForm()).concat("!/").concat(classAsResource));
           } else if (new File(result.getFile()).isDirectory()) {
             result = new URL(result, classAsResource);
           }
         } catch (MalformedURLException ignore) {
           // do nothing
         }
       }
     }
   }
   if (result == null) {
     // Try to find class definition as a resource
     final ClassLoader classLoader = classToFind.getClassLoader();
     result =
         classLoader != null
             ? classLoader.getResource(classAsResource)
             : ClassLoader.getSystemResource(classAsResource);
   }
   return result;
 }
  public void addJarDependency(BOperatorInvocation op, Class<?> clazz) {

    CodeSource thisCodeSource = this.getClass().getProtectionDomain().getCodeSource();

    CodeSource source = clazz.getProtectionDomain().getCodeSource();
    if (null == source || thisCodeSource.equals(source)) {
      return;
    }

    Path absolutePath = null;
    try {
      absolutePath = Paths.get(source.getLocation().toURI()).toAbsolutePath();
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }

    if (operatorToJarDependencies.containsKey(op)) {
      operatorToJarDependencies.get(op).add(absolutePath);
    } else {
      operatorToJarDependencies.put(op, new HashSet<Path>());
      operatorToJarDependencies.get(op).add(absolutePath);
    }
  }