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.camel.component.bean; 018 019import org.apache.camel.spi.ClassResolver; 020import org.apache.camel.util.ObjectHelper; 021import org.apache.camel.util.StringHelper; 022 023/** 024 * Helper for the bean component. 025 */ 026public final class BeanHelper { 027 028 private BeanHelper() { 029 // utility class 030 } 031 032 /** 033 * Determines and maps the given value is valid according to the supported 034 * values by the bean component. 035 * 036 * @param value the value 037 * @return the parameter type the given value is being mapped as, or <tt>null</tt> if not valid. 038 */ 039 public static Class<?> getValidParameterType(String value) { 040 if (ObjectHelper.isEmpty(value)) { 041 return null; 042 } 043 044 // trim value 045 value = value.trim(); 046 047 // single quoted is valid 048 if (value.startsWith("'") && value.endsWith("'")) { 049 return String.class; 050 } 051 052 // double quoted is valid 053 if (value.startsWith("\"") && value.endsWith("\"")) { 054 return String.class; 055 } 056 057 // true or false is valid (boolean) 058 if (value.equals("true") || value.equals("false")) { 059 return Boolean.class; 060 } 061 062 // null is valid (to force a null value) 063 if (value.equals("null")) { 064 return Object.class; 065 } 066 067 // simple language tokens is valid 068 if (StringHelper.hasStartToken(value, "simple")) { 069 return Object.class; 070 } 071 072 // numeric is valid 073 boolean numeric = true; 074 for (char ch : value.toCharArray()) { 075 if (!Character.isDigit(ch)) { 076 numeric = false; 077 break; 078 } 079 } 080 if (numeric) { 081 return Number.class; 082 } 083 084 // not valid 085 return null; 086 } 087 088 /** 089 * Determines if the given value is valid according to the supported 090 * values by the bean component. 091 * 092 * @param value the value 093 * @return <tt>true</tt> if valid, <tt>false</tt> otherwise 094 */ 095 public static boolean isValidParameterValue(String value) { 096 if (ObjectHelper.isEmpty(value)) { 097 // empty value is valid 098 return true; 099 } 100 101 return getValidParameterType(value) != null; 102 } 103 104 /** 105 * Determines if the given parameter type is assignable to the expected type. 106 * <p/> 107 * This implementation will check if the given parameter type matches the expected type as class using either 108 * <ul> 109 * <li>FQN class name - com.foo.MyOrder</li> 110 * <li>Simple class name - MyOrder</li> 111 * </ul> 112 * If the given parameter type is <b>not</b> a class, then <tt>null</tt> is returned 113 * 114 * @param resolver the class resolver 115 * @param parameterType the parameter type as a String, can be a FQN or a simple name of the class 116 * @param expectedType the expected type 117 * @return <tt>null</tt> if parameter type is <b>not</b> a class, <tt>true</tt> if parameter type is assignable, <tt>false</tt> if not assignable 118 */ 119 public static Boolean isAssignableToExpectedType(ClassResolver resolver, String parameterType, Class<?> expectedType) { 120 // if its a class, then it should be assignable 121 Class<?> parameterClass = resolver.resolveClass(parameterType); 122 if (parameterClass == null && parameterType.equals(expectedType.getSimpleName())) { 123 // it was not the FQN class name, but the simple name instead, which matched 124 return true; 125 } 126 127 // not a class so return null 128 if (parameterClass == null) { 129 return null; 130 } 131 132 // if there was a class, then it must be assignable to match 133 return parameterClass.isAssignableFrom(expectedType); 134 } 135 136}