1 /* 2 * Copyright (c) 2007, 2015, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.net; 26 27 import java.security.AccessController; 28 import java.security.PrivilegedAction; 29 import sun.security.action.GetPropertyAction; 30 31 /** 32 * This class defines a factory for creating DatagramSocketImpls. It defaults 33 * to creating plain DatagramSocketImpls, but may create other DatagramSocketImpls 34 * by setting the impl.prefix system property. 35 * 36 * For Windows versions lower than Windows Vista a TwoStacksPlainDatagramSocketImpl 37 * is always created. This impl supports IPv6 on these platform where available. 38 * 39 * On Windows platforms greater than Vista that support a dual layer TCP/IP stack 40 * a DualStackPlainDatagramSocketImpl is created for DatagramSockets. For MulticastSockets 41 * a TwoStacksPlainDatagramSocketImpl is always created. This is to overcome the lack 42 * of behavior defined for multicasting over a dual layer socket by the RFC. 43 * 44 * @author Chris Hegarty 45 */ 46 47 class DefaultDatagramSocketImplFactory 48 { 49 private static final Class<?> prefixImplClass; 50 51 /* java.net.preferIPv4Stack */ 52 private static final boolean preferIPv4Stack; 53 54 /* True if exclusive binding is on for Windows */ 55 private static final boolean exclusiveBind; 56 57 static { 58 Class<?> prefixImplClassLocal = null; 59 60 preferIPv4Stack = Boolean.parseBoolean( 61 AccessController.doPrivileged( 62 new GetPropertyAction("java.net.preferIPv4Stack"))); 63 64 String exclBindProp = AccessController.doPrivileged( 65 new GetPropertyAction("sun.net.useExclusiveBind", "")); 66 exclusiveBind = (exclBindProp.isEmpty()) 67 ? true 68 : Boolean.parseBoolean(exclBindProp); 69 70 // impl.prefix 71 String prefix = null; 72 try { 73 prefix = AccessController.doPrivileged( 74 new GetPropertyAction("impl.prefix", null)); 75 if (prefix != null) 76 prefixImplClassLocal = Class.forName("java.net."+prefix+"DatagramSocketImpl"); 77 } catch (Exception e) { 78 System.err.println("Can't find class: java.net." + 79 prefix + 80 "DatagramSocketImpl: check impl.prefix property"); 81 } 82 83 prefixImplClass = prefixImplClassLocal; 84 } 85 86 /** 87 * Creates a new <code>DatagramSocketImpl</code> instance. 88 * 89 * @param isMulticast true if this impl is to be used for a MutlicastSocket 90 * @return a new instance of <code>PlainDatagramSocketImpl</code>. 91 */ 92 static DatagramSocketImpl createDatagramSocketImpl(boolean isMulticast) 93 throws SocketException { 94 if (prefixImplClass != null) { 95 try { 96 return (DatagramSocketImpl) prefixImplClass.newInstance(); 97 } catch (Exception e) { 98 throw new SocketException("can't instantiate DatagramSocketImpl"); 99 } 100 } else { 101 if (!preferIPv4Stack && !isMulticast) 102 return new DualStackPlainDatagramSocketImpl(exclusiveBind); 103 else 104 return new TwoStacksPlainDatagramSocketImpl(exclusiveBind && !isMulticast); 105 } 106 } 107 }