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