Search in sources :

Example 16 with JarFile

use of java.util.jar.JarFile in project android_frameworks_base by ParanoidAndroid.

the class PackageParser method collectCertificates.

public boolean collectCertificates(Package pkg, int flags) {
    pkg.mSignatures = null;
    WeakReference<byte[]> readBufferRef;
    byte[] readBuffer = null;
    synchronized (mSync) {
        readBufferRef = mReadBuffer;
        if (readBufferRef != null) {
            mReadBuffer = null;
            readBuffer = readBufferRef.get();
        }
        if (readBuffer == null) {
            readBuffer = new byte[8192];
            readBufferRef = new WeakReference<byte[]>(readBuffer);
        }
    }
    try {
        JarFile jarFile = new JarFile(mArchiveSourcePath);
        Certificate[] certs = null;
        if ((flags & PARSE_IS_SYSTEM) != 0) {
            // If this package comes from the system image, then we
            // can trust it...  we'll just use the AndroidManifest.xml
            // to retrieve its signatures, not validating all of the
            // files.
            JarEntry jarEntry = jarFile.getJarEntry(ANDROID_MANIFEST_FILENAME);
            certs = loadCertificates(jarFile, jarEntry, readBuffer);
            if (certs == null) {
                Slog.e(TAG, "Package " + pkg.packageName + " has no certificates at entry " + jarEntry.getName() + "; ignoring!");
                jarFile.close();
                mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
                return false;
            }
            if (DEBUG_JAR) {
                Slog.i(TAG, "File " + mArchiveSourcePath + ": entry=" + jarEntry + " certs=" + (certs != null ? certs.length : 0));
                if (certs != null) {
                    final int N = certs.length;
                    for (int i = 0; i < N; i++) {
                        Slog.i(TAG, "  Public key: " + certs[i].getPublicKey().getEncoded() + " " + certs[i].getPublicKey());
                    }
                }
            }
        } else {
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                final JarEntry je = entries.nextElement();
                if (je.isDirectory())
                    continue;
                final String name = je.getName();
                if (name.startsWith("META-INF/"))
                    continue;
                if (ANDROID_MANIFEST_FILENAME.equals(name)) {
                    pkg.manifestDigest = ManifestDigest.fromInputStream(jarFile.getInputStream(je));
                }
                final Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
                if (DEBUG_JAR) {
                    Slog.i(TAG, "File " + mArchiveSourcePath + " entry " + je.getName() + ": certs=" + certs + " (" + (certs != null ? certs.length : 0) + ")");
                }
                if (localCerts == null) {
                    Slog.e(TAG, "Package " + pkg.packageName + " has no certificates at entry " + je.getName() + "; ignoring!");
                    jarFile.close();
                    mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
                    return false;
                } else if (certs == null) {
                    certs = localCerts;
                } else {
                    // Ensure all certificates match.
                    for (int i = 0; i < certs.length; i++) {
                        boolean found = false;
                        for (int j = 0; j < localCerts.length; j++) {
                            if (certs[i] != null && certs[i].equals(localCerts[j])) {
                                found = true;
                                break;
                            }
                        }
                        if (!found || certs.length != localCerts.length) {
                            Slog.e(TAG, "Package " + pkg.packageName + " has mismatched certificates at entry " + je.getName() + "; ignoring!");
                            jarFile.close();
                            mParseError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
                            return false;
                        }
                    }
                }
            }
        }
        jarFile.close();
        synchronized (mSync) {
            mReadBuffer = readBufferRef;
        }
        if (certs != null && certs.length > 0) {
            final int N = certs.length;
            pkg.mSignatures = new Signature[certs.length];
            for (int i = 0; i < N; i++) {
                pkg.mSignatures[i] = new Signature(certs[i].getEncoded());
            }
        } else {
            Slog.e(TAG, "Package " + pkg.packageName + " has no certificates; ignoring!");
            mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
            return false;
        }
    } catch (CertificateEncodingException e) {
        Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
        mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
        return false;
    } catch (IOException e) {
        Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
        mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
        return false;
    } catch (RuntimeException e) {
        Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
        mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
        return false;
    }
    return true;
}
Also used : CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Certificate(java.security.cert.Certificate)

Example 17 with JarFile

use of java.util.jar.JarFile in project pinpoint by naver.

the class PinpointBootStrap method appendToBootstrapClassLoader.

private static void appendToBootstrapClassLoader(Instrumentation instrumentation, BootstrapJarFile agentJarFile) {
    List<JarFile> jarFileList = agentJarFile.getJarFileList();
    for (JarFile jarFile : jarFileList) {
        logger.info("appendToBootstrapClassLoader:" + jarFile.getName());
        instrumentation.appendToBootstrapClassLoaderSearch(jarFile);
    }
}
Also used : JarFile(java.util.jar.JarFile)

Example 18 with JarFile

use of java.util.jar.JarFile in project pinpoint by naver.

the class ProfilerPluginLoader method load.

public List<SetupResult> load(URL[] pluginJars) {
    List<SetupResult> pluginContexts = new ArrayList<SetupResult>(pluginJars.length);
    for (URL pluginJar : pluginJars) {
        final JarFile pluginJarFile = createJarFile(pluginJar);
        final List<String> pluginPackageList = getPluginPackage(pluginJarFile);
        final ClassNameFilter pluginFilterChain = createPluginFilterChain(pluginPackageList);
        final List<ProfilerPlugin> original = PluginLoader.load(ProfilerPlugin.class, new URL[] { pluginJar });
        List<ProfilerPlugin> plugins = filterDisablePlugin(original);
        for (ProfilerPlugin plugin : plugins) {
            if (logger.isInfoEnabled()) {
                logger.info("{} Plugin {}:{}", plugin.getClass(), PluginConfig.PINPOINT_PLUGIN_PACKAGE, pluginPackageList);
            }
            logger.info("Loading plugin:{} pluginPackage:{}", plugin.getClass().getName(), plugin);
            PluginConfig pluginConfig = new PluginConfig(pluginJar, pluginFilterChain);
            final ClassInjector classInjector = new JarProfilerPluginClassInjector(pluginConfig, instrumentEngine);
            final SetupResult result = pluginSetup.setupPlugin(plugin, classInjector);
            pluginContexts.add(result);
        }
    }
    return pluginContexts;
}
Also used : ArrayList(java.util.ArrayList) ProfilerPlugin(com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin) JarFile(java.util.jar.JarFile) URL(java.net.URL) JarProfilerPluginClassInjector(com.navercorp.pinpoint.profiler.instrument.classloading.JarProfilerPluginClassInjector) ClassInjector(com.navercorp.pinpoint.profiler.instrument.classloading.ClassInjector) JarProfilerPluginClassInjector(com.navercorp.pinpoint.profiler.instrument.classloading.JarProfilerPluginClassInjector)

Example 19 with JarFile

use of java.util.jar.JarFile in project RocooFix by dodola.

the class PatchRefScan method addClasstoPatch.

public static void addClasstoPatch(JarEntry jarEntry, String className, JarFile alljar, File patchDir) throws Exception {
    System.out.println("============addClasstoPatch======" + className);
    File entryFile = new File(patchDir, className);
    if (!entryFile.exists()) {
        InputStream inputStreamX = alljar.getInputStream(jarEntry);
        byte[] bytes = NuwaProcessor.referHackWhenInit(inputStreamX);
        FileUtils.writeByteArrayToFile(entryFile, bytes);
    }
}
Also used : InputStream(java.io.InputStream) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 20 with JarFile

use of java.util.jar.JarFile in project RocooFix by dodola.

the class ClassReferenceListBuilder method addRoots.

public void addRoots(String path) throws IOException {
    this.path = new Path(path);
    this.jarOfRoots = new JarFile(path);
}
Also used : JarFile(java.util.jar.JarFile)

Aggregations

JarFile (java.util.jar.JarFile)1366 File (java.io.File)720 JarEntry (java.util.jar.JarEntry)616 IOException (java.io.IOException)587 URL (java.net.URL)272 InputStream (java.io.InputStream)271 ZipEntry (java.util.zip.ZipEntry)203 Manifest (java.util.jar.Manifest)186 ArrayList (java.util.ArrayList)158 Test (org.junit.Test)131 JarURLConnection (java.net.JarURLConnection)123 FileOutputStream (java.io.FileOutputStream)122 ZipFile (java.util.zip.ZipFile)121 FileInputStream (java.io.FileInputStream)111 Attributes (java.util.jar.Attributes)110 MalformedURLException (java.net.MalformedURLException)94 Enumeration (java.util.Enumeration)67 JarOutputStream (java.util.jar.JarOutputStream)65 HashSet (java.util.HashSet)58 URLClassLoader (java.net.URLClassLoader)55