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.file;
018    
019    import java.util.Comparator;
020    import java.util.Iterator;
021    import java.util.Map;
022    
023    import org.apache.camel.CamelContext;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.impl.DefaultComponent;
026    import org.apache.camel.util.CastUtils;
027    import org.apache.camel.util.EndpointHelper;
028    import org.apache.camel.util.ObjectHelper;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    import static org.apache.camel.util.ObjectHelper.isNotEmpty;
032    
033    /**
034     * Base class file component. To be extended.
035     */
036    public abstract class GenericFileComponent<T> extends DefaultComponent {
037    
038        protected Logger log = LoggerFactory.getLogger(getClass());
039    
040        public GenericFileComponent() {
041        }
042    
043        public GenericFileComponent(CamelContext context) {
044            super(context);
045        }
046    
047        protected GenericFileEndpoint<T> createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
048    
049            // create the correct endpoint based on the protocol
050            final GenericFileEndpoint<T> endpoint;
051    
052            // call to subclasses to build their custom version of a GenericFileEndpoint
053            endpoint = buildFileEndpoint(uri, remaining, parameters);
054    
055            // sort by using file language
056            String sortBy = getAndRemoveParameter(parameters, "sortBy", String.class);
057            if (isNotEmpty(sortBy) && !EndpointHelper.isReferenceParameter(sortBy)) {
058                // we support nested sort groups so they should be chained
059                String[] groups = sortBy.split(";");
060                Iterator<String> it = CastUtils.cast(ObjectHelper.createIterator(groups));
061                Comparator<Exchange> comparator = createSortByComparator(it);
062                endpoint.setSortBy(comparator);
063            }
064            setProperties(endpoint.getConfiguration(), parameters);
065            setProperties(endpoint, parameters);
066    
067            afterPropertiesSet(endpoint);
068    
069            return endpoint;
070        }
071    
072        /**
073         * A factory method for derived file components to create the endpoint
074         *
075         * @param uri the full URI of the endpoint
076         * @param remaining the remaining part of the URI without the query
077         *                parameters or component prefix
078         * @param parameters the optional parameters passed in
079         * @return a newly created endpoint or null if the endpoint cannot be
080         *         created based on the inputs
081         * @throws Exception can be thrown
082         */
083        protected abstract GenericFileEndpoint<T> buildFileEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception;
084    
085        /**
086         * A factory method for derived file components to perform validation of properties
087         *
088         * @param endpoint the endpoint
089         * @throws Exception can be thrown in case of validation errors
090         */
091        protected abstract void afterPropertiesSet(GenericFileEndpoint<T> endpoint) throws Exception;
092    
093        /**
094         * Helper to create a sort comparator
095         *
096         * @param it iterator
097         * @return Comparator<Exchange>
098         */
099        private Comparator<Exchange> createSortByComparator(Iterator<String> it) {
100            if (!it.hasNext()) {
101                return null;
102            }
103    
104            String group = it.next();
105    
106            boolean reverse = group.startsWith("reverse:");
107            String reminder = reverse ? ifStartsWithReturnRemainder("reverse:", group) : group;
108    
109            boolean ignoreCase = reminder.startsWith("ignoreCase:");
110            reminder = ignoreCase ? ifStartsWithReturnRemainder("ignoreCase:", reminder) : reminder;
111    
112            ObjectHelper.notEmpty(reminder, "sortBy expression", this);
113    
114            // recursive add nested sorters
115            return GenericFileDefaultSorter.sortByFileLanguage(getCamelContext(), 
116                reminder, reverse, ignoreCase, createSortByComparator(it));
117        }
118    }