Search in sources :

Example 1 with KernalBundle

use of android.taobao.atlas.startup.patch.KernalBundle in project atlas by alibaba.

the class Framework method installOrUpdate.

static void installOrUpdate(final String[] locations, final File[] files, String[] newBundleVersions, long dexPatchVersion) throws BundleException {
    if (locations == null || files == null || locations.length != files.length) {
        throw new IllegalArgumentException("locations and files must not be null and must be same length");
    }
    String writeAhead = String.valueOf(System.currentTimeMillis());
    File walsDir = new File(STORAGE_LOCATION, "wal");
    File walDir = new File(walsDir, writeAhead);
    walDir.mkdirs();
    for (int i = 0; i < locations.length; i++) {
        if (locations[i] == null || files[i] == null) {
            continue;
        }
        File bundleDir = null;
        try {
            BundleLock.WriteLock(locations[i]);
            if (isKernalBundle(locations[i])) {
                KernalBundle bundle = KernalBundle.kernalBundle;
                if (bundle != null) {
                    bundle.update(files[i], Framework.containerVersion, dexPatchVersion);
                } else {
                    bundleDir = new File(STORAGE_LOCATION, KernalBundle.KERNAL_BUNDLE_NAME);
                    if (!bundleDir.exists()) {
                        bundleDir.mkdirs();
                    }
                    AtlasFileLock.getInstance().LockExclusive(bundleDir);
                    KernalBundle b = new KernalBundle(bundleDir, files[i], Framework.containerVersion, dexPatchVersion);
                    if (b != null) {
                        FileUtils.createNewDirIfNotExist(b.getArchive().getRevisionDir(), UPDATED_MARK);
                    }
                }
            } else {
                Bundle bundle = Framework.getBundle(locations[i]);
                if (bundle != null) {
                    bundle.update(files[i], newBundleVersions[i], dexPatchVersion);
                } else {
                    if (dexPatchVersion > 0) {
                        bundleDir = new File(STORAGE_LOCATION, locations[i]);
                    } else {
                        bundleDir = new File(walDir, locations[i]);
                    }
                    if (!bundleDir.exists()) {
                        bundleDir.mkdirs();
                    }
                    // Hold the storage file lock
                    AtlasFileLock.getInstance().LockExclusive(bundleDir);
                    BundleImpl b = new BundleImpl(bundleDir, locations[i], new BundleContext(), null, files[i], newBundleVersions[i], false, dexPatchVersion);
                    if (b != null) {
                        FileUtils.createNewDirIfNotExist(b.archive.getCurrentRevision().getRevisionDir(), UPDATED_MARK);
                    }
                }
            }
        } catch (Exception e) {
            AtlasMonitor.getInstance().trace(AtlasMonitor.BUNDLE_INSTALL_FAIL, locations[i], AtlasMonitor.UPDATE_FAILED_MSG, FileUtils.getDataAvailableSpace());
            /**
                 * bundle 安装需要支持事务,失败时回滚已部署的bundle
                 */
            for (int x = 0; x <= i; x++) {
                if (isKernalBundle(locations[x])) {
                    if (KernalBundle.kernalBundle != null) {
                        try {
                            KernalBundle.downgradeRevision(new File(STORAGE_LOCATION, KernalBundle.KERNAL_BUNDLE_NAME), true);
                        } catch (IOException e2) {
                        }
                    } else {
                        deleteDirectory(new File(STORAGE_LOCATION, KernalBundle.KERNAL_BUNDLE_NAME));
                    }
                } else {
                    try {
                        BundleArchive.downgradeRevision(locations[x], new File(STORAGE_LOCATION, locations[x]), true);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    deleteDirectory(walDir);
                    if (walDir.exists()) {
                        try {
                            new File(walDir, DEPRECATED_MARK).createNewFile();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
            throw new BundleException("failed to installOrUpdate bundles ", e);
        } finally {
            if (bundleDir != null) {
                AtlasFileLock.getInstance().unLock(bundleDir);
            }
            BundleLock.WriteUnLock(locations[i]);
        }
    }
    bundleUpdated = true;
    WrapperUtil.appendLog("installedVersionWhenUpdated", Framework.containerVersion);
    WrapperUtil.appendLog("VersionWhenUpdated", WrapperUtil.getPackageInfo(RuntimeVariables.androidApplication).versionName);
    writeAheads.add(writeAhead);
    InstrumentationHook.notifyAppUpdated();
    storeMetadata();
}
Also used : Bundle(org.osgi.framework.Bundle) KernalBundle(android.taobao.atlas.startup.patch.KernalBundle) KernalBundle(android.taobao.atlas.startup.patch.KernalBundle) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) File(java.io.File) BundleException(org.osgi.framework.BundleException) LowDiskException(android.taobao.atlas.runtime.LowDiskException) IOException(java.io.IOException)

Aggregations

LowDiskException (android.taobao.atlas.runtime.LowDiskException)1 KernalBundle (android.taobao.atlas.startup.patch.KernalBundle)1 File (java.io.File)1 IOException (java.io.IOException)1 Bundle (org.osgi.framework.Bundle)1 BundleException (org.osgi.framework.BundleException)1