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

Revision 3290, 12.1 KB (checked in by chf, 11 months ago)

preliminary native code wrapping

Line 
1/*******************************************************************************
2    Copyright 2009 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.bytecode.cache", "${CACHES}/bytecode_cache");   
251    public static final String NATIVE_WRAPPER_CACHE_DIR = get("fortress.nativewrapper.cache", "${CACHES}/nativewrapper_cache");   
252
253    public static final Path SOURCE_PATH = new Path(searchDef("fortress.source.path", "FORTRESS_SOURCE_PATH", "."));
254
255
256    static {
257        ensureDirectoryExists(PRESYNTAX_CACHE_DIR);
258        ensureDirectoryExists(INTERPRETER_CACHE_DIR);
259        ensureDirectoryExists(ANALYZED_CACHE_DIR);
260        ensureDirectoryExists(SYNTAX_CACHE_DIR);
261        ensureDirectoryExists(BYTECODE_CACHE_DIR);
262        ensureDirectoryExists(NATIVE_WRAPPER_CACHE_DIR);
263    }
264
265
266    public static String ensureDirectoryExists(String s) throws Error {
267        File f = new File(s);
268        if (f.exists()) {
269            if (f.isDirectory()) {
270                // ok
271            } else {
272                throw new Error("Necessary 'directory' " + s + " is not a directory");
273            }
274        } else {
275            if (f.mkdirs()) {
276                // ok
277            } else {
278                throw new Error("Failed to create directory " + s );
279            }
280        }
281        return s;
282    }
283
284    public final static String COMP_SOURCE_SUFFIX = "fss";
285
286    public final static String COMP_TREE_SUFFIX = "tfs";
287
288    public final static String API_SOURCE_SUFFIX = "fsi";
289
290    public final static String API_TREE_SUFFIX = "tfi";
291
292    public final static boolean leakCheck = getBoolean("fortress.test.leaks", false);
293
294
295    /** Creates a new instance of ProjectProperties */
296    private ProjectProperties() {
297    }
298
299
300    public static String astSuffixForSource(String s) {
301        if (s.endsWith("." + COMP_SOURCE_SUFFIX))
302            return COMP_TREE_SUFFIX;
303
304        if (s.endsWith("." + API_SOURCE_SUFFIX))
305            return API_TREE_SUFFIX;
306
307        throw new Error("Unexpected suffix on Fortress(?) source file");
308    }
309   
310    public static String fileName(String dir, String name, String suffix) {
311        return dir + "/" + name + "." + suffix;
312    }
313   
314    public static String compFileName(String dir, String name) {
315        return fileName(dir, name, ProjectProperties.COMP_TREE_SUFFIX);
316    }
317
318    public static String apiFileName(String dir, String name) {
319        return fileName(dir, name, ProjectProperties.API_TREE_SUFFIX);
320    }   
321}
Note: See TracBrowser for help on using the browser.