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.util.Properties; 28 import sun.security.action.GetPropertyAction; 29 30 /** 31 * This class defines a factory for creating DatagramSocketImpls. It defaults 32 * to creating plain DatagramSocketImpls, but may create other DatagramSocketImpls 33 * by setting the impl.prefix system property. 34 * 35 * For Windows versions lower than Windows Vista a TwoStacksPlainDatagramSocketImpl 36 * is always created. This impl supports IPv6 on these platform where available. 37 * 38 * On Windows platforms greater than Vista that support a dual layer TCP/IP stack 39 * a DualStackPlainDatagramSocketImpl is created for DatagramSockets. For MulticastSockets 40 * a TwoStacksPlainDatagramSocketImpl is always created. This is to overcome the lack 41 * of behavior defined for multicasting over a dual layer socket by the RFC. 42 * 43 * @author Chris Hegarty 44 */ 45 46 class DefaultDatagramSocketImplFactory 47 { 48 private static final Class<?> prefixImplClass; 49 50 /* java.net.preferIPv4Stack */ 51 private static final boolean preferIPv4Stack; 52 53 /* True if exclusive binding is on for Windows */ 54 private static final boolean exclusiveBind; 55 56 static { 57 Class<?> prefixImplClassLocal = null; 58 59 Properties props = GetPropertyAction.getProperties(); 60 preferIPv4Stack = Boolean.parseBoolean( 61 props.getProperty("java.net.preferIPv4Stack")); 62 63 String exclBindProp = props.getProperty("sun.net.useExclusiveBind", ""); 64 exclusiveBind = (exclBindProp.isEmpty()) 65 ? true 66 : Boolean.parseBoolean(exclBindProp); 67 68 // impl.prefix 69 String prefix = null; 70 try { 71 prefix = props.getProperty("impl.prefix"); 72 if (prefix != null) 73 prefixImplClassLocal = Class.forName("java.net."+prefix+"DatagramSocketImpl"); 74 } catch (Exception e) { 75 System.err.println("Can't find class: java.net." + 76 prefix + 77 "DatagramSocketImpl: check impl.prefix property"); 78 } 79 80 prefixImplClass = prefixImplClassLocal; 81 } 82 83 /** 84 * Creates a new <code>DatagramSocketImpl</code> instance. 85 * 86 * @param isMulticast true if this impl is to be used for a MutlicastSocket 87 * @return a new instance of <code>PlainDatagramSocketImpl</code>. 88 */ 89 static DatagramSocketImpl createDatagramSocketImpl(boolean isMulticast) 90 throws SocketException { 91 if (prefixImplClass != null) { 92 try { 93 return (DatagramSocketImpl) prefixImplClass.newInstance(); 94 } catch (Exception e) { 95 throw new SocketException("can't instantiate DatagramSocketImpl"); 96 } 97 } else { 98 if (!preferIPv4Stack && !isMulticast) 99 return new DualStackPlainDatagramSocketImpl(exclusiveBind); 100 else 101 return new TwoStacksPlainDatagramSocketImpl(exclusiveBind && !isMulticast); 102 } 103 } 104 }