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