Author: deepal
Date: Mon Oct 24 20:40:10 2005
New Revision: 328247

URL: http://svn.apache.org/viewcvs?rev=328247&view=rev
Log:
Introduced a class called SynapseEngine which currently does RuleEngine 
population. And the most ineteredted thing is that you can dynamically change 
rules (mediator hot deployment), RuleEngine etc...

Added:
    
incubator/synapse/trunk/scratch/SynapseToy/core/src/org/apache/synapse/SynapseEngine.java
Modified:
    incubator/synapse/trunk/scratch/SynapseToy/core/src/conf/synapse.xml
    
incubator/synapse/trunk/scratch/SynapseToy/core/src/org/apache/synapse/SynapseServlet.java
    incubator/synapse/trunk/scratch/SynapseToy/dist/Synapse.mar
    incubator/synapse/trunk/scratch/SynapseToy/dist/Synapse.war
    
incubator/synapse/trunk/scratch/SynapseToy/synapse_module/src/org/apache/synapse/SynapseModule.java

Modified: incubator/synapse/trunk/scratch/SynapseToy/core/src/conf/synapse.xml
URL: 
http://svn.apache.org/viewcvs/incubator/synapse/trunk/scratch/SynapseToy/core/src/conf/synapse.xml?rev=328247&r1=328246&r2=328247&view=diff
==============================================================================
--- incubator/synapse/trunk/scratch/SynapseToy/core/src/conf/synapse.xml 
(original)
+++ incubator/synapse/trunk/scratch/SynapseToy/core/src/conf/synapse.xml Mon 
Oct 24 20:40:10 2005
@@ -1,5 +1,7 @@
 <synapse>

     <ruleengine class="org.apache.synapse.impl.SynapseDefaultRuleEngine"/>

+

+    <searchTime value="60000"/>

     

     <rule id="Rule1" mediator="org.apache.synapse.mediators.LogMediator"/>

     <rule id="Rule2" 
mediator="org.apache.synapse.mediators.ValueCheckMediator"/>


Added: 
incubator/synapse/trunk/scratch/SynapseToy/core/src/org/apache/synapse/SynapseEngine.java
URL: 
http://svn.apache.org/viewcvs/incubator/synapse/trunk/scratch/SynapseToy/core/src/org/apache/synapse/SynapseEngine.java?rev=328247&view=auto
==============================================================================
--- 
incubator/synapse/trunk/scratch/SynapseToy/core/src/org/apache/synapse/SynapseEngine.java
 (added)
+++ 
incubator/synapse/trunk/scratch/SynapseToy/core/src/org/apache/synapse/SynapseEngine.java
 Mon Oct 24 20:40:10 2005
@@ -0,0 +1,153 @@
+package org.apache.synapse;

+

+import org.apache.axis2.AxisFault;

+import org.apache.axis2.engine.AxisConfiguration;

+import org.apache.axis2.description.Parameter;

+import org.apache.axis2.description.ParameterImpl;

+import org.apache.axis2.om.OMFactory;

+import org.apache.axis2.om.OMAbstractFactory;

+import org.apache.axis2.om.OMElement;

+import org.apache.axis2.om.OMAttribute;

+import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;

+

+import javax.xml.stream.XMLStreamReader;

+import javax.xml.stream.XMLInputFactory;

+import javax.xml.stream.XMLStreamException;

+import javax.xml.namespace.QName;

+import java.io.InputStream;

+import java.io.File;

+import java.io.FileInputStream;

+import java.io.FileNotFoundException;

+import java.util.Iterator;

+/*

+* Copyright 2004,2005 The Apache Software Foundation.

+*

+* Licensed under the Apache License, Version 2.0 (the "License");

+* you may not use this file except in compliance with the License.

+* You may obtain a copy of the License at

+*

+*      http://www.apache.org/licenses/LICENSE-2.0

+*

+* Unless required by applicable law or agreed to in writing, software

+* distributed under the License is distributed on an "AS IS" BASIS,

+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

+* See the License for the specific language governing permissions and

+* limitations under the License.

+*

+* @author : Deepal Jayasinghe ([EMAIL PROTECTED])

+*

+*/

+

+//This is has to be improved, current impl only used to load synpase.xml , and 
this allow hot changing

+//of synapse.xml

+

+public class SynapseEngine extends Thread{

+

+    private RuleEngine ruleEngine;

+    private int refreshTime ;

+    private long lastUpdate;

+    private String location;

+    private AxisConfiguration axisConfig;

+

+    public SynapseEngine(String location,AxisConfiguration axisConfig) throws 
AxisFault {

+        this.location = location;

+        this.axisConfig = axisConfig;

+        ruleEngine = populeteRuleEngine(null);

+        Parameter par= new ParameterImpl();

+        par.setName(SynapseConstant.RULE_ENGINE);

+        par.setValue(ruleEngine);

+        axisConfig.addParameter(par);

+        this.start();

+    }

+

+    private RuleEngine populeteRuleEngine(RuleEngine ruleeng) throws AxisFault 
{

+        try {

+            File syanpseFile = new File(location);

+            lastUpdate = syanpseFile.lastModified();

+            InputStream in = new FileInputStream(syanpseFile);

+            XMLStreamReader xmlReader =

+                    XMLInputFactory.newInstance().createXMLStreamReader(in);

+            OMFactory fac = OMAbstractFactory.getOMFactory();

+            StAXOMBuilder staxOMBuilder = new StAXOMBuilder(fac, xmlReader);

+            OMElement element = staxOMBuilder.getDocumentElement();

+            element.build();

+

+            OMElement time = element.getFirstChildWithName(new 
QName("searchTime"));

+            OMAttribute value = time.getAttribute(new QName("value"));

+            refreshTime = Integer.parseInt(value.getAttributeValue());

+

+            OMElement ruleengine = element.getFirstChildWithName(new 
QName("ruleengine"));

+            if (ruleengine == null) {

+                throw new AxisFault("no rule engine found");

+            } else {

+                OMAttribute attr = ruleengine.getAttribute(new QName("class"));

+                if (attr == null) {

+                    throw new AxisFault("no rule engine found");

+                } else {

+                    String className = attr.getAttributeValue();

+                    ClassLoader loader = 
Thread.currentThread().getContextClassLoader();

+                    Class engineClass = Class.forName(className,

+                            true, loader);

+                    if(ruleeng == null){

+                        ruleeng = (RuleEngine) engineClass.newInstance();

+                    }

+                    processMediators(element, loader, ruleeng);

+                    return ruleeng;

+                }

+            }

+        } catch (XMLStreamException e) {

+            throw new AxisFault(e);

+        } catch (ClassNotFoundException e) {

+            throw new AxisFault(e);

+        } catch (IllegalAccessException e) {

+            throw new AxisFault(e);

+        } catch (InstantiationException e) {

+            throw new AxisFault(e);

+        } catch (FileNotFoundException e) {

+            throw new AxisFault(e);

+        }

+    }

+

+    private void processMediators(OMElement ele, ClassLoader loader, 
RuleEngine engine) throws AxisFault {

+        Iterator child = ele.getChildrenWithName(new QName("rule"));

+        while (child.hasNext()) {

+            OMElement omElement = (OMElement) child.next();

+            OMAttribute id = omElement.getAttribute(new QName("id"));

+            OMAttribute clazz = omElement.getAttribute(new QName("mediator"));

+            System.out.println("ID:" + id.getAttributeValue() + " mediator: " 
+ clazz.getAttributeValue());

+            try {

+                String className = clazz.getAttributeValue();

+                Class engineClass = Class.forName(className,

+                        true, loader);

+                Mediators medi = (Mediators) engineClass.newInstance();

+                engine.addRules(id.getAttributeValue(), medi);

+            } catch (ClassNotFoundException e) {

+                throw new AxisFault(e);

+            } catch (IllegalAccessException e) {

+                throw new AxisFault(e);

+            } catch (InstantiationException e) {

+                throw new AxisFault(e);

+            }

+        }

+    }

+

+    public void run() {

+        while(true){

+            try {

+                File  syanpseFile = new File(location);

+                if(lastUpdate != syanpseFile.lastModified()){

+                    ruleEngine = populeteRuleEngine(ruleEngine);

+                    Parameter par= new ParameterImpl();

+                    par.setName(SynapseConstant.RULE_ENGINE);

+                    par.setValue(ruleEngine);

+                    axisConfig.addParameter(par);

+                }

+                sleep(refreshTime);

+            } catch (Exception e) {

+                System.out.println(e.getMessage());

+                break;

+            }

+        }

+    }

+

+}


Modified: 
incubator/synapse/trunk/scratch/SynapseToy/core/src/org/apache/synapse/SynapseServlet.java
URL: 
http://svn.apache.org/viewcvs/incubator/synapse/trunk/scratch/SynapseToy/core/src/org/apache/synapse/SynapseServlet.java?rev=328247&r1=328246&r2=328247&view=diff
==============================================================================
--- 
incubator/synapse/trunk/scratch/SynapseToy/core/src/org/apache/synapse/SynapseServlet.java
 (original)
+++ 
incubator/synapse/trunk/scratch/SynapseToy/core/src/org/apache/synapse/SynapseServlet.java
 Mon Oct 24 20:40:10 2005
@@ -25,7 +25,7 @@
 */

 

 public class SynapseServlet extends AxisServlet {

-

+     

     protected void doGet(HttpServletRequest httpServletRequest, 
HttpServletResponse httpServletResponse) throws javax.servlet.ServletException, 
IOException {

        //this hsa to be overide the way you want

     }


Modified: incubator/synapse/trunk/scratch/SynapseToy/dist/Synapse.mar
URL: 
http://svn.apache.org/viewcvs/incubator/synapse/trunk/scratch/SynapseToy/dist/Synapse.mar?rev=328247&r1=328246&r2=328247&view=diff
==============================================================================
Binary files - no diff available.

Modified: incubator/synapse/trunk/scratch/SynapseToy/dist/Synapse.war
URL: 
http://svn.apache.org/viewcvs/incubator/synapse/trunk/scratch/SynapseToy/dist/Synapse.war?rev=328247&r1=328246&r2=328247&view=diff
==============================================================================
Binary files - no diff available.

Modified: 
incubator/synapse/trunk/scratch/SynapseToy/synapse_module/src/org/apache/synapse/SynapseModule.java
URL: 
http://svn.apache.org/viewcvs/incubator/synapse/trunk/scratch/SynapseToy/synapse_module/src/org/apache/synapse/SynapseModule.java?rev=328247&r1=328246&r2=328247&view=diff
==============================================================================
--- 
incubator/synapse/trunk/scratch/SynapseToy/synapse_module/src/org/apache/synapse/SynapseModule.java
 (original)
+++ 
incubator/synapse/trunk/scratch/SynapseToy/synapse_module/src/org/apache/synapse/SynapseModule.java
 Mon Oct 24 20:40:10 2005
@@ -1,25 +1,15 @@
 package org.apache.synapse;

 

 import org.apache.axis2.AxisFault;

-import org.apache.axis2.description.*;

+import org.apache.axis2.description.InOutOperationDescrition;

+import org.apache.axis2.description.ModuleDescription;

+import org.apache.axis2.description.OperationDescription;

+import org.apache.axis2.description.ServiceDescription;

 import org.apache.axis2.engine.AxisConfiguration;

 import org.apache.axis2.modules.Module;

-import org.apache.axis2.om.OMAbstractFactory;

-import org.apache.axis2.om.OMAttribute;

-import org.apache.axis2.om.OMElement;

-import org.apache.axis2.om.OMFactory;

-import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;

 import org.apache.synapse.receiver.SynapseMessageReceiver;

 

 import javax.xml.namespace.QName;

-import javax.xml.stream.XMLInputFactory;

-import javax.xml.stream.XMLStreamException;

-import javax.xml.stream.XMLStreamReader;

-import java.io.File;

-import java.io.FileInputStream;

-import java.io.FileNotFoundException;

-import java.io.InputStream;

-import java.util.Iterator;

 /*

 * Copyright 2004,2005 The Apache Software Foundation.

 *

@@ -42,24 +32,9 @@
 public class SynapseModule implements Module {

 

     public void init(AxisConfiguration axisConfiguration) throws AxisFault {

+

         ModuleDescription mpdule = axisConfiguration.getModule(

                 new QName(SynapseConstant.SYNAPSE_MODULE));

-        File syanpseFile = new File("../webapps/Synapse/WEB-INF/synapse.xml");

-        if (!syanpseFile.exists()) {

-            throw new AxisFault("synapse.xml does not exist");

-        } else {

-            try {

-                InputStream in = new FileInputStream(syanpseFile);

-                RuleEngine re= populeteRuleEngine(in, 
mpdule.getModuleClassLoader());

-                Parameter par= new ParameterImpl();

-                par.setName(SynapseConstant.RULE_ENGINE);

-                par.setValue(re);

-                axisConfiguration.addParameter(par);

-            } catch (FileNotFoundException e) {

-                throw new AxisFault(e);

-            }

-        }

-

         ServiceDescription service = new ServiceDescription(

                 new QName(SynapseConstant.SYNAPSE_SERVICE));

         service.setClassLoader(mpdule.getModuleClassLoader());

@@ -69,67 +44,13 @@
         service.addOperation(axisOp);

         axisConfiguration.addService(service);

 

+        //Starting SynapseEngine

+        new SynapseEngine("../webapps/Synapse/WEB-INF/synapse.xml", 
axisConfiguration);

+

         //todo have to process syanpse.xml

     }

 

     public void shutdown(AxisConfiguration axisConfiguration) throws AxisFault 
{

     }

 

-    private RuleEngine populeteRuleEngine(InputStream in, ClassLoader loader) 
throws AxisFault {

-        try {

-            XMLStreamReader xmlReader =

-                    XMLInputFactory.newInstance().createXMLStreamReader(in);

-            OMFactory fac = OMAbstractFactory.getOMFactory();

-            StAXOMBuilder staxOMBuilder = new StAXOMBuilder(fac, xmlReader);

-            OMElement element = staxOMBuilder.getDocumentElement();

-            element.build();

-

-            OMElement ruleengine = element.getFirstChildWithName(new 
QName("ruleengine"));

-            if (ruleengine == null) {

-                throw new AxisFault("no rule engine found");

-            } else {

-                OMAttribute attr = ruleengine.getAttribute(new QName("class"));

-                if (attr == null) {

-                    throw new AxisFault("no rule engine found");

-                } else {

-                    String className = attr.getAttributeValue();

-                    Class engineClass = Class.forName(className,

-                            true, loader);

-                    RuleEngine ruleeng = (RuleEngine) 
engineClass.newInstance();

-                    processMediators(element, loader, ruleeng);

-                    return ruleeng;

-                }

-            }

-        } catch (XMLStreamException e) {

-            throw new AxisFault(e);

-        } catch (ClassNotFoundException e) {

-            throw new AxisFault(e);

-        } catch (IllegalAccessException e) {

-            throw new AxisFault(e);

-        } catch (InstantiationException e) {

-            throw new AxisFault(e);

-        }

-    }

-

-    private void processMediators(OMElement ele, ClassLoader loader, 
RuleEngine engine) throws AxisFault {

-        Iterator child = ele.getChildrenWithName(new QName("rule"));

-        while (child.hasNext()) {

-            OMElement omElement = (OMElement) child.next();

-            OMAttribute id = omElement.getAttribute(new QName("id"));

-            OMAttribute clazz = omElement.getAttribute(new QName("mediator"));

-            try {

-                String className = clazz.getAttributeValue();

-                Class engineClass = Class.forName(className,

-                        true, loader);

-                Mediators medi = (Mediators) engineClass.newInstance();

-                engine.addRules(id.getAttributeValue(), medi);

-            } catch (ClassNotFoundException e) {

-                throw new AxisFault(e);

-            } catch (IllegalAccessException e) {

-                throw new AxisFault(e);

-            } catch (InstantiationException e) {

-                throw new AxisFault(e);

-            }

-        }

-    }

 }




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to