1 20 package org.apache.mina.common; 21 22 import java.io.InvalidObjectException ; 23 import java.io.ObjectStreamException ; 24 import java.io.Serializable ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.TreeSet ; 29 30 46 public final class TransportType implements Serializable { 47 private static final long serialVersionUID = 3258132470497883447L; 48 49 private static final Map <String , TransportType> name2type = new HashMap <String , TransportType>(); 50 51 private static void register(String [] names, TransportType type) { 52 synchronized (name2type) { 53 for (int i = names.length - 1; i >= 0; i--) { 54 if (name2type.containsKey(names[i])) { 55 throw new IllegalArgumentException ("Transport type name '" 56 + names[i] + "' is already taken."); 57 } 58 } 59 60 for (int i = names.length - 1; i >= 0; i--) { 61 name2type.put(names[i].toUpperCase(), type); 62 } 63 } 64 } 65 66 69 public static final TransportType SOCKET = new TransportType(new String [] { 70 "SOCKET", "TCP" }, false); 71 72 75 public static final TransportType DATAGRAM = new TransportType( 76 new String [] { "DATAGRAM", "UDP" }, true); 77 78 84 public static final TransportType VM_PIPE = new TransportType( 85 new String [] { "VM_PIPE" }, Object .class, false); 86 87 95 public static TransportType getInstance(String name) { 96 TransportType type = name2type.get(name.toUpperCase()); 97 if (type != null) { 98 return type; 99 } 100 101 throw new IllegalArgumentException ("Unknown transport type name: " 102 + name); 103 } 104 105 private final String [] names; 106 107 private final transient boolean connectionless; 108 109 private final transient Class <? extends Object > envelopeType; 110 111 120 public TransportType(String [] names, boolean connectionless) { 121 this(names, ByteBuffer.class, connectionless); 122 } 123 124 133 public TransportType(String [] names, Class <? extends Object > envelopeType, 134 boolean connectionless) { 135 if (names == null) { 136 throw new NullPointerException ("names"); 137 } 138 if (names.length == 0) { 139 throw new IllegalArgumentException ("names is empty"); 140 } 141 if (envelopeType == null) { 142 throw new NullPointerException ("envelopeType"); 143 } 144 145 for (int i = 0; i < names.length; i++) { 146 if (names[i] == null) { 147 throw new NullPointerException ("strVals[" + i + "]"); 148 } 149 150 names[i] = names[i].toUpperCase(); 151 } 152 153 register(names, this); 154 this.names = names; 155 this.connectionless = connectionless; 156 this.envelopeType = envelopeType; 157 } 158 159 163 public boolean isConnectionless() { 164 return connectionless; 165 } 166 167 public Class <? extends Object > getEnvelopeType() { 168 return envelopeType; 169 } 170 171 174 public Set <String > getNames() { 175 Set <String > result = new TreeSet <String >(); 176 for (int i = names.length - 1; i >= 0; i--) { 177 result.add(names[i]); 178 } 179 180 return result; 181 } 182 183 @Override 184 public String toString() { 185 return names[0]; 186 } 187 188 private Object readResolve() throws ObjectStreamException { 189 for (int i = names.length - 1; i >= 0; i--) { 190 try { 191 return getInstance(names[i]); 192 } catch (IllegalArgumentException e) { 193 } 195 } 196 197 throw new InvalidObjectException ("Unknown transport type."); 198 } 199 } 200 | Popular Tags |