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.component.pojo;
018    
019    import org.apache.camel.CamelContext;
020    import org.apache.camel.Component;
021    import org.apache.camel.EndpointResolver;
022    import org.apache.camel.util.ObjectHelper;
023    
024    /**
025     * An implementation of {@link EndpointResolver} that creates 
026     * {@link PojoEndpoint} objects.
027     *
028     * The synatax for a Pojo URI looks like:
029     * 
030     * <pre><code>pojo:component:queuename</code></pre>
031     * 
032     * @version $Revision: 519901 $
033     */
034    public class PojoEndpointResolver implements EndpointResolver<PojoExchange> {
035                    
036            /**
037             * Finds the {@see QueueComponent} specified by the uri.  If the {@see QueueComponent} 
038             * object do not exist, it will be created.
039             * 
040             * @see org.apache.camel.EndpointResolver#resolveComponent(org.apache.camel.CamelContext, java.lang.String)
041             */
042            public Component resolveComponent(CamelContext container, String uri) {
043                    String id[] = getEndpointId(uri);        
044                    return resolveQueueComponent(container, id[0]);
045            }
046    
047            /**
048             * Finds the {@see QueueEndpoint} specified by the uri.  If the {@see QueueEndpoint} or it's associated
049             * {@see QueueComponent} object do not exist, they will be created.
050             * 
051             * @see org.apache.camel.EndpointResolver#resolveEndpoint(org.apache.camel.CamelContext, java.lang.String)
052             */
053            public PojoEndpoint resolveEndpoint(CamelContext container, String uri) {
054                    String id[] = getEndpointId(uri);        
055            PojoComponent component = resolveQueueComponent(container, id[0]);        
056            Object pojo = component.lookupRegisteredPojo(id[1]);
057                    return new PojoEndpoint(uri, container, component, pojo);
058        }
059    
060            private PojoComponent resolveQueueComponent(CamelContext container, String componentName) {
061            Component rc = container.getComponent(componentName);
062            if( rc == null ) {
063                    throw new IllegalArgumentException("Invalid URI, pojo component does not exist: "+componentName);
064            }
065            return (PojoComponent) rc;
066            }
067    
068            /**
069             * @return an array that looks like: [componentName,endpointName] 
070             */
071            private String[] getEndpointId(String uri) {
072                    String rc [] = {null, null};
073                    String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 3);        
074            if( splitURI[2] != null ) {
075                    rc[0] =  splitURI[1];
076                    rc[1] =  splitURI[2];
077            } else {
078                    throw new IllegalArgumentException("Invalid URI, component not specified in URI: "+uri);
079            }
080                    return rc;
081            }
082    
083    }