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.util;
018    
019    import java.util.regex.PatternSyntaxException;
020    
021    import org.apache.camel.Endpoint;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.PollingConsumer;
024    import org.apache.camel.Processor;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    
028    /**
029     * Some helper methods for working with {@link Endpoint} instances
030     *
031     * @version $Revision: 771248 $
032     */
033    public final class EndpointHelper {
034    
035        private static final transient Log LOG = LogFactory.getLog(EndpointHelper.class);
036    
037        private EndpointHelper() {
038            //Utility Class
039        }
040        /**
041         * Creates a {@link PollingConsumer} and polls all pending messages on the endpoint
042         * and invokes the given {@link Processor} to process each {@link Exchange} and then closes
043         * down the consumer and throws any exceptions thrown.
044         */
045        public static void pollEndpoint(Endpoint endpoint, Processor processor, long timeout) throws Exception {
046            PollingConsumer consumer = endpoint.createPollingConsumer();
047            try {
048                consumer.start();
049    
050                while (true) {
051                    Exchange exchange = consumer.receive(timeout);
052                    if (exchange == null) {
053                        break;
054                    } else {
055                        processor.process(exchange);
056                    }
057                }
058            } finally {
059                try {
060                    consumer.stop();
061                } catch (Exception e) {
062                    LOG.warn("Failed to stop PollingConsumer: " + e, e);
063                }
064            }
065        }
066    
067        /**
068         * Creates a {@link PollingConsumer} and polls all pending messages on the
069         * endpoint and invokes the given {@link Processor} to process each
070         * {@link Exchange} and then closes down the consumer and throws any
071         * exceptions thrown.
072         */
073        public static void pollEndpoint(Endpoint endpoint, Processor processor) throws Exception {
074            pollEndpoint(endpoint, processor, 1000L);
075        }
076    
077        /**
078         * Matches the endpoint with the given pattern.
079         * <p/>
080         * The match rules are applied in this order:
081         * <ul>
082         *   <li>excact match, returns true</li>
083         *   <li>wildcard match (pattern ends with a * and the uri starts with the pattern), returns true</li>
084         *   <li>regular expression match, returns true</li>
085         *   <li>otherwise returns false</li>
086         * </ul>
087         *
088         * @param uri  the endpoint uri
089         * @param pattern a pattern to match
090         * @return <tt>true</tt> if match, <tt>false</tt> otherwise.
091         */
092        public static boolean matchEndpoint(String uri, String pattern) {
093            if (uri.equals(pattern)) {
094                // excact match
095                return true;
096            }
097    
098            // we have wildcard support in that hence you can match with: file* to match any file endpoints
099            if (pattern.endsWith("*") && uri.startsWith(pattern.substring(0, pattern.length() - 1))) {
100                return true;
101            }
102    
103            // match by regular expression
104            try {
105                if (uri.matches(pattern)) {
106                    return true;
107                }
108            } catch (PatternSyntaxException e) {
109                // ignore
110            }
111            
112            // no match
113            return false;
114        }
115    
116    }