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