/* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package io.netty.channel; import static io.netty.channel.ChannelOption.*; import io.netty.channel.socket.SocketChannelConfig; import java.util.IdentityHashMap; import java.util.Map; import java.util.Map.Entry; /** * The default {@link SocketChannelConfig} implementation. */ public class DefaultChannelConfig implements ChannelConfig { private static final int DEFAULT_CONNECT_TIMEOUT = 30000; private volatile int connectTimeoutMillis = DEFAULT_CONNECT_TIMEOUT; private volatile int writeSpinCount = 16; @Override public Map, Object> getOptions() { return getOptions(null, CONNECT_TIMEOUT_MILLIS, WRITE_SPIN_COUNT); } protected Map, Object> getOptions( Map, Object> result, ChannelOption... options) { if (result == null) { result = new IdentityHashMap, Object>(); } for (ChannelOption o: options) { result.put(o, getOption(o)); } return result; } @SuppressWarnings("unchecked") @Override public boolean setOptions(Map, ?> options) { if (options == null) { throw new NullPointerException("options"); } boolean setAllOptions = true; for (Entry, ?> e: options.entrySet()) { if (!setOption((ChannelOption) e.getKey(), e.getValue())) { setAllOptions = false; } } return setAllOptions; } @SuppressWarnings("unchecked") @Override public T getOption(ChannelOption option) { if (option == null) { throw new NullPointerException("option"); } if (option == CONNECT_TIMEOUT_MILLIS) { return (T) Integer.valueOf(getConnectTimeoutMillis()); } else if (option == WRITE_SPIN_COUNT) { return (T) Integer.valueOf(getWriteSpinCount()); } return null; } @Override public boolean setOption(ChannelOption option, T value) { validate(option, value); if (option == CONNECT_TIMEOUT_MILLIS) { setConnectTimeoutMillis((Integer) value); } else if (option == WRITE_SPIN_COUNT) { setWriteSpinCount((Integer) value); } else { return false; } return true; } protected void validate(ChannelOption option, T value) { if (option == null) { throw new NullPointerException("option"); } option.validate(value); } @Override public int getConnectTimeoutMillis() { return connectTimeoutMillis; } @Override public void setConnectTimeoutMillis(int connectTimeoutMillis) { if (connectTimeoutMillis < 0) { throw new IllegalArgumentException(String.format( "connectTimeoutMillis: %d (expected: >= 0)", connectTimeoutMillis)); } this.connectTimeoutMillis = connectTimeoutMillis; } @Override public int getWriteSpinCount() { return writeSpinCount; } @Override public void setWriteSpinCount(int writeSpinCount) { if (writeSpinCount <= 0) { throw new IllegalArgumentException( "writeSpinCount must be a positive integer."); } this.writeSpinCount = writeSpinCount; } }