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
039: .getLog(FlowProvider.class);
040:
041: private static final FactoryFinder FINDER = new FactoryFinder(
042: "META-INF/services/org/apache/servicemix/jbi/nmr/flow/");
043:
044: private FlowProvider() {
045: }
046:
047: /**
048: * Locate a Flow
049: *
050: * @param flow
051: * @return the Flow
052: * @throws JBIException
053: */
054: public static Flow getFlow(String flow) throws JBIException {
055: Object value;
056: String flowName = getFlowName(flow);
057: try {
058: value = FINDER.newInstance(flowName);
059: if (value instanceof Flow) {
060: String query = getQuery(flow);
061: if (query != null) {
062: Map map = URISupport.parseQuery(query);
063: if (map != null && !map.isEmpty()) {
064: IntrospectionSupport.setProperties(value, map);
065: }
066: }
067: return (Flow) value;
068: }
069: throw new JBIException("No implementation found for: "
070: + flow);
071: } catch (IllegalAccessException e) {
072: LOG.error("getFlow(" + flow + " failed: " + e, e);
073: throw new JBIException(e);
074: } catch (InstantiationException e) {
075: LOG.error("getFlow(" + flow + " failed: " + e, e);
076: throw new JBIException(e);
077: } catch (IOException e) {
078: LOG.error("getFlow(" + flow + " failed: " + e, e);
079: throw new JBIException(e);
080: } catch (ClassNotFoundException e) {
081: LOG.error("getFlow(" + flow + " failed: " + e, e);
082: throw new JBIException(e);
083: } catch (URISyntaxException e) {
084: LOG.error("getFlow(" + flow + " failed: " + e, e);
085: throw new JBIException(e);
086: }
087: }
088:
089: public static String getFlowName(String str) {
090: String result = str;
091: int index = str.indexOf('?');
092: if (index >= 0) {
093: result = str.substring(0, index);
094: }
095: return result;
096: }
097:
098: protected static String getQuery(String str) {
099: String result = null;
100: int index = str.indexOf('?');
101: if (index >= 0 && (index + 1) < str.length()) {
102: result = str.substring(index + 1);
103: }
104: return result;
105: }
106: }
|