J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * |
| 26 | * |
| 27 | * A collection of utility methods used by the SelectorProvider.inheritedChannel |
| 28 | * unit tests. |
| 29 | */ |
| 30 | import java.net.*; |
| 31 | import java.io.*; |
| 32 | import java.nio.channels.*; |
| 33 | import java.lang.reflect.*; |
| 34 | |
| 35 | // dependency on Sun implementation |
| 36 | import sun.nio.ch.*; |
| 37 | |
| 38 | public class Util { |
| 39 | |
| 40 | private static Object get(String className, String fieldName, Object o) throws Exception { |
| 41 | Class cl = Class.forName(className); |
| 42 | Field fld = cl.getDeclaredField(fieldName); |
| 43 | fld.setAccessible(true); |
| 44 | return fld.get(o); |
| 45 | } |
| 46 | |
| 47 | private static int fdVal(FileDescriptor fdObj) throws Exception { |
| 48 | Object fdVal = get("java.io.FileDescriptor", "fd", fdObj); |
| 49 | return ((Integer)fdVal).intValue(); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Return the file descriptor underlying a given SocketChannel |
| 54 | */ |
| 55 | public static int getFD(SocketChannel sc) { |
| 56 | try { |
| 57 | Object fdObj = get("sun.nio.ch.SocketChannelImpl", "fd", sc); |
| 58 | return fdVal((FileDescriptor)fdObj); |
| 59 | } catch (Exception x) { |
| 60 | x.printStackTrace(); |
| 61 | throw new InternalError(x.getMessage()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Return the file descriptor underlying a given ServerSocketChannel |
| 67 | */ |
| 68 | public static int getFD(ServerSocketChannel ssc) { |
| 69 | try { |
| 70 | Object fdObj = get("sun.nio.ch.ServerSocketChannelImpl", "fd", ssc); |
| 71 | return fdVal((FileDescriptor)fdObj); |
| 72 | } catch (Exception x) { |
| 73 | x.printStackTrace(); |
| 74 | throw new InternalError(x.getMessage()); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Return the file descriptor underlying a given DatagramChannel |
| 80 | */ |
| 81 | public static int getFD(DatagramChannel dc) { |
| 82 | try { |
| 83 | Object fdObj = get("sun.nio.ch.DatagramChannelImpl", "fd", dc); |
| 84 | return fdVal((FileDescriptor)fdObj); |
| 85 | } catch (Exception x) { |
| 86 | x.printStackTrace(); |
| 87 | throw new InternalError(x.getMessage()); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Return the "java" command and any initial arguments to start the runtime |
| 93 | * in the current configuration. |
| 94 | * |
| 95 | * Typically it will return something like :- |
| 96 | * cmd[0] = "/usr/local/java/solaris-sparc/bin/java" |
| 97 | * or |
| 98 | * cmd[0] = "/usr/local/java/solaris-sparc/bin/sparcv9/java" |
| 99 | * cmd[1] = "-d64" |
| 100 | */ |
| 101 | public static String[] javaCommand() { |
| 102 | String exe = System.getProperty("java.home") + File.separator + "bin" + |
| 103 | File.separator; |
| 104 | String arch = System.getProperty("os.arch"); |
| 105 | if (arch.equals("sparcv9")) { |
| 106 | String cmd[] = new String[2]; |
| 107 | cmd[0] = exe + "sparcv9/java"; |
| 108 | cmd[1] = "-d64"; |
| 109 | return cmd; |
| 110 | } else { |
| 111 | String cmd[] = new String[1]; |
| 112 | cmd[0] = exe += "java"; |
| 113 | return cmd; |
| 114 | } |
| 115 | } |
| 116 | } |