1/*
2 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * A collection of utility methods used by the SelectorProvider.inheritedChannel
26 * unit tests.
27 */
28import java.io.File;
29import java.io.FileDescriptor;
30import java.lang.reflect.Field;
31import java.nio.channels.DatagramChannel;
32import java.nio.channels.ServerSocketChannel;
33import java.nio.channels.SocketChannel;
34
35public class Util {
36
37    private static Object get(String className, String fieldName, Object o) throws Exception {
38        Class cl = Class.forName(className);
39        Field fld = cl.getDeclaredField(fieldName);
40        fld.setAccessible(true);
41        return fld.get(o);
42    }
43
44    private static int fdVal(FileDescriptor fdObj) throws Exception {
45        Object fdVal = get("java.io.FileDescriptor", "fd", fdObj);
46        return ((Integer)fdVal).intValue();
47    }
48
49    /*
50     * Return the file descriptor underlying a given SocketChannel
51     */
52    public static int getFD(SocketChannel sc) {
53        try {
54            Object fdObj = get("sun.nio.ch.SocketChannelImpl", "fd", sc);
55            return fdVal((FileDescriptor)fdObj);
56        } catch (Exception x) {
57            x.printStackTrace();
58            throw new InternalError(x.getMessage());
59        }
60    }
61
62    /*
63     * Return the file descriptor underlying a given ServerSocketChannel
64     */
65    public static int getFD(ServerSocketChannel ssc) {
66        try {
67            Object fdObj = get("sun.nio.ch.ServerSocketChannelImpl", "fd", ssc);
68            return fdVal((FileDescriptor)fdObj);
69        } catch (Exception x) {
70            x.printStackTrace();
71            throw new InternalError(x.getMessage());
72        }
73    }
74
75    /*
76     * Return the file descriptor underlying a given DatagramChannel
77     */
78    public static int getFD(DatagramChannel dc) {
79        try {
80            Object fdObj = get("sun.nio.ch.DatagramChannelImpl", "fd", dc);
81            return fdVal((FileDescriptor)fdObj);
82        } catch (Exception x) {
83            x.printStackTrace();
84            throw new InternalError(x.getMessage());
85        }
86    }
87
88    /*
89     * Return the "java" command and any initial arguments to start the runtime
90     * in the current configuration.
91     */
92    public static String javaCommand() {
93        return System.getProperty("java.home") + File.separator + "bin" +
94            File.separator + "java";
95    }
96}
97