KickJava   Java API By Example, From Geeks To Geeks.

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


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: 426415 $
33  */

34 public class ForwardConsequenceFactory implements ConsequenceFactory {
35     public Consequence newConsequence(Rule rule, RuleBaseContext ruleBaseContext, final Configuration configuration) throws FactoryException {
36         final String JavaDoc[] services = configuration.getAttribute("service").split(",");
37         return new JbiConsequence() {
38             protected void invokeJbiOperation(JbiHelper helper, Tuple tuple) throws MessagingException {
39                 for (int i = 0; i < services.length; i++) {
40                     QName JavaDoc service = toQName(configuration, services[i]);
41                     helper.forwardToService(service, null, null);
42                 }
43             }
44         };
45     }
46
47     /**
48      * Converts the String into a QName
49      */

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