View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.service;
21  
22  import java.net.SocketAddress;
23  import java.util.Collections;
24  import java.util.Set;
25  
26  import org.apache.mina.core.session.IoSessionConfig;
27  import org.apache.mina.util.IdentityHashSet;
28  
29  /**
30   * A default immutable implementation of {@link TransportMetadata}.
31   *
32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33   */
34  public class DefaultTransportMetadata implements TransportMetadata {
35  
36      private final String providerName;
37  
38      private final String name;
39  
40      private final boolean connectionless;
41  
42      /** The flag indicating that the transport support fragmentation or not */
43      private final boolean fragmentation;
44  
45      private final Class<? extends SocketAddress> addressType;
46  
47      private final Class<? extends IoSessionConfig> sessionConfigType;
48  
49      private final Set<Class<? extends Object>> envelopeTypes;
50  
51      public DefaultTransportMetadata(String providerName, String name, boolean connectionless, boolean fragmentation,
52              Class<? extends SocketAddress> addressType, Class<? extends IoSessionConfig> sessionConfigType,
53              Class<?>... envelopeTypes) {
54  
55          if (providerName == null) {
56              throw new IllegalArgumentException("providerName");
57          }
58          if (name == null) {
59              throw new IllegalArgumentException("name");
60          }
61  
62          providerName = providerName.trim().toLowerCase();
63          if (providerName.length() == 0) {
64              throw new IllegalArgumentException("providerName is empty.");
65          }
66          name = name.trim().toLowerCase();
67          if (name.length() == 0) {
68              throw new IllegalArgumentException("name is empty.");
69          }
70  
71          if (addressType == null) {
72              throw new IllegalArgumentException("addressType");
73          }
74  
75          if (envelopeTypes == null) {
76              throw new IllegalArgumentException("envelopeTypes");
77          }
78  
79          if (envelopeTypes.length == 0) {
80              throw new IllegalArgumentException("envelopeTypes is empty.");
81          }
82  
83          if (sessionConfigType == null) {
84              throw new IllegalArgumentException("sessionConfigType");
85          }
86  
87          this.providerName = providerName;
88          this.name = name;
89          this.connectionless = connectionless;
90          this.fragmentation = fragmentation;
91          this.addressType = addressType;
92          this.sessionConfigType = sessionConfigType;
93  
94          Set<Class<? extends Object>> newEnvelopeTypes = new IdentityHashSet<Class<? extends Object>>();
95          for (Class<? extends Object> c : envelopeTypes) {
96              newEnvelopeTypes.add(c);
97          }
98          this.envelopeTypes = Collections.unmodifiableSet(newEnvelopeTypes);
99      }
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 }