KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > NIOWorkarounds


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net;
5
6 import com.tc.util.runtime.Os;
7
8 import java.io.IOException JavaDoc;
9 import java.util.Properties JavaDoc;
10 import java.util.regex.Matcher JavaDoc;
11 import java.util.regex.Pattern JavaDoc;
12
13 public class NIOWorkarounds {
14
15   private NIOWorkarounds() {
16     //
17
}
18
19   public static boolean windowsWritevWorkaround(IOException JavaDoc ioe) {
20     final String JavaDoc err = ioe.getMessage();
21     if (null != err) {
22       // java.io.IOException can be thrown here, but it should be ignored on windows
23
// http://developer.java.sun.com/developer/bugParade/bugs/4854354.html
24
if (err.equals("A non-blocking socket operation could not be completed immediately")) {
25         if (Os.isWindows()) { return true; }
26       }
27     }
28
29     return false;
30   }
31
32   public static boolean linuxSelectWorkaround(IOException JavaDoc ioe) {
33     // workaround bug in Sun VM when select() gets interrupted
34
// see sun bug 4504001 (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4504001)
35

36     if (Os.isLinux()) {
37       String JavaDoc msg = ioe.getMessage();
38       if ("Interrupted system call".equals(msg)) { return true; }
39     }
40
41     return false;
42   }
43
44   public static void solaris10Workaround() {
45     boolean workaround = solaris10Workaround(System.getProperties());
46     if (workaround) {
47       String JavaDoc prev = System.getProperty("java.nio.channels.spi.SelectorProvider");
48       System.setProperty("java.nio.channels.spi.SelectorProvider", "sun.nio.ch.PollSelectorProvider");
49
50       String JavaDoc msg = "\nWARNING: Terracotta is forcing the use of poll based NIO selector to workaround Sun bug 6322825\n";
51       if (prev != null) {
52         msg += "This overrides the previous value of " + prev + "\n";
53       }
54       System.err.println(msg.toString());
55     }
56   }
57
58   /**
59    * see LKC-2436 and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6322825
60    *
61    * @return true if the workaround should be applied
62    */

63   static boolean solaris10Workaround(Properties JavaDoc props) {
64     String JavaDoc vendor = props.getProperty("java.vendor", "");
65     String JavaDoc osName = props.getProperty("os.name", "");
66     String JavaDoc osVersion = props.getProperty("os.version", "");
67     String JavaDoc javaVersion = props.getProperty("java.version", "");
68
69     if (vendor.toLowerCase().startsWith("sun") && "SunOS".equals(osName) && "5.10".equals(osVersion)) {
70       if (javaVersion.matches("^1\\.[12345]\\..*")) { // bug is fixed in 1.6+ (supposedly)
71
// Bug is fixed in 1.5.0_08+ (again, supposedly)
72
Pattern JavaDoc p = Pattern.compile("^1\\.5\\.0_(\\d\\d)$");
73         Matcher JavaDoc m = p.matcher(javaVersion);
74         if (m.matches()) {
75           String JavaDoc minorRev = m.group(1);
76           int ver = Integer.parseInt(minorRev);
77           if (ver >= 8) { return false; }
78         }
79
80         return true;
81       }
82     }
83     return false;
84   }
85
86   // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6427854
87
public static boolean selectorOpenRace(NullPointerException JavaDoc npe) {
88     StackTraceElement JavaDoc source = npe.getStackTrace()[0];
89     if (source.getClassName().equals("sun.nio.ch.Util") && source.getMethodName().equals("atBugLevel")) { return true; }
90     return false;
91   }
92
93   public static void main(String JavaDoc args[]) {
94     NIOWorkarounds.solaris10Workaround();
95   }
96
97 }
98
Popular Tags