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