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.extension.metadata; 018 019import java.util.Collections; 020import java.util.Map; 021 022import org.apache.camel.CamelContext; 023import org.apache.camel.component.extension.MetaDataExtension; 024 025public class DefaultMetaData implements MetaDataExtension.MetaData { 026 private final Map<String, Object> attributes; 027 private final Object payload; 028 private CamelContext camelContext; 029 030 public DefaultMetaData(CamelContext camelContext, Map<String, Object> attributes, Object payload) { 031 this.camelContext = camelContext; 032 this.attributes = attributes; 033 this.payload = payload; 034 } 035 036 @Override 037 public Object getAttribute(String name) { 038 return attributes.get(name); 039 } 040 041 @Override 042 public Map<String, Object> getAttributes() { 043 return Collections.unmodifiableMap(attributes); 044 } 045 046 @Override 047 public <T> T getAttribute(String name, Class<T> type) { 048 Object value = attributes.get(name); 049 if (camelContext != null) { 050 return camelContext.getTypeConverter().convertTo(type, value); 051 } 052 053 throw new IllegalStateException("Unable to perform conversion as CamelContext is not set"); 054 } 055 056 @Override 057 public Object getPayload() { 058 return payload; 059 } 060 061 @Override 062 public <T> T getPayload(Class<T> type) { 063 if (camelContext != null) { 064 return camelContext.getTypeConverter().convertTo(type, payload); 065 } 066 067 throw new IllegalStateException("Unable to perform conversion as CamelContext is not set"); 068 } 069}