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.servicemix.jbi.nmr.flow;
018    
019    import java.io.IOException;
020    import java.net.URISyntaxException;
021    import java.util.Map;
022    
023    import javax.jbi.JBIException;
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.apache.servicemix.finder.FactoryFinder;
028    import org.apache.servicemix.jbi.util.IntrospectionSupport;
029    import org.apache.servicemix.jbi.util.URISupport;
030    
031    /**
032     * Find a Flow by Name
033     * 
034     * @version $Revision: 564900 $
035     */
036    public final class FlowProvider {
037    
038        private static final Log LOG = LogFactory.getLog(FlowProvider.class);
039    
040        private static final FactoryFinder FINDER = new FactoryFinder("META-INF/services/org/apache/servicemix/jbi/nmr/flow/");
041    
042        private FlowProvider() {
043        }
044    
045        /**
046         * Locate a Flow
047         * 
048         * @param flow
049         * @return the Flow
050         * @throws JBIException
051         */
052        public static Flow getFlow(String flow) throws JBIException {
053            Object value;
054            String flowName = getFlowName(flow);
055            try {
056                value = FINDER.newInstance(flowName);
057                if (value instanceof Flow) {
058                    String query = getQuery(flow);
059                    if (query != null) {
060                        Map map = URISupport.parseQuery(query);
061                        if (map != null && !map.isEmpty()) {
062                            IntrospectionSupport.setProperties(value, map);
063                        }
064                    }
065                    return (Flow) value;
066                }
067                throw new JBIException("No implementation found for: " + flow);
068            } catch (IllegalAccessException e) {
069                LOG.error("getFlow(" + flow + " failed: " + e, e);
070                throw new JBIException(e);
071            } catch (InstantiationException e) {
072                LOG.error("getFlow(" + flow + " failed: " + e, e);
073                throw new JBIException(e);
074            } catch (IOException e) {
075                LOG.error("getFlow(" + flow + " failed: " + e, e);
076                throw new JBIException(e);
077            } catch (ClassNotFoundException e) {
078                LOG.error("getFlow(" + flow + " failed: " + e, e);
079                throw new JBIException(e);
080            } catch (URISyntaxException e) {
081                LOG.error("getFlow(" + flow + " failed: " + e, e);
082                throw new JBIException(e);
083            }
084        }
085    
086        public static String getFlowName(String str) {
087            String result = str;
088            int index = str.indexOf('?');
089            if (index >= 0) {
090                result = str.substring(0, index);
091            }
092            return result;
093        }
094    
095        protected static String getQuery(String str) {
096            String result = null;
097            int index = str.indexOf('?');
098            if (index >= 0 && (index + 1) < str.length()) {
099                result = str.substring(index + 1);
100            }
101            return result;
102        }
103    }