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.flatpack;
018
019 import java.io.IOException;
020 import java.io.InputStreamReader;
021 import java.io.Reader;
022
023 import net.sf.flatpack.DataSet;
024 import net.sf.flatpack.DefaultParserFactory;
025 import net.sf.flatpack.Parser;
026 import net.sf.flatpack.ParserFactory;
027 import org.apache.camel.Consumer;
028 import org.apache.camel.Exchange;
029 import org.apache.camel.InvalidPayloadException;
030 import org.apache.camel.Message;
031 import org.apache.camel.Processor;
032 import org.apache.camel.Producer;
033 import org.apache.camel.impl.DefaultPollingEndpoint;
034 import org.apache.camel.processor.loadbalancer.LoadBalancer;
035 import org.apache.camel.processor.loadbalancer.LoadBalancerConsumer;
036 import org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer;
037 import org.apache.camel.util.ExchangeHelper;
038 import org.apache.camel.util.ObjectHelper;
039 import org.springframework.core.io.Resource;
040
041 /**
042 * A <a href="http://flatpack.sourceforge.net/">Flatpack Endpoint</a>
043 * for working with fixed width and delimited files
044 *
045 * @version $Revision: 2781 $
046 */
047 public class FixedLengthEndpoint extends DefaultPollingEndpoint {
048 private final Resource resource;
049 private LoadBalancer loadBalancer = new RoundRobinLoadBalancer();
050 private ParserFactory parserFactory = DefaultParserFactory.getInstance();
051 private boolean splitRows = true;
052
053 public FixedLengthEndpoint(String uri, Resource resource) {
054 super(uri);
055 this.resource = resource;
056 }
057
058 public boolean isSingleton() {
059 return true;
060 }
061
062 public Producer createProducer() throws Exception {
063 return new FlatpackProducer(this);
064 }
065
066 public Consumer createConsumer(Processor processor) throws Exception {
067 return new LoadBalancerConsumer(this, processor, loadBalancer);
068 }
069
070 public void processDataSet(DataSet dataSet, int counter) throws Exception {
071 Exchange exchange = createExchange(dataSet, counter);
072 loadBalancer.process(exchange);
073 }
074
075 public Exchange createExchange(DataSet dataSet, int counter) {
076 Exchange answer = createExchange();
077 Message in = answer.getIn();
078 in.setBody(dataSet);
079 in.setHeader("camelFlatpackCounter", counter);
080 return answer;
081 }
082
083 public Parser createParser(Exchange exchange) throws InvalidPayloadException, IOException {
084 Resource resource = getResource();
085 ObjectHelper.notNull(resource, "resource");
086 Reader bodyReader = ExchangeHelper.getMandatoryInBody(exchange, Reader.class);
087 return createParser(resource, bodyReader);
088 }
089
090 protected Parser createParser(Resource resource, Reader bodyReader) throws IOException {
091 return getParserFactory().newFixedLengthParser(new InputStreamReader(resource.getInputStream()), bodyReader);
092 }
093
094 // Properties
095 //-------------------------------------------------------------------------
096
097 public Resource getResource() {
098 return resource;
099 }
100
101 public ParserFactory getParserFactory() {
102 return parserFactory;
103 }
104
105 public void setParserFactory(ParserFactory parserFactory) {
106 this.parserFactory = parserFactory;
107 }
108
109 public LoadBalancer getLoadBalancer() {
110 return loadBalancer;
111 }
112
113 public void setLoadBalancer(LoadBalancer loadBalancer) {
114 this.loadBalancer = loadBalancer;
115 }
116
117 public boolean isSplitRows() {
118 return splitRows;
119 }
120
121 public void setSplitRows(boolean splitRows) {
122 this.splitRows = splitRows;
123 }
124 }