Search in sources :

Example 41 with CodeSource

use of java.security.CodeSource in project sic by belluccifranco.

the class Utilidades method getArchivoDelDirectorioDelJAR.

/**
     * Busca un archivo especificado en el directorio donde se encuentra el JAR
     *
     * @param archivo Nombre del archivo que se desea buscar
     * @return archivo encontrado
     * @throws java.io.FileNotFoundException
     * @throws java.net.URISyntaxException
     */
public static File getArchivoDelDirectorioDelJAR(String archivo) throws FileNotFoundException, URISyntaxException {
    File fileBuscado = null;
    CodeSource codeSource = Utilidades.class.getProtectionDomain().getCodeSource();
    File jarFile = new File(codeSource.getLocation().toURI().getPath());
    File jarDir = jarFile.getParentFile();
    if (jarDir != null && jarDir.isDirectory()) {
        File propFile = new File(jarDir, archivo);
        fileBuscado = propFile.getAbsoluteFile();
    }
    return fileBuscado;
}
Also used : CodeSource(java.security.CodeSource) File(java.io.File)

Example 42 with CodeSource

use of java.security.CodeSource in project dubbo by alibaba.

the class Version method getVersion.

public static String getVersion(Class<?> cls, String defaultVersion) {
    try {
        // find version info from MANIFEST.MF first
        String version = cls.getPackage().getImplementationVersion();
        if (version == null || version.length() == 0) {
            version = cls.getPackage().getSpecificationVersion();
        }
        if (version == null || version.length() == 0) {
            // guess version fro jar file name if nothing's found from MANIFEST.MF
            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 default version if no version info is found
        return version == null || version.length() == 0 ? defaultVersion : version;
    } catch (Throwable e) {
        // return default version when any exception is thrown
        logger.error("return default version, ignore exception " + e.getMessage(), e);
        return defaultVersion;
    }
}
Also used : CodeSource(java.security.CodeSource)

Example 43 with CodeSource

use of java.security.CodeSource in project jdk8u_jdk by JetBrains.

the class DGCImplInsulation method main.

public static void main(String[] args) throws Exception {
    TestLibrary.suggestSecurityManager(null);
    Permissions perms = new Permissions();
    perms.add(new SocketPermission("*:1024-", "listen"));
    AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(new CodeSource(null, (Certificate[]) null), perms) });
    Remote impl = new DGCImplInsulation();
    ;
    try {
        Remote stub = (Remote) java.security.AccessController.doPrivileged(new ExportAction(impl));
        System.err.println("exported remote object; local stub: " + stub);
        MarshalledObject mobj = new MarshalledObject(stub);
        stub = (Remote) mobj.get();
        System.err.println("marshalled/unmarshalled stub: " + stub);
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference weakRef = new WeakReference(impl, refQueue);
        impl = null;
        System.gc();
        if (refQueue.remove(TIMEOUT) == weakRef) {
            throw new RuntimeException("TEST FAILED: remote object garbage collected");
        } else {
            System.err.println("TEST PASSED");
            stub = null;
            System.gc();
            Thread.sleep(2000);
            System.gc();
        }
    } finally {
        try {
            UnicastRemoteObject.unexportObject(impl, true);
        } catch (Exception e) {
        }
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ReferenceQueue(java.lang.ref.ReferenceQueue) Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) SocketPermission(java.net.SocketPermission) Remote(java.rmi.Remote) CodeSource(java.security.CodeSource) AccessControlContext(java.security.AccessControlContext) MarshalledObject(java.rmi.MarshalledObject) WeakReference(java.lang.ref.WeakReference) Permissions(java.security.Permissions) Certificate(java.security.cert.Certificate)

Example 44 with CodeSource

use of java.security.CodeSource in project gradle by gradle.

the class DefaultScriptCompilationHandler method compileScript.

private void compileScript(final ScriptSource source, ClassLoader classLoader, CompilerConfiguration configuration, File metadataDir, final CompileOperation<?> extractingTransformer, final Action<? super ClassNode> customVerifier) {
    final Transformer transformer = extractingTransformer != null ? extractingTransformer.getTransformer() : null;
    logger.info("Compiling {} using {}.", source.getDisplayName(), transformer != null ? transformer.getClass().getSimpleName() : "no transformer");
    final EmptyScriptDetector emptyScriptDetector = new EmptyScriptDetector();
    final PackageStatementDetector packageDetector = new PackageStatementDetector();
    GroovyClassLoader groovyClassLoader = new GroovyClassLoader(classLoader, configuration, false) {

        @Override
        protected CompilationUnit createCompilationUnit(CompilerConfiguration compilerConfiguration, CodeSource codeSource) {
            CompilationUnit compilationUnit = new CustomCompilationUnit(source, compilerConfiguration, codeSource, customVerifier, this);
            if (transformer != null) {
                transformer.register(compilationUnit);
            }
            compilationUnit.addPhaseOperation(packageDetector, Phases.CANONICALIZATION);
            compilationUnit.addPhaseOperation(emptyScriptDetector, Phases.CANONICALIZATION);
            return compilationUnit;
        }
    };
    groovyClassLoader.setResourceLoader(NO_OP_GROOVY_RESOURCE_LOADER);
    String scriptText = source.getResource().getText();
    String scriptName = source.getClassName();
    GroovyCodeSource codeSource = new GroovyCodeSource(scriptText == null ? "" : scriptText, scriptName, "/groovy/script");
    try {
        try {
            groovyClassLoader.parseClass(codeSource, false);
        } catch (MultipleCompilationErrorsException e) {
            wrapCompilationFailure(source, e);
        } catch (CompilationFailedException e) {
            throw new GradleException(String.format("Could not compile %s.", source.getDisplayName()), e);
        }
        if (packageDetector.hasPackageStatement) {
            throw new UnsupportedOperationException(String.format("%s should not contain a package statement.", StringUtils.capitalize(source.getDisplayName())));
        }
        serializeMetadata(source, extractingTransformer, metadataDir, emptyScriptDetector.isEmptyScript(), emptyScriptDetector.getHasMethods());
    } finally {
        ClassLoaderUtils.tryClose(groovyClassLoader);
    }
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) Transformer(org.gradle.groovy.scripts.Transformer) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyCodeSource(groovy.lang.GroovyCodeSource) CodeSource(java.security.CodeSource) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyCodeSource(groovy.lang.GroovyCodeSource) GradleException(org.gradle.api.GradleException) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException)

Example 45 with CodeSource

use of java.security.CodeSource in project wcomponents by BorderTech.

the class DefaultInternalConfiguration method getDebuggingInfo.

/**
 * @return debugging information for logging on application start-up.
 */
protected String getDebuggingInfo() {
    final String paramsFile = "log4j.appender.PARAMS.File";
    File cwd = new File(".");
    String workingDir;
    try {
        workingDir = cwd.getCanonicalPath();
    } catch (IOException ex) {
        workingDir = "UNKNOWN";
    }
    String codesourceStr = "";
    // Try to be sneaky and print the codesource location (for orientation of user)
    try {
        ProtectionDomain domain = getClass().getProtectionDomain();
        CodeSource codesource = null;
        if (domain != null) {
            codesource = domain.getCodeSource();
        }
        codesourceStr = (codesource != null ? " code location of ParamImpl: " + codesource.getLocation() : "");
    } catch (Throwable failed) {
    // Okay
    }
    StringBuffer info = new StringBuffer();
    info.append("----Parameters start----");
    info.append(codesourceStr);
    info.append("\nWorking directory is ");
    info.append(workingDir);
    info.append("\nParameters have loaded, there is a full parameter dump in log4j FILE appender at ");
    info.append(get(paramsFile));
    info.append("\nTo dump all params to stdout set ");
    info.append(DUMP);
    info.append(" to true; currently value is ");
    info.append(get(DUMP));
    info.append("\n----Parameters end------");
    return info.toString();
}
Also used : ProtectionDomain(java.security.ProtectionDomain) IOException(java.io.IOException) CodeSource(java.security.CodeSource) File(java.io.File)

Aggregations

CodeSource (java.security.CodeSource)104 URL (java.net.URL)49 ProtectionDomain (java.security.ProtectionDomain)39 File (java.io.File)30 IOException (java.io.IOException)20 Certificate (java.security.cert.Certificate)17 JarFile (java.util.jar.JarFile)13 PermissionCollection (java.security.PermissionCollection)12 URI (java.net.URI)11 URISyntaxException (java.net.URISyntaxException)11 Permissions (java.security.Permissions)11 Policy (java.security.Policy)10 FilePermission (java.io.FilePermission)7 InputStream (java.io.InputStream)6 AccessControlContext (java.security.AccessControlContext)6 MalformedURLException (java.net.MalformedURLException)5 Permission (java.security.Permission)4 JarEntry (java.util.jar.JarEntry)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)3 URLClassLoader (java.net.URLClassLoader)3