001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.jms;
019    
020    import org.apache.axis.transport.jms.JMSEndpoint;
021    import org.apache.camel.CamelContext;
022    import org.apache.camel.Component;
023    import org.apache.camel.EndpointResolver;
024    import org.apache.camel.util.ObjectHelper;
025    
026    import java.util.concurrent.Callable;
027    
028    /**
029     * An implementation of {@link EndpointResolver} that creates
030     * {@link JMSEndpoint} objects.
031     * <p/>
032     * The syntax for a JMS URI looks like:
033     * <p/>
034     * <pre><code>jms:[component:]destination</code></pre>
035     * the component is optional, and if it is not specified, the default component name
036     * is assumed.
037     *
038     * @version $Revision:520964 $
039     */
040    public class JmsEndpointResolver implements EndpointResolver<JmsExchange> {
041        public static final String DEFAULT_COMPONENT_NAME = JmsEndpointResolver.class.getName();
042    
043        /**
044         * Finds the {@see JmsComponent} specified by the uri.  If the {@see JmsComponent}
045         * object do not exist, it will be created.
046         */
047        public Component resolveComponent(CamelContext container, String uri) {
048            String id[] = getEndpointId(uri);
049            return resolveJmsComponent(container, id[0]);
050        }
051    
052        /**
053         * Finds the {@see QueueEndpoint} specified by the uri.  If the {@see QueueEndpoint} or it's associated
054         * {@see QueueComponent} object do not exist, they will be created.
055         */
056        public JmsEndpoint resolveEndpoint(CamelContext container, String uri) {
057            String id[] = getEndpointId(uri);
058            JmsComponent component = resolveJmsComponent(container, id[0]);
059            return component.createEndpoint(uri, id[1]);
060        }
061    
062        /**
063         * @return an array that looks like: [componentName,endpointName]
064         */
065        private String[] getEndpointId(String uri) {
066            String rc[] = {DEFAULT_COMPONENT_NAME, null};
067            String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 3);
068            if (splitURI[2] != null) {
069                rc[0] = splitURI[1];
070                rc[1] = splitURI[2];
071            }
072            else {
073                rc[1] = splitURI[1];
074            }
075            return rc;
076        }
077    
078        @SuppressWarnings("unchecked")
079        private JmsComponent resolveJmsComponent(final CamelContext container, final String componentName) {
080            Component rc = container.getOrCreateComponent(componentName, new Callable() {
081                public JmsComponent call() throws Exception {
082                    return new JmsComponent(container);
083                }
084            });
085            return (JmsComponent) rc;
086        }
087    
088    
089    }