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-2006 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:
042: package org.netbeans.lib.collab.util;
043:
044: import java.util.*;
045: import java.net.*;
046: import java.io.*;
047: import java.lang.reflect.*;
048:
049: /**
050: *
051: *
052: * @param
053: *
054: */
055: public class PlatformUtil {
056: static private boolean is95 = false;
057: static private boolean is98 = false;
058: static private boolean isNT = false;
059: static private boolean is2000 = false;
060: static private boolean is2003 = false;
061: static private boolean isXP = false;
062: static private boolean isos2 = false;
063: static private boolean isunix = false;
064: static private boolean isSun = false;
065: static private boolean isLinux = false;
066: static private boolean ismac = false;
067: static private boolean isHpux = false;
068:
069: static private boolean is1_1 = false;
070: static private boolean is1_2 = false;
071: static private boolean is1_3 = false;
072: static private boolean is1_4 = false;
073: static private boolean is1_5 = false;
074: static private boolean isJava2 = true;
075:
076: static private boolean isJview = false;
077: static public boolean isJview40 = false;
078:
079: final static private String WIN95_ID = "windows 95";
080: final static private String WIN98_ID = "windows 98";
081: final static private String WINNT_ID = "windows nt";
082: final static private String WIN2K_ID = "windows 2000";
083: final static private String WIN3K_ID = "windows 2003";
084: final static private String WINXP_ID = "windows xp";
085: final static private String OS2_ID = "os/2";
086: final static private String MAC_ID = "mac os";
087: final static private String UNIX_ID = "unix";
088: final static private String SUN_ID = "SunOS";
089: final static private String LINUX_ID = "Linux";
090: final static private String HPUX_ID = "HP-UX";
091:
092: /**
093: * @param
094: */
095: static {
096: String t = getOSName();
097: //System.out.println("<< OPERATING SYSTEM >> " + t);
098: if (t.equalsIgnoreCase(WIN95_ID)) {
099: if (getOSVersion().equals("4.0"))
100: is95 = true;
101: else
102: is98 = true;
103: } else if (t.equalsIgnoreCase(WIN98_ID)) {
104: is98 = true;
105: } else if (t.equalsIgnoreCase(WINNT_ID)) {
106: if (getOSVersion().equals("5.0"))
107: is2000 = true;
108: else
109: isNT = true;
110: } else if (t.equalsIgnoreCase(WIN2K_ID)) {
111: is2000 = true;
112: } else if (t.equalsIgnoreCase(WIN3K_ID)) {
113: is2003 = true;
114:
115: } else if (t.equalsIgnoreCase(WINXP_ID)) {
116: isXP = true;
117: } else if (t.equalsIgnoreCase(OS2_ID)) {
118: isos2 = true;
119: } else if (t.equalsIgnoreCase(SUN_ID)) {
120: isSun = true;
121: } else if (t.equalsIgnoreCase(HPUX_ID)) {
122: isHpux = true;
123: } else if (t.toLowerCase().startsWith(MAC_ID)) {
124: ismac = true;
125: } else if (t.equalsIgnoreCase(LINUX_ID)) {
126: isLinux = true;
127: }
128:
129: t = getJavaVersion();
130: if (t.startsWith("1.5")) {
131: is1_5 = true;
132: } else if (t.startsWith("1.4")) {
133: is1_4 = true;
134: } else if (t.startsWith("1.3")) {
135: is1_3 = true;
136: } else if (t.startsWith("1.2")) {
137: is1_2 = true;
138: } else if (t.startsWith("1.1")) {
139: isJava2 = false;
140: is1_1 = true;
141: }
142:
143: t = System.getProperty("java.vendor");
144: if (t.startsWith("Microsoft")) {
145: isJview = true;
146: getMsVersion();
147: }
148: t = null;
149: }
150:
151: /**
152: * If on MS platform, get the JView Version
153: */
154: final private static String getMsVersion() {
155: StringBuffer ret = new StringBuffer();
156:
157: try {
158: Class c = Class.forName("com.ms.util.SystemVersionManager");
159: Method m = c.getDeclaredMethod("getVMVersion", null);
160: Object o = null;
161: Properties p = (Properties) m.invoke(o, new Object[] {});
162: Enumeration e = p.keys();
163: while (e.hasMoreElements()) {
164: String key = (String) e.nextElement();
165: ret.append("MS ");
166: ret.append(key);
167: ret.append(" :: ");
168: String tmp = (String) p.getProperty(key);
169: if (tmp.indexOf("3240") >= 0) {
170: isJview40 = true;
171: }
172: ret.append(tmp);
173: ret.append("\n");
174: }
175: e = null;
176: } catch (Exception e) {
177: System.out.println("getMsVersion:" + e);
178: }
179:
180: return ret.toString();
181: }
182:
183: /**
184: *
185: *
186: * @return program working directory
187: */
188: static final public String getUserDir() {
189: return System.getProperty("user.dir");
190: }
191:
192: /**
193: * Provides home directory for the system account running this
194: * jvm instance. Equivalent to getting the value of the
195: * user.home JVM variable.
196: *
197: * @return home directory
198: */
199: static final public String getHomeDir() {
200: return System.getProperty("user.home");
201: }
202:
203: /**
204: *
205: *
206: * @return operating system name.
207: */
208: static final public String getOSName() {
209: return System.getProperty("os.name");
210: }
211:
212: static final public String getNoSpaceOSName() {
213: if (isWin())
214: return "Windows";
215: if (isMac())
216: return "MacOS";
217: return System.getProperty("os.name");
218: }
219:
220: /**
221: *
222: *
223: * @param
224: */
225: static final public String getOSArch() {
226: String arch = System.getProperty("os.arch");
227: if (isLinux()) {
228: // mismatch between build system (x86) and os.arch (i386);
229: if (arch.endsWith("86"))
230: arch = "x86";
231: } else if (isSun()) {
232: if (arch.endsWith("86"))
233: arch = "i386";
234: }
235: return arch;
236: }
237:
238: /**
239: *
240: *
241: * @param
242: */
243: static final public String getOSVersion() {
244: return System.getProperty("os.version");
245: }
246:
247: /**
248: *
249: *
250: * @param
251: */
252: static final public String getJavaVersion() {
253: return System.getProperty("java.version");
254: }
255:
256: /**
257: *
258: *
259: * @param
260: */
261: static final public String getUserName() {
262: return System.getProperty("user.name");
263: }
264:
265: /**
266: *
267: *
268: * @param
269: */
270: static final public String getVMVersion() {
271: String ret = System.getProperty("java.vm.version");
272: if (ret == null)
273: return getMsVersion();
274: else
275: return ret;
276: }
277:
278: /**
279: *
280: *
281: * @param
282: */
283: static final public String getVMInfo() {
284: String ret = System.getProperty("java.vm.info");
285: if (ret == null)
286: return getMsVersion();
287: else
288: return ret;
289: }
290:
291: /**
292: *
293: *
294: * @param
295: */
296: static final public boolean isWin() {
297: return is95 || is98 || isNT || is2000 || isXP || is2003;
298: }
299:
300: /**
301: *
302: *
303: * @param
304: */
305: static final public boolean is98() {
306: return is98;
307: }
308:
309: /**
310: *
311: *
312: * @param
313: */
314: static final public boolean is95() {
315: return is95;
316: }
317:
318: /**
319: *
320: *
321: * @param
322: */
323: static final public boolean isNT() {
324: return isNT;
325: }
326:
327: /**
328: *
329: *
330: * @param
331: */
332: static final public boolean is2000() {
333: return is2000;
334: }
335:
336: /**
337: *
338: *
339: * @param
340: */
341: static final public boolean is2003() {
342: return is2003;
343: }
344:
345: /**
346: *
347: *
348: * @param
349: */
350: static final public boolean isOS2() {
351: return isos2;
352: }
353:
354: /**
355: *
356: *
357: * @param
358: */
359: static final public boolean isMac() {
360: return ismac;
361: }
362:
363: /**
364: *
365: *
366: * @param
367: */
368: static final public boolean isSun() {
369: return isSun;
370: }
371:
372: /**
373: *
374: *
375: * @param
376: */
377: static final public boolean isHpux() {
378: return isHpux;
379: }
380:
381: /**
382: *
383: *
384: * @param
385: */
386: static final public boolean isLinux() {
387: return isLinux;
388: }
389:
390: /**
391: *
392: *
393: * @param
394: */
395: static final public boolean isUnix() {
396: return (isSun || isLinux || isHpux);
397: }
398:
399: /**
400: *
401: *
402: * @param
403: */
404: static final public boolean isJava2() {
405: return (isJava2);
406: }
407:
408: /**
409: *
410: *
411: * @param
412: */
413: static final public boolean isJava1() {
414: return is1_1;
415: }
416:
417: /**
418: *
419: *
420: * @param
421: */
422: static final public boolean isJview() {
423: return isJview;
424: }
425:
426: /**
427: *
428: *
429: * @param
430: */
431: static final public boolean isJDK1_4() {
432: return is1_4;
433: }
434:
435: /**
436: *
437: *
438: * @param
439: */
440: static final public boolean isJDK1_5() {
441: return is1_5;
442: }
443:
444: /**
445: *
446: *
447: * @param
448: */
449: static final public boolean isJDK1_3() {
450: return is1_3;
451: }
452:
453: /**
454: *
455: *
456: * @param
457: */
458: static final public boolean isJDK1_2() {
459: return is1_2;
460: }
461:
462: /**
463: * @param
464: */
465: static final public boolean isJDK1_1() {
466: return is1_1;
467: }
468:
469: /**
470: * This was added as a fix for win98 platforms -
471: * Some of the platforms locked up completely when creating a new socket
472: * Sun insisted problem was fixed, but was still occurring here
473: * This seemed to stop the problem
474: */
475: final static public void loadWinSock() {
476: if (isJview)
477: return;
478: try {
479: System.out.println("%% LOAD WINSOCK");
480: System.loadLibrary("wsock32");
481: getLocalHost();
482: } catch (Throwable e) {
483: System.out.println("loadWinSock:" + e);
484: }
485: }
486:
487: /**
488: *
489: *
490: * @param
491: */
492: final static public String viewSystemInfo() {
493: StringBuffer s = new StringBuffer("System Properties!! \n");
494: s.append("\n VM Version ");
495: s.append(getVMVersion());
496: s.append("\n VM Info ");
497: s.append(getVMInfo());
498: s.append("\n");
499: Enumeration e = System.getProperties().keys();
500: String tmp = "";
501: while (e.hasMoreElements()) {
502: tmp = (String) e.nextElement();
503: s.append(tmp + " = " + System.getProperty(tmp) + "\n");
504: }
505: tmp = null;
506: e = null;
507: return s.toString();
508: }
509:
510: /**
511: *
512: *
513: * @param
514: */
515: final static public String getLocalHost() {
516: try {
517: System.out.println(".*. Getting Local Host .*.");
518: InetAddress addr = InetAddress.getLocalHost();
519: return addr.toString();
520: } catch (Exception e) {
521: System.out.println("getLocalHost:" + e);
522: }
523: return "";
524: }
525:
526: /**
527: *
528: *
529: * @param
530: */
531: //For Debugging -- forces system to be viewed as Windows
532: final static public void forceWin() {
533: is98 = true;
534: isos2 = false;
535: }
536:
537: /**
538: *
539: *
540: * @param
541: */
542: //For Debugging -- forces system to be viewed as OS2
543: final static public void forceOS2() {
544: isos2 = true;
545: is98 = false;
546: isNT = false;
547: is95 = false;
548: ismac = false;
549: isunix = false;
550: }
551:
552: /**
553: *
554: *
555: * @param
556: */
557: //For Debugging -- forces system to be viewed as MAC
558: final static public void forceMac() {
559: isos2 = false;
560: is98 = false;
561: isNT = false;
562: is95 = false;
563: ismac = true;
564: isunix = false;
565: }
566:
567: /**
568: *
569: *
570: * @param
571: */
572: //For Debugging -- forces system to be viewed as UNIX
573: final static public void forceUnix(boolean b) {
574: isos2 = false;
575: is98 = false;
576: isNT = false;
577: is95 = false;
578: ismac = false;
579: isunix = true;
580: }
581:
582: /**
583: *
584: *
585: * @param
586: */
587: //For Debugging -- forces system to view JDK as version 2
588: final static public void forceJava2(boolean b) {
589: is1_2 = b;
590: is1_1 = !b;
591: }
592:
593: /**
594: *
595: *
596: * @param
597: */
598: //For Debugging -- forces system to view JDK as NOT version 2
599: final static public void forceJava1(boolean b) {
600: is1_1 = b;
601: is1_2 = !b;
602: }
603:
604: // renameTo method fails on Windows if the dest file already exists
605: // Hence fake the creation of a temp file on this platform
606: final static public File getTempFile(File f) {
607: if (isWin()) {
608: return f;
609: } else {
610: return new File(f.getAbsolutePath() + ".tmp");
611: }
612: }
613:
614: final static public boolean renameTempFile(File temp, File f) {
615: if (!isWin()) {
616: return temp.renameTo(f);
617: }
618: return true;
619: }
620:
621: }
|