Home | History | Annotate | Download | only in idegen
      1 /*
      2  * Copyright (C) 2012 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 
     17 package com.android.idegen;
     18 
     19 import com.google.common.base.Preconditions;
     20 import com.google.common.collect.Maps;
     21 
     22 import java.io.File;
     23 import java.io.IOException;
     24 import java.util.HashMap;
     25 import java.util.logging.Logger;
     26 
     27 /**
     28  * Cache to hold built modules.
     29  */
     30 public class ModuleCache {
     31 
     32     private static final Logger logger = Logger.getLogger(ModuleCache.class.getName());
     33 
     34     private static ModuleCache cache = new ModuleCache();
     35 
     36     ModuleIndexes indexes;
     37 
     38     // Mapping of canonical module directory to module.  Use string instead of File since File
     39     // does not provide equality based on canonical path.
     40     HashMap<String, Module> modulesByPath = Maps.newHashMap();
     41 
     42     private ModuleCache() {
     43     }
     44 
     45     public static ModuleCache getInstance() {
     46         return cache;
     47     }
     48 
     49     public void init(File indexFile) throws IOException {
     50         indexes = new ModuleIndexes(indexFile);
     51         indexes.build();
     52     }
     53 
     54     public Module getAndCacheByDir(File moduleDir) throws IOException {
     55         Preconditions.checkNotNull(moduleDir);
     56 
     57         if (moduleDir.exists()) {
     58             Module module = getModule(moduleDir);
     59             if (module == null) {
     60                 module = new Module(moduleDir);
     61                 // Must put module before building it.  Otherwise infinite loop.
     62                 putModule(moduleDir, module);
     63                 module.build();
     64             }
     65             return module;
     66         }
     67         return null;
     68     }
     69 
     70     public Module getAndCacheByName(String moduleName) throws IOException {
     71         Preconditions.checkState(indexes != null, "You must call init() first.");
     72         Preconditions.checkNotNull(moduleName);
     73 
     74         String makeFile = indexes.getMakeFile(moduleName);
     75         if (makeFile == null) {
     76             logger.warning("Unable to find make file for module: " + moduleName);
     77             return null;
     78         }
     79         return getAndCacheByDir(new File(makeFile).getParentFile());
     80     }
     81 
     82     private void putModule(File moduleDir, Module module) throws IOException {
     83         modulesByPath.put(moduleDir.getCanonicalPath(), module);
     84     }
     85 
     86     private Module getModule(File moduleDir) throws IOException {
     87         return modulesByPath.get(moduleDir.getCanonicalPath());
     88     }
     89 
     90     public Iterable<Module> getModules() {
     91         return modulesByPath.values();
     92     }
     93 
     94     public void put(Module module) throws IOException {
     95         Preconditions.checkNotNull(module);
     96         putModule(module.getDir(), module);
     97     }
     98 }
     99