Home | History | Annotate | Download | only in build
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.tradefed.build;
     17 
     18 import java.io.File;
     19 import java.util.Collections;
     20 import java.util.HashMap;
     21 import java.util.Map;
     22 
     23 /**
     24  * A factory for creating {@link FileDownloadCache}
     25  */
     26 public class FileDownloadCacheFactory {
     27 
     28     // use the "singleton inner class" pattern
     29     // http://en.wikipedia.org/wiki/Singleton_pattern#The_solution_of_Bill_Pugh
     30     private static class SingletonHolder {
     31         public static final FileDownloadCacheFactory INSTANCE = new FileDownloadCacheFactory();
     32     }
     33 
     34     private Map<String, FileDownloadCache> mCacheObjectMap = Collections.synchronizedMap(
     35             new HashMap<String, FileDownloadCache>());
     36 
     37     /**
     38      * Get the singleton instance of FileDownloadCacheFactory
     39      */
     40     public static FileDownloadCacheFactory getInstance() {
     41         return SingletonHolder.INSTANCE;
     42     }
     43 
     44     /**
     45      * Retrieve the {@link FileDownloadCache} with the given cache directory, creating if necessary.
     46      * <p/>
     47      * Note that the cache assumes that this process has exclusive access to the <var>cacheDir</var>
     48      * directory. If multiple TF processes will be run on the same machine, they MUST each use
     49      * unique cache directories.
     50      *
     51      * @param cacheDir the local filesystem directory to use as a cache
     52      * @return the {@link FileDownloadCache} for given cacheDir
     53      */
     54     public synchronized FileDownloadCache getCache(File cacheDir) {
     55         FileDownloadCache cache = mCacheObjectMap.get(cacheDir.getAbsolutePath());
     56         if (cache == null) {
     57             cache = new FileDownloadCache(cacheDir);
     58             mCacheObjectMap.put(cacheDir.getAbsolutePath(), cache);
     59         }
     60         return cache;
     61     }
     62 }
     63