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  /**
31   * A default immutable implementation of {@link TransportMetadata}.
32   *
33   * @author The Apache MINA Project (dev@mina.apache.org)
34   */
35  public class DefaultTransportMetadata implements TransportMetadata {
36  
37      private final String providerName;
38      private final String name;
39      private final boolean connectionless;
40      private final boolean fragmentation;
41      private final Class<? extends SocketAddress> addressType;
42      private final Class<? extends IoSessionConfig> sessionConfigType;
43      private final Set<Class<? extends Object>> envelopeTypes;
44  
45      public DefaultTransportMetadata(
46              String providerName,
47              String name,
48              boolean connectionless,
49              boolean fragmentation,
50              Class<? extends SocketAddress> addressType,
51              Class<? extends IoSessionConfig> sessionConfigType,
52              Class<?>... envelopeTypes) {
53  
54          if (providerName == null) {
55              throw new NullPointerException("providerName");
56          }
57          if (name == null) {
58              throw new NullPointerException("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 NullPointerException("addressType");
72          }
73  
74          if (envelopeTypes == null) {
75              throw new NullPointerException("envelopeTypes");
76          }
77  
78          if (envelopeTypes.length == 0) {
79              throw new NullPointerException("envelopeTypes is empty.");
80          }
81  
82          if (sessionConfigType == null) {
83              throw new NullPointerException("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 =
94              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     public Class<? extends SocketAddress> getAddressType() {
102         return addressType;
103     }
104 
105     public Set<Class<? extends Object>> getEnvelopeTypes() {
106         return envelopeTypes;
107     }
108 
109     public Class<? extends IoSessionConfig> getSessionConfigType() {
110         return sessionConfigType;
111     }
112 
113     public String getProviderName() {
114         return providerName;
115     }
116 
117     public String getName() {
118         return name;
119     }
120 
121     public boolean isConnectionless() {
122         return connectionless;
123     }
124 
125     public boolean hasFragmentation() {
126         return fragmentation;
127     }
128 
129     @Override
130     public String toString() {
131         return name;
132     }
133 }