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.model.validator; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlType; 023 024import org.apache.camel.CamelContext; 025import org.apache.camel.spi.Metadata; 026import org.apache.camel.spi.Validator; 027 028/** 029 * Represents a CustomValidator. One of the bean reference (ref) or fully qualified class name (className) 030 * of the custom {@link Validator} needs to be specified. 031 * 032 * {@see ValidatorDefinition} 033 * {@see Validator} 034 */ 035@Metadata(label = "validation") 036@XmlType(name = "customValidator") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class CustomValidatorDefinition extends ValidatorDefinition { 039 040 @XmlAttribute 041 private String ref; 042 @XmlAttribute 043 private String className; 044 045 @Override 046 protected Validator doCreateValidator(CamelContext context) throws Exception { 047 if (ref == null && className == null) { 048 throw new IllegalArgumentException("'ref' or 'type' must be specified for customValidator"); 049 } 050 Validator validator; 051 if (ref != null) { 052 validator = context.getRegistry().lookupByNameAndType(ref, Validator.class); 053 if (validator == null) { 054 throw new IllegalArgumentException("Cannot find validator with ref:" + ref); 055 } 056 if (validator.getType() != null) { 057 throw new IllegalArgumentException(String.format("Validator '%s' is already in use. Please check if duplicate validator exists.", ref)); 058 } 059 } else { 060 Class<Validator> validatorClass = context.getClassResolver().resolveMandatoryClass(className, Validator.class); 061 if (validatorClass == null) { 062 throw new IllegalArgumentException("Cannot find validator class: " + className); 063 } 064 validator = context.getInjector().newInstance(validatorClass); 065 066 } 067 validator.setCamelContext(context); 068 return validator.setType(getType()); 069 } 070 071 public String getRef() { 072 return ref; 073 } 074 075 /** 076 * Set a bean reference of the {@link Validator} 077 * 078 * @param ref the bean reference of the Transformer 079 */ 080 public void setRef(String ref) { 081 this.ref = ref; 082 } 083 084 public String getClassName() { 085 return className; 086 } 087 088 /** 089 * Set a class name of the {@link Validator} 090 * 091 * @param className the class name of the Validator 092 */ 093 public void setClassName(String className) { 094 this.className = className; 095 } 096 097} 098