KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > integration > spring > InetSocketAddressEditor


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 JavaDoc;
23 import java.net.InetSocketAddress JavaDoc;
24 import java.net.SocketAddress JavaDoc;
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 (금, 13 7월 2007) $
42  *
43  * @see java.net.InetSocketAddress
44  */

45 public class InetSocketAddressEditor extends PropertyEditorSupport JavaDoc {
46     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
47         setValue(parseSocketAddress(text));
48     }
49
50     private SocketAddress JavaDoc parseSocketAddress(String JavaDoc s) {
51         Assert.notNull(s, "null SocketAddress string");
52         s = s.trim();
53         int colonIndex = s.indexOf(":");
54         if (colonIndex > 0) {
55             String JavaDoc host = s.substring(0, colonIndex);
56             int port = parsePort(s.substring(colonIndex + 1));
57             return new InetSocketAddress JavaDoc(host, port);
58         } else {
59             int port = parsePort(s.substring(colonIndex + 1));
60             return new InetSocketAddress JavaDoc(port);
61         }
62     }
63
64     private int parsePort(String JavaDoc s) {
65         try {
66             return Integer.parseInt(s);
67         } catch (NumberFormatException JavaDoc nfe) {
68             throw new IllegalArgumentException JavaDoc("Illegal port number: " + s);
69         }
70     }
71 }
72
Popular Tags