1 20 package org.apache.mina.integration.spring; 21 22 import java.beans.PropertyEditorSupport ; 23 import java.net.InetSocketAddress ; 24 import java.net.SocketAddress ; 25 26 import org.springframework.util.Assert; 27 28 45 public class InetSocketAddressEditor extends PropertyEditorSupport { 46 public void setAsText(String text) throws IllegalArgumentException { 47 setValue(parseSocketAddress(text)); 48 } 49 50 private SocketAddress parseSocketAddress(String s) { 51 Assert.notNull(s, "null SocketAddress string"); 52 s = s.trim(); 53 int colonIndex = s.indexOf(":"); 54 if (colonIndex > 0) { 55 String host = s.substring(0, colonIndex); 56 int port = parsePort(s.substring(colonIndex + 1)); 57 return new InetSocketAddress (host, port); 58 } else { 59 int port = parsePort(s.substring(colonIndex + 1)); 60 return new InetSocketAddress (port); 61 } 62 } 63 64 private int parsePort(String s) { 65 try { 66 return Integer.parseInt(s); 67 } catch (NumberFormatException nfe) { 68 throw new IllegalArgumentException ("Illegal port number: " + s); 69 } 70 } 71 } 72 | Popular Tags |