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      private final boolean fragmentation;
43  
44      private final Class<? extends SocketAddress> addressType;
45  
46      private final Class<? extends IoSessionConfig> sessionConfigType;
47  
48      private final Set<Class<? extends Object>> envelopeTypes;
49  
50      public DefaultTransportMetadata(String providerName, String name, boolean connectionless, boolean fragmentation,
51              Class<? extends SocketAddress> addressType, Class<? extends IoSessionConfig> sessionConfigType,
52              Class<?>... envelopeTypes) {
53  
54          if (providerName == null) {
55              throw new IllegalArgumentException("providerName");
56          }
57          if (name == null) {
58              throw new IllegalArgumentException("name");
59          }
60  
61          providerName = providerName.trim().toLowerCase();
62          if (providerName.length() == 0) {
63              throw new IllegalArgumentException("providerName is empty.");
64          }
65          name = name.trim().toLowerCase();
66          if (name.length() == 0) {
67              throw new IllegalArgumentException("name is empty.");
68          }
69  
70          if (addressType == null) {
71              throw new IllegalArgumentException("addressType");
72          }
73  
74          if (envelopeTypes == null) {
75              throw new IllegalArgumentException("envelopeTypes");
76          }
77  
78          if (envelopeTypes.length == 0) {
79              throw new IllegalArgumentException("envelopeTypes is empty.");
80          }
81  
82          if (sessionConfigType == null) {
83              throw new IllegalArgumentException("sessionConfigType");
84          }
85  
86          this.providerName = providerName;
87          this.name = name;
88          this.connectionless = connectionless;
89          this.fragmentation = fragmentation;
90          this.addressType = addressType;
91          this.sessionConfigType = sessionConfigType;
92  
93          Set<Class<? extends Object>> newEnvelopeTypes = new IdentityHashSet<Class<? extends Object>>();
94          for (Class<? extends Object> c : envelopeTypes) {
95              newEnvelopeTypes.add(c);
96          }
97          this.envelopeTypes = Collections.unmodifiableSet(newEnvelopeTypes);
98      }
99  
100     public Class<? extends SocketAddress> getAddressType() {
101         return addressType;
102     }
103 
104     public Set<Class<? extends Object>> getEnvelopeTypes() {
105         return envelopeTypes;
106     }
107 
108     public Class<? extends IoSessionConfig> getSessionConfigType() {
109         return sessionConfigType;
110     }
111 
112     public String getProviderName() {
113         return providerName;
114     }
115 
116     public String getName() {
117         return name;
118     }
119 
120     public boolean isConnectionless() {
121         return connectionless;
122     }
123 
124     public boolean hasFragmentation() {
125         return fragmentation;
126     }
127 
128     @Override
129     public String toString() {
130         return name;
131     }
132 }