001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.core.service;
021
022import java.net.SocketAddress;
023import java.util.Collections;
024import java.util.Set;
025
026import org.apache.mina.core.session.IoSessionConfig;
027import org.apache.mina.util.IdentityHashSet;
028
029/**
030 * A default immutable implementation of {@link TransportMetadata}.
031 *
032 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
033 */
034public class DefaultTransportMetadata implements TransportMetadata {
035
036    private final String providerName;
037
038    private final String name;
039
040    private final boolean connectionless;
041
042    /** The flag indicating that the transport support fragmentation or not */
043    private final boolean fragmentation;
044
045    private final Class<? extends SocketAddress> addressType;
046
047    private final Class<? extends IoSessionConfig> sessionConfigType;
048
049    private final Set<Class<? extends Object>> envelopeTypes;
050
051    public DefaultTransportMetadata(String providerName, String name, boolean connectionless, boolean fragmentation,
052            Class<? extends SocketAddress> addressType, Class<? extends IoSessionConfig> sessionConfigType,
053            Class<?>... envelopeTypes) {
054
055        if (providerName == null) {
056            throw new IllegalArgumentException("providerName");
057        }
058        if (name == null) {
059            throw new IllegalArgumentException("name");
060        }
061
062        providerName = providerName.trim().toLowerCase();
063        if (providerName.length() == 0) {
064            throw new IllegalArgumentException("providerName is empty.");
065        }
066        name = name.trim().toLowerCase();
067        if (name.length() == 0) {
068            throw new IllegalArgumentException("name is empty.");
069        }
070
071        if (addressType == null) {
072            throw new IllegalArgumentException("addressType");
073        }
074
075        if (envelopeTypes == null) {
076            throw new IllegalArgumentException("envelopeTypes");
077        }
078
079        if (envelopeTypes.length == 0) {
080            throw new IllegalArgumentException("envelopeTypes is empty.");
081        }
082
083        if (sessionConfigType == null) {
084            throw new IllegalArgumentException("sessionConfigType");
085        }
086
087        this.providerName = providerName;
088        this.name = name;
089        this.connectionless = connectionless;
090        this.fragmentation = fragmentation;
091        this.addressType = addressType;
092        this.sessionConfigType = sessionConfigType;
093
094        Set<Class<? extends Object>> newEnvelopeTypes = new IdentityHashSet<Class<? extends Object>>();
095        for (Class<? extends Object> c : envelopeTypes) {
096            newEnvelopeTypes.add(c);
097        }
098        this.envelopeTypes = Collections.unmodifiableSet(newEnvelopeTypes);
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    public Class<? extends SocketAddress> getAddressType() {
105        return addressType;
106    }
107
108    /**
109     * {@inheritDoc}
110     */
111    public Set<Class<? extends Object>> getEnvelopeTypes() {
112        return envelopeTypes;
113    }
114
115    /**
116     * {@inheritDoc}
117     */
118    public Class<? extends IoSessionConfig> getSessionConfigType() {
119        return sessionConfigType;
120    }
121
122    /**
123     * {@inheritDoc}
124     */
125    public String getProviderName() {
126        return providerName;
127    }
128
129    /**
130     * {@inheritDoc}
131     */
132    public String getName() {
133        return name;
134    }
135
136    /**
137     * {@inheritDoc}
138     */
139    public boolean isConnectionless() {
140        return connectionless;
141    }
142
143    /**
144     * {@inheritDoc}
145     */
146    public boolean hasFragmentation() {
147        return fragmentation;
148    }
149
150    @Override
151    public String toString() {
152        return name;
153    }
154}