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      /**
52       * Creates a new DefaultTransportMetadata instance
53       * 
54       * @param providerName The provider name
55       * @param name The name
56       * @param connectionless If the transport is UDP
57       * @param fragmentation If fragmentation is supported
58       * @param addressType The address type (IP V4 or IPV6)
59       * @param sessionConfigType The session configuration type
60       * @param envelopeTypes The types of supported messages
61       */
62      public DefaultTransportMetadata(String providerName, String name, boolean connectionless, boolean fragmentation,
63              Class<? extends SocketAddress> addressType, Class<? extends IoSessionConfig> sessionConfigType,
64              Class<?>... envelopeTypes) {
65  
66          if (providerName == null) {
67              throw new IllegalArgumentException("providerName");
68          } else {
69              this.providerName = providerName.trim().toLowerCase();
70  
71              if (this.providerName.length() == 0) {
72                  throw new IllegalArgumentException("providerName is empty.");
73              }
74          }
75          
76          if (name == null) {
77              throw new IllegalArgumentException("name");
78          } else {
79              this.name = name.trim().toLowerCase();
80              
81              if (this.name.length() == 0) {
82                  throw new IllegalArgumentException("name is empty.");
83              }
84          }
85  
86          if (addressType == null) {
87              throw new IllegalArgumentException("addressType");
88          }
89  
90          if (envelopeTypes == null) {
91              throw new IllegalArgumentException("envelopeTypes");
92          }
93  
94          if (envelopeTypes.length == 0) {
95              throw new IllegalArgumentException("envelopeTypes is empty.");
96          }
97  
98          if (sessionConfigType == null) {
99              throw new IllegalArgumentException("sessionConfigType");
100         }
101 
102         this.connectionless = connectionless;
103         this.fragmentation = fragmentation;
104         this.addressType = addressType;
105         this.sessionConfigType = sessionConfigType;
106 
107         Set<Class<? extends Object>> newEnvelopeTypes = new IdentityHashSet<>();
108         for (Class<? extends Object> c : envelopeTypes) {
109             newEnvelopeTypes.add(c);
110         }
111         this.envelopeTypes = Collections.unmodifiableSet(newEnvelopeTypes);
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public Class<? extends SocketAddress> getAddressType() {
119         return addressType;
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public Set<Class<? extends Object>> getEnvelopeTypes() {
127         return envelopeTypes;
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public Class<? extends IoSessionConfig> getSessionConfigType() {
135         return sessionConfigType;
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public String getProviderName() {
143         return providerName;
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public String getName() {
151         return name;
152     }
153 
154     /**
155      * {@inheritDoc}
156      */
157     @Override
158     public boolean isConnectionless() {
159         return connectionless;
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     public boolean hasFragmentation() {
167         return fragmentation;
168     }
169 
170     @Override
171     public String toString() {
172         return name;
173     }
174 }