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.Iterator;
020 import java.util.Map;
021 import java.util.regex.PatternSyntaxException;
022
023 import org.apache.camel.CamelContext;
024 import org.apache.camel.Endpoint;
025 import org.apache.camel.Exchange;
026 import org.apache.camel.PollingConsumer;
027 import org.apache.camel.Processor;
028 import org.apache.commons.logging.Log;
029 import org.apache.commons.logging.LogFactory;
030
031 /**
032 * Some helper methods for working with {@link Endpoint} instances
033 *
034 * @version $Revision: 795369 $
035 */
036 public final class EndpointHelper {
037
038 private static final transient Log LOG = LogFactory.getLog(EndpointHelper.class);
039
040 private EndpointHelper() {
041 //Utility Class
042 }
043 /**
044 * Creates a {@link PollingConsumer} and polls all pending messages on the endpoint
045 * and invokes the given {@link Processor} to process each {@link Exchange} and then closes
046 * down the consumer and throws any exceptions thrown.
047 */
048 public static void pollEndpoint(Endpoint endpoint, Processor processor, long timeout) throws Exception {
049 PollingConsumer consumer = endpoint.createPollingConsumer();
050 try {
051 consumer.start();
052
053 while (true) {
054 Exchange exchange = consumer.receive(timeout);
055 if (exchange == null) {
056 break;
057 } else {
058 processor.process(exchange);
059 }
060 }
061 } finally {
062 try {
063 consumer.stop();
064 } catch (Exception e) {
065 LOG.warn("Failed to stop PollingConsumer: " + e, e);
066 }
067 }
068 }
069
070 /**
071 * Creates a {@link PollingConsumer} and polls all pending messages on the
072 * endpoint and invokes the given {@link Processor} to process each
073 * {@link Exchange} and then closes down the consumer and throws any
074 * exceptions thrown.
075 */
076 public static void pollEndpoint(Endpoint endpoint, Processor processor) throws Exception {
077 pollEndpoint(endpoint, processor, 1000L);
078 }
079
080 /**
081 * Matches the endpoint with the given pattern.
082 * <p/>
083 * The match rules are applied in this order:
084 * <ul>
085 * <li>excact match, returns true</li>
086 * <li>wildcard match (pattern ends with a * and the uri starts with the pattern), returns true</li>
087 * <li>regular expression match, returns true</li>
088 * <li>otherwise returns false</li>
089 * </ul>
090 *
091 * @param uri the endpoint uri
092 * @param pattern a pattern to match
093 * @return <tt>true</tt> if match, <tt>false</tt> otherwise.
094 */
095 public static boolean matchEndpoint(String uri, String pattern) {
096 // we need to test with and without scheme separators (//)
097 if (uri.indexOf("://") != -1) {
098 // try without :// also
099 String scheme = ObjectHelper.before(uri, "://");
100 String path = ObjectHelper.after(uri, "://");
101 if (doMatchEndpoint(scheme + ":" + path, pattern)) {
102 return true;
103 }
104 } else {
105 // try with :// also
106 String scheme = ObjectHelper.before(uri, ":");
107 String path = ObjectHelper.after(uri, ":");
108 if (doMatchEndpoint(scheme + "://" + path, pattern)) {
109 return true;
110 }
111 }
112
113 // and fallback to test with the uri as is
114 return doMatchEndpoint(uri, pattern);
115 }
116
117
118 private static boolean doMatchEndpoint(String uri, String pattern) {
119 if (uri.equals(pattern)) {
120 // excact match
121 return true;
122 }
123
124 // we have wildcard support in that hence you can match with: file* to match any file endpoints
125 if (pattern.endsWith("*") && uri.startsWith(pattern.substring(0, pattern.length() - 1))) {
126 return true;
127 }
128
129 // match by regular expression
130 try {
131 if (uri.matches(pattern)) {
132 return true;
133 }
134 } catch (PatternSyntaxException e) {
135 // ignore
136 }
137
138 // no match
139 return false;
140 }
141
142 /**
143 * Sets the regular properties on the given bean
144 *
145 * @param context the camel context
146 * @param bean the bean
147 * @param parameters parameters
148 * @throws Exception is thrown if setting property fails
149 */
150 public static void setProperties(CamelContext context, Object bean, Map parameters) throws Exception {
151 IntrospectionSupport.setProperties(context.getTypeConverter(), bean, parameters);
152 }
153
154 /**
155 * Sets the reference properties on the given bean
156 * <p/>
157 * This is convention over configuration, setting all reference parameters (using {@link #isReferenceParameter(String)}
158 * by looking it up in registry and setting it on the bean if possible.
159 *
160 * @param context the camel context
161 * @param bean the bean
162 * @param parameters parameters
163 * @throws Exception is thrown if setting property fails
164 */
165 @SuppressWarnings("unchecked")
166 public static void setReferenceProperties(CamelContext context, Object bean, Map parameters) throws Exception {
167 Iterator<Map.Entry> it = parameters.entrySet().iterator();
168 while (it.hasNext()) {
169 Map.Entry entry = it.next();
170 Object key = entry.getKey();
171 Object v = entry.getValue();
172 String value = v != null ? v.toString() : null;
173 if (value != null && isReferenceParameter(value)) {
174 Object ref = context.getRegistry().lookup(value.substring(1));
175 String name = key.toString();
176 if (ref != null) {
177 boolean hit = IntrospectionSupport.setProperty(context.getTypeConverter(), bean, name, ref);
178 if (hit) {
179 if (LOG.isDebugEnabled()) {
180 LOG.debug("Configued property: " + name + " on bean: " + bean + " with value: " + ref);
181 }
182 // must remove as its a valid option and we could configure it
183 it.remove();
184 }
185 }
186 }
187 }
188 }
189
190 /**
191 * Is the given parameter a reference parameter (starting with a # char)
192 *
193 * @param parameter the parameter
194 * @return <tt>true</tt> if its a reference parameter
195 */
196 public static boolean isReferenceParameter(String parameter) {
197 return parameter != null && parameter.startsWith("#");
198 }
199
200 }