KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > libraries > RbConfigLibrary


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
15  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
16  * Copyright (C) 2005 Charles O Nutter
17  * Copyright (C) 2006 Nick Sieger
18  *
19  * Alternatively, the contents of this file may be used under the terms of
20  * either of the GNU General Public License Version 2 or later (the "GPL"),
21  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22  * in which case the provisions of the GPL or the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of either the GPL or the LGPL, and not to allow others to
25  * use your version of this file under the terms of the CPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the GPL or the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of any one of the CPL, the GPL or the LGPL.
30  ***** END LICENSE BLOCK *****/

31 package org.jruby.libraries;
32
33 import java.io.IOException JavaDoc;
34
35 import org.jruby.Ruby;
36 import org.jruby.RubyHash;
37 import org.jruby.RubyModule;
38 import org.jruby.runtime.Constants;
39 import org.jruby.runtime.load.Library;
40 import org.jruby.util.NormalizedFile;
41
42 public class RbConfigLibrary implements Library {
43     /**
44      * Just enough configuration settings (most don't make sense in Java) to run the rubytests
45      * unit tests. The tests use <code>bindir</code>, <code>RUBY_INSTALL_NAME</code> and
46      * <code>EXEEXT</code>.
47      */

48     public void load(Ruby runtime) {
49         RubyModule configModule = runtime.defineModule("Config");
50         RubyHash configHash = RubyHash.newHash(runtime);
51         configModule.defineConstant("CONFIG", configHash);
52
53         String JavaDoc[] versionParts = Constants.RUBY_VERSION.split("\\.");
54         setConfig(configHash, "MAJOR", versionParts[0]);
55         setConfig(configHash, "MINOR", versionParts[1]);
56         setConfig(configHash, "TEENY", versionParts[2]);
57         setConfig(configHash, "ruby_version", versionParts[0] + '.' + versionParts[1]);
58         setConfig(configHash, "arch", "java");
59
60         setConfig(configHash, "bindir", new NormalizedFile(runtime.getJRubyHome(), "bin").getAbsolutePath());
61         setConfig(configHash, "RUBY_INSTALL_NAME", jruby_script());
62         setConfig(configHash, "ruby_install_name", jruby_script());
63         setConfig(configHash, "SHELL", jruby_shell());
64         setConfig(configHash, "prefix", new NormalizedFile(runtime.getJRubyHome()).getAbsolutePath());
65
66         setConfig(configHash, "host_os", System.getProperty("os.name"));
67         setConfig(configHash, "LIBRUBY", "jruby");
68         setConfig(configHash, "LIBRUBY_SO", "jruby");
69         setConfig(configHash, "target", "java");
70         setConfig(configHash, "build", "java");
71         setConfig(configHash, "host_vendor", System.getProperty("java.vendor"));
72         setConfig(configHash, "host_cpu", System.getProperty("os.arch"));
73         setConfig(configHash, "target_cpu", System.getProperty("os.arch"));
74
75         String JavaDoc libdir = System.getProperty("jruby.lib");
76         if (libdir == null) {
77             libdir = new NormalizedFile(runtime.getJRubyHome(), "lib").getAbsolutePath();
78         } else {
79             try {
80             // Our shell scripts pass in non-canonicalized paths, but even if we didn't
81
// anyone who did would become unhappy because Ruby apps expect no relative
82
// operators in the pathname (rubygems, for example).
83
libdir = new NormalizedFile(libdir).getCanonicalPath();
84             } catch (IOException JavaDoc e) {
85                 libdir = new NormalizedFile(libdir).getAbsolutePath();
86             }
87         }
88
89         setConfig(configHash, "libdir", libdir);
90         setConfig(configHash, "rubylibdir", new NormalizedFile(libdir, "ruby/1.8").getAbsolutePath());
91         setConfig(configHash, "sitedir", new NormalizedFile(libdir, "ruby/site_ruby").getAbsolutePath());
92         setConfig(configHash, "sitelibdir", new NormalizedFile(libdir, "ruby/site_ruby/1.8").getAbsolutePath());
93         setConfig(configHash, "sitearchdir", new NormalizedFile(libdir, "ruby/site_ruby/1.8/java").getAbsolutePath());
94         setConfig(configHash, "configure_args", "");
95         setConfig(configHash, "datadir", new NormalizedFile(runtime.getJRubyHome(), "share").getAbsolutePath());
96         setConfig(configHash, "mandir", new NormalizedFile(runtime.getJRubyHome(), "man").getAbsolutePath());
97         setConfig(configHash, "sysconfdir", new NormalizedFile(runtime.getJRubyHome(), "etc").getAbsolutePath());
98
99         if (isWindows()) {
100             setConfig(configHash, "EXEEXT", ".exe");
101         } else {
102             setConfig(configHash, "EXEEXT", "");
103         }
104     }
105
106     private static void setConfig(RubyHash configHash, String JavaDoc key, String JavaDoc value) {
107         Ruby runtime = configHash.getRuntime();
108         configHash.aset(runtime.newString(key), runtime.newString(value));
109     }
110
111     private static boolean isWindows() {
112         return System.getProperty("os.name", "").startsWith("Windows");
113     }
114
115     private String JavaDoc jruby_script() {
116         return System.getProperty("jruby.script", isWindows() ? "jruby.bat" : "jruby").replace('\\', '/');
117     }
118
119     private String JavaDoc jruby_shell() {
120         return System.getProperty("jruby.shell", isWindows() ? "cmd.exe" : "/bin/sh").replace('\\', '/');
121     }
122 }
123
Popular Tags