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 26 package com.sun.net.httpserver.spi; 27 28 import java.io.FileDescriptor; 29 import java.io.IOException; 30 import java.net.*; 31 import java.security.AccessController; 32 import java.security.PrivilegedAction; 33 import java.util.Iterator; 34 import java.util.ServiceLoader; 35 import sun.misc.ServiceConfigurationError; 36 import sun.security.action.GetPropertyAction; 37 import com.sun.net.httpserver.*; 38 39 /** 40 * Service provider class for HttpServer. 41 * Sub-classes of HttpServerProvider provide an implementation of {@link HttpServer} and 42 * associated classes. Applications do not normally use this class. 43 * See {@link #provider()} for how providers are found and loaded. 44 */ 45 public abstract class HttpServerProvider { 46 47 /** 48 * creates a HttpServer from this provider 49 * @param addr the address to bind to. May be <code>null</code> 50 * @param backlog the socket backlog. A value of <code>zero</code> means the systems default 51 */ 52 public abstract HttpServer createHttpServer (InetSocketAddress addr, int backlog) throws IOException; 53 54 /** 55 * creates a HttpsServer from this provider 56 * @param addr the address to bind to. May be <code>null</code> 57 * @param backlog the socket backlog. A value of <code>zero</code> means the systems default 58 */ 59 public abstract HttpsServer createHttpsServer (InetSocketAddress addr, int backlog) throws IOException; 60 61 62 63 private static final Object lock = new Object(); 64 private static HttpServerProvider provider = null; 65 66 /** 67 * Initializes a new instance of this class. </p> 68 * 69 * @throws SecurityException 70 * If a security manager has been installed and it denies 71 * {@link RuntimePermission}<tt>("httpServerProvider")</tt> 72 */ 73 protected HttpServerProvider() { 74 SecurityManager sm = System.getSecurityManager(); 75 if (sm != null) 76 sm.checkPermission(new RuntimePermission("httpServerProvider")); 77 } 78 79 private static boolean loadProviderFromProperty() { 80 String cn = System.getProperty("com.sun.net.httpserver.HttpServerProvider"); 81 if (cn == null) 82 return false; 83 try { 84 Class<?> c = Class.forName(cn, true, 85 ClassLoader.getSystemClassLoader()); 86 provider = (HttpServerProvider)c.newInstance(); 87 return true; 88 } catch (ClassNotFoundException | 89 IllegalAccessException | 90 InstantiationException | 91 SecurityException x) { 92 throw new ServiceConfigurationError(x); 93 } 94 } 95 96 private static boolean loadProviderAsService() { 97 Iterator<HttpServerProvider> i = 98 ServiceLoader.load(HttpServerProvider.class, 99 ClassLoader.getSystemClassLoader()) 100 .iterator(); 101 for (;;) { 102 try { 103 if (!i.hasNext()) 104 return false; 105 provider = i.next(); 106 return true; 107 } catch (ServiceConfigurationError sce) { 108 if (sce.getCause() instanceof SecurityException) { 109 // Ignore the security exception, try the next provider 110 continue; 111 } 112 throw sce; 113 } 114 } 115 } 116 117 /** 118 * Returns the system wide default HttpServerProvider for this invocation of 119 * the Java virtual machine. 120 * 121 * <p> The first invocation of this method locates the default provider 122 * object as follows: </p> 123 * 124 * <ol> 125 * 126 * <li><p> If the system property 127 * <tt>com.sun.net.httpserver.HttpServerProvider</tt> is defined then it is 128 * taken to be the fully-qualified name of a concrete provider class. 129 * The class is loaded and instantiated; if this process fails then an 130 * unspecified unchecked error or exception is thrown. </p></li> 131 * 132 * <li><p> If a provider class has been installed in a jar file that is 133 * visible to the system class loader, and that jar file contains a 134 * provider-configuration file named 135 * <tt>com.sun.net.httpserver.HttpServerProvider</tt> in the resource 136 * directory <tt>META-INF/services</tt>, then the first class name 137 * specified in that file is taken. The class is loaded and 138 * instantiated; if this process fails then an unspecified unchecked error or exception is 139 * thrown. </p></li> 140 * 141 * <li><p> Finally, if no provider has been specified by any of the above 142 * means then the system-default provider class is instantiated and the 143 * result is returned. </p></li> 144 * 145 * </ol> 146 * 147 * <p> Subsequent invocations of this method return the provider that was 148 * returned by the first invocation. </p> 149 * 150 * @return The system-wide default HttpServerProvider 151 */ 152 public static HttpServerProvider provider () { 153 synchronized (lock) { 154 if (provider != null) 155 return provider; 156 return (HttpServerProvider)AccessController 157 .doPrivileged(new PrivilegedAction<Object>() { 158 public Object run() { 159 if (loadProviderFromProperty()) | 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 26 package com.sun.net.httpserver.spi; 27 28 import java.io.IOException; 29 import java.net.*; 30 import java.security.AccessController; 31 import java.security.PrivilegedAction; 32 import java.util.Iterator; 33 import java.util.ServiceLoader; 34 import java.util.ServiceConfigurationError; 35 import com.sun.net.httpserver.*; 36 37 /** 38 * Service provider class for HttpServer. 39 * Sub-classes of HttpServerProvider provide an implementation of 40 * {@link HttpServer} and associated classes. Applications do not normally use 41 * this class. See {@link #provider()} for how providers are found and loaded. 42 */ 43 public abstract class HttpServerProvider { 44 45 /** 46 * creates a HttpServer from this provider 47 * 48 * @param addr 49 * the address to bind to. May be {@code null} 50 * 51 * @param backlog 52 * the socket backlog. A value of {@code zero} means the systems default 53 */ 54 public abstract HttpServer createHttpServer(InetSocketAddress addr, 55 int backlog) 56 throws IOException; 57 58 /** 59 * creates a HttpsServer from this provider 60 * 61 * @param addr 62 * the address to bind to. May be {@code null} 63 * 64 * @param backlog 65 * the socket backlog. A value of {@code zero} means the systems default 66 */ 67 public abstract HttpsServer createHttpsServer(InetSocketAddress addr, 68 int backlog) 69 throws IOException; 70 71 private static final Object lock = new Object(); 72 private static HttpServerProvider provider = null; 73 74 /** 75 * Initializes a new instance of this class. 76 * 77 * @throws SecurityException 78 * If a security manager has been installed and it denies 79 * {@link RuntimePermission}{@code("httpServerProvider")} 80 */ 81 protected HttpServerProvider() { 82 SecurityManager sm = System.getSecurityManager(); 83 if (sm != null) 84 sm.checkPermission(new RuntimePermission("httpServerProvider")); 85 } 86 87 private static boolean loadProviderFromProperty() { 88 String cn = System.getProperty("com.sun.net.httpserver.HttpServerProvider"); 89 if (cn == null) 90 return false; 91 try { 92 Class<?> c = Class.forName(cn, true, 93 ClassLoader.getSystemClassLoader()); 94 provider = (HttpServerProvider)c.newInstance(); 95 return true; 96 } catch (ClassNotFoundException | 97 IllegalAccessException | 98 InstantiationException | 99 SecurityException x) { 100 throw new ServiceConfigurationError(null, x); 101 } 102 } 103 104 private static boolean loadProviderAsService() { 105 Iterator<HttpServerProvider> i = 106 ServiceLoader.load(HttpServerProvider.class, 107 ClassLoader.getSystemClassLoader()) 108 .iterator(); 109 for (;;) { 110 try { 111 if (!i.hasNext()) 112 return false; 113 provider = i.next(); 114 return true; 115 } catch (ServiceConfigurationError sce) { 116 if (sce.getCause() instanceof SecurityException) { 117 // Ignore the security exception, try the next provider 118 continue; 119 } 120 throw sce; 121 } 122 } 123 } 124 125 /** 126 * Returns the system wide default HttpServerProvider for this invocation of 127 * the Java virtual machine. 128 * 129 * <p> The first invocation of this method locates the default provider 130 * object as follows: </p> 131 * 132 * <ol> 133 * 134 * <li><p> If the system property 135 * {@code com.sun.net.httpserver.HttpServerProvider} is defined then it 136 * is taken to be the fully-qualified name of a concrete provider class. 137 * The class is loaded and instantiated; if this process fails then an 138 * unspecified unchecked error or exception is thrown. </p></li> 139 * 140 * <li><p> If a provider class has been installed in a jar file that is 141 * visible to the system class loader, and that jar file contains a 142 * provider-configuration file named 143 * {@code com.sun.net.httpserver.HttpServerProvider} in the resource 144 * directory <tt>META-INF/services</tt>, then the first class name 145 * specified in that file is taken. The class is loaded and 146 * instantiated; if this process fails then an unspecified unchecked error 147 * or exception is thrown. </p></li> 148 * 149 * <li><p> Finally, if no provider has been specified by any of the above 150 * means then the system-default provider class is instantiated and the 151 * result is returned. </p></li> 152 * 153 * </ol> 154 * 155 * <p> Subsequent invocations of this method return the provider that was 156 * returned by the first invocation. </p> 157 * 158 * @return The system-wide default HttpServerProvider 159 */ 160 public static HttpServerProvider provider () { 161 synchronized (lock) { 162 if (provider != null) 163 return provider; 164 return (HttpServerProvider)AccessController 165 .doPrivileged(new PrivilegedAction<Object>() { 166 public Object run() { 167 if (loadProviderFromProperty()) |