001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.api.ruby.platform;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.List;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import org.netbeans.api.options.OptionsDisplayer;
050: import org.netbeans.modules.ruby.platform.Util;
051: import org.openide.modules.InstalledFileLocator;
052: import org.openide.util.Exceptions;
053: import org.openide.util.Utilities;
054:
055: /**
056: * Information about a Ruby installation.
057: *
058: * @author Tor Norbye
059: */
060: public class RubyInstallation {
061:
062: private static final Logger LOGGER = Logger
063: .getLogger(RubyInstallation.class.getName());
064:
065: /** NOTE: Keep this in sync with ruby/jruby/nbproject/project.properties */
066: private static final String JRUBY_RELEASE = "1.1RC1"; // NOI18N
067:
068: /** NOTE: Keep this in sync with ruby/jruby/nbproject/project.properties */
069: private static final String JRUBY_RELEASEDIR = "jruby-"
070: + JRUBY_RELEASE; // NOI18N
071:
072: /**
073: * MIME type for Ruby. Don't change this without also consulting the various XML files
074: * that cannot reference this value directly, as well as RUBY_MIME_TYPE in the editing plugin
075: */
076: public static final String RUBY_MIME_TYPE = "text/x-ruby"; // NOI18N
077: public static final String RHTML_MIME_TYPE = "application/x-httpd-eruby"; // NOI18N
078: private static final RubyInstallation INSTANCE = new RubyInstallation();
079:
080: private String jrubyHome;
081:
082: private RubyInstallation() {
083: }
084:
085: public static RubyInstallation getInstance() {
086: return INSTANCE;
087: }
088:
089: // Ensure that JRuby can find its libraries etc.
090: public void setJRubyLoadPaths() {
091: String jh = getJRubyHome();
092: if (jh != null) {
093: System.setProperty("jruby.home", jh); // NOI18N
094: }
095: }
096:
097: public String getJRuby() {
098: String binDir = getJRubyBin();
099: if (binDir == null) {
100: return null;
101: }
102:
103: String binary = Utilities.isWindows() ? "jruby.bat" : "jruby"; // NOI18N
104: String jruby = binDir + File.separator + binary;
105:
106: // Normalize path
107: try {
108: jruby = new File(jruby).getCanonicalFile()
109: .getAbsolutePath();
110: } catch (IOException ioe) {
111: Exceptions.printStackTrace(ioe);
112: }
113:
114: return jruby;
115: }
116:
117: static void displayRubyOptions() {
118: OptionsDisplayer.getDefault().open("RubyOptions"); // NOI18N
119: }
120:
121: /**
122: * Returns directory where bundle JRuby is installed. Return <tt>null</tt> if
123: * not installed.
124: *
125: * @return might be <tt>null</tt>
126: */
127: public String getJRubyHome() {
128: if (jrubyHome == null) {
129: File jrubyDir = InstalledFileLocator.getDefault().locate(
130: JRUBY_RELEASEDIR,
131: "org.netbeans.modules.ruby.project", // NOI18N
132: false); // NOI18N
133:
134: if ((jrubyDir == null) || !jrubyDir.isDirectory()) {
135: // The JRuby distribution may not be installed
136: return null;
137: }
138:
139: jrubyHome = jrubyDir.getPath();
140: }
141:
142: return jrubyHome;
143: }
144:
145: private String getJRubyBin() {
146: String jh = getJRubyHome();
147: if (jh != null) {
148: return jh + File.separator + "bin"; // NOI18N
149: } else {
150: return null;
151: }
152: }
153:
154: /**
155: * AutoUpdate may not set execute permissions on the bundled JRuby files,
156: * so try to fix that here
157: * @todo Do this lazily before trying to actually execute any of these bits?
158: */
159: public void ensureExecutable() {
160: // No excute permissions on Windows. On Unix and Mac, try.
161: if (Utilities.isWindows()) {
162: return;
163: }
164:
165: String binDirPath = getJRubyBin();
166: if (binDirPath == null) {
167: return;
168: }
169:
170: File binDir = new File(binDirPath);
171: if (!binDir.exists()) {
172: // No JRuby bundled installation?
173: return;
174: }
175:
176: // Ensure that the binaries are installed as expected
177: // The following logic is from CLIHandler in core/bootstrap:
178: File chmod = new File("/bin/chmod"); // NOI18N
179:
180: if (!chmod.isFile()) {
181: // Linux uses /bin, Solaris /usr/bin, others hopefully one of those
182: chmod = new File("/usr/bin/chmod"); // NOI18N
183: }
184:
185: if (chmod.isFile()) {
186: try {
187: List<String> argv = new ArrayList<String>();
188: argv.add(chmod.getAbsolutePath());
189: argv.add("u+rx"); // NOI18N
190:
191: String[] files = binDir.list();
192:
193: for (String file : files) {
194: argv.add(file);
195: }
196:
197: ProcessBuilder pb = new ProcessBuilder(argv);
198: pb.directory(binDir);
199: Util.adjustProxy(pb);
200:
201: Process process = pb.start();
202:
203: int chmoded = process.waitFor();
204:
205: if (chmoded != 0) {
206: throw new IOException("could not run " + argv
207: + " : Exit value=" + chmoded); // NOI18N
208: }
209: } catch (Throwable e) {
210: // 108252 - no loud complaints
211: LOGGER.log(Level.INFO, "Can't chmod+x JRuby bits", e);
212: }
213: }
214: }
215:
216: // public String getShortName() {
217: // String r = Util.getPreferences().get(KEY_RUBY, null);
218: // final String BUILTIN_JRUBY = NbBundle.getMessage(RubyInstallation.class, "BuiltinRuby");
219: // if (r == null) {
220: // return BUILTIN_JRUBY;
221: // } else {
222: // r = getRuby();
223: // if (r == null) {
224: // return "";
225: // }
226: //
227: // final String jh = getJRubyHome();
228: // if (jh != null & r.startsWith(jh)) {
229: // return BUILTIN_JRUBY;
230: // }
231: // if (isJRubySet()) {
232: // return NbBundle.getMessage(RubyInstallation.class, "JRuby"); // TODO version
233: // }
234: // }
235: //
236: // // How do I summary other interpreters?? For now, just use path
237: // final File rubyFile = new File(r);
238: // String basename = rubyFile.getName();
239: // return NbBundle.getMessage(RubyInstallation.class, "RubyInPath", basename, rubyFile.getParent());
240: // }
241:
242: }
|