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 
ModuleCache()42     private ModuleCache() {
43     }
44 
getInstance()45     public static ModuleCache getInstance() {
46         return cache;
47     }
48 
init(File indexFile)49     public void init(File indexFile) throws IOException {
50         indexes = new ModuleIndexes(indexFile);
51         indexes.build();
52     }
53 
getAndCacheByDir(File moduleDir)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 
getAndCacheByName(String moduleName)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 makeFileName = indexes.getMakeFile(moduleName);
75         if (makeFileName == null) {
76             logger.warning("Unable to find make file for module: " + moduleName);
77             return null;
78         }
79 
80         File makeFile = new File(makeFileName);
81         if (!makeFile.exists()) {
82             logger.warning("Unable to find make file for module: " + moduleName);
83             return null;
84         }
85 
86         return getAndCacheByDir(makeFile.getParentFile());
87     }
88 
putModule(File moduleDir, Module module)89     private void putModule(File moduleDir, Module module) throws IOException {
90         modulesByPath.put(moduleDir.getCanonicalPath(), module);
91     }
92 
getModule(File moduleDir)93     private Module getModule(File moduleDir) throws IOException {
94         return modulesByPath.get(moduleDir.getCanonicalPath());
95     }
96 
getModules()97     public Iterable<Module> getModules() {
98         return modulesByPath.values();
99     }
100 
put(Module module)101     public void put(Module module) throws IOException {
102         Preconditions.checkNotNull(module);
103         putModule(module.getDir(), module);
104     }
105 }
106