001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 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-2008 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.modules.ruby.platform;
042:
043: import java.io.ByteArrayOutputStream;
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.util.Map;
048: import java.util.logging.Logger;
049: import java.util.prefs.Preferences;
050: import javax.swing.JComboBox;
051: import org.netbeans.api.ruby.platform.RubyPlatform;
052: import org.netbeans.api.ruby.platform.RubyPlatformManager;
053: import org.openide.DialogDisplayer;
054: import org.openide.NotifyDescriptor;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.util.NbBundle;
057: import org.openide.util.NbPreferences;
058:
059: public final class Util {
060:
061: private static final Logger LOGGER = Logger.getLogger(Util.class
062: .getName());
063:
064: // FIXME: get rid of those proxy constants as soon as some NB Proxy API is available
065: private static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication"; // NOI18N
066: private static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername"; // NOI18N
067: private static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword"; // NOI18N
068:
069: private static final String FIRST_TIME_KEY = "platform-manager-called-first-time"; // NOI18N
070:
071: private Util() {
072: }
073:
074: /** Returns {@link NbPreferences preferences} for this module. */
075: public static Preferences getPreferences() {
076: return NbPreferences.forModule(Util.class);
077: }
078:
079: /** Return true iff the given line seems to be colored using ANSI terminal escape codes */
080: public static boolean containsAnsiColors(String line) {
081: // RSpec will color output with ANSI color sequence terminal escapes
082: return line.indexOf("\033[") != -1; // NOI18N
083: }
084:
085: /**
086: * Remove ANSI terminal escape codes from a line.
087: */
088: public static String stripAnsiColors(String line) {
089: StringBuilder sb = new StringBuilder(line.length());
090: int index = 0;
091: int max = line.length();
092: while (index < max) {
093: int nextEscape = line.indexOf("\033[", index); // NOI18N
094: if (nextEscape == -1) {
095: nextEscape = line.length();
096: }
097:
098: for (int n = (nextEscape == -1) ? max : nextEscape; index < n; index++) {
099: sb.append(line.charAt(index));
100: }
101:
102: if (nextEscape != -1) {
103: for (; index < max; index++) {
104: char c = line.charAt(index);
105: if (c == 'm') {
106: index++;
107: break;
108: }
109: }
110: }
111: }
112:
113: return sb.toString();
114: }
115:
116: public static void adjustProxy(final ProcessBuilder pb) {
117: String proxy = Util.getNetBeansHttpProxy();
118: if (proxy != null) {
119: Map<String, String> env = pb.environment();
120: if ((env.get("HTTP_PROXY") == null)
121: && (env.get("http_proxy") == null)) { // NOI18N
122: env.put("HTTP_PROXY", proxy); // NOI18N
123: env.put("http_proxy", proxy); // NOI18N
124: }
125: // PENDING - what if proxy was null so the user has TURNED off
126: // proxies while there is still an environment variable set - should
127: // we honor their environment, or honor their NetBeans proxy
128: // settings (e.g. unset HTTP_PROXY in the environment before
129: // launching plugin?
130: }
131: }
132:
133: /**
134: * FIXME: get rid of the whole method as soon as some NB Proxy API is
135: * available.
136: */
137: private static String getNetBeansHttpProxy() {
138: String host = System.getProperty("http.proxyHost"); // NOI18N
139:
140: if (host == null) {
141: return null;
142: }
143:
144: String portHttp = System.getProperty("http.proxyPort"); // NOI18N
145: int port;
146:
147: try {
148: port = Integer.parseInt(portHttp);
149: } catch (NumberFormatException e) {
150: port = 8080;
151: }
152:
153: Preferences prefs = NbPreferences.root().node(
154: "org/netbeans/core"); // NOI18N
155: boolean useAuth = prefs.getBoolean(USE_PROXY_AUTHENTICATION,
156: false);
157: String auth = "";
158: if (useAuth) {
159: auth = prefs.get(PROXY_AUTHENTICATION_USERNAME, "") + ":"
160: + prefs.get(PROXY_AUTHENTICATION_PASSWORD, "")
161: + '@'; // NOI18N
162: }
163:
164: // Gem requires "http://" in front of the port name if it's not already there
165: if (host.indexOf(':') == -1) {
166: host = "http://" + auth + host; // NOI18N
167: }
168:
169: return host + ":" + port; // NOI18N
170: }
171:
172: public static String findOnPath(final String toFind) {
173: String rubyLib = System.getenv("PATH"); // NOI18N
174: if (rubyLib != null) {
175: String[] paths = rubyLib.split("[:;]"); // NOI18N
176: for (String path : paths) {
177: String result = path + File.separator + toFind;
178: if (new File(result).isFile()) {
179: return result;
180: }
181: }
182: }
183: return null;
184: }
185:
186: public static void preselectPlatform(final JComboBox platforms,
187: final String preferencePlatformIDKey) {
188: String lastPlatformID = Util.getPreferences().get(
189: preferencePlatformIDKey, null);
190: if (lastPlatformID != null) {
191: RubyPlatform platform = RubyPlatformManager
192: .getPlatformByID(lastPlatformID);
193: if (platform != null) {
194: platforms.setSelectedItem(platform);
195: }
196: }
197: }
198:
199: public static void notifyLocalized(Class aClass, String resName,
200: int type, Object... params) {
201: String message = NbBundle.getMessage(aClass, resName, params);
202: if (type == NotifyDescriptor.ERROR_MESSAGE) {
203: LOGGER.severe(message);
204: }
205: DialogDisplayer.getDefault().notify(
206: new NotifyDescriptor.Message(message, type));
207: }
208:
209: public static void notifyLocalized(Class aClass, String resName,
210: Object... params) {
211: notifyLocalized(aClass, resName,
212: NotifyDescriptor.INFORMATION_MESSAGE, params);
213: }
214:
215: /** Returns whether the user confirmed the question or not. */
216: public static boolean confirmLocalized(Class aClass,
217: String resName, Object... params) {
218: String message = NbBundle.getMessage(aClass, resName, params);
219: Object result = DialogDisplayer
220: .getDefault()
221: .notify(
222: new NotifyDescriptor.Confirmation(
223: message,
224: NotifyDescriptor.Confirmation.OK_CANCEL_OPTION));
225: return result.equals(NotifyDescriptor.OK_OPTION);
226: }
227:
228: public static void setFirstPlatformTouch(boolean b) {
229: Util.getPreferences().putBoolean(FIRST_TIME_KEY, b);
230: }
231:
232: static boolean isFirstPlatformTouch() {
233: return Util.getPreferences().getBoolean(FIRST_TIME_KEY, true);
234: }
235:
236: public static String readAsString(final InputStream is)
237: throws IOException {
238: try {
239: ByteArrayOutputStream baos = new ByteArrayOutputStream();
240: FileUtil.copy(is, baos);
241: return baos.toString("UTF-8"); // NOI18N
242: } finally {
243: is.close();
244: }
245: }
246:
247: }
|