KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > drools > dsl > RouteConsequenceFactory


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.components.drools.dsl;
18
19 import org.apache.servicemix.components.drools.JbiHelper;
20 import org.drools.rule.Rule;
21 import org.drools.smf.Configuration;
22 import org.drools.smf.ConsequenceFactory;
23 import org.drools.smf.FactoryException;
24 import org.drools.spi.Consequence;
25 import org.drools.spi.RuleBaseContext;
26 import org.drools.spi.Tuple;
27
28 import javax.jbi.messaging.MessagingException;
29 import javax.xml.namespace.QName JavaDoc;
30
31 /**
32  * @version $Revision: 359186 $
33  */

34 public class RouteConsequenceFactory implements ConsequenceFactory {
35     public Consequence newConsequence(Rule rule, RuleBaseContext ruleBaseContext, Configuration configuration) throws FactoryException {
36         final QName JavaDoc operation = toQName(configuration, configuration.getAttribute("operation"));
37         final QName JavaDoc service = toQName(configuration, configuration.getAttribute("service"));
38         final QName JavaDoc interfaceName = toQName(configuration, configuration.getAttribute("interface"));
39         return new JbiConsequence() {
40             protected void invokeJbiOperation(JbiHelper helper, Tuple tuple) throws MessagingException {
41                 helper.route(service, operation, interfaceName);
42             }
43         };
44     }
45
46     /**
47      * Converts the String into a QName
48      */

49     protected QName JavaDoc toQName(Configuration configuration, String JavaDoc text) {
50         if (text == null) {
51             return null;
52         }
53         String JavaDoc[] names = configuration.getAttributeNames();
54         String JavaDoc localPart = text;
55         String JavaDoc prefix = null;
56         int idx = text.indexOf(':');
57         if (idx >= 0) {
58             prefix = "xmlns:" + text.substring(0, idx);
59             localPart = text.substring(idx + 1);
60         }
61         String JavaDoc uri = "";
62         for (int i = 0; i < names.length; i++) {
63             String JavaDoc name = names[i];
64             if (prefix == null) {
65                 if ("xmlns".equals(name)) {
66                     uri = configuration.getAttribute(name);
67                     break;
68                 }
69             }
70             else {
71                 if (name.equals(prefix)) {
72                     uri = configuration.getAttribute(name);
73                     break;
74                 }
75             }
76         }
77         System.out.println("Creating QName with uri: " + uri + " name: " + localPart);
78         return new QName JavaDoc(uri, localPart);
79     }
80 }
81
Popular Tags