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.terasology.game.Terasology.java

public static void main(String[] args) {
    try {/*from  w  ww .ja  v  a  2 s .  c o m*/
        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();
        }

        engine = new TerasologyEngine();

        PathManager.getInstance().determineRootPath(true);

        Spout.setEngine(engine);
        Spout.getFilesystem().init();
        Arguments main = new Arguments();
        JCommander commands = new JCommander(main);
        commands.parse(args);
        engine.init(main);
        engine.start();

        //         engine.init();
        //         engine.run(new StateMainMenu());
        //         engine.dispose();
    } catch (Throwable t) {
        Logger.getLogger(Terasology.class.getName()).log(Level.SEVERE, "Uncaught Exception", t);
    }
    System.exit(0);
}

From source file:Main.java

/**
 * Returns the folder that contains a jar that contains the class
 *
 * @param aclass a class to find a jar/* w  w  w.  j a  va 2s .  c o  m*/
 * @return
 */
public static String getJarContainingFolderPath(Class aclass) throws Exception {
    CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();

    File jarFile;

    if (codeSource.getLocation() != null) {
        jarFile = new File(codeSource.getLocation().toURI());
    } else {
        String path = aclass.getResource(aclass.getSimpleName() + ".class").getPath();

        int startIndex = path.indexOf(":") + 1;
        int endIndex = path.indexOf("!");
        if (startIndex == -1 || endIndex == -1) {
            throw new IllegalStateException(
                    "Class " + aclass.getSimpleName() + " is located not within a jar: " + path);
        }
        String jarFilePath = path.substring(startIndex, endIndex);
        jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
        jarFile = new File(jarFilePath);
    }
    return jarFile.getParentFile().getAbsolutePath();
}

From source file:org.apache.commons.net.examples.Main.java

private static boolean fromJar() {
    final CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
    if (codeSource != null) {
        return codeSource.getLocation().getFile().endsWith(".jar");
    }//from  w w  w  . ja  v a 2  s .c  om
    return false; // No idea if this can happen
}

From source file:cognition.pipeline.Main.java

private static String getCurrentFolder() {
    CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
    File jarFile;//from   ww w.ja  v a2s . c om
    try {
        jarFile = new File(codeSource.getLocation().toURI().getPath());
    } catch (URISyntaxException e) {
        e.printStackTrace();
        return "";
    }
    return jarFile.getParentFile().getPath();
}

From source file:org.covito.kit.utility.ClassUtil.java

public static URL getClassLocation(final String clsName) {
    if (clsName == null) {
        return null;
    }// ww w  . j  a  v a2  s . com
    Class cls = null;
    try {
        cls = Class.forName(clsName);
    } catch (Exception e) {
        return null;
    }
    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
        // documentd 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;
}

From source file:com.navercorp.pinpoint.bootstrap.AgentDirBaseClassPathResolverTest.java

private static String getClassLocation(Class<?> clazz) {
    CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
    URL location = codeSource.getLocation();
    logger.debug("codeSource.getLocation:{}", location);
    File file = FileUtils.toFile(location);
    return file.getPath();
}

From source file:Main.java

private static String getJaxpImplementationInfo(String componentName, Class componentClass) {
    CodeSource source = componentClass.getProtectionDomain().getCodeSource();
    return MessageFormat.format("{0} implementation: {1} loaded from: {2}", componentName,
            componentClass.getName(), source == null ? "Java Runtime" : source.getLocation());
}

From source file:com.intuit.autumn.utils.PropertyFactory.java

private static Properties getPropertyFromJar(final Class base, final String property, final CodeSource src) {
    Properties properties = new Properties();
    URL jar = src.getLocation();

    return jar != null ? readZip(base, jar, property, properties) : properties;
}

From source file:org.openmrs.module.rwandamanuals.Manuals.java

/**
 * Lists resources in this modules JAR//ww w  .  j  a v  a 2  s .  co  m
 * @param path the internal path within the JAR
 * @return the 
 * @throws IOException if error occurs
 */
private static List<String> getResourceListing(String path) throws IOException {
    CodeSource src = Manuals.class.getProtectionDomain().getCodeSource();
    List<String> list = new ArrayList<String>();

    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(path) && !ze.isDirectory()) {
                list.add(entryName);
            }
        }
    }
    return list;
}

From source file:org.openadaptor.util.ClasspathUtils.java

/**
 * Utility to find where a class was originally loaded from.
 * This may be handy to debug issues where it seems an incorrect
 * class is being loaded (e.g. where the same class may exist in 
 * multiple jars).//from  www  .ja va  2s.c  o m
 * It returns null if the class was loaded from JRE (I think!)
 * @param cls The class to lookup
 * @return URL where class was loaded from, or null if JRE
 * @since 3.4.5
 */
public static URL getClassOrigin(Class cls) {
    URL result = null;
    ProtectionDomain domain = cls.getProtectionDomain();
    if (cls != null) {
        CodeSource source = domain.getCodeSource();
        if (source != null) {
            result = source.getLocation();
        }
    }
    return result;
}