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.ArrayList;
020    import java.util.Arrays;
021    import java.util.Collections;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.regex.PatternSyntaxException;
026    
027    import org.apache.camel.CamelContext;
028    import org.apache.camel.Endpoint;
029    import org.apache.camel.Exchange;
030    import org.apache.camel.PollingConsumer;
031    import org.apache.camel.Processor;
032    import org.apache.camel.ResolveEndpointFailedException;
033    import org.apache.commons.logging.Log;
034    import org.apache.commons.logging.LogFactory;
035    
036    /**
037     * Some helper methods for working with {@link Endpoint} instances
038     *
039     * @version $Revision: 900309 $
040     */
041    public final class EndpointHelper {
042    
043        private static final transient Log LOG = LogFactory.getLog(EndpointHelper.class);
044    
045        private EndpointHelper() {
046            //Utility Class
047        }
048    
049        /**
050         * Creates a {@link PollingConsumer} and polls all pending messages on the endpoint
051         * and invokes the given {@link Processor} to process each {@link Exchange} and then closes
052         * down the consumer and throws any exceptions thrown.
053         */
054        public static void pollEndpoint(Endpoint endpoint, Processor processor, long timeout) throws Exception {
055            PollingConsumer consumer = endpoint.createPollingConsumer();
056            try {
057                consumer.start();
058    
059                while (true) {
060                    Exchange exchange = consumer.receive(timeout);
061                    if (exchange == null) {
062                        break;
063                    } else {
064                        processor.process(exchange);
065                    }
066                }
067            } finally {
068                try {
069                    consumer.stop();
070                } catch (Exception e) {
071                    LOG.warn("Failed to stop PollingConsumer: " + e, e);
072                }
073            }
074        }
075    
076        /**
077         * Creates a {@link PollingConsumer} and polls all pending messages on the
078         * endpoint and invokes the given {@link Processor} to process each
079         * {@link Exchange} and then closes down the consumer and throws any
080         * exceptions thrown.
081         */
082        public static void pollEndpoint(Endpoint endpoint, Processor processor) throws Exception {
083            pollEndpoint(endpoint, processor, 1000L);
084        }
085    
086        /**
087         * Matches the endpoint with the given pattern.
088         * <p/>
089         * The match rules are applied in this order:
090         * <ul>
091         *   <li>exact match, returns true</li>
092         *   <li>wildcard match (pattern ends with a * and the uri starts with the pattern), returns true</li>
093         *   <li>regular expression match, returns true</li>
094         *   <li>otherwise returns false</li>
095         * </ul>
096         *
097         * @param uri  the endpoint uri
098         * @param pattern a pattern to match
099         * @return <tt>true</tt> if match, <tt>false</tt> otherwise.
100         */
101        public static boolean matchEndpoint(String uri, String pattern) {
102            // normalize uri so we can do endpoint hits with minor mistakes and parameters is not in the same order
103            try {
104                uri = URISupport.normalizeUri(uri);
105            } catch (Exception e) {
106                throw new ResolveEndpointFailedException(uri, e);
107            }
108    
109            // we need to test with and without scheme separators (//)
110            if (uri.indexOf("://") != -1) {
111                // try without :// also
112                String scheme = ObjectHelper.before(uri, "://");
113                String path = ObjectHelper.after(uri, "://");
114                if (doMatchEndpoint(scheme + ":" + path, pattern)) {
115                    return true;
116                }
117            } else {
118                // try with :// also
119                String scheme = ObjectHelper.before(uri, ":");
120                String path = ObjectHelper.after(uri, ":");
121                if (doMatchEndpoint(scheme + "://" + path, pattern)) {
122                    return true;
123                }
124            }
125    
126            // and fallback to test with the uri as is
127            return doMatchEndpoint(uri, pattern);
128        }
129    
130    
131        private static boolean doMatchEndpoint(String uri, String pattern) {
132            if (uri.equals(pattern)) {
133                // exact match
134                return true;
135            }
136    
137            // we have wildcard support in that hence you can match with: file* to match any file endpoints
138            if (pattern.endsWith("*") && uri.startsWith(pattern.substring(0, pattern.length() - 1))) {
139                return true;
140            }
141    
142            // match by regular expression
143            try {
144                if (uri.matches(pattern)) {
145                    return true;
146                }
147            } catch (PatternSyntaxException e) {
148                // ignore
149            }
150            
151            // no match
152            return false;
153        }
154    
155        /**
156         * Sets the regular properties on the given bean
157         *
158         * @param context the camel context
159         * @param bean the bean
160         * @param parameters parameters
161         * @throws Exception is thrown if setting property fails
162         */
163        public static void setProperties(CamelContext context, Object bean, Map<String, Object> parameters) throws Exception {
164            IntrospectionSupport.setProperties(context.getTypeConverter(), bean, parameters);
165        }
166    
167        /**
168         * Sets the reference properties on the given bean
169         * <p/>
170         * This is convention over configuration, setting all reference parameters (using {@link #isReferenceParameter(String)}
171         * by looking it up in registry and setting it on the bean if possible.
172         *
173         * @param context the camel context
174         * @param bean the bean
175         * @param parameters parameters
176         * @throws Exception is thrown if setting property fails
177         */
178        public static void setReferenceProperties(CamelContext context, Object bean, Map<String, Object> parameters) throws Exception {
179            Iterator<Map.Entry<String, Object>> it = parameters.entrySet().iterator();
180            while (it.hasNext()) {
181                Map.Entry<String, Object> entry = it.next();
182                String name = entry.getKey();
183                Object v = entry.getValue();
184                String value = v != null ? v.toString() : null;
185                if (value != null && isReferenceParameter(value)) {
186                    // For backwards-compatibility reasons, no mandatory lookup is done here
187                    Object ref = resolveReferenceParameter(context, value, Object.class, false);
188                    if (ref != null) {
189                        boolean hit = IntrospectionSupport.setProperty(context.getTypeConverter(), bean, name, ref);
190                        if (hit) {
191                            if (LOG.isDebugEnabled()) {
192                                LOG.debug("Configured property: " + name + " on bean: " + bean + " with value: " + ref);
193                            }
194                            // must remove as its a valid option and we could configure it
195                            it.remove();
196                        }
197                    }
198                }
199            }
200        }
201    
202        /**
203         * Is the given parameter a reference parameter (starting with a # char)
204         *
205         * @param parameter the parameter
206         * @return <tt>true</tt> if its a reference parameter
207         */
208        public static boolean isReferenceParameter(String parameter) {
209            return parameter != null && parameter.trim().startsWith("#");
210        }
211        
212        /**
213         * Resolves a reference parameter by making a lookup in the registry.
214         * 
215         * @param <T> type of object to lookup.
216         * @param context Camel context to use for lookup.
217         * @param value reference parameter value.
218         * @param type type of object to lookup.
219         * @return lookup result. 
220         * @throws IllegalArgumentException if referenced object was not found in 
221         *         registry.
222         */
223        public static <T> T resolveReferenceParameter(CamelContext context, String value, Class<T> type) {
224            return resolveReferenceParameter(context, value, type, true);
225        }
226    
227        /**
228         * Resolves a reference parameter by making a lookup in the registry.
229         * 
230         * @param <T> type of object to lookup.
231         * @param context Camel context to use for lookup.
232         * @param value reference parameter value.
233         * @param type type of object to lookup.
234         * @return lookup result (or <code>null</code> only if 
235         *         <code>mandatory</code> is <code>false</code>). 
236         * @throws IllegalArgumentException if object was not found in registry and 
237         *         <code>mandatory</code> is <code>true</code>.
238         */
239        public static <T> T resolveReferenceParameter(CamelContext context, String value, Class<T> type, boolean mandatory) {
240            String valueNoHash = value.replaceAll("#", "");
241            if (mandatory) {
242                return CamelContextHelper.mandatoryLookup(context, valueNoHash, type);
243            } else {
244                return CamelContextHelper.lookup(context, valueNoHash, type);
245            }
246        }
247    
248        /**
249         * Resolves a reference list parameter by making lookups in the registry.
250         * The parameter value must be one of the following:
251         * <ul>
252         * <li>a comma-separated list of references to beans of type T</li>
253         * <li>a single reference to a bean type T</li>
254         * <li>a single reference to a bean of type java.util.List</li>
255         * </ul>
256         * 
257         * @param context
258         *            Camel context to use for lookup.
259         * @param value
260         *            reference parameter value.
261         * @param elementType
262         *            result list element type.
263         * @return list of lookup results.
264         * @throws IllegalArgumentException if any referenced object was not found 
265         *         in registry.
266         */
267        @SuppressWarnings("unchecked")
268        public static <T> List<T> resolveReferenceListParameter(CamelContext context, String value, Class<T> elementType) {
269            if (value == null) {
270                return Collections.emptyList();
271            }
272            List<String> elements = Arrays.asList(value.split(","));
273            if (elements.size() == 1) {
274                Object bean = resolveReferenceParameter(context, elements.get(0).trim(), Object.class);
275                if (bean instanceof List) {
276                    // The bean is a list
277                    return (List)bean;
278                } else {
279                    // The bean is a list element
280                    return Arrays.asList(elementType.cast(bean));
281                }
282            } else { // more than one list element
283                ArrayList<T> result = new ArrayList<T>(elements.size());
284                for (String element : elements) {
285                    result.add(resolveReferenceParameter(context, element.trim(), elementType));
286                }
287                return result;
288            }
289        }
290        
291    }