Search in sources :

Example 21 with FilenameFilter

use of java.io.FilenameFilter in project atlas by alibaba.

the class Framework method restoreFromExistedBundle.

/**
     *         boolean lockSuccess = false;
     try {
     lockSuccess = BundleLock.ReadLock(location);
     if (location == null){
     return null;
     }
     Bundle bundle = bundles.get(location);
     if(bundle==null) {
     bundle = restoreFromExistedBundle(location);
     }
     return bundle;

     }finally {
     if(lockSuccess) {
     try {
     BundleLock.ReadUnLock(location);
     }catch(Throwable e){}
     }
     }
     * @param location
     * @return
     */
public static BundleImpl restoreFromExistedBundle(final String location) {
    boolean lockSuccess = false;
    File bundleDir = new File(STORAGE_LOCATION, location);
    BundleImpl bundle = null;
    // just restore
    if (bundleDir.exists() && new File(bundleDir, "meta").exists()) {
        try {
            lockSuccess = BundleLock.ReadLock(location);
            AtlasFileLock.getInstance().LockExclusive(bundleDir);
            BundleContext impl = new BundleContext();
            if (BaselineInfoManager.instance().dexPatchVersion() > 0 && BaselineInfoManager.instance().isDexPatched(location)) {
                impl.dexPatchVersion = BaselineInfoManager.instance().dexPatchVersion();
                bundle = new BundleImpl(bundleDir, impl);
                if (bundle != null) {
                    bundle.optDexFile();
                }
            } else {
                String[] versions = bundleDir.list(new FilenameFilter() {

                    @Override
                    public boolean accept(File dir, String filename) {
                        return filename.startsWith(BundleArchive.REVISION_DIRECTORY);
                    }
                });
                if (versions != null && versions.length > 0) {
                    bundle = new BundleImpl(bundleDir, impl);
                    if (bundle != null) {
                        bundle.optDexFile();
                    }
                }
            }
        } catch (Exception e) {
            if (e instanceof BundleArchive.MisMatchException) {
                if (bundleDir.exists()) {
                    bundle = null;
                }
            }
            AtlasMonitor.getInstance().trace(AtlasMonitor.BUNDLE_INSTALL_FAIL, location, AtlasMonitor.RESTORED_FAILED_MSG, FileUtils.getDataAvailableSpace());
            Log.e("Framework", "restore bundle failed" + location, e);
        } finally {
            if (lockSuccess) {
                try {
                    BundleLock.ReadUnLock(location);
                } catch (Throwable e) {
                }
            }
            AtlasFileLock.getInstance().unLock(bundleDir);
        }
    }
    return bundle;
}
Also used : FilenameFilter(java.io.FilenameFilter) BundleArchive(android.taobao.atlas.framework.bundlestorage.BundleArchive) File(java.io.File) BundleException(org.osgi.framework.BundleException) LowDiskException(android.taobao.atlas.runtime.LowDiskException) IOException(java.io.IOException)

Example 22 with FilenameFilter

use of java.io.FilenameFilter in project atlas by alibaba.

the class Framework method mergeWalsDir.

private static void mergeWalsDir(File walsDir, File storageDir) {
    if (writeAheads != null && writeAheads.size() > 0) {
        for (int i = 0; i < writeAheads.size(); i++) {
            if (writeAheads.get(i) == null) {
                continue;
            }
            File walDir = new File(walsDir, writeAheads.get(i));
            try {
                if (walDir != null && walDir.exists()) {
                    // merge wal dir to storage
                    final File[] walBundleDirs = walDir.listFiles();
                    if ((walBundleDirs != null) && (walBundleDirs.length > 0)) {
                        for (File walBundleDir : walBundleDirs) {
                            if (walBundleDir.isDirectory()) {
                                File bundleDir = new File(storageDir, walBundleDir.getName());
                                //                                    if (bundleDir.exists()) {
                                //                                        deleteDirectory(bundleDir);
                                //                                    }
                                File newRevDir = new File(walBundleDir, "version.1");
                                if (!newRevDir.exists()) {
                                    return;
                                }
                                File[] revisions = bundleDir.listFiles(new FilenameFilter() {

                                    @Override
                                    public boolean accept(File dir, String filename) {
                                        return filename.startsWith(BundleArchive.REVISION_DIRECTORY);
                                    }
                                });
                                int targetREV = 1;
                                List<String> useless = new ArrayList<>();
                                File latestRevision = null;
                                String targetVersion = BundleArchive.REVISION_DIRECTORY + ".1";
                                int rev;
                                if (revisions != null && revisions.length > 0) {
                                    for (File revision : revisions) {
                                        if ((rev = Integer.parseInt(revision.getName().substring(8))) >= targetREV) {
                                            targetREV = rev;
                                            if (!new File(revision, BundleArchive.DEPRECATED_MARK).exists()) {
                                                if (latestRevision != null) {
                                                    useless.add(latestRevision.getName());
                                                }
                                                latestRevision = revision;
                                            }
                                        } else {
                                            useless.add(revision.getName());
                                        }
                                        if (new File(revision, BundleArchive.DEPRECATED_MARK).exists()) {
                                            useless.add(revision.getName());
                                        }
                                    }
                                    if (!BaselineInfoManager.instance().isCachePreVersion()) {
                                        useless.add(latestRevision.getName());
                                    }
                                    targetVersion = BundleArchive.REVISION_DIRECTORY + "." + (++targetREV);
                                }
                                File targetDir = new File(bundleDir, targetVersion);
                                targetDir.mkdirs();
                                File meta = new File(bundleDir, "meta");
                                if (meta.exists()) {
                                    meta.delete();
                                }
                                new File(walBundleDir, "meta").renameTo(meta);
                                // move bundle to storage
                                boolean result = newRevDir.renameTo(targetDir);
                                if (!result || !targetDir.exists()) {
                                    targetDir.mkdirs();
                                    result = newRevDir.renameTo(targetDir);
                                    new File(walBundleDir, "meta").renameTo(meta);
                                    if (!result || !targetDir.exists() || !meta.exists()) {
                                        BaselineInfoManager.instance().rollbackHardly();
                                        android.os.Process.killProcess(android.os.Process.myPid());
                                    }
                                } else {
                                    //remove old bundles
                                    for (String uselessDir : useless) {
                                        File uselessFile = new File(bundleDir, uselessDir);
                                        if (uselessFile.exists()) {
                                            deleteDirectory(uselessFile);
                                        }
                                        if (uselessFile.exists()) {
                                            new File(uselessFile, BundleArchive.DEPRECATED_MARK).createNewFile();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                writeAheads.set(i, null);
            } catch (Exception e) {
                Log.e("Framework", "Error while merge wal dir", e);
            }
        }
    }
    if (walsDir.exists()) {
        deleteDirectory(walsDir);
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) ArrayList(java.util.ArrayList) File(java.io.File) BundleException(org.osgi.framework.BundleException) LowDiskException(android.taobao.atlas.runtime.LowDiskException) IOException(java.io.IOException)

Example 23 with FilenameFilter

use of java.io.FilenameFilter in project atlas by alibaba.

the class Framework method checkInstallDebugBundle.

/**
     * for debug not release
     */
static void checkInstallDebugBundle() {
    File debugDirectory = new File(ATLAS_DEBUG_DIRECTORY);
    if (debugDirectory.exists()) {
        File[] bundles = debugDirectory.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String filename) {
                if (filename.endsWith(".so")) {
                    return true;
                }
                return false;
            }
        });
        if (bundles != null) {
            String[] packageNames = new String[bundles.length];
            String[] versions = new String[bundles.length];
            for (int x = 0; x < bundles.length; x++) {
                versions[x] = "1.0.0";
                if (bundles[x].getName().startsWith("kernal") || bundles[x].getName().contains("com_taobao_mainDex")) {
                    //主dexbundle
                    packageNames[x] = "com.taobao.maindex";
                } else {
                    //业务bundle
                    PackageInfo info = RuntimeVariables.androidApplication.getPackageManager().getPackageArchiveInfo(bundles[x].getAbsolutePath(), 0);
                    if (info != null) {
                        packageNames[x] = info.applicationInfo.packageName;
                    } else {
                        String fileName = bundles[x].getName();
                        fileName = fileName.substring(3, fileName.length() - 3);
                        packageNames[x] = fileName.replace("_", ".");
                    }
                }
            }
            try {
                Atlas.getInstance().installOrUpdate(packageNames, bundles, versions, -1);
                Log.d("Framework", "patch success and delete file");
                try {
                    for (File installFile : bundles) {
                        if (installFile.exists()) {
                            installFile.delete();
                        }
                    }
                } catch (Exception e) {
                }
            } catch (BundleException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) PackageInfo(android.content.pm.PackageInfo) BundleException(org.osgi.framework.BundleException) File(java.io.File) BundleException(org.osgi.framework.BundleException) LowDiskException(android.taobao.atlas.runtime.LowDiskException) IOException(java.io.IOException)

Example 24 with FilenameFilter

use of java.io.FilenameFilter in project atlas by alibaba.

the class BundleArchive method downgradeRevision.

public static boolean downgradeRevision(String location, File bundleDir, boolean forceDelete) throws IOException {
    File[] revisionFiles = bundleDir.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String filename) {
            if (filename.startsWith(REVISION_DIRECTORY) && !new File(dir + File.separator + filename, DEPRECATED_MARK).exists()) {
                return true;
            }
            return false;
        }
    });
    if (revisionFiles != null && revisionFiles.length > 0) {
        File deprecatedDir = revisionFiles[revisionFiles.length - 1];
        if (forceDelete) {
            try {
                BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(location);
                if (impl != null && impl.getArchive().getCurrentRevision() != null) {
                    if (impl.getArchive().getCurrentRevision().getRevisionDir().equals(deprecatedDir)) {
                        return true;
                    }
                }
            } catch (Throwable e) {
            }
            //deprecatedDir.delete();
            Framework.deleteDirectory(deprecatedDir);
        }
        if (deprecatedDir.exists()) {
            File deprecatedFile = new File(deprecatedDir, DEPRECATED_MARK);
            deprecatedFile.createNewFile();
            if (!deprecatedFile.exists()) {
                return false;
            }
        }
        Log.d("BundleArchive", "downgrade " + bundleDir);
        return true;
    }
    return false;
}
Also used : FilenameFilter(java.io.FilenameFilter) BundleImpl(android.taobao.atlas.framework.BundleImpl) File(java.io.File)

Example 25 with FilenameFilter

use of java.io.FilenameFilter in project canal by alibaba.

the class SpringInstanceConfigMonitor method scan.

private void scan() {
    File rootdir = new File(rootConf);
    if (!rootdir.exists()) {
        return;
    }
    File[] instanceDirs = rootdir.listFiles(new FileFilter() {

        public boolean accept(File pathname) {
            String filename = pathname.getName();
            return pathname.isDirectory() && !"spring".equalsIgnoreCase(filename);
        }
    });
    // 扫描目录的新增
    Set<String> currentInstanceNames = new HashSet<String>();
    // 判断目录内文件的变化
    for (File instanceDir : instanceDirs) {
        String destination = instanceDir.getName();
        currentInstanceNames.add(destination);
        File[] instanceConfigs = instanceDir.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                // 限制一下,只针对instance.properties文件,避免因为.svn或者其他生成的临时文件导致出现reload
                return StringUtils.equalsIgnoreCase(name, "instance.properties");
            }
        });
        if (!actions.containsKey(destination) && instanceConfigs.length > 0) {
            // 存在合法的instance.properties,并且第一次添加时,进行启动操作
            notifyStart(instanceDir, destination);
        } else if (actions.containsKey(destination)) {
            // 历史已经启动过
            if (instanceConfigs.length == 0) {
                // 如果不存在合法的instance.properties
                notifyStop(destination);
            } else {
                InstanceConfigFiles lastFile = lastFiles.get(destination);
                boolean hasChanged = judgeFileChanged(instanceConfigs, lastFile.getInstanceFiles());
                // 通知变化
                if (hasChanged) {
                    notifyReload(destination);
                }
                if (hasChanged || CollectionUtils.isEmpty(lastFile.getInstanceFiles())) {
                    // 更新内容
                    List<FileInfo> newFileInfo = new ArrayList<FileInfo>();
                    for (File instanceConfig : instanceConfigs) {
                        newFileInfo.add(new FileInfo(instanceConfig.getName(), instanceConfig.lastModified()));
                    }
                    lastFile.setInstanceFiles(newFileInfo);
                }
            }
        }
    }
    // 判断目录是否删除
    Set<String> deleteInstanceNames = new HashSet<String>();
    for (String destination : actions.keySet()) {
        if (!currentInstanceNames.contains(destination)) {
            deleteInstanceNames.add(destination);
        }
    }
    for (String deleteInstanceName : deleteInstanceNames) {
        notifyStop(deleteInstanceName);
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) ArrayList(java.util.ArrayList) List(java.util.List) FileFilter(java.io.FileFilter) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

FilenameFilter (java.io.FilenameFilter)354 File (java.io.File)350 IOException (java.io.IOException)87 ArrayList (java.util.ArrayList)61 Test (org.junit.Test)49 URL (java.net.URL)19 RandomAccessFile (java.io.RandomAccessFile)18 List (java.util.List)17 HashSet (java.util.HashSet)15 FileOutputStream (java.io.FileOutputStream)14 MalformedURLException (java.net.MalformedURLException)12 FileFilter (java.io.FileFilter)11 FileNotFoundException (java.io.FileNotFoundException)11 TestClient (org.syncany.tests.util.TestClient)11 FileWriter (java.io.FileWriter)10 HashMap (java.util.HashMap)10 ZipFile (java.util.zip.ZipFile)10 BufferedWriter (java.io.BufferedWriter)9 FileInputStream (java.io.FileInputStream)9 FileReader (java.io.FileReader)9