Example usage for java.security CodeSource getLocation

List of usage examples for java.security CodeSource getLocation

Introduction

In this page you can find the example usage for java.security CodeSource getLocation.

Prototype

public final URL getLocation() 

Source Link

Document

Returns the location associated with this CodeSource.

Usage

From source file:org.apache.catalina.loader.WebappClassLoader.java

/**
 * Get the Permissions for a CodeSource.  If this instance
 * of WebappClassLoader is for a web application context,
 * add read FilePermission or JndiPermissions for the base
 * directory (if unpacked),// w w w.  j  av a  2s.c om
 * the context URL, and jar file resources.
 *
 * @param codeSource where the code was loaded from
 * @return PermissionCollection for CodeSource
 */
protected PermissionCollection getPermissions(CodeSource codeSource) {

    String codeUrl = codeSource.getLocation().toString();
    PermissionCollection pc;
    if ((pc = (PermissionCollection) loaderPC.get(codeUrl)) == null) {
        pc = super.getPermissions(codeSource);
        if (pc != null) {
            Iterator perms = permissionList.iterator();
            while (perms.hasNext()) {
                Permission p = (Permission) perms.next();
                pc.add(p);
            }
            loaderPC.put(codeUrl, pc);
        }
    }
    return (pc);

}

From source file:Creator.WidgetPanel.java

public void loadWidgetCode() {

    boolean ide = true;
    String pathWidgetCode = "textFiles/widgets/";
    ArrayList<String> results = new ArrayList<>();
    String dirCode = new File("").getAbsolutePath() + "/src/Creator/" + pathWidgetCode;

    try {//  www. j  ava 2s.  c  om
        getWidgetFiles(dirCode, results);

        results.stream().forEach((s) -> {
            readCodeFile(s);
        });

        updateWidgetCode();

    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
        ide = false;
    }

    if (!ide) {
        CodeSource src = WidgetPanel.class.getProtectionDomain().getCodeSource();
        try {
            List<String> list = new ArrayList<>();
            if (src != null) {
                URL jar = src.getLocation();
                ZipInputStream zip = new ZipInputStream(jar.openStream());
                ZipEntry ze = null;

                while ((ze = zip.getNextEntry()) != null) {

                    String entryName = ze.getName();
                    if (entryName.startsWith("Creator/textFiles/widgets") && entryName.endsWith(".txt")) {

                        list.add("/" + entryName);
                        //System.out.println("Added name: " + entryName);
                    }
                }
                list.stream().forEach((s) -> {
                    readJarFile(s);
                });

                updateWidgetCode();

            } else {
                System.out.println("Src null");
            }

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    readWidgetVars();
}

From source file:SeedGenerator.MainForm.java

private SearchEngines getSearchEnginesFromXml() {

    try {/*from  w w  w  .j a va2  s . c o  m*/

        CodeSource codeSource = MainForm.class.getProtectionDomain().getCodeSource();
        File jarFile = new File(codeSource.getLocation().toURI().getPath());
        String jarDir = jarFile.getParentFile().getPath();

        File file = new File(jarDir + "/SearchEngines.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(SearchEngines.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        SearchEngines searchEngineList = (SearchEngines) jaxbUnmarshaller.unmarshal(file);
        return searchEngineList;
        //      System.out.println(searchEngineList.getSearchEngines());

    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.openTwoFactor.client.util.TwoFactorClientCommonUtils.java

/**
 * get a jar file from a sample class//  www . j  a  v a 2 s  . c o m
 * @param sampleClass
 * @return the jar file
 */
public static File jarFile(Class sampleClass) {
    try {
        CodeSource codeSource = sampleClass.getProtectionDomain().getCodeSource();
        if (codeSource != null && codeSource.getLocation() != null) {
            String fileName = URLDecoder.decode(codeSource.getLocation().getFile(), "UTF-8");
            return new File(fileName);
        }
        String resourcePath = sampleClass.getName();
        resourcePath = resourcePath.replace('.', '/') + ".class";
        URL url = computeUrl(resourcePath, true);
        String urlPath = url.toString();

        if (urlPath.startsWith("jar:")) {
            urlPath = urlPath.substring(4);
        }
        if (urlPath.startsWith("file:")) {
            urlPath = urlPath.substring(5);
        }
        urlPath = prefixOrSuffix(urlPath, "!", true);

        urlPath = URLDecoder.decode(urlPath, "UTF-8");

        File file = new File(urlPath);
        if (urlPath.endsWith(".jar") && file.exists() && file.isFile()) {
            return file;
        }
    } catch (Exception e) {
        LOG.warn("Cant find jar for class: " + sampleClass + ", " + e.getMessage(), e);
    }
    return null;
}