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.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.queue.QueueComponent;
025    import org.apache.camel.util.ObjectHelper;
026    
027    import java.util.concurrent.Callable;
028    
029    /**
030     * An implementation of {@link EndpointResolver} that creates
031     * {@link JMSEndpoint} objects.
032     *
033     * The syntax for a JMS URI looks like:
034     *
035     * <pre><code>jms:[component:]destination</code></pre>
036     * the component is optional, and if it is not specified, the default component name
037     * is assumed.
038     *
039     * @version $Revision: 520404 $
040     */
041    public class JmsEndpointResolver implements EndpointResolver<JmsExchange> {
042    
043            public static final String DEFAULT_COMPONENT_NAME = QueueComponent.class.getName();
044    
045            /**
046             * Finds the {@see JmsComponent} specified by the uri.  If the {@see JmsComponent}
047             * object do not exist, it will be created.
048             */
049            public Component resolveComponent(CamelContext container, String uri) {
050                    String id[] = getEndpointId(uri);        
051                    return resolveJmsComponent(container, id[0]);
052            }
053    
054            /**
055             * Finds the {@see QueueEndpoint} specified by the uri.  If the {@see QueueEndpoint} or it's associated
056             * {@see QueueComponent} object do not exist, they will be created.
057             */
058            public JmsEndpoint resolveEndpoint(CamelContext container, String uri) {
059                    String id[] = getEndpointId(uri);        
060            JmsComponent component = resolveJmsComponent(container, id[0]);
061            return component.createEndpoint(uri, id[1]);
062        }
063    
064            /**
065             * @return an array that looks like: [componentName,endpointName] 
066             */
067            private String[] getEndpointId(String uri) {
068                    String rc [] = {DEFAULT_COMPONENT_NAME, null};
069                    String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 3);        
070            if( splitURI[2] != null ) {
071                    rc[0] =  splitURI[1];
072                    rc[1] =  splitURI[2];
073            } else {
074                    rc[1] =  splitURI[1];
075            }
076                    return rc;
077            }
078            
079            @SuppressWarnings("unchecked")
080            private JmsComponent resolveJmsComponent(final CamelContext container, final String componentName) {
081            Component rc = container.getOrCreateComponent(componentName, new Callable<JmsComponent>(){
082                            public JmsComponent call() throws Exception {
083                    return new JmsComponent(container);
084                            }});
085            return (JmsComponent) rc;
086            }
087    
088    
089    }