[prev in list] [next in list] [prev in thread] [next in thread] 

List:       wsf-java-dev
Subject:    [wsf-java-dev] svn commit r1809 -
From:       svn () wso2 ! org (svn () wso2 ! org)
Date:       2007-04-12 6:31:32
Message-ID: E1HbzOI-0001sn-Ep () wso2 ! org
[Download RAW message or body]

Author: thilina
Date: Thu Apr 12 06:30:48 2007
New Revision: 1809

Added:
   trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptEngineUtils.java
 Modified:
   trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptEngine.java
  trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptReceiver.java
 Log:
Adding the ability to specify host objects in the configuration(axis2.xml)

Modified: trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptEngine.java
 ==============================================================================
--- trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptEngine.java	(original)
                
+++ trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptEngine.java	Thu \
Apr 12 06:30:48 2007 @@ -165,7 +165,6 @@
             }
         }
 
-
         // Evaluates the javascript file
         try {
             evaluate(reader);
@@ -177,19 +176,18 @@
         Object fObj = this.get(method, this);
         if (!(fObj instanceof Function) || (fObj == Scriptable.NOT_FOUND)) {
             throw new AxisFault("Method " + method + " is undefined or not a \
function"); +        }
+        Object functionArgs[] = { args };
+        Function f = (Function) fObj;
+        result = f.call(cx, this, this, functionArgs);
+        if (json) {
+            result = ((String) result).substring(1, ((String) result).length() - 1);
+            InputStream in = new ByteArrayInputStream(((String) result).getBytes());
+            JSONOMBuilder builder = new JSONOMBuilder();
+            result = builder.processDocument(in, null, null);
         } else {
-            Object functionArgs[] = {args};
-            Function f = (Function) fObj;
-            result = f.call(cx, this, this, functionArgs);
-            if (json) {
-                result = ((String) result).substring(1, ((String) result).length() - \
                1);
-                InputStream in = new ByteArrayInputStream(((String) \
                result).getBytes());
-                JSONOMBuilder builder = new JSONOMBuilder();
-                result = builder.processDocument(in, null, null);
-            } else {
-                //Get the OMNode inside the resulting object
-                result = ((XML) result).getAxiomFromXML();
-            }
+            //Get the OMNode inside the resulting object
+            result = ((XML) result).getAxiomFromXML();
         }
         return (OMNode) result;
     }

Added: trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptEngineUtils.java
 ==============================================================================
--- (empty file)
+++ trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptEngineUtils.java	Thu \
Apr 12 06:30:48 2007 @@ -0,0 +1,100 @@
+/*
+ * Copyright 2006,2007 WSO2, Inc. http://www.wso2.org
+ *
+ * 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.
+ */
+
+package org.wso2.javascript.rhino;
+
+import java.lang.reflect.InvocationTargetException;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.util.Loader;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.mozilla.javascript.ScriptableObject;
+
+public class JavaScriptEngineUtils {
+    
+    /*
+     * setup for logging
+     */
+    private static final Log log = LogFactory.getLog(JavaScriptEngineUtils.class);
+
+    public static void loadHostObjects(JavaScriptEngine cx, List list) throws \
AxisFault { +        Iterator iterator = list.iterator();
+        while (iterator.hasNext()) {
+            final String className = (String) iterator.next();
+            try {
+                ScriptableObject.defineClass(cx, loadClass(className));
+            } catch (PrivilegedActionException e) {
+                log.fatal(e);
+                throw new AxisFault("Error occured while loading the host object :" \
+ className, e); +            } catch (IllegalAccessException e) {
+                log.fatal(e);
+                throw new AxisFault("Error occured while loading the host object :" \
+ className, e); +            } catch (InstantiationException e) {
+                log.fatal(e);
+                throw new AxisFault("Error occured while loading the host object :" \
+ className, e); +            } catch (InvocationTargetException e) {
+                log.fatal(e);
+                throw new AxisFault("Error occured while loading the host object :" \
+ className, e); +            }
+        }
+    }
+
+    public static Class loadClass(final String className) throws \
PrivilegedActionException { +        return (Class) \
org.apache.axis2.java.security.AccessController +                .doPrivileged(new \
PrivilegedExceptionAction() { +                    public Object run() throws \
AxisFault { +                        Class selectorClass;
+                        try {
+                            if ((className != null) && !"".equals(className)) {
+                                selectorClass = \
Loader.loadClass(Thread.currentThread() +                                        \
.getContextClassLoader(), className); +                            } else {
+                                log.fatal("Invalid Class Name for the HostObject");
+                                throw new AxisFault("Invalid Class Name");
+                            }
+                        } catch (ClassNotFoundException e) {
+                            log.fatal(e);
+                            throw new AxisFault("Error occured while loading the \
host object :" +                                    + className, e);
+                        }
+                        return selectorClass;
+                    }
+                });
+    }
+
+    public static List getHostObjectsMap(OMElement hostObjectElement) {
+        ArrayList list = new ArrayList();
+        Iterator iterator = hostObjectElement.getChildrenWithName(new \
QName("class")); +        while (iterator.hasNext()) {
+            OMElement element = (OMElement) iterator.next();
+            String className = element.getText();
+            if ((className != null) & (!className.equals(""))) {
+                list.add(className);
+            }
+        }
+        return list;
+    }
+}

Modified: trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptReceiver.java
 ==============================================================================
--- trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptReceiver.java	(original)
                
+++ trunk/wsf/javascript/rhino/message_receiver/src/org/wso2/javascript/rhino/JavaScriptReceiver.java	Thu \
Apr 12 06:30:48 2007 @@ -16,7 +16,18 @@
 
 package org.wso2.javascript.rhino;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URL;
+import java.util.List;
+
 import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
 import org.apache.axiom.soap.SOAPEnvelope;
@@ -32,9 +43,6 @@
 import org.apache.axis2.json.JSONDataSource;
 import org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver;
 
-import java.io.*;
-import java.net.URL;
-
 /**
  * Class JavaScriptReceiver implements the AbstractInOutSyncMessageReceiver,
  * which, is the abstract IN-OUT MEP message receiver.
@@ -53,6 +61,13 @@
      */
     public void invokeBusinessLogic(MessageContext inMessage, MessageContext \
outMessage) throws AxisFault {  JavaScriptEngine engine = new JavaScriptEngine();
+        Parameter parameter=inMessage.getParameter("javascript.hostobjects");
+        if (parameter.getParameterType() ==1)
+        {
+            OMElement paraElement = parameter.getParameterElement();
+            List list = JavaScriptEngineUtils.getHostObjectsMap(paraElement);
+            JavaScriptEngineUtils.loadHostObjects(engine,list);
+        }
         //Get the method, arguments and the reader from the MessageContext
         String method = null;
         method = getJSMethod(inMessage);


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic