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 */ 017package org.apache.mina.integration.ognl; 018 019import java.beans.PropertyEditor; 020import java.lang.reflect.Member; 021import java.util.Map; 022 023import ognl.OgnlContext; 024import ognl.TypeConverter; 025 026import org.apache.mina.integration.beans.PropertyEditorFactory; 027 028/** 029 * {@link PropertyEditor}-based implementation of OGNL {@link TypeConverter}. 030 * This converter uses the {@link PropertyEditor} implementations in 031 * <tt>mina-integration-beans</tt> module to perform conversion. To use this 032 * converter: 033 * <pre><code> 034 * OgnlContext ctx = Ognl.createDefaultContext(root); 035 * ctx.put(OgnlContext.TYPE_CONVERTER_CONTEXT_KEY, new PropertyTypeConverter()); 036 * </code></pre> 037 * You can also override {@link #getPropertyEditor(Class, String, Class)} 038 * method to have more control over how an appropriate {@link PropertyEditor} 039 * is chosen. 040 * 041 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 042 */ 043public class PropertyTypeConverter implements TypeConverter { 044 045 @SuppressWarnings("unchecked") 046 public Object convertValue(Map ctx, Object target, Member member, String attrName, Object value, Class toType) { 047 if (value == null) { 048 return null; 049 } 050 051 if (attrName == null) { 052 // I don't know why but OGNL gives null attrName almost always. 053 // Fortunately, we can get the actual attrName with a tiny hack. 054 OgnlContext ognlCtx = (OgnlContext) ctx; 055 attrName = ognlCtx.getCurrentNode().toString().replaceAll("[\" \']+", ""); 056 } 057 058 if (toType.isAssignableFrom(value.getClass())) { 059 return value; 060 } 061 062 PropertyEditor e1 = getPropertyEditor(target.getClass(), attrName, value.getClass()); 063 if (e1 == null) { 064 throw new IllegalArgumentException("Can't convert " + value.getClass().getSimpleName() + " to " 065 + String.class.getSimpleName()); 066 } 067 e1.setValue(value); 068 069 PropertyEditor e2 = getPropertyEditor(target.getClass(), attrName, toType); 070 if (e2 == null) { 071 throw new IllegalArgumentException("Can't convert " + String.class.getSimpleName() + " to " 072 + toType.getSimpleName()); 073 } 074 075 e2.setAsText(e1.getAsText()); 076 return e2.getValue(); 077 } 078 079 protected PropertyEditor getPropertyEditor(Class<?> type, String attrName, Class<?> attrType) { 080 return PropertyEditorFactory.getInstance(attrType); 081 } 082}