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.dataset; 018 019 import java.util.HashMap; 020 import java.util.Map; 021 022 import org.apache.camel.Exchange; 023 import org.apache.camel.Message; 024 import org.apache.camel.Processor; 025 import org.apache.camel.util.ExchangeHelper; 026 027 /** 028 * Base class for DataSet 029 * 030 * @version $Revision: 567 $ 031 */ 032 public abstract class DataSetSupport implements DataSet { 033 private Map<String, Object> defaultHeaders; 034 private Processor outputTransformer; 035 private long size = 10; 036 private long reportCount = -1; 037 038 public DataSetSupport() { 039 } 040 041 public DataSetSupport(int size) { 042 setSize(size); 043 } 044 045 public void populateMessage(Exchange exchange, long messageIndex) throws Exception { 046 Message in = exchange.getIn(); 047 in.setBody(createMessageBody(messageIndex)); 048 in.setHeaders(getDefaultHeaders()); 049 applyHeaders(exchange, messageIndex); 050 051 if (outputTransformer != null) { 052 outputTransformer.process(exchange); 053 } 054 } 055 056 public void assertMessageExpected(DataSetEndpoint dataSetEndpoint, Exchange expected, Exchange actual, long index) throws Exception { 057 Object expectedBody = expected.getIn().getBody(); 058 Object actualBody = actual.getIn().getBody(); 059 if (expectedBody != null) { 060 // lets coerce to the correct type 061 actualBody = ExchangeHelper.getMandatoryInBody(actual, expectedBody.getClass()); 062 } 063 DataSetEndpoint.assertEquals("message body", expectedBody, actualBody, actual); 064 } 065 066 // Properties 067 //------------------------------------------------------------------------- 068 069 public long getSize() { 070 return size; 071 } 072 073 public void setSize(long size) { 074 this.size = size; 075 } 076 077 public long getReportCount() { 078 if (reportCount <= 0) { 079 reportCount = getSize() / 5; 080 } 081 return reportCount; 082 } 083 084 /** 085 * Sets the number of messages in a group on which we will report that messages have been received. 086 */ 087 public void setReportCount(long reportCount) { 088 this.reportCount = reportCount; 089 } 090 091 public Map<String, Object> getDefaultHeaders() { 092 if (defaultHeaders == null) { 093 defaultHeaders = new HashMap<String, Object>(); 094 populateDefaultHeaders(defaultHeaders); 095 } 096 return defaultHeaders; 097 } 098 099 public void setDefaultHeaders(Map<String, Object> defaultHeaders) { 100 this.defaultHeaders = defaultHeaders; 101 } 102 103 public Processor getOutputTransformer() { 104 return outputTransformer; 105 } 106 107 public void setOutputTransformer(Processor outputTransformer) { 108 this.outputTransformer = outputTransformer; 109 } 110 111 // Implementation methods 112 //------------------------------------------------------------------------- 113 114 protected abstract Object createMessageBody(long messageIndex); 115 116 /** 117 * Allows derived classes to add some custom headers for a given message 118 */ 119 protected void applyHeaders(Exchange exchange, long messageIndex) { 120 } 121 122 /** 123 * Allows derived classes to customize a default set of properties 124 */ 125 protected void populateDefaultHeaders(Map<String, Object> map) { 126 } 127 }