KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > nmr > flow > FlowProvider


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.nmr.flow;
18
19 import java.io.IOException JavaDoc;
20 import java.net.URISyntaxException JavaDoc;
21 import java.util.Map JavaDoc;
22 import javax.jbi.JBIException;
23 import org.apache.activeio.util.FactoryFinder;
24 import org.apache.activemq.util.IntrospectionSupport;
25 import org.apache.activemq.util.URISupport;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * Find a Flow by Name
31  *
32  * @version $Revision: 426415 $
33  */

34 public class FlowProvider{
35     private static final Log log=LogFactory.getLog(FlowProvider.class);
36     private static FactoryFinder finder=new FactoryFinder("META-INF/services/org/apache/servicemix/jbi/nmr/flow/");
37
38     /**
39      * Locate a Flow
40      *
41      * @param flow
42      * @return the Flow
43      * @throws JBIException
44      */

45     public static Flow getFlow(String JavaDoc flow) throws JBIException{
46         Object JavaDoc value;
47         String JavaDoc flowName=getFlowName(flow);
48         try{
49             value=finder.newInstance(flowName);
50             if(value!=null&&value instanceof Flow){
51                 String JavaDoc query=getQuery(flow);
52                 if(query!=null){
53                     Map JavaDoc map=URISupport.parseQuery(query);
54                     if(map!=null&&!map.isEmpty()){
55                         IntrospectionSupport.setProperties(value,map);
56                     }
57                 }
58                 return (Flow) value;
59             }
60             throw new JBIException("No implementation found for: "+flow);
61         }catch(IllegalAccessException JavaDoc e){
62             log.error("getFlow("+flow+" failed: "+e,e);
63             throw new JBIException(e);
64         }catch(InstantiationException JavaDoc e){
65             log.error("getFlow("+flow+" failed: "+e,e);
66             throw new JBIException(e);
67         }catch(IOException JavaDoc e){
68             log.error("getFlow("+flow+" failed: "+e,e);
69             throw new JBIException(e);
70         }catch(ClassNotFoundException JavaDoc e){
71             log.error("getFlow("+flow+" failed: "+e,e);
72             throw new JBIException(e);
73         }catch(URISyntaxException JavaDoc e){
74             log.error("getFlow("+flow+" failed: "+e,e);
75             throw new JBIException(e);
76         }
77     }
78
79     public static String JavaDoc getFlowName(String JavaDoc str){
80         String JavaDoc result=str;
81         int index=str.indexOf('?');
82         if(index>=0){
83             result=str.substring(0,index);
84         }
85         return result;
86     }
87
88     protected static String JavaDoc getQuery(String JavaDoc str){
89         String JavaDoc result=null;
90         int index=str.indexOf('?');
91         if(index>=0&&(index+1)<str.length()){
92             result=str.substring(index+1);
93         }
94         return result;
95     }
96 }
Popular Tags