Author: ajborley
Date: Fri Nov 17 01:53:29 2006
New Revision: 476094

URL: http://svn.apache.org/viewvc?view=rev&rev=476094
Log:
Added support for SDO in the Python extension via conversion of SDO objects to 
Python xml.etree.ElementTree.Element objects

Modified:
    
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceProxy.cpp
    
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceWrapper.cpp
    
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/sca_module.cpp

Modified: 
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceProxy.cpp
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceProxy.cpp?view=diff&rev=476094&r1=476093&r2=476094
==============================================================================
--- 
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceProxy.cpp
 (original)
+++ 
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceProxy.cpp
 Fri Nov 17 01:53:29 2006
@@ -21,18 +21,7 @@
 
 #include "tuscany/sca/python/PythonServiceProxy.h"
 #include "tuscany/sca/util/Logging.h"
-#include "tuscany/sca/util/Exceptions.h"
-#include "tuscany/sca/core/SCARuntime.h"
-#include "tuscany/sca/model/Reference.h"
-#include "tuscany/sca/model/ReferenceType.h"
-#include "tuscany/sca/model/Service.h"
-#include "tuscany/sca/model/ServiceType.h"
-#include "tuscany/sca/model/Component.h"
-#include "tuscany/sca/model/ComponentType.h"
-#include "tuscany/sca/core/ServiceWrapper.h"
-#include "tuscany/sca/model/Composite.h"
 #include "tuscany/sca/model/ServiceBinding.h"
-#include "tuscany/sca/python/model/PythonImplementation.h"
 #include "tuscany/sca/python/model/PythonReferenceBinding.h"
 
 using namespace tuscany::sca::model;

Modified: 
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceWrapper.cpp
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceWrapper.cpp?view=diff&rev=476094&r1=476093&r2=476094
==============================================================================
--- 
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceWrapper.cpp
 (original)
+++ 
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceWrapper.cpp
 Fri Nov 17 01:53:29 2006
@@ -291,6 +291,10 @@
                         PyObject* pArgs = PyTuple_New(operation.getNParms());
                         PyObject* pValue = NULL;
 
+                        // Load up the xml.etree.ElementTree module for 
dealing with SDO params and return values
+                        PyObject* elementTreeModuleName = 
PyString_FromString("xml.etree.ElementTree"); 
+                        PyObject* elementTreeModule = 
PyImport_Import(elementTreeModuleName);                        
+
                         for(unsigned int i = 0; i < operation.getNParms(); 
i++) 
                         {
                             const Operation::Parameter& parm = 
operation.getParameter(i);
@@ -364,6 +368,27 @@
                                     pValue = 
PyString_FromString((*(string*)parm.getValue()).c_str());
                                                break;
                                        }
+                                case Operation::DATAOBJECT: 
+                                {
+                                    DataObjectPtr dob = 
*(DataObjectPtr*)parm.getValue();
+
+                                    // Convert a DataObject to a 
xml.etree.ElementTree Element object
+                                    Composite* composite = 
component->getComposite();                                    
+                                    XMLHelper* xmlHelper = 
composite->getXMLHelper();
+                                    char* str = xmlHelper->save(
+                                        dob,
+                                        dob->getType().getURI(),
+                                        dob->getType().getName());             
                       
+
+                                    // Get the xml.etree.ElementTree.XML 
function
+                                    PyObject* elementTreeXMLFunc = 
PyObject_GetAttrString(elementTreeModule, "XML");
+
+                                    // Call the XML() function with the XML 
string 
+                                    pValue = 
PyObject_CallFunction(elementTreeXMLFunc, "s", str);
+
+                                    Py_DECREF(elementTreeXMLFunc);
+                                    break;
+                                }
                                 default:
                                     throwException(ServiceDataException, 
"Operation parameter type not supported");
                                }
@@ -735,20 +760,69 @@
                             }
                             else
                             {
-                                PyObject* valueRepr = PyObject_Repr(pValue); 
-                                PyObject* valueType = PyObject_Type(pValue);   
          
-                                PyObject* valueTypeRepr = 
PyObject_Repr(valueType);    
-                                loginfo("Return value of unknown type (%s) has 
repr: %s", PyString_AsString(valueTypeRepr), PyString_AsString(valueRepr));
-                                Py_DECREF(valueTypeRepr);
-                                Py_DECREF(valueType);
-                                Py_DECREF(valueRepr);
+                                // Get the xml.etree.ElementTree.iselement 
function
+                                PyObject* elementTreeIsElementFunc = 
PyObject_GetAttrString(elementTreeModule, "iselement");
+
+                                // Call the iselement() function with pValue 
to check it
+                                PyObject* pIsElement = 
PyObject_CallFunction(elementTreeIsElementFunc, "O", pValue);
+
+                                if(PyObject_IsTrue(pIsElement) == 1)
+                                {
+                                    // pValue is an 
xml.etree.ElementTree.Element - convert to SDO
+                                    PyObject* elementTreeToStringFunc = 
PyObject_GetAttrString(elementTreeModule, "tostring");
+                                    PyObject* pElemString = 
PyObject_CallFunction(elementTreeToStringFunc, "O", pValue);
+                                    char* data = 
PyString_AsString(pElemString);
+
+                                    Py_DECREF(elementTreeToStringFunc);
+                                    Py_DECREF(pElemString);
+
+                                    Composite* composite = 
component->getComposite();                                   
+                                    XMLHelper* xmlHelper = 
composite->getXMLHelper();
+                                    XMLDocumentPtr xmlDoc = 
xmlHelper->load(data);
+                                    DataObjectPtr* dataObjectData = new 
DataObjectPtr;
+                                    if (xmlDoc != NULL)
+                                    {
+                                        *dataObjectData = 
xmlDoc->getRootDataObject();
+                                    }
+                                    else
+                                    {
+                                        *dataObjectData = NULL;
+                                    }
+                                    if (*dataObjectData != NULL)
+                                    {
+                                        
operation.setReturnValue(dataObjectData);
+                                    }
+                                    else
+                                    {
+                                        string msg = 
"xml.etree.ElementTree.Element could not be converted to a DataObject";
+                                        throwException(ServiceDataException, 
msg.c_str());
+                                    }                                    
+                                }
+                                else
+                                {
+                                    PyObject* valueRepr = 
PyObject_Repr(pValue); 
+                                    PyObject* valueType = 
PyObject_Type(pValue);             
+                                    PyObject* valueTypeRepr = 
PyObject_Repr(valueType);    
+                                    loginfo("Return value of unknown type (%s) 
has repr: %s", PyString_AsString(valueTypeRepr), PyString_AsString(valueRepr));
+                                    Py_DECREF(valueTypeRepr);
+                                    Py_DECREF(valueType);
+                                    Py_DECREF(valueRepr);
+                                }
+
+                                Py_DECREF(pIsElement);
+                                Py_DECREF(elementTreeIsElementFunc);
                             }
 
+                            Py_DECREF(elementTreeModule);
+                            Py_DECREF(elementTreeModuleName);
                             Py_DECREF(pValue);
                         }
                         else 
                         {
                             Py_DECREF(pFunc);
+                            Py_DECREF(elementTreeModule);
+                            Py_DECREF(elementTreeModuleName);
+
                             if(PyErr_Occurred())
                             {
                                 PyErr_Print();
@@ -756,6 +830,7 @@
                             string msg = "Error whilst calling Python module";
                             throwException(ServiceInvocationException, 
msg.c_str());
                         }
+
                     }
                     else 
                     {
@@ -844,8 +919,21 @@
                                 // Serialize a DataObject and create a python 
string object from the XML
                                 DataObjectPtr data = 
properties->getDataObject(pl[i]);                                
                                 XMLHelperPtr helper = 
HelperProvider::getXMLHelper(properties->getDataFactory());
-                                string serializedData = helper->save(data, "", 
propName);
-                                property = 
PyString_FromString(serializedData.c_str());
+                                string serializedData = helper->save(data, 
+                                        data->getType().getURI(),
+                                        data->getType().getName());
+
+                                // Get the xml.etree.ElementTree.XML function
+                                PyObject* elementTreeModuleName = 
PyString_FromString("xml.etree.ElementTree"); 
+                                PyObject* elementTreeModule = 
PyImport_Import(elementTreeModuleName);                        
+                                PyObject* elementTreeXMLFunc = 
PyObject_GetAttrString(elementTreeModule, "XML");
+
+                                // Call the XML() function with the XML string 
+                                property = 
PyObject_CallFunction(elementTreeXMLFunc, "s", serializedData.c_str());
+
+                                Py_DECREF(elementTreeXMLFunc);
+                                Py_DECREF(elementTreeModule);
+                                Py_DECREF(elementTreeModuleName);
                                 break;
                             }
                         case Type::CharacterType:

Modified: 
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/sca_module.cpp
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/sca_module.cpp?view=diff&rev=476094&r1=476093&r2=476094
==============================================================================
--- 
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/sca_module.cpp
 (original)
+++ 
incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/sca_module.cpp
 Fri Nov 17 01:53:29 2006
@@ -35,6 +35,10 @@
 using namespace tuscany::sca::python;
 
 #include "tuscany/sca/util/Logging.h"
+#include "tuscany/sca/util/Exceptions.h"
+
+#include "commonj/sdo/SDO.h"
+using namespace commonj::sdo;
 
 #include <string>
 #include <iostream>
@@ -180,6 +184,20 @@
     if(!pythonServiceProxy)
     {
         return NULL;
+    } 
+
+    // Get the component from the reference or service provided
+    Component* component = NULL; 
+    SCARuntime* runtime = SCARuntime::getInstance();
+
+    PyObject* isReference = PyTuple_GetItem(args, 1);   
+    if(PyObject_IsTrue(isReference))
+    {
+        component = runtime->getCurrentComponent(); 
+    }
+    else
+    {
+        component = runtime->getDefaultComponent();
     }
 
     // Get the name of the operation to invoke
@@ -205,6 +223,10 @@
     // Create the Operation object
     Operation operation(operationName.c_str());
 
+    // Load up the xml.etree.ElementTree module for dealing with SDO params 
and return values
+    PyObject* elementTreeModuleName = 
PyString_FromString("xml.etree.ElementTree"); 
+    PyObject* elementTreeModule = PyImport_Import(elementTreeModuleName);      
                  
+
     // Parameters are the fourth argument
     PyObject* paramTuple = PyTuple_GetItem(args, 3);
 
@@ -250,25 +272,69 @@
         }
         else
         {
-            PyObject* paramRepr = PyObject_Repr(param); 
-            PyObject* paramType = PyObject_Type(param);             
-            PyObject* paramTypeRepr = PyObject_Repr(paramType);    
-            
-            string msg = "sca_invoke Param ";
-            msg += i;
-            msg += "is of unknown type (";
-            msg += PyString_AsString(paramTypeRepr);
-            msg += ") and has repr: ";
-            msg += PyString_AsString(paramRepr);
+            // Get the xml.etree.ElementTree.iselement function
+            PyObject* elementTreeIsElementFunc = 
PyObject_GetAttrString(elementTreeModule, "iselement");
 
-            logerror(msg.c_str());
-            PyErr_SetString(scaError, msg.c_str());
-            
-            Py_DECREF(paramTypeRepr);
-            Py_DECREF(paramType);
-            Py_DECREF(paramRepr);
+            // Call the iselement() function with pValue to check it
+            PyObject* pIsElement = 
PyObject_CallFunction(elementTreeIsElementFunc, "O", param);
 
-            return NULL;
+            if(PyObject_IsTrue(pIsElement) == 1)
+            {
+                // pValue is an xml.etree.ElementTree.Element - convert to SDO
+                PyObject* elementTreeToStringFunc = 
PyObject_GetAttrString(elementTreeModule, "tostring");
+                PyObject* pElemString = 
PyObject_CallFunction(elementTreeToStringFunc, "O", param);
+                char* data = PyString_AsString(pElemString);
+                loginfo("SDO param %d: %s", i, data);
+
+                Py_DECREF(elementTreeToStringFunc);
+                Py_DECREF(pElemString);
+
+                Composite* composite = component->getComposite();              
                     
+                XMLHelper* xmlHelper = composite->getXMLHelper();
+                XMLDocumentPtr xmlDoc = xmlHelper->load(data);
+
+                DataObjectPtr* dataObjectData = new DataObjectPtr;
+                if (xmlDoc != NULL)
+                {
+                    *dataObjectData = xmlDoc->getRootDataObject();
+                }
+                else
+                {
+                    *dataObjectData = NULL;
+                }
+                if (*dataObjectData != NULL)
+                {
+                    operation.addParameter(dataObjectData);
+                }
+                else
+                {
+                    string msg = "xml.etree.ElementTree.Element could not be 
converted to a DataObject";
+                    throwException(ServiceDataException, msg.c_str());
+                }                                    
+            }
+            else
+            {
+
+                PyObject* paramRepr = PyObject_Repr(param); 
+                PyObject* paramType = PyObject_Type(param);             
+                PyObject* paramTypeRepr = PyObject_Repr(paramType);    
+                
+                string msg = "sca_invoke Param ";
+                msg += i;
+                msg += "is of unknown type (";
+                msg += PyString_AsString(paramTypeRepr);
+                msg += ") and has repr: ";
+                msg += PyString_AsString(paramRepr);
+    
+                logerror(msg.c_str());
+                PyErr_SetString(scaError, msg.c_str());
+                
+                Py_DECREF(paramTypeRepr);
+                Py_DECREF(paramType);
+                Py_DECREF(paramRepr);
+
+                return NULL;
+            }
         }
     }
    
@@ -281,7 +347,10 @@
     }
     catch(...)
     {
-        string msg = "Exception whilst invoking the service";
+        string msg = "Exception whilst invoking the ";
+        msg += operationName.c_str();
+        msg += " operation on the service/reference";
+
         logwarning(msg.c_str());
         PyErr_SetString(scaError, msg.c_str());
         return NULL;
@@ -347,6 +416,27 @@
                 returnValue = 
PyString_FromString((*(string*)operation.getReturnValue()).c_str());
                 break;
             }
+        case Operation::DATAOBJECT: 
+            {
+                DataObjectPtr dob = 
*(DataObjectPtr*)operation.getReturnValue();
+
+                // Convert a DataObject to a xml.etree.ElementTree Element 
object
+                Composite* composite = component->getComposite();              
                      
+                XMLHelper* xmlHelper = composite->getXMLHelper();
+                char* str = xmlHelper->save(
+                    dob,
+                    dob->getType().getURI(),
+                    dob->getType().getName());                                 
   
+
+                // Get the xml.etree.ElementTree.XML function
+                PyObject* elementTreeXMLFunc = 
PyObject_GetAttrString(elementTreeModule, "XML");
+
+                // Call the XML() function with the XML string 
+                returnValue = PyObject_CallFunction(elementTreeXMLFunc, "s", 
str);
+
+                Py_DECREF(elementTreeXMLFunc);
+                break;
+            }
         default:
             {
                 Py_INCREF(Py_None);
@@ -354,6 +444,9 @@
             }
 
     }
+
+    Py_DECREF(elementTreeModuleName); 
+    Py_DECREF(elementTreeModule);                        
 
     return returnValue;
 }



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

Reply via email to