1a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot/* 2a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. 3a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * 4a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * Redistribution and use in source and binary forms, with or without 5a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * modification, are permitted provided that the following conditions 6a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * are met: 7a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * 8a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * - Redistributions of source code must retain the above copyright 9a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * notice, this list of conditions and the following disclaimer. 10a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * 11a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * - Redistributions in binary form must reproduce the above copyright 12a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * notice, this list of conditions and the following disclaimer in the 13a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * documentation and/or other materials provided with the distribution. 14a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * 15a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * - Neither the name of Oracle nor the names of its 16a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * contributors may be used to endorse or promote products derived 17a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * from this software without specific prior written permission. 18a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * 19a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot */ 31a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 32a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot/* 33a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * This source code is provided to illustrate the usage of a given feature 34a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * or technique and has been deliberately simplified. Additional steps 35a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * required for a production-quality application, such as security checks, 36a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * input validation and proper error handling, might not be present in 37a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * this sample code. 38a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot */ 39a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 40a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 41a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robotimport java.io.*; 42a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robotimport java.nio.channels.*; 43a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robotimport javax.net.ssl.*; 44a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 45a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot/** 46a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * A single threaded Handler that performs accepts SocketChannels and 47a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * registers the Channels with the read/write Selector. 48a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * 49a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * @author Mark Reinhold 50a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot * @author Brad R. Wetmore 51a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot */ 52a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robotclass AcceptHandler implements Handler { 53a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 54a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot private ServerSocketChannel channel; 55a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot private Dispatcher dsp; 56a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 57a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot private SSLContext sslContext; 58a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 59a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot AcceptHandler(ServerSocketChannel ssc, Dispatcher dsp, 60a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot SSLContext sslContext) { 61a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot channel = ssc; 62a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot this.dsp = dsp; 63a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot this.sslContext = sslContext; 64a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot } 65a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 66a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot public void handle(SelectionKey sk) throws IOException { 67a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 68a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot if (!sk.isAcceptable()) 69a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot return; 70a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 71a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot SocketChannel sc = channel.accept(); 72a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot if (sc == null) { 73a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot return; 74a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot } 75a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 76a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot ChannelIO cio = (sslContext != null ? 77a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot ChannelIOSecure.getInstance( 78a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot sc, false /* non-blocking */, sslContext) : 79a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot ChannelIO.getInstance( 80a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot sc, false /* non-blocking */)); 81a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot 82a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot RequestHandler rh = new RequestHandler(cio); 83a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot dsp.register(cio.getSocketChannel(), SelectionKey.OP_READ, rh); 84a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot } 85a782115c262f0bc08ed2b8ffcd7d85c72c894adandroid-build-team Robot} 86