/*
 * Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package org.glassfish.grizzly;

import org.glassfish.grizzly.nio.NIOTransport;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.Channel;
import java.util.Random;

Since:2.2.19
/** * @since 2.2.19 */
public abstract class AbstractBindingHandler implements SocketBinder { protected static final Random RANDOM = new Random(); protected final NIOTransport transport; protected Processor processor; protected ProcessorSelector processorSelector; // ------------------------------------------------------------ Constructors public AbstractBindingHandler(final NIOTransport transport) { this.transport = transport; this.processor = transport.getProcessor(); this.processorSelector = transport.getProcessorSelector(); } // ---------------------------------------------------------- Public Methods
Get the default Processor to process IOEvent, occurring on connection phase.
Returns:the default Processor to process IOEvent, occurring on connection phase.
/** * Get the default {@link Processor} to process {@link IOEvent}, occurring * on connection phase. * * @return the default {@link Processor} to process {@link IOEvent}, * occurring on connection phase. */
public Processor getProcessor() { return processor; }
Set the default Processor to process IOEvent, occurring on connection phase.
Params:
  • processor – the default Processor to process IOEvent, occurring on connection phase.
/** * Set the default {@link Processor} to process {@link IOEvent}, occurring * on connection phase. * * @param processor the default {@link Processor} to process * {@link IOEvent}, occurring on connection phase. */
public void setProcessor(Processor processor) { this.processor = processor; }
Gets the default ProcessorSelector, which will be used to get Processor to process I/O events, occurring on connection phase.
Returns:the default ProcessorSelector, which will be used to get Processor to process I/O events, occurring on connection phase.
/** * Gets the default {@link ProcessorSelector}, which will be used to get * {@link Processor} to process I/O events, occurring on connection phase. * * @return the default {@link ProcessorSelector}, which will be used to get * {@link Processor} to process I/O events, occurring on connection phase. */
public ProcessorSelector getProcessorSelector() { return processorSelector; }
Sets the default ProcessorSelector, which will be used to get Processor to process I/O events, occurring on connection phase.
Params:
  • processorSelector – the default ProcessorSelector, which will be used to get Processor to process I/O events, occurring on connection phase.
/** * Sets the default {@link ProcessorSelector}, which will be used to get * {@link Processor} to process I/O events, occurring on connection phase. * * @param processorSelector the default {@link ProcessorSelector}, * which will be used to get {@link Processor} to process I/O events, * occurring on connection phase. */
public void setProcessorSelector(final ProcessorSelector processorSelector) { this.processorSelector = processorSelector; } // ----------------------------------------------- Methods from SocketBinder
{@inheritDoc}
/** * {@inheritDoc} */
@Override public Connection bind(final int port) throws IOException { return bind(new InetSocketAddress(port)); }
{@inheritDoc}
/** * {@inheritDoc} */
@Override public Connection bind(final String host, final int port) throws IOException { return bind(new InetSocketAddress(host, port)); }
{@inheritDoc}
/** * {@inheritDoc} */
@Override public Connection bind(final String host, final int port, final int backlog) throws IOException { return bind(new InetSocketAddress(host, port), backlog); }
{@inheritDoc}
/** * {@inheritDoc} */
@Override public Connection bind(final String host, final PortRange portRange, final int backlog) throws IOException { IOException ioException; final int lower = portRange.getLower(); final int range = portRange.getUpper() - lower + 1; int offset = RANDOM.nextInt(range); final int start = offset; do { final int port = lower + offset; try { return bind(host, port, backlog); } catch (IOException e) { ioException = e; } offset = (offset + 1) % range; } while (offset != start); throw ioException; }
This operation is not supported by implementations of AbstractBindingHandler.
Throws:
/** * This operation is not supported by implementations of {@link AbstractBindingHandler}. * * @throws UnsupportedOperationException */
@Override public final void unbindAll() { throw new UnsupportedOperationException(); } // ------------------------------------------------------- Protected Methods @SuppressWarnings("unchecked") protected <T> T getSystemInheritedChannel(final Class<?> channelType) throws IOException { final Channel inheritedChannel = System.inheritedChannel(); if (inheritedChannel == null) { throw new IOException("Inherited channel is not set"); } if (!(channelType.isInstance(inheritedChannel))) { throw new IOException("Inherited channel is not " + channelType.getName() + ", but " + inheritedChannel.getClass().getName()); } return (T) inheritedChannel; } // ----------------------------------------------------------- Inner Classes
Builder
Type parameters:
  • <E> –
/** * Builder * * @param <E> */
@SuppressWarnings("unchecked") public abstract static class Builder<E extends Builder> { protected Processor processor; protected ProcessorSelector processorSelector; public E processor(final Processor processor) { this.processor = processor; return (E) this; } public E processorSelector(final ProcessorSelector processorSelector) { this.processorSelector = processorSelector; return (E) this; } public AbstractBindingHandler build() { AbstractBindingHandler bindingHandler = create(); if (processor != null) { bindingHandler.setProcessor(processor); } if (processorSelector != null) { bindingHandler.setProcessorSelector(processorSelector); } return bindingHandler; } protected abstract AbstractBindingHandler create(); } }