001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.integration.beans; 021 022import java.beans.PropertyEditor; 023import java.net.InetAddress; 024import java.net.UnknownHostException; 025 026/** 027 * A {@link PropertyEditor} which converts a {@link String} into an 028 * {@link InetAddress}. 029 * This editor simply calls {@link InetAddress#getByName(java.lang.String)} 030 * when converting from a {@link String}, and {@link InetAddress#getHostAddress()} 031 * when converting to a {@link String}. 032 * 033 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 034 * 035 * @see java.net.InetAddress 036 */ 037public class InetAddressEditor extends AbstractPropertyEditor { 038 @Override 039 protected String toText(Object value) { 040 String hostname = ((InetAddress) value).getHostAddress(); 041 if (hostname.equals("0:0:0:0:0:0:0:0") || hostname.equals("0.0.0.0") 042 || hostname.equals("00:00:00:00:00:00:00:00")) { 043 hostname = "*"; 044 } 045 return hostname; 046 } 047 048 @Override 049 protected Object toValue(String text) throws IllegalArgumentException { 050 if (text.length() == 0 || text.equals("*")) { 051 return defaultValue(); 052 } 053 054 try { 055 return InetAddress.getByName(text); 056 } catch (UnknownHostException uhe) { 057 IllegalArgumentException iae = new IllegalArgumentException(); 058 iae.initCause(uhe); 059 throw iae; 060 } 061 } 062 063 @Override 064 protected String defaultText() { 065 return "*"; 066 } 067 068 @Override 069 protected Object defaultValue() { 070 try { 071 return InetAddress.getByName("0.0.0.0"); 072 } catch (UnknownHostException e) { 073 throw new InternalError(); 074 } 075 } 076}