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.dataformat; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlRootElement; 023import javax.xml.bind.annotation.XmlTransient; 024 025import org.apache.camel.CamelContext; 026import org.apache.camel.model.DataFormatDefinition; 027import org.apache.camel.spi.DataFormat; 028import org.apache.camel.spi.Metadata; 029import org.apache.camel.spi.RouteContext; 030import org.apache.camel.util.ObjectHelper; 031 032/** 033 * Boon data format 034 */ 035@Metadata(label = "dataformat,transformation,json", title = "Boon") 036@XmlRootElement(name = "boon") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class BoonDataFormat extends DataFormatDefinition { 039 040 @XmlAttribute(required = true) 041 private String unmarshalTypeName; 042 @XmlTransient 043 private Class<?> unmarshalType; 044 045 public BoonDataFormat() { 046 super("boon"); 047 } 048 049 public BoonDataFormat(Class<?> unmarshalType) { 050 this(); 051 setUnmarshalType(unmarshalType); 052 } 053 054 public Class<?> getUnmarshalType() { 055 return unmarshalType; 056 } 057 058 /** 059 * Class name of the java type to use when unarmshalling 060 */ 061 public void setUnmarshalType(Class<?> unmarshalType) { 062 this.unmarshalType = unmarshalType; 063 } 064 065 public String getUnmarshalTypeName() { 066 return unmarshalTypeName; 067 } 068 069 /** 070 * Class name of the java type to use when unarmshalling 071 */ 072 public void setUnmarshalTypeName(String unmarshalTypeName) { 073 this.unmarshalTypeName = unmarshalTypeName; 074 } 075 076 @Override 077 protected DataFormat createDataFormat(RouteContext routeContext) { 078 if (unmarshalType == null && unmarshalTypeName != null) { 079 try { 080 unmarshalType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(unmarshalTypeName); 081 } catch (ClassNotFoundException e) { 082 throw ObjectHelper.wrapRuntimeCamelException(e); 083 } 084 } 085 return super.createDataFormat(routeContext); 086 } 087 088 @Override 089 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 090 if (unmarshalType != null) { 091 setProperty(camelContext, dataFormat, "unmarshalType", unmarshalType); 092 } 093 } 094}