001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.xbean.server.propertyeditor; 018 019 import java.beans.PropertyEditorSupport; 020 import java.net.InetAddress; 021 import java.net.UnknownHostException; 022 023 /** 024 * InetAddressEditor is a java beans property editor that can convert an InetAddreass to and from a String. 025 * 026 * @author Dain Sundstrom 027 * @version $Id$ 028 * @since 2.0 029 */ 030 public class InetAddressEditor extends PropertyEditorSupport { 031 /** 032 * Converts the specified string value into an InetAddress and stores the value in this instance. 033 * @param value the string to convert into an InetAddress 034 * @throws IllegalArgumentException if the specified string value is not a valid InetAddress 035 */ 036 public void setAsText(String value) throws IllegalArgumentException { 037 try { 038 setValue(InetAddress.getByName(value)); 039 } catch (UnknownHostException e) { 040 throw (IllegalArgumentException) new IllegalArgumentException().initCause(e); 041 } 042 } 043 044 /** 045 * Converts the stored InetAddress value into a String. 046 * @return the string form of the current InetAddress value 047 * @throws NullPointerException if the current InetAddress is null 048 */ 049 public String getAsText() throws NullPointerException { 050 InetAddress inetAddress = (InetAddress) getValue(); 051 if (inetAddress == null) { 052 throw new NullPointerException("Current InetAddress value is null"); 053 } 054 String text = inetAddress.toString(); 055 return text; 056 } 057 }