root/trunk/ProjectFortress/src/com/sun/fortress/repository/ProjectProperties.java @ 3040

Revision 3040, 11.9 KB (checked in by dr2chase, 13 months ago)

Trimmed memory consumption

Line 
1/*******************************************************************************
2    Copyright 2008 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************/
17
18package com.sun.fortress.repository;
19
20import java.io.File;
21import java.io.FileNotFoundException;
22import java.io.IOException;
23
24import com.sun.fortress.useful.Path;
25import com.sun.fortress.useful.StringMap;
26import com.sun.fortress.useful.Useful;
27
28public class ProjectProperties {
29 
30    private static String fortressAutoHome() {
31        String s = null;
32        try {
33            s = System.getProperty("fortress.autohome");
34        } catch (Throwable th) {
35
36        }
37        if (s == null) {
38            s = System.getenv("FORTRESS_AUTOHOME");
39        }
40        if (s == null) {
41            Path p = new Path(System.getProperty("java.class.path"));
42            File f = null;
43            try {
44                f = p.findDir("../ProjectFortress");
45                try {
46                    s = (new File(f, "..")).getCanonicalPath();
47                } catch (IOException ex) {
48                    throw new Error("Failure to evaluate relative path .. from " + f);
49                }
50            } catch (FileNotFoundException ex1) {
51                try {
52                    f = p.findDir("../../ProjectFortress/build");
53                    try {
54                        s = (new File(f, "../..")).getCanonicalPath();
55                    } catch (IOException ex) {
56                        throw new Error("Failure to evaluate relative path ../.. from " + f);
57                    }
58                } catch (FileNotFoundException ex2) {
59                    throw new Error("Could not find fortress home using fortress.autohome, FORTRESS_AUTOHOME, or probing classpath.");
60                }
61            }
62        }
63        return s;
64    }
65
66
67    /**
68     * If the property is defined, return that value,
69     * else return BASE_DIR + default_name .
70     *
71     * @param property_name
72     * @param default_name
73     * @return
74     */
75
76    private static String someImplDir(String property_name, String default_name) {
77        String s = null;
78
79        try {
80            s = System.getProperty(property_name);
81        } catch (Throwable th) {
82
83        }
84
85        if (s == null) {
86            s = BASEDIR + default_name;
87        }
88
89        s = s + File.separator;
90
91        // It is a myth that Windows requires backslashes.  Only the DOS shell
92        // requires backslashes.
93        s = backslashToSlash(s);
94        return s;
95    }
96
97
98    /**
99     * No-op right now.
100     *
101     * Intended to replace backslashes in a string with slashes.
102     * It is a myth that Windows needs backslashes in path names;
103     * in fact, it is only the DOS shell.  This fix was backed out
104     * because a different fix for the same problem was added; however,
105     * once tested this may be re-enabled.
106     *
107     * @param s
108     */
109    public static String backslashToSlash(String s) {
110        //return s.replaceAll("\\\\", "/");
111        return s;
112    }
113
114    public static final String FORTRESS_AUTOHOME = fortressAutoHome(); // MUST PRECEDE FORTRESS_HOME
115   
116    static final String home = System.getenv("HOME");
117
118    /**
119     * The search path for the repository, which will provide configuration information, etc.
120     */
121   
122    static final StringMap searchHead = new StringMap.ComposedMaps(
123            new StringMap.FromReflection(ProjectProperties.class, "FORTRESS_"),
124            new StringMap.FromSysProps(),
125            new StringMap.FromEnv());
126   
127    private static final String explicitRepository = searchHead.get("fortress.repository");
128    public static final String REPOSITORY_PATH_STRING = explicitRepository != null ? explicitRepository :
129        searchHead.getCompletely(";.fortress;${HOME}/.fortress;${FORTRESS_AUTOHOME}/local_repository;${FORTRESS_AUTOHOME}/default_repository",
130                1000);
131    public static final Path REPOSITORY_PATH = new Path(REPOSITORY_PATH_STRING);
132    static {
133        if (REPOSITORY_PATH.length() == 0) {
134            if (explicitRepository != null)
135                throw new Error("User-specified repository " + explicitRepository + " is not a readable directory");
136            else
137                throw new Error("Could not find any readable directories in the (semicolon-separated) repository list " + REPOSITORY_PATH_STRING);
138        }
139    }
140    public static final String REPOSITORY = REPOSITORY_PATH.findDirName(".", "").toString();
141
142    static {
143        if ("".equals(REPOSITORY)) {
144            throw new Error("Could not find any readable directories in the (semicolon-separated) repository list " + REPOSITORY_PATH_STRING);
145        }
146       
147        File config = new File(REPOSITORY + "/configuration");
148        File otherConfig = null;
149        try {
150            otherConfig = REPOSITORY_PATH.findFile("configuration");               
151        } catch (IOException ex) {
152            throw new Error(ex.getMessage());
153        }
154        if (otherConfig != null) {
155            if (! config.exists()) {
156                throw new Error("Repository " + REPOSITORY + " does not contain configuration" + " but " + otherConfig + " is ok ");
157            } else if (! config.isFile()) {
158                throw new Error("Repository " + REPOSITORY + "/configuration is not a regular file" + " but " + otherConfig + " is ok ");
159            } else if (! config.canRead()) {
160                throw new Error("Repository " + REPOSITORY + "/configuration is not readable" + " but " + otherConfig + " is ok ");
161            }
162        } else {
163            if (! config.exists()) {
164                throw new Error("Repository " + REPOSITORY + " does not contain configuration");
165            } else if (! config.isFile()) {
166                throw new Error("Repository " + REPOSITORY + "/configuration is not a regular file");
167            } else if (! config.canRead()) {
168                throw new Error("Repository " + REPOSITORY + "/configuration is not readable");
169            }
170        }
171    }
172    static final StringMap searchTail =
173            new StringMap.FromFileProps(REPOSITORY+"/configuration");
174
175    static final StringMap allProps = new StringMap.ComposedMaps(
176            searchHead,
177            searchTail
178    );
179
180    static final public String get(String s) {
181        String result = Useful.substituteVarsCompletely(s, allProps, 1000);
182        return result;
183    }
184
185    static final public String get(String s, String ifMissing) {
186        String result =  allProps.get(s);
187        if (result == null)
188            result = ifMissing;
189        if (result != null)
190            result = Useful.substituteVarsCompletely(result, allProps, 1000);
191        if (result == null) {
192            throw new Error("Must supply a definition (as property, environment variable, or repository configuration property) for " + s);
193        }
194        return result;
195    }
196
197    static final public boolean getBoolean(String s, boolean ifMissing) {
198        String result =  allProps.get(s);
199        if (result != null)
200            result = Useful.substituteVarsCompletely(result, allProps, 1000);
201        if (result == null) return ifMissing;
202        if (result.length() == 0)
203            return true;
204        s = result.toLowerCase();
205        char c = result.charAt(0);
206        if (c == 'y' || c == 't' || c == '1') return true;
207        if (c == 'n' || c == 'f' || c == '0') return false;
208
209        throw new Error("Unexpected definition of prop/env " + s + ", got " + result + ", need t/f/y/n/1/0[...]");
210    }
211
212    /**
213     * Searches for property/environment definition in the following order
214     *
215     * System.getProperty("fortress.cache")
216     * System.getenv("FORTRESS_CACHE")
217     * ./.fortress/configuration .getProperty("fortress.cache")
218     * ~/.fortress/configuration .getProperty("fortress.cache")
219     * ${FORTRESS_HOME}/local_repository/configuration .getProperty("fortress.cache") .
220     * ${FORTRESS_HOME}/default_repository/configuration .getProperty("fortress.cache") .
221     */
222
223    private static String searchDef(String asProp, String asEnv, String defaultValue) {
224        String result = null;
225        result = System.getProperty(asProp);
226        if (result == null)
227            result = System.getenv(asEnv);
228        if (result == null)
229            result = searchTail.get(asProp);
230        result =  result == null ? defaultValue : result;
231        result = Useful.substituteVarsCompletely(result, allProps, 1000);
232        return result;
233    }
234
235 
236    /** This static field holds the absolute path of the (sub)project location, as
237     * computed by reflectively finding the file location of the unnamed
238     * package, and grabbing the parent directory.
239     *
240     * The path name includes a trailing slash!
241     */
242    public static final String BASEDIR = searchDef("BASEDIR", "BASEDIR", "${FORTRESS_AUTOHOME}/ProjectFortress/");
243
244    public static final String CACHES = get("fortress.caches", "${REPOSITORY}/caches");
245   
246    public static final String INTERPRETER_CACHE_DIR = get("fortress.interpreter.cache", "${CACHES}/interpreter_cache");
247    public static final String PRESYNTAX_CACHE_DIR = get("fortress.presyntax.cache", "${CACHES}/presyntax_cache");
248    public static final String ANALYZED_CACHE_DIR = get("fortress.analyzed.cache", "${CACHES}/analyzed_cache");
249    public static final String SYNTAX_CACHE_DIR = get("fortress.syntax.cache", "${CACHES}/syntax_cache");
250    public static final String BYTECODE_CACHE_DIR = get("fortress.syntax.cache", "${CACHES}/bytecode_cache");   
251
252    public static final Path SOURCE_PATH = new Path(searchDef("fortress.source.path", "FORTRESS_SOURCE_PATH", "."));
253
254
255    static {
256        ensureDirectoryExists(PRESYNTAX_CACHE_DIR);
257        ensureDirectoryExists(INTERPRETER_CACHE_DIR);
258        ensureDirectoryExists(ANALYZED_CACHE_DIR);
259        ensureDirectoryExists(SYNTAX_CACHE_DIR);
260        ensureDirectoryExists(BYTECODE_CACHE_DIR);
261    }
262
263
264    public static String ensureDirectoryExists(String s) throws Error {
265        File f = new File(s);
266        if (f.exists()) {
267            if (f.isDirectory()) {
268                // ok
269            } else {
270                throw new Error("Necessary 'directory' " + s + " is not a directory");
271            }
272        } else {
273            if (f.mkdirs()) {
274                // ok
275            } else {
276                throw new Error("Failed to create directory " + s );
277            }
278        }
279        return s;
280    }
281
282    public final static String COMP_SOURCE_SUFFIX = "fss";
283
284    public final static String COMP_TREE_SUFFIX = "tfs";
285
286    public final static String API_SOURCE_SUFFIX = "fsi";
287
288    public final static String API_TREE_SUFFIX = "tfi";
289
290    public final static boolean leakCheck = getBoolean("fortress.test.leaks", false);
291
292
293    /** Creates a new instance of ProjectProperties */
294    private ProjectProperties() {
295    }
296
297
298    public static String astSuffixForSource(String s) {
299        if (s.endsWith("." + COMP_SOURCE_SUFFIX))
300            return COMP_TREE_SUFFIX;
301
302        if (s.endsWith("." + API_SOURCE_SUFFIX))
303            return API_TREE_SUFFIX;
304
305        throw new Error("Unexpected suffix on Fortress(?) source file");
306    }
307   
308    public static String fileName(String dir, String name, String suffix) {
309        return dir + "/" + name + "." + suffix;
310    }
311   
312    public static String compFileName(String dir, String name) {
313        return fileName(dir, name, ProjectProperties.COMP_TREE_SUFFIX);
314    }
315
316    public static String apiFileName(String dir, String name) {
317        return fileName(dir, name, ProjectProperties.API_TREE_SUFFIX);
318    }   
319}
Note: See TracBrowser for help on using the browser.