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.processor; 018 019import java.util.Collections; 020import java.util.Comparator; 021import java.util.List; 022 023import org.apache.camel.AsyncCallback; 024import org.apache.camel.AsyncProcessor; 025import org.apache.camel.Exchange; 026import org.apache.camel.Expression; 027import org.apache.camel.Message; 028import org.apache.camel.spi.IdAware; 029import org.apache.camel.support.ServiceSupport; 030import org.apache.camel.util.AsyncProcessorHelper; 031 032/** 033 * A processor that sorts the expression using a comparator 034 */ 035public class SortProcessor<T> extends ServiceSupport implements AsyncProcessor, Traceable, IdAware { 036 037 private String id; 038 private final Expression expression; 039 private final Comparator<? super T> comparator; 040 041 public SortProcessor(Expression expression, Comparator<? super T> comparator) { 042 this.expression = expression; 043 this.comparator = comparator; 044 } 045 046 public void process(Exchange exchange) throws Exception { 047 AsyncProcessorHelper.process(this, exchange); 048 } 049 050 @Override 051 public boolean process(Exchange exchange, AsyncCallback callback) { 052 try { 053 Message in = exchange.getIn(); 054 055 @SuppressWarnings("unchecked") 056 List<T> list = expression.evaluate(exchange, List.class); 057 Collections.sort(list, comparator); 058 059 if (exchange.getPattern().isOutCapable()) { 060 Message out = exchange.getOut(); 061 out.copyFrom(in); 062 out.setBody(list); 063 } else { 064 in.setBody(list); 065 } 066 } catch (Exception e) { 067 exchange.setException(e); 068 } 069 070 callback.done(true); 071 return true; 072 } 073 074 public String toString() { 075 return "Sort[" + expression + "]"; 076 } 077 078 @Override 079 public String getTraceLabel() { 080 return "sort[" + expression + "]"; 081 } 082 083 public String getId() { 084 return id; 085 } 086 087 public void setId(String id) { 088 this.id = id; 089 } 090 091 public Expression getExpression() { 092 return expression; 093 } 094 095 public Comparator<? super T> getComparator() { 096 return comparator; 097 } 098 099 @Override 100 protected void doStart() throws Exception { 101 // noop 102 } 103 104 @Override 105 protected void doStop() throws Exception { 106 // noop 107 } 108} 109 110