[mashup-dev] svn commit r3475 - in trunk/mashup/java/modules/hostobjects: src/org/wso2/mashup/hostobjects/file src/org/wso2/mashup/hostobjects/system test/org/wso2/mashup/hostobjects/file

svn at wso2.org svn at wso2.org
Sun Jun 3 23:46:00 PDT 2007


Author: thilina
Date: Sun Jun  3 23:45:38 2007
New Revision: 3475

Modified:
   trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/file/JavaScriptFileObject.java
   trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java
   trunk/mashup/java/modules/hostobjects/test/org/wso2/mashup/hostobjects/file/JavaScriptFileObjectTest.java
Log:
adding a new function for the system host object to import scripts which are inside the service.resources folder... fixing the read(noOfCharacters) of the filehostobject

Modified: trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/file/JavaScriptFileObject.java
==============================================================================
--- trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/file/JavaScriptFileObject.java	(original)
+++ trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/file/JavaScriptFileObject.java	Sun Jun  3 23:45:38 2007
@@ -146,8 +146,17 @@
         if (reader != null) {
             char[] buffer = new char[noOfCharacters];
             int index = 0;
-            while (reader.read(buffer, index, noOfCharacters - index) > 0);
-            return new String(buffer);
+            StringBuffer buffer2 =  new StringBuffer();
+            int count;
+            while ( ((count=reader.read(buffer)) > 0) & index<noOfCharacters){
+                buffer2.append(buffer);
+                index +=count;
+                if(index<noOfCharacters)
+                {
+                    buffer = new char[noOfCharacters-index];
+                }
+            }
+            return buffer2.toString();
         }
         throw new MashupFault(
                 "File not open for reading. Please call openFileForReading() beforehand. ");

Modified: trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java
==============================================================================
--- trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java	(original)
+++ trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java	Sun Jun  3 23:45:38 2007
@@ -15,13 +15,22 @@
  */
 package org.wso2.mashup.hostobjects.system;
 
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
 import java.net.SocketException;
+import java.net.URL;
 
 import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.Parameter;
 import org.mozilla.javascript.Context;
 import org.mozilla.javascript.Function;
 import org.mozilla.javascript.Scriptable;
 import org.mozilla.javascript.ScriptableObject;
+import org.wso2.javascript.rhino.JavaScriptEngine;
+import org.wso2.javascript.rhino.JavaScriptEngineConstants;
 import org.wso2.mashup.MashupFault;
 import org.wso2.utils.NetworkUtils;
 
@@ -65,6 +74,39 @@
             throw new MashupFault(e);
         }
     }
+    public static void jsFunction_import(Context cx, Scriptable thisObj, Object[] arguments,
+            Function funObj) throws FileNotFoundException, IOException {
+        SystemHostObject system = checkInstance(thisObj);
+        JavaScriptEngine engine = (JavaScriptEngine) getTopLevelScope(thisObj);
+        Object object = cx.getThreadLocal(JavaScriptEngineConstants.AXIS2_MESSAGECONTEXT);
+        MessageContext messageContext;
+        if (object instanceof MessageContext) {
+            messageContext = (MessageContext) object;
+        } else {
+            throw new MashupFault("Error obtaining the MessageContext.");
+        }
+        Parameter parameter = messageContext
+                .getParameter(JavaScriptEngineConstants.RESOURCES_FOLDER);
+        Object resourceFileObject = parameter.getValue();
+        File resourceFolder;
+        if (resourceFileObject != null && resourceFileObject instanceof File) {
+            resourceFolder = (File) resourceFileObject;
+        } else {
+            throw new MashupFault("Mashup Resources folder not found.");
+        }
+        for (int i = 0; i < arguments.length; i++) {
+            if (arguments[i] instanceof String) {
+                String filePath = (String) arguments[i];
+                File f = new File(resourceFolder, filePath);
+                if (!f.exists() || f.isDirectory()) {
+                    throw new MashupFault("Imported JS file " + f.getAbsolutePath()
+                            + " not found.");
+                }
+                FileReader fReader = new FileReader(f);
+                engine.evaluate(fReader);
+            }
+        }
+    }
 
     private static SystemHostObject checkInstance(Scriptable obj) {
         if (obj == null || !(obj instanceof SystemHostObject)) {

Modified: trunk/mashup/java/modules/hostobjects/test/org/wso2/mashup/hostobjects/file/JavaScriptFileObjectTest.java
==============================================================================
--- trunk/mashup/java/modules/hostobjects/test/org/wso2/mashup/hostobjects/file/JavaScriptFileObjectTest.java	(original)
+++ trunk/mashup/java/modules/hostobjects/test/org/wso2/mashup/hostobjects/file/JavaScriptFileObjectTest.java	Sun Jun  3 23:45:38 2007
@@ -29,6 +29,7 @@
         String firstLine =fileObject1.jsFunction_readLine();
         assertEquals("WSO2 Mashup Server File Object", firstLine);
         
+        System.out.println( fileObject1.jsFunction_read(2));
         assertNotNull(fileObject1.jsFunction_readLine());
         assertNotNull(fileObject1.jsFunction_readLine());
         //Check whether calling toString fails in middle of reading
@@ -44,11 +45,9 @@
             success= true;
         }
         assertTrue(success);
-        
-//        assertTrue(fileObject.jsFunction_deleteFile());
     }
 
-    public void testJsFunction_finishReading() throws IOException {
+    public void testJsFunction_close() throws IOException {
         JavaScriptFileObject fileObject = new JavaScriptFileObject();
         fileObject.file= new File("target/test-classes/test_finishreading.txt");
         fileObject.jsFunction_openFileForReading();
@@ -66,7 +65,6 @@
         fileObject.jsFunction_write("This should not fail");
         fileObject.jsFunction_write("This should not fail");
         fileObject.jsFunction_close();
-//        fileObject.jsFunction_write("This should not fail1");
     }
 //
 //    public void testJsFunction_finishWriting() {




More information about the Mashup-dev mailing list