查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.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  /**
29   * Java Bean {@link java.beans.PropertyEditor} which converts Strings into 
30   * {@link InetSocketAddress} objects. Valid values include a hostname or ip
31   * address and a port number separated by a ':'. If the hostname or ip address
32   * is omitted the wildcard address will be used. E.g.: 
33   * <code>google.com:80</code>, <code>:22</code>, <code>192.168.0.1:110</code>.
34   * <p>
35   * Use Spring's CustomEditorConfigurer to use this property editor in a Spring
36   * configuration file. See chapter 3.14 of the Spring Reference Documentation 
37   * for more info.
38   * </p>
39   * 
40   * @author The Apache Directory Project (mina-dev@directory.apache.org)
41   * @version $Revision: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
42   * 
43   * @see java.net.InetSocketAddress
44   */
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  }