diff options
Diffstat (limited to 'cpp/sca/runtime/core/src')
86 files changed, 12879 insertions, 0 deletions
diff --git a/cpp/sca/runtime/core/src/tuscany/sca/core/Exceptions.cpp b/cpp/sca/runtime/core/src/tuscany/sca/core/Exceptions.cpp new file mode 100644 index 0000000000..7fa9c5e6b9 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/core/Exceptions.cpp @@ -0,0 +1,285 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <sstream> +#if defined(WIN32) || defined (_WINDOWS) || defined (IS_DARWIN) +#else +#include <execinfo.h> +#endif + +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/util/Logging.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + + // ======================================================================== + // Constructor + // ======================================================================== + TuscanyRuntimeException :: TuscanyRuntimeException(const char* name, + severity_level sev, + const char* msg_text) + { + severity = sev; + location_set = 0; + class_name = new char[strlen(name) + 1]; + strcpy(class_name,name); + message_text = new char[strlen(msg_text)+1]; + strcpy(message_text,msg_text); + +#if defined(WIN32) || defined (_WINDOWS) || defined (IS_DARWIN) +#else + void* array[25]; + stacktrace_size = backtrace(array, 25); + stacktrace_symbols = backtrace_symbols(array, stacktrace_size); +#endif + + logwarning("%s raised: %s", class_name, message_text); + + } // end TuscanyRuntimeException constuctor + + // ======================================================================== + // Constructor + // ======================================================================== + TuscanyRuntimeException :: TuscanyRuntimeException(const TuscanyRuntimeException& c) + { + severity = c.getSeverity(); + location_set = c.location_set; + class_name = new char[strlen(c.getEClassName()) + 1]; + strcpy(class_name, c.getEClassName()); + message_text = new char[strlen(c.getMessageText())+1]; + strcpy(message_text,c.getMessageText()); + + for (int i=0; i < location_set; i++) + { + locations[i].file = new char[strlen(c.locations[i].file) + 1]; + strcpy(locations[i].file,c.locations[i].file); + locations[i].line = c.locations[i].line; + locations[i].function = new char[strlen(c.locations[i].function) + 1]; + strcpy(locations[i].function, c.locations[i].function); + } + +#if defined(WIN32) || defined (_WINDOWS) || defined (IS_DARWIN) +#else + void* array[25]; + stacktrace_size = backtrace(array, 25); + stacktrace_symbols = backtrace_symbols(array, stacktrace_size); +#endif + + logwarning("%s raised: %s", class_name, message_text); + } + + // ======================================================================== + // Constructor + // ======================================================================== + TuscanyRuntimeException :: TuscanyRuntimeException(const commonj::sdo::SDORuntimeException& c) + { + class_name = new char[strlen(c.getEClassName()) + 1]; + strcpy(class_name, c.getEClassName()); + message_text = new char[strlen(c.getMessageText())+1]; + strcpy(message_text,c.getMessageText()); + severity = Warning; + const char* file = c.getFileName(); + unsigned long line = c.getLineNumber(); + const char* function = c.getFunctionName(); + location_set = 0; + if (file) + { + setLocation(file, line, function); + } + +#if defined(WIN32) || defined (_WINDOWS) || defined (IS_DARWIN) +#else + void* array[25]; + stacktrace_size = backtrace(array, 25); + stacktrace_symbols = backtrace_symbols(array, stacktrace_size); +#endif + + logwarning("%s raised: %s", class_name, message_text); + } + + // ======================================================================== + // Destructor + // ======================================================================== + TuscanyRuntimeException :: ~TuscanyRuntimeException() + { + if (class_name) delete class_name; + if (message_text) delete message_text; + for (int i=0;i<location_set;i++) + { + if (locations[i].file) delete locations[i].file; + if (locations[i].function) delete locations[i].function; + } + +#if defined(WIN32) || defined (_WINDOWS) || defined (IS_DARWIN) +#else + free(stacktrace_symbols); +#endif + + } // end TuscanyRuntimeException destructor + + // ======================================================================== + // Return class name of this exception + // ======================================================================== + const char* TuscanyRuntimeException :: getEClassName() const + { + return class_name; + } // end getClassName() + + // ======================================================================== + // Return severity + // ======================================================================== + TuscanyRuntimeException::severity_level TuscanyRuntimeException :: getSeverity() const + { + return severity; + } // end getSeverity() + + // ======================================================================== + // Return message text associated with exception + // ======================================================================== + const char* TuscanyRuntimeException :: getMessageText() const + { + return message_text; + } // end getMessageText() + + // ======================================================================== + // Return file name where exception was raised + // ======================================================================== + const char* TuscanyRuntimeException :: getFileName() const + { + return locations[0].file; + } // end getFileName() + + // ======================================================================== + // Return line number where exception was raised + // ======================================================================== + unsigned long TuscanyRuntimeException :: getLineNumber() const + { + return locations[0].line; + } // end getLineNumber() + + // ======================================================================== + // Return function name where exception was raised + // ======================================================================== + const char* TuscanyRuntimeException :: getFunctionName() const + { + return locations[0].function; + } // end getFunctionName() + + + // ======================================================================== + // set severity of exception + // ======================================================================== + void TuscanyRuntimeException :: setSeverity(severity_level sev) + { + severity = sev; + } // end setSeverity(severity_level sev) const + + // ======================================================================== + // set message text associated with exception + // ======================================================================== + void TuscanyRuntimeException :: setMessageText(const char* msg_text) + { + if (message_text != 0) delete message_text; + message_text = new char[strlen(msg_text) + 1]; + strcpy(message_text,msg_text); + } // end setMessageText(const string &msg_text) const + + // ======================================================================== + // set location of most recent handling of the exception + // ======================================================================== + void TuscanyRuntimeException :: setLocation(const char* file, + unsigned long line, + const char* function) + { + if (location_set < num_locations) + { + locations[location_set].file = new char[strlen(file) + 1]; + strcpy(locations[location_set].file,file); + locations[location_set].line = line; + locations[location_set].function = new char[strlen(function) + 1]; + strcpy(locations[location_set].function,function); + + location_set++; + } + } // end setLocation() + + + // ======================================================================== + // print self + // ======================================================================== + ostream& TuscanyRuntimeException :: PrintSelf(ostream &os) const + { + + os << "Exception" << endl; + os << " Class: " << class_name << endl; + os << " Description: " << message_text << endl; + if (location_set != 0) + { + os << " Origin:" << endl; + os << " File: " << locations[0].file << endl; + char lineNumber[100]; + sprintf(lineNumber, "%lu",locations[0].line); + os << " Line: " << lineNumber << endl; + os << " Function: " << locations[0].function << endl; + + if (location_set >1) + { + os << " Path:" << endl; + int i=1; + while (i < location_set) + { + os << " File: " << locations[i].file << endl; + os << " Line: " << locations[i].line << endl; + os << " Function: " << locations[i].function << endl; + i++; + } + } + } +#if defined(WIN32) || defined (_WINDOWS) || defined (IS_DARWIN) +#else + if (stacktrace_size != 0) + { + os << " Backtrace:" << endl; + for (int j = 0; j < stacktrace_size; j++) + { + os << " " << stacktrace_symbols[j] << endl; + } + } +#endif + return os; + } // end ostream operator << + + + // ======================================================================== + // ostream operator << + // ======================================================================== + SCA_API ostream& operator<< (ostream &os, const TuscanyRuntimeException &except) + { + return except.PrintSelf(os); + } // end ostream operator << + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/core/Exceptions.h b/cpp/sca/runtime/core/src/tuscany/sca/core/Exceptions.h new file mode 100644 index 0000000000..50cf91f7d4 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/core/Exceptions.h @@ -0,0 +1,346 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_core_exceptions_h +#define tuscany_sca_core_exceptions_h + +#include <ostream> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" + + +namespace tuscany +{ + namespace sca + { + /** + * Top level exception to represent all the exceptions that may be + * thrown by an SCA runtime implementation. + */ + class SCA_API TuscanyRuntimeException + { + public: + /** + * Represents the possible severity levels for an exception. + */ + enum severity_level + { + Normal, + Warning, + Error, + Severe + }; + + /** + * Constructor. + * @param name Class name of the exception. + * @param sev Severity level. + * @param msg_text Detailed description of the exception. + */ + TuscanyRuntimeException( + const char *name = "TuscanyRuntimeException", + severity_level sev = Severe, + const char* msg_text = ""); + + TuscanyRuntimeException(const TuscanyRuntimeException& c); + TuscanyRuntimeException(const commonj::sdo::SDORuntimeException& c); + + // Destructor + virtual ~TuscanyRuntimeException(); + + /** + * Return class name of this exception. + */ + const char* getEClassName() const; + + /** + * Return severity. + */ + severity_level getSeverity() const; + + /** + * Return message text associated with exception. + */ + const char* getMessageText() const; + + /* + * Return file name where the exception was raised. + */ + const char* getFileName() const; + + /** + * Return line number where the exception was raised. + */ + unsigned long getLineNumber() const; + + /** + * Return function name where the exception was raised. + */ + const char* getFunctionName() const; + + /** + * Set the exception severity. + */ + void setSeverity(severity_level sev); + + /** + * Set the message text associated with exception. + */ + void setMessageText(const char* msg_text); + + /** + * Set the location where the exception was raised. + * @param file Name of the file. + * @param line Line number in the file. + * @param function Name of the function. + */ + void setLocation(const char* file, + unsigned long line, + const char* function=""); + + /** + * Append exception details to ostream. + */ + virtual std::ostream& PrintSelf(std::ostream &os) const; + + /** + * Operator to send exceptions details to a stream. + */ + SCA_API friend std::ostream& operator<< (std::ostream &os, const TuscanyRuntimeException &except); + + protected: + + private: + /** + * Class name of the exception. + */ + char* class_name; + + /** + * Severity level of the exception. + */ + severity_level severity; + + /** + * Description of the exception. + */ + char* message_text; // Description of exception + + /** + * Location where the exception was thrown or handled and thrown. + */ + class location + { + public: + char* file; // File name (from __FILE__) + unsigned long line; // Line number (from __LINE__) + char* function; // Function name + }; + + + enum {num_locations=5}; + /** + * Array of locations where the exception has been handled and thrown. + */ + location locations[num_locations]; + + /** + * The current location (index into TuscanyRuntimeException#location). + */ + int location_set; + + /** + * A snapshot of the stack when the exception was constructed + */ +#if defined(WIN32) || defined (_WINDOWS) +#else + int stacktrace_size; + char** stacktrace_symbols; +#endif + + }; // End TuscanyRuntimeException class definition + + + /** + * Indicates a problem in the consistency of the SCA model provided to the + * Tuscany runtime. + */ + class SCA_API SystemConfigurationException: public TuscanyRuntimeException + { + public: + SystemConfigurationException(const char* msg) + : TuscanyRuntimeException("SystemConfigurationException", Severe, + msg) + { + } + + SystemConfigurationException( + const char *name, + severity_level sev, + const char* msg_text) + : TuscanyRuntimeException(name, sev, msg_text) + { + } + + SystemConfigurationException(const commonj::sdo::SDORuntimeException& c) + : TuscanyRuntimeException(c) + { + } + private: + }; + + /** + * Indicates a problem while invoking a service. + */ + class SCA_API ServiceInvocationException: public TuscanyRuntimeException + { + public: + ServiceInvocationException(const char* msg) + : TuscanyRuntimeException("ServiceInvocationException", Severe, msg) + { + } + + ServiceInvocationException( + const char *name, + severity_level sev, + const char* msg_text) + : TuscanyRuntimeException(name, sev, msg_text) + { + } + + ServiceInvocationException(const commonj::sdo::SDORuntimeException& c) + : TuscanyRuntimeException(c) + { + } + + private: + }; + + /** + * Indicates a problem while working with service data. + */ + class SCA_API ServiceDataException: public TuscanyRuntimeException + { + public: + ServiceDataException(const char* msg) + : TuscanyRuntimeException("ServiceDataException", Severe, + msg) + { + } + + ServiceDataException( + const char *name, + severity_level sev, + const char* msg_text) + : TuscanyRuntimeException(name, sev, msg_text) + { + } + + ServiceDataException(const commonj::sdo::SDORuntimeException& c) + : TuscanyRuntimeException(c) + { + } + private: + }; + + } // End namespace sca +} // End namespace tuscany + + +/** + * ========================================================================= + * Macro - throwException + * + * adds the current file name, line number and function name to the exception. + * then throws the exception. + * The parameter 'function_name' should be the name of the function throwing + * this exception. + * The parameter 'type' is the class of exception to throw and must be a + * SDORuntimeException or a class derived from SDORuntimeException. + * The parameter 'parameter' is the construction parameter for the exception + * ========================================================================= +*/ + +#if defined(WIN32) || defined (_WINDOWS) +#define throwException(type, parameter) \ +{\ + type __TuscanyThrownException__(parameter); \ + __TuscanyThrownException__.setLocation(__FILE__,__LINE__,__FUNCTION__); \ + throw __TuscanyThrownException__;\ +} +#else +#define throwException(type, parameter) \ +{\ + type __TuscanyThrownException__(parameter); \ + __TuscanyThrownException__.setLocation(__FILE__,__LINE__,__PRETTY_FUNCTION__); \ + throw __TuscanyThrownException__;\ +} +#endif + +/** + ========================================================================= + * Macro - rethrowException + * + * adds the current file name, line number and function name to the exception. + * then re-throws the exception. + * The parameter 'function_name' should be the name of the function throwing + * this exception. + * ========================================================================= +*/ +#if defined(WIN32) || defined (_WINDOWS) +#define rethrowException(exception) \ +{\ + (exception).setLocation(__FILE__,__LINE__,__FUNCTION__); \ + throw (exception);\ +} +#else +#define rethrowException(exception) \ +{\ + (exception).setLocation(__FILE__,__LINE__,__PRETTY_FUNCTION__); \ + throw (exception);\ +} +#endif + +/** + * ========================================================================= + * Macro - handleException + * + * adds the current file name, line number and function name to the exception. + * Writes an exception trace entry then continues. + * The parameter 'function_name' should be the name of the function handling + * this exception. + * ========================================================================= +*/ +#if defined(WIN32) || defined (_WINDOWS) +#define handleException(__PRETTY_FUNCTION__, exception) \ +{\ + (exception).setLocation(__FILE__,__LINE__,__FUNCTION__); \ +} +#else +#define handleException(__PRETTY_FUNCTION__, exception) \ +{\ + (exception).setLocation(__FILE__,__LINE__,__PRETTY_FUNCTION__); \ +} +#endif + +#endif // tuscany_sca_core_exceptions_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/core/Operation.cpp b/cpp/sca/runtime/core/src/tuscany/sca/core/Operation.cpp new file mode 100644 index 0000000000..335ed5d369 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/core/Operation.cpp @@ -0,0 +1,578 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <stdarg.h> +#include <sstream> + +#include "tuscany/sca/core/Operation.h" +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/core/SCARuntime.h" + +using namespace std; +using namespace commonj::sdo; + + +namespace tuscany +{ + namespace sca + { + // =========== + // Constructor + // =========== + Operation::Operation(const char* operationName) + { + logentry(); + if (operationName != 0) + { + name = operationName; + } + } + + // ========== + // Destructor + // ========== + Operation::~Operation() + { + logentry(); + clean(); + } + + // ========== + // Copy Constructor + // ========== + Operation::Operation(const Operation& op) + { + logentry(); + copy(op); + } + + // ========== + // operator= + // ========== + Operation& Operation::operator=(const Operation& op) + { + logentry(); + if (&op != this) + { + copy(op); + } + return *this; + } + + // ========== + // Clean + // ========== + void Operation::clean() + { + logentry(); + for (unsigned int i = 0; i < getNParms(); i++) + { + const Parameter& parm = getParameter(i); + if (parm.getType() == DATAOBJECT) + { + delete (DataObjectPtr*)parm.getValue(); + } + } + + parameters.empty(); + + if (getReturnType() == DATAOBJECT) + { + delete (DataObjectPtr*)getReturnValue(); + } + } + + // ========== + // Copy + // ========== + void Operation::copy(const Operation& op) + { + logentry(); + clean(); + for (unsigned int i = 0; i < op.getNParms(); i++) + { + const Parameter& parm = op.getParameter(i); + if (parm.getType() == DATAOBJECT) + { + addParameter((const DataObject*)parm.getValue()); + } + else + { + parameters.insert(parameters.end(), parm); + } + } + + if (getReturnType() == DATAOBJECT) + { + setReturnValue((const DataObjectPtr*)op.getReturnValue()); + } + else + { + returnValue = op.returnValue; + } + } + + // ============================================== + // getParameter: return parameter at position pos + // ============================================== + void* Operation::getParameterValue(unsigned int pos) const + { + logentry(); + if (pos < parameters.size()) + { + return parameters[pos].getValue(); + } + + return 0; + } + + // ================================================== + // getParameter: return parameter value based on name + // ================================================== + void* Operation::getParameterValue(const string& name) const + { + logentry(); + try + { + return getParameter(name).getValue(); + } + catch(ServiceInvocationException) + {} + + return 0; + } + + // ====================================================== + // getParameter: return parameter based on index position + // ====================================================== + const Operation::Parameter& Operation::getParameter(unsigned int pos) const + { + logentry(); + if (pos < parameters.size()) + { + return parameters[pos]; + } + + throwException(ServiceInvocationException, "Index out of range"); + } + + // =============================================== + // getParameter: return of parameter based on name + // =============================================== + const Operation::Parameter& Operation::getParameter(const string& name) const + { + logentry(); + for(unsigned int pos=0; pos < parameters.size(); pos++) + { + if(parameters[pos].getName() == name) + { + return parameters[pos]; + } + } + + throwException(ServiceInvocationException, "Parameter name not found"); + } + + + // ============================================================ + // getParameterType: return type of parameter based on position + // ============================================================ + Operation::ParameterType Operation::getParameterType(unsigned int pos) const + { + logentry(); + if (pos < parameters.size()) + { + return parameters[pos].getType(); + } + + return VOID_TYPE; + } + + // ======================================================== + // getParameterType: return type of parameter based on name + // ======================================================== + Operation::ParameterType Operation::getParameterType(const string& name) const + { + logentry(); + try + { + return getParameter(name).getType(); + } + catch(ServiceInvocationException) + {} + + return VOID_TYPE; + } + + const string emptyString = string(); + + // ============================================== + // getParameterName: return name of parameter + // ============================================== + const string& Operation::getParameterName(unsigned int pos) const + { + logentry(); + if (pos < parameters.size()) + { + return parameters[pos].getName(); + } + + return emptyString; + } + + // =========================================== + // addParameter: set parameter at position pos + // =========================================== + void Operation::addParameter(const void *parm) + { + logentry(); + loginfo("Adding operation parameter, type: void, value: %p", parm); + parameters.insert(parameters.end(), Parameter((void*)parm, VOID_TYPE)); + } + + void Operation::addParameter(const bool *parm) + { + logentry(); + loginfo("Adding operation parameter, type: bool, value: %d", (int)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, BOOL)); + } + + void Operation::addParameter(const short *parm) + { + logentry(); + loginfo("Adding operation parameter, type: short, value: %hd", (short)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, SHORT)); + } + + void Operation::addParameter(const int *parm) + { + logentry(); + loginfo("Adding operation parameter, type: int, value: %d", (int)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, INT)); + } + + void Operation::addParameter(const long *parm) + { + logentry(); + loginfo("Adding operation parameter, type: long, value: %ld", (long)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, LONG)); + } + + void Operation::addParameter(const unsigned short *parm) + { + logentry(); + loginfo("Adding operation parameter, type: unsigned short, value: %hu", (unsigned short)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, USHORT)); + } + + void Operation::addParameter(const unsigned int *parm) + { + logentry(); + loginfo("Adding operation parameter, type: unsigned int, value: %u", (unsigned int)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, UINT)); + } + + void Operation::addParameter(const unsigned long *parm) + { + logentry(); + loginfo("Adding operation parameter, type: unsigned long, value: %lu", (unsigned long)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, ULONG)); + } + + void Operation::addParameter(const float *parm) + { + logentry(); + loginfo("Adding operation parameter, type: float, value: %f", (float)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, FLOAT)); + } + + void Operation::addParameter(const double *parm) + { + logentry(); + loginfo("Adding operation parameter, type: double, value: %lf", (double)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, DOUBLE)); + } + + void Operation::addParameter(const long double *parm) + { + logentry(); + loginfo("Adding operation parameter, type: long double, value: %Lf", (long double)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, LONGDOUBLE)); + } + + void Operation::addParameter(const char* *parm) + { + logentry(); + loginfo("Adding operation parameter, type: char*, value: %s", (const char*)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, CHARS)); + } + + void Operation::addParameter(const char *parm) + { + logentry(); + loginfo("Adding operation parameter, type: char, value: %d", (int)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, CHAR)); + } + + void Operation::addParameter(const string *parm) + { + logentry(); + loginfo("Adding operation parameter, type: string, value: %s", (const char*)(*parm).c_str()); + parameters.insert(parameters.end(), Parameter((void*)parm, STRING)); + } + + void Operation::addParameter(const DataObjectPtr *parm) + { + logentry(); + ostringstream os; + os << *parm; + loginfo("Adding operation parameter, type: DataObject, value: %s", os.str().c_str()); + parameters.insert(parameters.end(), Parameter((void*)new DataObjectPtr(*parm), DATAOBJECT)); + } + + // ======================================================= + // addParameter: set parameter at position pos with a name + // ======================================================= + void Operation::addParameter(const string& name, const void *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: void, value: %p", name.c_str(), parm); + parameters.insert(parameters.end(), Parameter((void*)parm, VOID_TYPE, (string&) name)); + } + + void Operation::addParameter(const string& name, const bool *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: bool, value: %d", name.c_str(), (int)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, BOOL, (string&) name)); + } + + void Operation::addParameter(const string& name, const short *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: short, value: %hd", name.c_str(), (short)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, SHORT, (string&) name)); + } + + void Operation::addParameter(const string& name, const int *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: int, value: %d", name.c_str(), (int)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, INT, (string&) name)); + } + + void Operation::addParameter(const string& name, const long *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: long, value: %ld", name.c_str(), (long)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, LONG, (string&) name)); + } + + void Operation::addParameter(const string& name, const unsigned short *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: unsigned short, value: %hu", (unsigned short)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, USHORT, (string&) name)); + } + + void Operation::addParameter(const string& name, const unsigned int *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: unsigned int, value: %u", name.c_str(), (unsigned int)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, UINT, (string&) name)); + } + + void Operation::addParameter(const string& name, const unsigned long *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: unsigned long, value: %lu", name.c_str(), (unsigned long)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, ULONG, (string&) name)); + } + + void Operation::addParameter(const string& name, const float *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: float, value: %f", name.c_str(), (float)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, FLOAT, (string&) name)); + } + + void Operation::addParameter(const string& name, const double *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: double, value: %lf", name.c_str(), (double)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, DOUBLE, (string&) name)); + } + + void Operation::addParameter(const string& name, const long double *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: long double, value: %Lf", name.c_str(), (long double)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, LONGDOUBLE, (string&) name)); + } + + void Operation::addParameter(const string& name, const char* *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: char*, value: %s", name.c_str(), (const char*)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, CHARS, (string&) name)); + } + + void Operation::addParameter(const string& name, const char *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: char, value: %d", name.c_str(), (int)*parm); + parameters.insert(parameters.end(), Parameter((void*)parm, CHAR, (string&) name)); + } + + void Operation::addParameter(const string& name, const string *parm) + { + logentry(); + loginfo("Adding operation parameter, name: %s, type: string, value: %s", name.c_str(), (const char*)(*parm).c_str()); + parameters.insert(parameters.end(), Parameter((void*)parm, STRING, (string&) name)); + } + + void Operation::addParameter(const string& name, const DataObjectPtr *parm) + { + logentry(); + ostringstream os; + os << *parm; + loginfo("Adding operation parameter, name: %s, type: DataObject, value: %s", name.c_str(), os.str().c_str()); + parameters.insert(parameters.end(), Parameter((void*)new DataObjectPtr(*parm), DATAOBJECT, (string&) name)); + } + + Operation::Parameter::Parameter(void* val, Operation::ParameterType typ, const string& nam) + : value(val), type(typ), name(nam) + { + } + + // =========================================== + // setReturnValue + // =========================================== + void Operation::setReturnValue(const void *parm) + { + logentry(); + loginfo("Setting operation return value, type: void, value: %p", parm); + returnValue = Parameter((void*)parm, VOID_TYPE); + } + + void Operation::setReturnValue(const bool *parm) + { + logentry(); + loginfo("Setting operation return value, type: bool, value: %d", (bool)*parm); + returnValue = Parameter((void*)parm, BOOL); + } + + void Operation::setReturnValue(const short *parm) + { + logentry(); + loginfo("Setting operation return value, type: short, value: %h", (short)*parm); + returnValue = Parameter((void*)parm, SHORT); + } + + void Operation::setReturnValue(const int *parm) + { + logentry(); + loginfo("Setting operation return value, type: int, value: %d", (int)*parm); + returnValue = Parameter((void*)parm, INT); + } + + void Operation::setReturnValue(const long *parm) + { + logentry(); + loginfo("Setting operation return value, type: long, value: %l", (long)*parm); + returnValue = Parameter((void*)parm, LONG); + } + + void Operation::setReturnValue(const unsigned short *parm) + { + logentry(); + loginfo("Setting operation return value, type: unsigned short, value: %hu", (unsigned short)*parm); + returnValue = Parameter((void*)parm, USHORT); + } + + void Operation::setReturnValue(const unsigned int *parm) + { + logentry(); + loginfo("Setting operation return value, type: unsigned short, value: %u", (unsigned int)*parm); + returnValue = Parameter((void*)parm, UINT); + } + + void Operation::setReturnValue(const unsigned long *parm) + { + logentry(); + loginfo("Setting operation return value, type: unsigned long, value: %lu", (unsigned long)*parm); + returnValue = Parameter((void*)parm, ULONG); + } + + void Operation::setReturnValue(const float *parm) + { + logentry(); + loginfo("Setting operation return value, type: float, value: %f", (float)*parm); + returnValue = Parameter((void*)parm, FLOAT); + } + + void Operation::setReturnValue(const double *parm) + { + logentry(); + loginfo("Setting operation return value, type: double, value: %lf", (double)*parm); + returnValue = Parameter((void*)parm, DOUBLE); + } + + void Operation::setReturnValue(const long double *parm) + { + logentry(); + loginfo("Setting operation return value, type: long double, value: %Lf", (long double)*parm); + returnValue = Parameter((void*)parm, LONGDOUBLE); + } + + void Operation::setReturnValue(const char *parm) + { + logentry(); + loginfo("Setting operation return value, type: char, value: %d", (char)*parm); + returnValue = Parameter((void*)parm, CHAR); + } + + void Operation::setReturnValue(const char* *parm) + { + logentry(); + loginfo("Setting operation return value, type: char*, value: %s", (char*)*parm); + returnValue = Parameter((void*)parm, CHARS); + } + + void Operation::setReturnValue(const string *parm) + { + logentry(); + loginfo("Setting operation return value, type: string, value: %s", (*parm).c_str()); + returnValue = Parameter((void*)parm, STRING); + } + + void Operation::setReturnValue(const DataObjectPtr *parm) + { + logentry(); + ostringstream os; + os << *parm; + loginfo("Setting operation return value, type: DataObject, value: %s", os.str().c_str()); + returnValue = Parameter((void*)new DataObjectPtr(*parm), DATAOBJECT); + } + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/core/Operation.h b/cpp/sca/runtime/core/src/tuscany/sca/core/Operation.h new file mode 100644 index 0000000000..89cf0b63b7 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/core/Operation.h @@ -0,0 +1,251 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_core_operation_h +#define tuscany_sca_core_operation_h + +#include <string> +#include <vector> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" + + +namespace tuscany +{ + namespace sca + { + /** + * Holds the details of a single invocation of a business method. + * This class is used to pass the parameters and operation name from the + * client to a service. It will also hold the return value on the + * return from the business method. + */ + class Operation + { + public: + /** + * Create a new operation. + * @param operationName The method name of the business method to be invoked. + * @param numParameters The number of parameters to be passed. + */ + SCA_API Operation(const char* operationName = 0); + + /** + * Destructor. + */ + SCA_API virtual ~Operation(); + + /** + * Copy constructor. + */ + SCA_API Operation(const Operation& op); + + /** + * Assignment operator. + */ + SCA_API Operation& operator=(const Operation& op); + + /** + * Return the operation name. + * @return The name of the operation. + */ + SCA_API const std::string& getName() const {return name;} + + + enum ParameterType + { + UNSET = 0, + VOID_TYPE, + BOOL, + SHORT, + INT, + LONG, + USHORT, + UINT, + ULONG, + FLOAT, + DOUBLE, + LONGDOUBLE, + CHARS, + CHAR, + STRING, + DATAOBJECT + }; + + class Parameter + { + public: + SCA_API Parameter(void* value = NULL, ParameterType type = VOID_TYPE, const std::string& name = ""); + SCA_API void* getValue() const {return value;} + SCA_API ParameterType getType() const {return type;} + SCA_API const std::string& getName() const {return name;} + SCA_API bool hasName() const {return (name.length() > 0);} + + private: + std::string name; + void* value; + ParameterType type; + }; + + /** + * Set a return value for the operation. + * @param retVal Pointer to the return value. + */ + SCA_API void setReturnValue(const void *retVal); + SCA_API void setReturnValue(const bool *retVal); + SCA_API void setReturnValue(const short *retVal); + SCA_API void setReturnValue(const int *retVal); + SCA_API void setReturnValue(const long *retVal); + SCA_API void setReturnValue(const unsigned short *retVal); + SCA_API void setReturnValue(const unsigned int *retVal); + SCA_API void setReturnValue(const unsigned long *retVal); + SCA_API void setReturnValue(const float *retVal); + SCA_API void setReturnValue(const double *retVal); + SCA_API void setReturnValue(const long double *retVal); + SCA_API void setReturnValue(const char *retVal); + SCA_API void setReturnValue(const char* *retVal); + SCA_API void setReturnValue(const std::string *retVal); + SCA_API void setReturnValue(const commonj::sdo::DataObjectPtr *retVal); + + /** + * Set a parameter on the operation. + * @param parm Pointer to the parameter to be passed. + */ + SCA_API void addParameter(const void *parm); + SCA_API void addParameter(const bool *parm); + SCA_API void addParameter(const short *parm); + SCA_API void addParameter(const int *parm); + SCA_API void addParameter(const long *parm); + SCA_API void addParameter(const unsigned short *parm); + SCA_API void addParameter(const unsigned int *parm); + SCA_API void addParameter(const unsigned long *parm); + SCA_API void addParameter(const float *parm); + SCA_API void addParameter(const double *parm); + SCA_API void addParameter(const long double *parm); + SCA_API void addParameter(const char *parm); + SCA_API void addParameter(const char* *parm); + SCA_API void addParameter(const std::string *parm); + SCA_API void addParameter(const commonj::sdo::DataObjectPtr *parm); + + /** + * Set a parameter on the operation. + * @param name The name of the parameter in the parameter list. + * @param parm Pointer to the parameter to be passed. + */ + SCA_API void addParameter(const std::string& name, const void *parm); + SCA_API void addParameter(const std::string& name, const bool *parm); + SCA_API void addParameter(const std::string& name, const short *parm); + SCA_API void addParameter(const std::string& name, const int *parm); + SCA_API void addParameter(const std::string& name, const long *parm); + SCA_API void addParameter(const std::string& name, const unsigned short *parm); + SCA_API void addParameter(const std::string& name, const unsigned int *parm); + SCA_API void addParameter(const std::string& name, const unsigned long *parm); + SCA_API void addParameter(const std::string& name, const float *parm); + SCA_API void addParameter(const std::string& name, const double *parm); + SCA_API void addParameter(const std::string& name, const long double *parm); + SCA_API void addParameter(const std::string& name, const char *parm); + SCA_API void addParameter(const std::string& name, const char* *parm); + SCA_API void addParameter(const std::string& name, const std::string *parm); + SCA_API void addParameter(const std::string& name, const commonj::sdo::DataObjectPtr *parm); + + SCA_API unsigned int getNParms() const {return parameters.size();} + + /** + * Get a parameter from the operation. + * @param pos The position of the parameter in the parameter list. + * @return Pointer to the paramter at the given postion. Should be + * cast to the appropriate type. + */ + SCA_API const Parameter& getParameter(unsigned int pos) const; + + /** + * Get a parameter from the operation. + * @param name The name of the parameter in the parameter list. + * @return Pointer to the paramter with the given name. Should be + * cast to the appropriate type. + */ + SCA_API const Parameter& getParameter(const std::string& name) const; + + /** + * Get a parameter type from the operation. + * @param pos The position of the parameter in the parameter list. + * @return Type of the parameter at the given position. + */ + SCA_API ParameterType getParameterType(unsigned int pos) const; + + /** + * Get a parameter type from the operation. + * @param name The name of the parameter in the parameter list. + * @return Type of the parameter with the given name. + */ + SCA_API ParameterType getParameterType(const std::string& name) const; + + /** + * Get a parameter name from the operation. + * @param pos The position of the parameter in the parameter list. + * @return Name of the parameter at the given position. + */ + SCA_API const std::string& getParameterName(unsigned int pos) const; + + /** + * Get the parameter value from the operation. + * @param pos The position of the parameter in the parameter list. + * @return Pointer to the value of the parameter at the given postion. Should be + * cast to the appropriate type. + */ + SCA_API void* getParameterValue(unsigned int pos) const; + + /** + * Get the parameter value from the operation. + * @param name The name of the parameter in the parameter list. + * @return Pointer to the value of the parameter with the given name. Should be + * cast to the appropriate type. + */ + SCA_API void* getParameterValue(const std::string& name) const; + + SCA_API ParameterType getReturnType() const {return returnValue.getType();} + SCA_API void* getReturnValue() const {return returnValue.getValue();} + + private: + /** + * Operation name (method name). + */ + std::string name; + + /** + * Array of parameters. + */ + typedef std::vector<Parameter> PARAMETER_VECTOR; + + PARAMETER_VECTOR parameters; + + Parameter returnValue; + + void clean(); + void copy(const Operation& op); + + }; + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_core_operation_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/core/SCARuntime.cpp b/cpp/sca/runtime/core/src/tuscany/sca/core/SCARuntime.cpp new file mode 100644 index 0000000000..80c2c1a46a --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/core/SCARuntime.cpp @@ -0,0 +1,613 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/core/SCARuntime.h" +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/model/ModelLoader.h" +#include "tuscany/sca/util/File.h" +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/Component.h" + +using namespace std; +using namespace tuscany::sca::model; +using namespace tuscany::sca::util; + +namespace tuscany +{ + namespace sca + { + /** + * Environment variable names + */ + static const char* TUSCANY_SCACPP = "TUSCANY_SCACPP"; + static const char* TUSCANY_SCACPP_SYSTEM_ROOT = "TUSCANY_SCACPP_SYSTEM_ROOT"; + static const char* TUSCANY_SCACPP_DEFAULT_COMPONENT = "TUSCANY_SCACPP_DEFAULT_COMPONENT"; + + static const char* TUSCANY_SCACPP_ROOT = "TUSCANY_SCACPP_ROOT"; + static const char* TUSCANY_SCACPP_COMPONENT = "TUSCANY_SCACPP_COMPONENT"; + static const char* TUSCANY_SCACPP_PATH = "TUSCANY_SCACPP_PATH"; + static const char* TUSCANY_SCACPP_BASE_URI = "TUSCANY_SCACPP_BASE_URI"; + + // Initialize statics + SCARuntime* SCARuntime::sharedRuntime = NULL; + Mutex SCARuntime::sharedRuntimeLock; + ThreadLocal SCARuntime::current; + + + // =================================================================== + // Constructor for the SCARuntime class. This will + // hold all the information about the current runtime. + // =================================================================== + SCARuntime::SCARuntime(const string& insRoot, + const string& sysRoot, const string& sysPath, + const string& base, const string& defName) + : system(0), + installRoot(insRoot), systemRoot(sysRoot), systemPath(sysPath), + defaultBaseURI(base), defaultComponentName(defName) + { + logentry(); + + if (installRoot == "") + { + // Get install dir from environment variable TUSCANY_SCACPP + const char* root = getenv(TUSCANY_SCACPP); + if (root != NULL) + { + installRoot = root; + } + else + { + string msg = TUSCANY_SCACPP; + msg += " environment variable not set"; + throwException(SystemConfigurationException, msg.c_str()); + } + } + loginfo("SCA runtime install root: %s", installRoot.c_str()); + + if (systemRoot == "") + { + // Get root from environment variable TUSCANY_SCACPP_ROOT + char* systemRootEnv = getenv(TUSCANY_SCACPP_ROOT); + if (systemRootEnv == 0) + { + // Get root from environment variable TUSCANY_SCACPP_SYSTEM_ROOT + systemRootEnv = getenv(TUSCANY_SCACPP_SYSTEM_ROOT); + } + if (systemRootEnv == 0) + { + string msg = TUSCANY_SCACPP_ROOT; + msg += " environment variable not set"; + throwException(SystemConfigurationException, msg.c_str()); + } + + systemRoot = systemRootEnv; + } + else + { + loginfo("System root: %s", systemRoot.c_str()); + } + + if (systemPath == "") + { + + // Get system path from environment variable TUSCANY_SCACPP_PATH + char* systemPathEnv = getenv(TUSCANY_SCACPP_PATH); + if (systemPathEnv != 0) + { + systemPath = systemPathEnv; + } + } + else + { + loginfo("System path: %s", systemPath.c_str()); + } + + if (defaultBaseURI == "") + { + + // Get default base URI from environment variable TUSCANY_SCACPP_BASE_URI + char* baseURI = getenv(TUSCANY_SCACPP_BASE_URI); + if (baseURI != 0) + { + defaultBaseURI = baseURI; + } + } + else + { + loginfo("Default base URI: %s", defaultBaseURI.c_str()); + } + + if (defaultComponentName == "") + { + const char* defComp = getenv(TUSCANY_SCACPP_COMPONENT); + if (!defComp) + { + defComp = getenv(TUSCANY_SCACPP_DEFAULT_COMPONENT); + } + if (defComp) + { + defaultComponentName = defComp; + } + } + loginfo("Default component: %s", defaultComponentName.c_str()); + + SCARuntime* currentRuntime = (SCARuntime*)current.getValue(); + current.setValue(this); + try + { + + // Load the runtime extensions + loadExtensions(); + + // Load the system composite + loadSystem(); + } + catch (...) + { + current.setValue(currentRuntime); + throw; + } + current.setValue(currentRuntime); + + // Find the default component + if (defaultComponentName != "") + { + Component* comp = system->findComponent(defaultComponentName); + if (!comp) + { + string message = "Component \'" + defaultComponentName + "\' not found"; + throwException(SystemConfigurationException, message.c_str()); + } + defaultComponent.setValue(comp); + } + + } + + // =================================================================== + // Destructor for the SCARuntime class. + // =================================================================== + SCARuntime::~SCARuntime() + { + logentry(); + + if (system) + { + delete system; + } + } + + // ========================================================== + // Returns the system configuration root + // ========================================================== + const string& SCARuntime::getSystemRoot() + { + return systemRoot; + } + + // ========================================================== + // Returns the system path + // ========================================================== + const string& SCARuntime::getSystemPath() + { + return systemPath; + } + + // ========================================================== + // Returns the default component name + // ========================================================== + const string& SCARuntime::getDefaultComponentName() + { + return defaultComponentName ; + } + + // ========================================================== + // Returns the default base URI + // ========================================================== + const string& SCARuntime::getDefaultBaseURI() + { + return defaultBaseURI; + } + + // ========================================================== + // Returns the install root + // ========================================================== + const string& SCARuntime::getInstallRoot() + { + return installRoot; + } + + // ============================================================= + // Get the runtime associated with the current thread. + // ============================================================= + SCARuntime* SCARuntime::getCurrentRuntime() + { + logentry(); + + SCARuntime* runtime = (SCARuntime*)current.getValue(); + if (runtime == NULL) + { + runtime = getSharedRuntime(); + if (runtime != NULL) + { + setCurrentRuntime(runtime); + } + else + { + runtime = new SCARuntime(); + setCurrentRuntime(runtime); + } + } + loginfo("Runtime: %p", runtime); + return runtime; + } + + // ============================================================= + // Set the runtime associated with the current thread. + // ============================================================= + void SCARuntime::setCurrentRuntime(SCARuntime* runtime) + { + logentry(); + + loginfo("Runtime: %p", runtime); + current.setValue(runtime); + } + + // ============================================================= + // Get the runtime associated with the current process. + // ============================================================= + SCARuntime* SCARuntime::getSharedRuntime() + { + logentry(); + + sharedRuntimeLock.lock(); + SCARuntime* runtime = sharedRuntime; + sharedRuntimeLock.unlock(); + + return runtime; + } + + // ============================================================= + // Initialize the runtime associated with the current process. + // ============================================================= + SCARuntime* SCARuntime::initializeSharedRuntime(const string& installRoot, const string& systemRoot, + const string& systemPath, const string& baseURI, const string& defaultComponentName) + { + logentry(); + + SCARuntime* runtime; + + sharedRuntimeLock.lock(); + try + { + if (sharedRuntime == NULL) + { + sharedRuntime = new SCARuntime(installRoot, systemRoot, systemPath, baseURI, defaultComponentName); + } + else + { + if (installRoot.size() != 0 && sharedRuntime->getInstallRoot() != installRoot) + { + string msg = "Cannot reconfigure runtime installation directory: " + string(installRoot); + throwException(SystemConfigurationException, msg.c_str()); + } + if (systemRoot.size() != 0 && sharedRuntime->getSystemRoot() != systemRoot) + { + string msg = "Cannot reconfigure SCA system root: " + string(systemRoot); + throwException(SystemConfigurationException, msg.c_str()); + } + if (systemPath.size() != 0 && sharedRuntime->getSystemPath() != systemPath) + { + string msg = "Cannot reconfigure SCA system path: " + string(systemPath); + throwException(SystemConfigurationException, msg.c_str()); + } + if (baseURI.size() != 0 && sharedRuntime->getDefaultBaseURI() != baseURI) + { + string msg = "Cannot reconfigure SCA system URI: " + string(baseURI); + throwException(SystemConfigurationException, msg.c_str()); + } + if (defaultComponentName.size() != 0 && sharedRuntime->getDefaultComponentName() != defaultComponentName) + { + string msg = "Cannot reconfigure main SCA component: " + string(baseURI); + throwException(SystemConfigurationException, msg.c_str()); + } + } + + runtime = sharedRuntime; + } + catch (...) + { + sharedRuntimeLock.unlock(); + throw; + } + sharedRuntimeLock.unlock(); + + return runtime; + } + + // ====================================== + // Load the system composite + // ====================================== + void SCARuntime::loadSystem() + { + logentry(); + + system = new Composite("tuscany/sca/system", ""); + ModelLoader loader(this, system); + loader.load(systemRoot, systemPath); + } + + // ====================================== + // Load up extensions to the runtime + // ====================================== + void SCARuntime::loadExtensions() + { + logentry(); + + string extensionsRoot = installRoot + "/extensions"; + +#if defined(WIN32) || defined (_WINDOWS) + string libraryExtension = ".dll"; +#else +#if defined(IS_DARWIN) + string libraryExtension = ".dylib"; +#else + string libraryExtension = ".so"; +#endif +#endif + string pattern = "*" + libraryExtension; + + // Get list of all directories named "module" + Files extensionModules(extensionsRoot, "module", true, true); + for (unsigned int emI=0; emI < extensionModules.size(); emI++) + { + string extensionRoot = extensionModules[emI].getDirectory().c_str(); + extensionRoot += "/module"; + loginfo("Loading extension module: %s", extensionRoot.c_str() ); + + Files files(extensionRoot, pattern, true); + for (unsigned int i=0; i < files.size(); i++) + { + try + { + string filename = files[i].getFileName(); + Library lib = Library( files[i].getDirectory() + "/" + filename); + + // Determine the name of the initialize method + // 1) strip the .dll/.so/.dylib suffix + // 2) for non-Windows strip any lib prefix + string initializeMethod; + #if defined(WIN32) || defined (_WINDOWS) + #else + if (filename.substr(0,3) == "lib") + { + initializeMethod = filename.substr(3, filename.size()-libraryExtension.size() - 3); + } + else + #endif + { + initializeMethod = filename.substr(0, filename.size()-libraryExtension.size()); + } + initializeMethod += "_initialize"; + TUSCANY_IMPLEMENTATION_EXTENSION_INITIALIZE extension = + (TUSCANY_IMPLEMENTATION_EXTENSION_INITIALIZE)lib.getSymbol(initializeMethod); + if (extension) + { + extension(); + extensionsList.push_back(lib); + } + } + catch (TuscanyRuntimeException& ex) + { + logwarning("Failed to load extension library: %s: %s: %s", + files[i].getFileName().c_str(), ex.getEClassName(), ex.getMessageText()); + } + } + } + } + + + // ====================================== + // register an interfaceExtension + // ====================================== + void SCARuntime::registerInterfaceExtension(InterfaceExtension* extension) + { + logentry(); + if (extension) + { + loginfo("Registering interface: %s", extension->getExtensionTypeQName().c_str()); + interfaceExtensions[extension->getExtensionTypeQName()] = extension; + } + } + + // ====================================== + // find an InterfaceExtension + // ====================================== + InterfaceExtension* SCARuntime::getInterfaceExtension(const string& extensionTypeQName) + { + return interfaceExtensions[extensionTypeQName]; + } + + // ====================================== + // register an implementationExtension + // ====================================== + void SCARuntime::registerImplementationExtension(ImplementationExtension* extension) + { + logentry(); + if (extension) + { + loginfo("Registering implementation: %s", extension->getExtensionTypeQName().c_str()); + implementationExtensions[extension->getExtensionTypeQName()] = extension; + } + } + + // ====================================== + // find an implementationExtension + // ====================================== + ImplementationExtension* SCARuntime::getImplementationExtension(const string& extensionTypeQName) + { + return implementationExtensions[extensionTypeQName]; + } + + // ====================================== + // register a referenceBindingExtension + // ====================================== + void SCARuntime::registerReferenceBindingExtension(ReferenceBindingExtension* extension) + { + logentry(); + if (extension) + { + loginfo("Registering reference binding: %s", extension->getExtensionTypeQName().c_str()); + referenceBindingExtensions[extension->getExtensionTypeQName()] = extension; + } + } + + // ====================================== + // find a referenceBindingExtension + // ====================================== + ReferenceBindingExtension* SCARuntime::getReferenceBindingExtension(const string& extensionTypeQName) + { + return referenceBindingExtensions[extensionTypeQName]; + } + + // ====================================== + // register a serviceBindingExtension + // ====================================== + void SCARuntime::registerServiceBindingExtension(ServiceBindingExtension* extension) + { + logentry(); + if (extension) + { + loginfo("Registering service binding: %s", extension->getExtensionTypeQName().c_str()); + serviceBindingExtensions[extension->getExtensionTypeQName()] = extension; + } + } + + // ====================================== + // find a serviceBindingExtension + // ====================================== + ServiceBindingExtension* SCARuntime::getServiceBindingExtension(const string& extensionTypeQName) + { + return serviceBindingExtensions[extensionTypeQName]; + } + + + // =================================== + // Return the top of the runtime model + // =================================== + Composite* SCARuntime::getSystem() + { + logentry(); + + return system; + } + + + // =================================================== + // setCurrentComponent: push component for this thread + // =================================================== + void SCARuntime::setCurrentComponent(Component* component) + { + logentry(); + + COMPONENT_STACK* compStack = (COMPONENT_STACK*)componentStack.getValue(); + if (compStack == NULL) + { + compStack = new COMPONENT_STACK(); + componentStack.setValue(compStack); + } + compStack->push(component); + } + + // ==================================================== + // unsetCurrentComponent: pop component for this thread + // ==================================================== + Component* SCARuntime::unsetCurrentComponent() + { + logentry(); + + COMPONENT_STACK* compStack = (COMPONENT_STACK*)componentStack.getValue(); + if (compStack != NULL) + { + if (compStack->size() > 0) + { + Component* component = compStack->top(); + compStack->pop(); + return component; + } + } + + return NULL; + } + + // ============================================================= + // getCurrentComponent: return current component for this thread + // ============================================================= + Component* SCARuntime::getCurrentComponent() + { + logentry(); + + COMPONENT_STACK* compStack = (COMPONENT_STACK*)componentStack.getValue(); + if (compStack != NULL) + { + if (compStack->size() > 0) + { + return compStack->top(); + } + } + + return NULL; + } + + // =========================================== + // getDefaultComponent: return the default composite component + // =========================================== + Component* SCARuntime::getDefaultComponent() + { + logentry(); + + Component* comp = (Component*)defaultComponent.getValue(); + if (comp == NULL && defaultComponentName != "") + { + comp = system->findComponent(defaultComponentName); + if (!comp) + { + string message = "Component \'" + defaultComponentName + "\' not found"; + throwException(SystemConfigurationException, message.c_str()); + } + defaultComponent.setValue(comp); + } + return comp; + } + + // =========================================== + // Set the default composite component + // =========================================== + void SCARuntime::setDefaultComponent(Component* component) + { + logentry(); + + return defaultComponent.setValue(component); + } + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/core/SCARuntime.h b/cpp/sca/runtime/core/src/tuscany/sca/core/SCARuntime.h new file mode 100644 index 0000000000..ff93bd5005 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/core/SCARuntime.h @@ -0,0 +1,295 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_core_scaruntime_h +#define tuscany_sca_core_scaruntime_h + +#if defined(WIN32) || defined (_WINDOWS) +#include <windows.h> +#else +#include <pthread.h> +#endif + +#include <stack> +#include <string> +#include <map> +#include <list> + +#include "tuscany/sca/export.h" +#include "tuscany/sca/extension/InterfaceExtension.h" +#include "tuscany/sca/extension/ImplementationExtension.h" +#include "tuscany/sca/extension/ReferenceBindingExtension.h" +#include "tuscany/sca/extension/ServiceBindingExtension.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/util/Library.h" +#include "tuscany/sca/util/ThreadLocal.h" +#include "tuscany/sca/util/Mutex.h" + + +namespace tuscany +{ + namespace sca + { + + /** + * Represents an executing SCA runtime. + */ + class SCARuntime { + public: + + /** + * Constructor + */ + SCA_API SCARuntime(const std::string& installRoot = "", + const std::string& systemRoot = "", const std::string& systemPath = "", + const std::string& baseURI = "", const std::string& defaultComponentName = ""); + + /** + * Destructor + */ + SCA_API virtual ~SCARuntime(); + + /** + * Get the runtime associated with the current thread. + * @return The runtime associated with the current thread. + */ + SCA_API static SCARuntime* getCurrentRuntime(); + + /** + * Get the runtime associated with the current thread. + * @return The runtime associated with the current thread. + */ + SCA_API static void setCurrentRuntime(SCARuntime* runtime); + + /** + * Get the runtime associated with the current process. + * @return The runtime associated with the current process. + */ + SCA_API static SCARuntime* getSharedRuntime(); + + /** + * Initialize the runtime associated with the current process. + * @return The runtime associated with the current process. + */ + SCA_API static SCARuntime* initializeSharedRuntime(const std::string& installRoot = "", + const std::string& systemRoot = "", const std::string& systemPath = "", + const std::string& baseURI = "", const std::string& defaultComponentName = ""); + + /** + * Returns the directory in which the Tuscany runtime has been installed. + */ + SCA_API const std::string& getInstallRoot(); + + /** + * Returns the system root + */ + SCA_API const std::string& getSystemRoot(); + + /** + * Returns the search path for composites. + */ + SCA_API const std::string& getSystemPath(); + + /** + * Returns the default component name. + */ + SCA_API const std::string& getDefaultComponentName(); + + /** + * Returns the default base URI for the system + */ + SCA_API const std::string& getDefaultBaseURI(); + + /** + * Set the current component for the current thread. + * @param component The current component. + */ + SCA_API void setCurrentComponent(tuscany::sca::model::Component* component); + + /** + * Remove the current component from this thread, and return + * to the previous component (if there was one). + * @return The previous component. + */ + SCA_API tuscany::sca::model::Component* unsetCurrentComponent(); + + /** + * Get a pointer to the configured SCA system which this + * SCA runtime represents. + * The rest of the SCA configuration can be navigated from + * the System. + * @return The configured SCA system. + */ + SCA_API tuscany::sca::model::Composite* getSystem(); + + /** + * Return the current component for this thread. + * @return The current component for this thread. + */ + SCA_API tuscany::sca::model::Component* getCurrentComponent(); + + /** + * Get the default component set for the current thread. + * @return The default composite. + */ + SCA_API tuscany::sca::model::Component* getDefaultComponent(); + + /** + * Set the default component for the current thread. + * @return The default component. + */ + SCA_API void setDefaultComponent(tuscany::sca::model::Component* component); + + /** + * Register an implementation extension + */ + SCA_API void registerImplementationExtension(ImplementationExtension* extension); + + /** + * Returns the implementation extension associated with + * the specified qname + */ + SCA_API ImplementationExtension* getImplementationExtension(const std::string& typeQname); + + /** + * Register a reference binding extension + */ + SCA_API void registerReferenceBindingExtension(ReferenceBindingExtension* extension); + + /** + * Returns the reference binding extension associated with + * the specified qname + */ + SCA_API ReferenceBindingExtension* getReferenceBindingExtension(const std::string& typeQname); + + /** + * Register a service binding extension + */ + SCA_API void registerServiceBindingExtension(ServiceBindingExtension* extension); + + /** + * Returns the service binding extension associated with + * the specified qname + */ + SCA_API ServiceBindingExtension* getServiceBindingExtension(const std::string& typeQname); + + /** + * Register an interface extension + */ + SCA_API void registerInterfaceExtension(InterfaceExtension* extension); + + /** + * Returns the interface extension associated with + * the specified qname + */ + SCA_API InterfaceExtension* getInterfaceExtension(const std::string& typeQname); + + private: + + /** + * The runtime associated with the current thread. + */ + static tuscany::sca::util::ThreadLocal current; + + /** + * The runtime shared by all threads of the current process. + */ + static tuscany::sca::util::Mutex sharedRuntimeLock; + static SCARuntime* sharedRuntime; + + /** + * Pointer to the top of the runtime model. + */ + tuscany::sca::model::Composite* system; + + /** + * The installed path of the Tuscany runtime. + */ + std::string installRoot; + + /** + * The path to the system configuration + */ + std::string systemRoot; + + /** + * The search path for composites. + */ + std::string systemPath; + + /** + * The default base URI. + */ + std::string defaultBaseURI; + + /** + * The default CompositeComponent name. + */ + std::string defaultComponentName; + + /** + * Load the SCA configuration from the scdl files (sca.composite, + * *.fragment, etc). + * This will create the runtime model from which the SCA runtime + * will operate. + */ + void loadSystem(); + + /** + * Component stack for the current thread. + */ + typedef std::stack<tuscany::sca::model::Component*> COMPONENT_STACK; + tuscany::sca::util::ThreadLocal componentStack; + + /** + * The default component for the current thread. + */ + tuscany::sca::util::ThreadLocal defaultComponent; + + /** + * Runtime Extensions + */ + typedef std::map<std::string, ImplementationExtension*> IMPLEMENTATION_EXTENSIONS_MAP; + IMPLEMENTATION_EXTENSIONS_MAP implementationExtensions; + + typedef std::map<std::string, ReferenceBindingExtension*> REFERENCE_BINDING_EXTENSIONS_MAP; + REFERENCE_BINDING_EXTENSIONS_MAP referenceBindingExtensions; + + typedef std::map<std::string, ServiceBindingExtension*> SERVICE_BINDING_EXTENSIONS_MAP; + SERVICE_BINDING_EXTENSIONS_MAP serviceBindingExtensions; + + typedef std::map<std::string, InterfaceExtension*> INTERFACE_EXTENSIONS_MAP; + INTERFACE_EXTENSIONS_MAP interfaceExtensions; + + void loadExtensions(); + + typedef std::list<tuscany::sca::util::Library> EXTENSIONS_LIST; + EXTENSIONS_LIST extensionsList; + + }; + + } // End namespace sca +} // End namespace tuscany + +typedef void (* TUSCANY_IMPLEMENTATION_EXTENSION_INITIALIZE) (); + +#endif // tuscany_sca_core_scaruntime_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceProxy.cpp b/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceProxy.cpp new file mode 100644 index 0000000000..c54d7648e3 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceProxy.cpp @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/core/ServiceProxy.h" +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/core/Exceptions.h" + +using namespace tuscany::sca::model; + +namespace tuscany +{ + namespace sca + { + // ============================ + // Constructor: Create a proxy + // ============================ + ServiceProxy::ServiceProxy(Reference* reference) + : reference(reference) + { + logentry(); + } + + // ========== + // Destructor + // ========== + ServiceProxy::~ServiceProxy() + { + logentry(); + } + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceProxy.h b/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceProxy.h new file mode 100644 index 0000000000..f1a0309be1 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceProxy.h @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + + +#ifndef tuscany_sca_core_serviceproxy_h +#define tuscany_sca_core_serviceproxy_h + +#include "tuscany/sca/export.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/Reference.h" + + +namespace tuscany +{ + namespace sca + { + + /** + * Holds a proxy for a given component and reference. + * The proxy which is held inside a ServiceProxy will be specific to the component + * and reference and will have been code generated and be contained in a dll + * created by a developer of an SCA application. + */ + class SCA_API ServiceProxy + { + public: + /** + * Create a new service proxy for a reference. The proxy will contain a pointer to + * the target ServiceWrapper. + * @param reference The reference on the source component. + * @param target The wrapper of the service which is wired to this reference. + */ + ServiceProxy(tuscany::sca::model::Reference* reference); + + /** + * Destructor. + */ + virtual ~ServiceProxy(); + + /** + * Returns the reference represented by this proxy. + * @return The Reference represented by this proxy. + */ + tuscany::sca::model::Reference* getReference() const { return reference; }; + + private: + + /** + * The reference represented by this proxy. + */ + tuscany::sca::model::Reference* reference; + + }; + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_core_serviceproxy_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceWrapper.cpp b/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceWrapper.cpp new file mode 100644 index 0000000000..17666a5bb7 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceWrapper.cpp @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/core/ServiceWrapper.h" +#include "tuscany/sca/util/Logging.h" + +using namespace tuscany::sca::model; + +namespace tuscany +{ + namespace sca + { + + // =========== + // Constructor + // =========== + ServiceWrapper::ServiceWrapper(Service* service) + : service(service) + { + logentry(); + } + + // ========== + // Destructor + // ========== + ServiceWrapper::~ServiceWrapper() + { + logentry(); + } + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceWrapper.h b/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceWrapper.h new file mode 100644 index 0000000000..6a44c57675 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/core/ServiceWrapper.h @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_core_servicewrapper_h +#define tuscany_sca_core_servicewrapper_h + +#include "tuscany/sca/export.h" +#include "tuscany/sca/core/Operation.h" +#include "tuscany/sca/model/Service.h" + + +namespace tuscany +{ + namespace sca + { + + /** + * An abstract class that wraps a component implementation or an external + * service. + */ + class SCA_API ServiceWrapper + { + public: + /** + * Constructor. + * @param target The service wrapper wraps the target of a wire. + */ + ServiceWrapper(tuscany::sca::model::Service* service); + + /** + * Destructor. + */ + virtual ~ServiceWrapper(); + + /** + * Get the service represented by this wrapper. + * @return The service represented by this wrapper. + */ + tuscany::sca::model::Service* getService() const { return service; } + + /** + * All business method calls on the target service are performed through + * this invoke method. + * @param operation The details of the method, paramaters and return value for the + * business method to be called on the target service. + */ + virtual void invoke(Operation& operation) = 0; + + private: + /** + * The target represented by this wrapper. + */ + tuscany::sca::model::Service* service; + + }; + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_core_servicewrapper_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/export.h b/cpp/sca/runtime/core/src/tuscany/sca/export.h new file mode 100644 index 0000000000..79b02fe4cf --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/export.h @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_export_h +#define tuscany_sca_export_h + +#if defined(WIN32) || defined (_WINDOWS) +#pragma warning(disable: 4786) + +#ifdef SCA_EXPORTS +#define SCA_API __declspec(dllexport) +#else +#define SCA_API __declspec(dllimport) +#endif + +#else +#include <sys/time.h> +#include <inttypes.h> +#include <stdlib.h> +#define SCA_API +#endif + +#endif // tuscany_sca_export_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/extension/ImplementationExtension.cpp b/cpp/sca/runtime/core/src/tuscany/sca/extension/ImplementationExtension.cpp new file mode 100644 index 0000000000..a20cf1f6da --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/extension/ImplementationExtension.cpp @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/extension/ImplementationExtension.h" +#include "tuscany/sca/util/Logging.h" + +namespace tuscany +{ + namespace sca + { + // =================================================================== + // Constructor for the ImplementationExtension class. + // =================================================================== + ImplementationExtension::ImplementationExtension() + { + logentry(); + } + + // =================================================================== + // Destructor for the ImplementationExtension class. + // =================================================================== + ImplementationExtension::~ImplementationExtension() + { + logentry(); + } + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/extension/ImplementationExtension.h b/cpp/sca/runtime/core/src/tuscany/sca/extension/ImplementationExtension.h new file mode 100644 index 0000000000..524af2df77 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/extension/ImplementationExtension.h @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_extension_implementationextension_h +#define tuscany_sca_extension_implementationextension_h + +#include <string> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/Service.h" +#include "tuscany/sca/core/ServiceWrapper.h" +#include "tuscany/sca/model/Composite.h" + +namespace tuscany +{ + namespace sca + { + class SCA_API ImplementationExtension + { + public: + /** + * Default constructor + */ + ImplementationExtension(); + + /** + * Destructor + */ + virtual ~ImplementationExtension(); + + /** + * return the name of the extension (e.g. "cpp" "php") + */ + virtual const std::string& getExtensionName() = 0; + + /** + * return the QName of schema elemant for this implementation extension + * (e.g. "http://www.osoa.org/xmlns/sca/1.0#implementation.cpp") + */ + virtual const std::string& getExtensionTypeQName() = 0; + + /** + * Get an implementation from a DataObject representing + * an SCDL implementation element + */ + virtual tuscany::sca::model::ComponentType* getImplementation( + tuscany::sca::model::Composite* composite, + commonj::sdo::DataObjectPtr scdlImplementation) = 0; + + }; + + + } // End namespace sca +} // End namespace tuscany + + +#endif // tuscany_sca_extension_implementationextension_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/extension/InterfaceExtension.cpp b/cpp/sca/runtime/core/src/tuscany/sca/extension/InterfaceExtension.cpp new file mode 100644 index 0000000000..584945344b --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/extension/InterfaceExtension.cpp @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/extension/InterfaceExtension.h" +#include "tuscany/sca/util/Logging.h" + +namespace tuscany +{ + namespace sca + { + // =================================================================== + // Constructor for the InterfaceExtension class. + // =================================================================== + InterfaceExtension::InterfaceExtension() + { + logentry(); + } + + // =================================================================== + // Destructor for the InterfaceExtension class. + // =================================================================== + InterfaceExtension::~InterfaceExtension() + { + logentry(); + } + + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/extension/InterfaceExtension.h b/cpp/sca/runtime/core/src/tuscany/sca/extension/InterfaceExtension.h new file mode 100644 index 0000000000..5329104c77 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/extension/InterfaceExtension.h @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_extension_interfaceextension_h +#define tuscany_sca_extension_interfaceextension_h + +#include <string> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" +#include "tuscany/sca/model/Interface.h" +#include "tuscany/sca/model/Composite.h" + + +namespace tuscany +{ + namespace sca + { + class SCA_API InterfaceExtension + { + public: + /** + * Default constructor + */ + InterfaceExtension(); + + /** + * Destructor + */ + virtual ~InterfaceExtension(); + + /** + * return the name of the extension (e.g. "cpp" "php") + */ + virtual const std::string& getExtensionName() = 0; + + /** + * return the QName of schema elemant for this interface extension + * (e.g. "http://www.osoa.org/xmlns/sca/1.0#interface.cpp") + */ + virtual const std::string& getExtensionTypeQName() = 0; + + /** + * Get an interface from a DataObject representing an + * SCDL interface + */ + virtual tuscany::sca::model::Interface* getInterface( + tuscany::sca::model::Composite *composite, + commonj::sdo::DataObjectPtr scdlInterface) = 0; + + }; + + + } // End namespace sca +} // End namespace tuscany + + +#endif // tuscany_sca_extension_interfaceextension_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/extension/ReferenceBindingExtension.cpp b/cpp/sca/runtime/core/src/tuscany/sca/extension/ReferenceBindingExtension.cpp new file mode 100644 index 0000000000..a7f60dcbec --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/extension/ReferenceBindingExtension.cpp @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/extension/ReferenceBindingExtension.h" +#include "tuscany/sca/util/Logging.h" + +namespace tuscany +{ + namespace sca + { + // =================================================================== + // Constructor for the ReferenceBindingExtension class. + // =================================================================== + ReferenceBindingExtension::ReferenceBindingExtension() + { + logentry(); + } + + // =================================================================== + // Destructor for the ReferenceBindingExtension class. + // =================================================================== + ReferenceBindingExtension::~ReferenceBindingExtension() + { + logentry(); + } + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/extension/ReferenceBindingExtension.h b/cpp/sca/runtime/core/src/tuscany/sca/extension/ReferenceBindingExtension.h new file mode 100644 index 0000000000..c3d1e97c9e --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/extension/ReferenceBindingExtension.h @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_extension_referencebindingextension_h +#define tuscany_sca_extension_referencebindingextension_h + +#include <string> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/Reference.h" +#include "tuscany/sca/core/ServiceWrapper.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/ReferenceBinding.h" + + +namespace tuscany +{ + namespace sca + { + class SCA_API ReferenceBindingExtension + { + public: + /** + * Default constructor + */ + ReferenceBindingExtension(); + + /** + * Destructor + */ + virtual ~ReferenceBindingExtension(); + + /** + * return the name of the extension (e.g. "cpp" "php") + */ + virtual const std::string& getExtensionName() = 0; + + /** + * return the QName of schema elemant for this binding extension + * (e.g. "http://www.osoa.org/xmlns/sca/1.0#binding.ws") + */ + virtual const std::string& getExtensionTypeQName() = 0; + + /** + * Get a reference binding from a DataObject representing + * an SCDL binding element + */ + virtual tuscany::sca::model::ReferenceBinding* getReferenceBinding( + tuscany::sca::model::Composite* composite, + tuscany::sca::model::Reference* reference, + commonj::sdo::DataObjectPtr scdlBinding) = 0; + + }; + + + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_extension_referencebindingextension_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/extension/ServiceBindingExtension.cpp b/cpp/sca/runtime/core/src/tuscany/sca/extension/ServiceBindingExtension.cpp new file mode 100644 index 0000000000..f5fe361d36 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/extension/ServiceBindingExtension.cpp @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/extension/ServiceBindingExtension.h" +#include "tuscany/sca/util/Logging.h" + +namespace tuscany +{ + namespace sca + { + // =================================================================== + // Constructor for the ServiceBindingExtension class. + // =================================================================== + ServiceBindingExtension::ServiceBindingExtension() + { + logentry(); + } + + // =================================================================== + // Destructor for the ServiceBindingExtension class. + // =================================================================== + ServiceBindingExtension::~ServiceBindingExtension() + { + logentry(); + } + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/extension/ServiceBindingExtension.h b/cpp/sca/runtime/core/src/tuscany/sca/extension/ServiceBindingExtension.h new file mode 100644 index 0000000000..d2f7e72ed0 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/extension/ServiceBindingExtension.h @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_extension_servicebindingextension_h +#define tuscany_sca_extension_servicebindingextension_h + +#include <string> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/Service.h" +#include "tuscany/sca/core/ServiceWrapper.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/ServiceBinding.h" + + +namespace tuscany +{ + namespace sca + { + class SCA_API ServiceBindingExtension + { + public: + /** + * Default constructor + */ + ServiceBindingExtension(); + + /** + * Destructor + */ + virtual ~ServiceBindingExtension(); + + /** + * return the name of the extension (e.g. "cpp" "php") + */ + virtual const std::string& getExtensionName() = 0; + + /** + * return the QName of schema elemant for this binding extension + * (e.g. "http://www.osoa.org/xmlns/sca/1.0#binding.ws") + */ + virtual const std::string& getExtensionTypeQName() = 0; + + /** + * Get a reference binding from a DataObject representing + * an SCDL binding element + */ + virtual tuscany::sca::model::ServiceBinding* getServiceBinding( + tuscany::sca::model::Composite* composite, + tuscany::sca::model::Service* service, + commonj::sdo::DataObjectPtr scdlBinding) = 0; + + }; + + + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_extension_servicebindingextension_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Binding.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/Binding.cpp new file mode 100644 index 0000000000..a654b9f0f7 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Binding.cpp @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/model/Binding.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + + namespace model + { + + // Constructor + Binding::Binding(const string& uri) : uri(uri) + { + logentry(); + } + + // Destructor + Binding::~Binding() + { + logentry(); + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Binding.h b/cpp/sca/runtime/core/src/tuscany/sca/model/Binding.h new file mode 100644 index 0000000000..66dc66e853 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Binding.h @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_binding_h +#define tuscany_sca_model_binding_h + +#include <string> + +namespace tuscany +{ + namespace sca + { + namespace model + { + /** + * Represents a binding. + * Bindings are used by services and references. References use bindings + * to describe the access mechanism used to call an external service (which can + * be a service provided by another SCA composite). Services use bindings to describe + * the access mechanism that clients (which can be a client from another SCA composite) + * have to use to call the service. + * This interface will typically be extended by binding implementations to allow + * specification of binding/transport specific information. + */ + class Binding + { + + public: + + /** + * Constructor to create a new binding. + * @param uri The binding URI. + */ + SCA_API Binding(const std::string& uri); + + /** + * Destructor. + */ + SCA_API virtual ~Binding(); + + /** + * Returns the binding type + * @return The binding type. + */ + SCA_API virtual std::string getType() = 0; + + /** + * Returns the binding URI. + * @return The binding URI. + */ + SCA_API virtual const std::string& getURI() const { return uri; }; + + private: + + /** + * The binding URI. + */ + std::string uri; + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_binding_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Component.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/Component.cpp new file mode 100644 index 0000000000..37bf381811 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Component.cpp @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <iostream> + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/Service.h" +#include "tuscany/sca/model/Reference.h" +#include "tuscany/sca/model/ServiceType.h" +#include "tuscany/sca/model/ReferenceType.h" +#include "tuscany/sca/model/ComponentType.h" + +using namespace std; +using namespace commonj::sdo; + +namespace tuscany +{ + namespace sca + { + namespace model + { + // Constructor + Component::Component(Composite* composite, const std::string& componentName, ComponentType *componentType) + : name(componentName), composite(composite), type(componentType) + { + logentry(); + loginfo("Component name: %s", name.c_str()); + + // Initialize the component from its component type + componentType->initializeComponent(this); + + } + + Component::~Component() + { + logentry(); + } + + void Component::addService(Service* service) + { + logentry(); + services[service->getType()->getName()] = service; + } + + Service* Component::findService(const string& serviceName) + { + logentry(); + + // If serviceName is empty then return the ONLY service + if (serviceName == "" + && services.size() == 1) + { + return services.begin()->second; + } + else + { + Service* service = services[serviceName]; + if (service == NULL) + { + // A service with an empty name will match any service name + service = services[""]; + } + return service; + } + } + + void Component::addReference(Reference* reference) + { + logentry(); + references[reference->getType()->getName()] = reference; + } + + Reference* Component::findReference(const std::string& referenceName) + { + logentry(); + return references[referenceName]; + } + + DataObjectPtr Component::getProperties() + { + logentry(); + + if (!properties) + { + properties = type->getPropertyDataFactory()->create("org/osoa/sca", "Properties"); + } + return properties; + } + + void Component::setProperty(const string& name, DataObjectPtr value) + { + logentry(); + + DataObjectPtr props = getProperties(); + + // Get the property's type + PropertyPtr propProperty = props->getInstanceProperty(name); + if (!propProperty) { + std::string msg("Property not found: "); + msg += name; + throwException(SystemConfigurationException, msg.c_str()); + } + const Type& propType = propProperty->getType(); + if (propType.isDataType()) + { + if (propProperty->isMany()) + { + DataObjectList& dol = props->getList(*propProperty); + dol.append(value->getCString("")); + } + else + { + props->setCString(*propProperty, value->getCString("")); + } + } + else + { + // Create a new instance of the DO + // iterate over properties setting each one + // for now: + props->setDataObject(*propProperty, value); + } + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Component.h b/cpp/sca/runtime/core/src/tuscany/sca/model/Component.h new file mode 100644 index 0000000000..882d79a94c --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Component.h @@ -0,0 +1,183 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_component_h +#define tuscany_sca_model_component_h + +#include <string> +#include <map> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" + + +namespace tuscany +{ + namespace sca + { + namespace model + { + + class Composite; + class ComponentType; + class Reference; + class ReferenceType; + class Service; + class ServiceType; + + /** + * A component is a configured instance of an implementation. Components provide + * and consume services. More than one component can use and configure the same + * implementation, where each component configures the implementation differently. + * For example each component may configure a reference of the same implementation + * to consume a different service. + */ + class Component + { + public: + + /** + * Constructor + * @param composite The composite containing the component. + * @param name The name of the component. + */ + SCA_API Component(Composite *composite, const std::string& name, ComponentType *type); + + /** + * Destructor. + */ + SCA_API virtual ~Component(); + + /** + * Returns the name of this component. + * @return the name of this component + */ + SCA_API const std::string& getName() const { return name; } + + /** + * Returns the composite containing this component. + * @return The composite containing this component. + */ + SCA_API Composite* getComposite() const { return composite; } + + /** + * Returns the type of this component. + * @return The type of this component. + */ + SCA_API ComponentType* getType() const { return type; } + + /** + * Add a new service to this component. + * @param service The service to add. + */ + SCA_API void addService(Service* service); + + /** + * Find an existing service on this component. + * @param serviceName The name of the service to find. + * If the serviceName is the zero length string then if there is + * only one service it will be returned. + * @return The found service, or 0 if not found. + */ + SCA_API Service* findService(const std::string& serviceName); + + /** + * Add a new reference to this component. + * @param reference The reference to add. + */ + SCA_API void addReference(Reference* reference); + + /** + * Find an existing reference on this component. + * @param referenceName The name of the reference to find. + * @return The found reference, or 0 if not found. + */ + SCA_API Reference* findReference(const std::string& referenceName); + + /** + * Returns all the services defined on this component. + * @return All the services defined on this component. + */ + typedef std::map<std::string, Service*> SERVICE_MAP; + SCA_API const SERVICE_MAP& getServices() const { return services; }; + + /** + * Returns all the references defined on this component. + * @return All the references defined on this component. + */ + typedef std::map<std::string, Reference*> REFERENCE_MAP; + SCA_API const REFERENCE_MAP& getReferences() const { return references; }; + + /** + * Set the value of a property defined on this component. The values + * will usually come from a component declaration in a composite file. + * @param name The name of the property. + * @param value The value of the property. + */ + SCA_API void setProperty(const std::string& name, commonj::sdo::DataObjectPtr value); + + /** + * Returns a data object from which all the properties of the component + * and their values can be accessed. + * @return A data object holding the property values. + */ + SCA_API commonj::sdo::DataObjectPtr getProperties(); + + private: + + /** + * Name of the component. + */ + std::string name; + + /** + * Composite containing the component. + */ + Composite* composite; + + /** + * Type of the component. + */ + ComponentType* type; + + /** + * Map of all the services defined on this component. + */ + SERVICE_MAP services; + + /** + * Map of all the references defined on this component. + */ + REFERENCE_MAP references; + + /** + * The properties and their values for this component. + */ + commonj::sdo::DataObjectPtr properties; + + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_component_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ComponentType.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/ComponentType.cpp new file mode 100644 index 0000000000..20e70ede20 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ComponentType.cpp @@ -0,0 +1,294 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <iostream> + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/model/ComponentType.h" +#include "tuscany/sca/model/ServiceType.h" +#include "tuscany/sca/model/ReferenceType.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/Service.h" +#include "tuscany/sca/model/Reference.h" + +using namespace std; +using namespace commonj::sdo; +using namespace tuscany::sca::util; + +namespace tuscany +{ + namespace sca + { + namespace model + { + + // Constructor + ComponentType::ComponentType(Composite* composite, const string& name) + : composite(composite), name(name) + { + logentry(); + } + + // Destructor + ComponentType::~ComponentType() + { + logentry(); + } + + void ComponentType::addServiceType(ServiceType* serviceType) + { + logentry(); + serviceTypes[serviceType->getName()] = serviceType; + } + + ServiceType* ComponentType::findServiceType(const string& serviceName) + { + logentry(); + + // If serviceName is empty then return the ONLY service + if (serviceName == "" + && serviceTypes.size() == 1) + { + return serviceTypes.begin()->second; + } + else + { + return serviceTypes[serviceName]; + } + } + + void ComponentType::addReferenceType(ReferenceType* referenceType) + { + logentry(); + + referenceTypes[referenceType->getName()] = referenceType; + } + + ReferenceType* ComponentType::findReferenceType(const string& referenceName) + { + logentry(); + + return referenceTypes[referenceName]; + } + + void ComponentType::addPropertyType(const string& name, + const string& type, + bool many, + DataObjectPtr* defaultValue) + { + logentry(); + + // Create a Type in the Properties dataFactory + DataFactoryPtr factory = getPropertyDataFactory(); + + string typeUri, typeName; + Utils::tokeniseQName(type, typeUri, typeName); + + if (typeUri == "http://www.w3.org/2001/XMLSchema") + { + typeUri = "commonj.sdo"; + if (typeName == "string") + { + typeName = "String"; + } + else if (typeName == "anyType") + { + typeName = "DataObject"; + } + else if (typeName == "int") + { + typeName = "Int"; + } + else if (typeName == "integer") + { + typeName = "Integer"; + } + else if (typeName == "negativeInteger") + { + typeName = "Integer"; + } + else if (typeName == "nonNegativeInteger") + { + typeName = "Integer"; + } + else if (typeName == "positiveInteger") + { + typeName = "Integer"; + } + else if (typeName == "nonPositiveInteger") + { + typeName = "Integer"; + } + else if (typeName == "unsignedLong") + { + typeName = "Integer"; + } + else if (typeName == "unsignedShort") + { + typeName = "Integer"; + } + else if (typeName == "unsignedInt") + { + typeName = "Long"; + } + else if (typeName == "long") + { + typeName = "Long"; + } + else if (typeName == "double") + { + typeName = "Double"; + } + else if (typeName == "short") + { + typeName = "Short"; + } + else if (typeName == "unsignedByte") + { + typeName = "Short"; + } + else if (typeName == "float") + { + typeName = "Float"; + } + else if (typeName == "boolean") + { + typeName = "Boolean"; + } + else if (typeName == "byte") + { + typeName = "Byte"; + } + else if (typeName == "base64Binary") + { + typeName = "Bytes"; + } + else if (typeName == "hexBinary") + { + typeName = "Bytes"; + } + else if (typeName == "anyURI") + { + typeName = "URI"; + } + else if (typeName == "QName") + { + typeName = "URI"; + } + else + { + // Default unknown xs: types to string?? + typeName = "String"; + } + } + else + { + // It's not an XML type + } + + factory->addPropertyToType( + "org/osoa/sca", + "Properties", + name.c_str(), + typeUri.c_str(), + typeName.c_str(), + many, + false, + true); + + // Set the default for a dataType + if (defaultValue!=NULL) + { + try + { + const Type& propType = factory->getType(typeUri.c_str(), typeName.c_str()); + if (propType.isDataType()) + { + factory->setDefault("org/osoa/sca", "Properties", + name.c_str(), + (char*)(*defaultValue)->getCString("")); + } + } + catch (SDOTypeNotFoundException&) + { + } + } + } + + const Property* ComponentType::findPropertyType(const string& propertyName) + { + logentry(); + const Property* property = NULL; + + DataFactoryPtr dataFactory = getPropertyDataFactory(); + const Type& propertiesType = dataFactory->getType("org/osoa/sca", "Properties"); + + try + { + const Property& prop = propertiesType.getProperty(propertyName); + property = ∝ + } + catch(SDOPropertyNotFoundException) + { + loginfo("Property named %s not found, returning null", propertyName.c_str()); + } + return property; + } + + DataFactoryPtr ComponentType::getPropertyDataFactory() + { + logentry(); + + if (!propertyFactory) + { + propertyFactory = DataFactory::getDataFactory(); + // Add the root type + propertyFactory->addType("org/osoa/sca", "Properties", false, false, false, false); + } + return propertyFactory; + } + + void ComponentType::initializeComponent(Component* component) + { + logentry(); + + for (SERVICETYPE_MAP::iterator iter = serviceTypes.begin(); + iter != serviceTypes.end(); + iter++) + { + Service* service = new Service(component, iter->second); + component->addService(service); + } + for (REFERENCETYPE_MAP::iterator refiter = referenceTypes.begin(); + refiter != referenceTypes.end(); + refiter++) + { + Reference* reference = new Reference(component, refiter->second); + component->addReference(reference); + } + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ComponentType.h b/cpp/sca/runtime/core/src/tuscany/sca/model/ComponentType.h new file mode 100644 index 0000000000..86f17890e9 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ComponentType.h @@ -0,0 +1,186 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_componenttype_h +#define tuscany_sca_model_componenttype_h + +#include <string> +#include <map> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/model/ReferenceType.h" + +namespace tuscany +{ + namespace sca + { + namespace model + { + class ServiceType; + class ReferenceType; + class Interface; + class Component; + class Composite; + + /** + * Component type represents the configurable aspects of an implementation. + * A component type consists of services that are offered, references to other services + * that can be wired and properties that can be set. The settable properties and the settable + * references to services are configured by a component which uses the implementation. + * The component type can be thought of as the contract which is honoured by an implementation. + */ + class ComponentType + { + public: + + /** + * Constructor + */ + SCA_API ComponentType(Composite* composite, const std::string& name); + + /** + * Destructor. + */ + SCA_API virtual ~ComponentType(); + + /** + * Returns the name of the component type + */ + SCA_API const std::string& getName() const { return name; }; + + /** + * Returns the composite containing this component type + */ + SCA_API Composite* getComposite() const { return composite; }; + + /** + * Add a new service type to this component type. + * @param serviceType The service type to add. + */ + SCA_API virtual void addServiceType(ServiceType* serviceType); + + /** + * Find an existing service type on this component type. + * @param serviceName The name of the service type to find. + * If the serviceName is the zero length string then if there is + * only one service type it will be returned. + * @return The found service, or 0 if not found. + */ + SCA_API virtual ServiceType* findServiceType(const std::string& serviceName); + + /** + * Add a new reference type to this component type. + * @param referenceType The reference type to add. + */ + SCA_API virtual void addReferenceType(ReferenceType* referenceType); + + /** + * Find an existing reference type on this component type. + * @param referenceName The name of the reference type to find. + * @return The found reference type, or 0 if not found. + */ + SCA_API virtual ReferenceType* findReferenceType(const std::string& referenceName); + + /** + * Returns the service types defined on this component. + * @return The service types defined on this component. + */ + typedef std::map<std::string, ServiceType*> SERVICETYPE_MAP; + SERVICETYPE_MAP getServiceTypes() const { return serviceTypes; }; + + /** + * Returns the reference types defined on this component. + * @return The reference types defined on this component. + */ + typedef std::map<std::string, ReferenceType*> REFERENCETYPE_MAP; + REFERENCETYPE_MAP getReferenceTypes() const { return referenceTypes; }; + + /** + * Add a new property type to this component type. Property types are added + * one at a time. The property definitions usually come from a component type file. + * @param name The name of the property type. + * @param type The full name of the property data type (including uri and local name). + * @param many True if this is a many valued property. + * @param defaultValue The default value if the property does not have a + * value set. + */ + SCA_API virtual void addPropertyType(const std::string& name, + const std::string& type, + bool many, + commonj::sdo::DataObjectPtr* defaultValue); + + /** + * Find an existing property type on this component type. + * @param propertyName The name of the property type to find. + * @return The found property, or 0 if not found. + */ + SCA_API virtual const commonj::sdo::Property* findPropertyType(const std::string& propertyName); + + /** + * Return the SDO data factory which has the types of the properties defined + * in this component type. + * @return The data factory. + */ + SCA_API virtual commonj::sdo::DataFactoryPtr getPropertyDataFactory(); + + /** + * Initialize a component of this type. + * @param component The component to initialize. + */ + SCA_API virtual void initializeComponent(Component* component); + + private: + + /** + * The name of the component type + */ + std::string name; + + /** + * The composite containing this component type + */ + Composite* composite; + + /** + * Map of all the service types defined on this component. + */ + SERVICETYPE_MAP serviceTypes; + + /** + * Map of all the reference types defined on this component. + */ + REFERENCETYPE_MAP referenceTypes; + + /** + * SDO data factory which has all the types of the properties defined in + * this component type + */ + commonj::sdo::DataFactoryPtr propertyFactory; + + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_componenttype_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Composite.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/Composite.cpp new file mode 100644 index 0000000000..b855f8eb95 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Composite.cpp @@ -0,0 +1,324 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/CompositeReference.h" +#include "tuscany/sca/model/CompositeReferenceBinding.h" +#include "tuscany/sca/model/CompositeService.h" +#include "tuscany/sca/model/ServiceType.h" +#include "tuscany/sca/model/ReferenceType.h" +#include "tuscany/sca/model/Wire.h" +#include "tuscany/sca/model/WSDLDefinition.h" +#include "tuscany/sca/model/Service.h" +#include "tuscany/sca/model/Reference.h" +#include "tuscany/sca/model/ServiceBinding.h" +#include "tuscany/sca/model/ReferenceBinding.h" + +using namespace std; +using namespace tuscany::sca::util; + +namespace tuscany +{ + namespace sca + { + + namespace model + { + + // Constructor + Composite::Composite(const string& name, const string& root) + : ComponentType(0, name), root(root) + { + logentry(); + } + + // Destructor + Composite::~Composite() + { + logentry(); + } + + void Composite::addComponent(Component* component) + { + logentry(); + components[component->getName()] = component; + } + + void Composite::addCompositeReference(CompositeReference* compositeReference) + { + logentry(); + components[compositeReference->getName()] = compositeReference; + + // Create a reference type describing the composite reference + ServiceType* serviceType = compositeReference->getType()->findServiceType(""); + ReferenceType* referenceType = new ReferenceType( + this, compositeReference->getName(), + serviceType->getInterface(), + serviceType->getCallbackInterface(), + compositeReference->getMultiplicity()); + addReferenceType(referenceType); + } + + void Composite::addCompositeService(CompositeService* compositeService) + { + logentry(); + components[compositeService->getName()] = compositeService; + + // Create a service type describing the composite service + ReferenceType* referenceType = compositeService->getType()->findReferenceType(""); + ServiceType* serviceType = new ServiceType( + this, compositeService->getName(), + referenceType->getInterface(), + referenceType->getCallbackInterface()); + addServiceType(serviceType); + } + + void Composite::initializeComponent(Component* component) + { + ComponentType::initializeComponent(component); + + // Create bindings for all the services + const Component::SERVICE_MAP& services = component->getServices(); + Component::SERVICE_MAP::const_iterator iter = services.begin(); + for (unsigned int i=0; i< services.size(); i++) + { + Service *service = iter->second; + //CompositeServiceBinding* binding = new CompositeServiceBinding(service); + //service->setBinding(binding); + iter++; + } + + // Create bindings for all the references + const Component::REFERENCE_MAP& references = component->getReferences(); + Component::REFERENCE_MAP::const_iterator refiter = references.begin(); + for (unsigned int ri=0; ri< references.size(); ri++) + { + Reference *reference = refiter->second; + CompositeReferenceBinding* binding = new CompositeReferenceBinding(reference); + reference->setBinding(binding); + refiter++; + } + } + + Component* Composite::findComponent(const std::string& name) + { + logentry(); + Component* component = components[name]; + return component; + } + + std::list<std::string> Composite::getComponents() const + { + logentry(); + + std::list<std::string> componentList; + COMPONENT_MAP::const_iterator iter = components.begin(); + COMPONENT_MAP::const_iterator iterEnd = components.end(); + + for( ; iter != iterEnd; ++iter ) + { + componentList.push_back( iter->first ); + } + + return componentList; + } + + Service* Composite::findComponentService(const std::string& name) + { + logentry(); + + Service* service = 0; + + string componentName; + string serviceName; + Utils::tokeniseUri(name, componentName, serviceName); + + // Locate the component + Component* component = findComponent(componentName); + if (component) + { + // Locate the service + service = component->findService(serviceName); + } + return service; + } + + void Composite::addWire(const std::string& source, const std::string& target) + { + logentry(); + Wire* wire=new Wire(source, target); + wires.push_back(wire); + } + + void Composite::addInclude(Composite* composite) + { + logentry(); + includes[composite->getName()] = composite; + + for (COMPONENT_MAP::iterator iter = composite->components.begin(); + iter != composite->components.end(); + iter++) + { + components[iter->first] = iter->second; + } + } + + void Composite::resolveWires() + { + logentry(); + + for (WIRES::iterator iter = wires.begin(); + iter != wires.end(); + iter++) + { + Wire* wire = *iter; + + // Find the source component and reference + Component* component = findComponent(wire->getSourceComponent()); + Reference* reference; + if (component) + { + reference = component->findReference(wire->getSourceReference()); + if (!reference) + { + logerror("Wire source reference %s not found", wire->getSourceReference().c_str()); + } + } + else + { + reference = NULL; + logerror("Wire source %s not found", wire->getSourceComponent().c_str()); + } + + // Configure the reference binding with the wire target URI + if (reference) + { + reference->getBinding()->configure(wire->getTarget()); + } + } + } + + void Composite::addWSDLDefinition(commonj::sdo::DataObjectPtr wsdlModel) + { + logentry(); + WSDLDefinition* wsdlDefinition = new WSDLDefinition(wsdlModel); + + WSDLDefinition* existingDefinition = wsdlDefinitions[wsdlDefinition->getNamespace()]; + if (existingDefinition == NULL) + { + wsdlDefinitions[wsdlDefinition->getNamespace()] = wsdlDefinition; + } + else{ + existingDefinition->addWSDLModel(wsdlModel); + } + } + + WSDLDefinition* Composite::findWSDLDefinition(const std::string& wsdlNamespace ) + { + logentry(); + // remember the map operator[] inserts a blank object if key not found + WSDL_MAP::iterator iter = wsdlDefinitions.find( wsdlNamespace ); + if( iter == wsdlDefinitions.end() ) + { + return NULL; + } + + return iter->second; + } + + std::list<std::string> Composite::getWSDLNamespaces() const + { + logentry(); + std::list<std::string> namespaceList; + WSDL_MAP::const_iterator iter = wsdlDefinitions.begin(); + WSDL_MAP::const_iterator iterEnd = wsdlDefinitions.end(); + + for( ; iter != iterEnd; ++iter ) + { + namespaceList.push_back( iter->first ); + } + + return namespaceList; + } + + Composite* Composite::findIncludedComposite(const std::string& compositeName) + { + logentry(); + + // remember the map operator[] inserts a blank object if key not found + INCLUDES::iterator iter = includes.find( compositeName ); + if( iter == includes.end() ) + { + return NULL; + } + + return iter->second; + } + + std::list<std::string> Composite::getIncludedComposites() const + { + logentry(); + std::list<std::string> compositeList; + INCLUDES::const_iterator iter = includes.begin(); + INCLUDES::const_iterator iterEnd = includes.end(); + + for( ; iter != iterEnd; ++iter ) + { + compositeList.push_back( iter->first ); + } + + return compositeList; + } + + commonj::sdo::XSDHelperPtr Composite::getXSDHelper() + { + logentry(); + if (xsdHelper == 0) + { + xsdHelper = commonj::sdo::HelperProvider::getXSDHelper(); + } + + return xsdHelper; + } + + commonj::sdo::XMLHelperPtr Composite::getXMLHelper() + { + logentry(); + if (xmlHelper == 0) + { + xmlHelper = commonj::sdo::HelperProvider::getXMLHelper(getXSDHelper()->getDataFactory()); + } + + return xmlHelper; + } + + commonj::sdo::DataFactoryPtr Composite::getDataFactory() + { + logentry(); + return getXSDHelper()->getDataFactory(); + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Composite.h b/cpp/sca/runtime/core/src/tuscany/sca/model/Composite.h new file mode 100644 index 0000000000..a50216c1cb --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Composite.h @@ -0,0 +1,240 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_composite_h +#define tuscany_sca_model_composite_h + +#include <string> +#include <map> +#include <vector> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" +#include "tuscany/sca/model/ComponentType.h" + + +namespace tuscany +{ + namespace sca + { + + namespace model + { + class Component; + class ComponentType; + class CompositeReference; + class CompositeService; + class Service; + class WSDLDefinition; + class Wire; + + /** + * Represents a composite. + * A composite is used to assemble SCA elements in logical groupings. + * It is the basic unit of composition within an SCA System. An SCA composite contains a + * set of components, services, references and the wires that interconnect them, plus a set + * of properties which can be used to configure components. + */ + class Composite : public ComponentType + { + public: + + /** + * Constructor. + * @param name the name of the composite. + * @param root the root of the composite in the file system. + */ + SCA_API Composite(const std::string& name, const std::string& root); + + /** + * Destructor. + */ + SCA_API virtual ~Composite(); + + /** + * Returns the root directory of the composite. + * @return The root of the composite in the file system. + */ + SCA_API const std::string& getRoot() const { return root; } + + /** + * Add a new component to the composite. + * @param component The component to add. + */ + SCA_API void addComponent(Component* component); + + /** + * Add a new composite reference to the composite. + * @param compositeReference The composite reference to add. + */ + SCA_API void addCompositeReference(CompositeReference* compositeReference); + + /** + * Add a new composite service to the composite. + * @param compositeService The composite service to add. + */ + SCA_API void addCompositeService(CompositeService* compositeService); + + /** + * Add/include a composite in this composite. + * @param composite The composite included in this composite. + */ + SCA_API void addInclude(Composite* composite); + + /** + * Add a wire to the model. + * @param source The source location. Either the source component and + * reference (optional), or an entry point. + * @param target The target location. Either the target component and + * service (optional), or an external service. + */ + SCA_API void addWire(const std::string& source, const std::string& target); + + /** + * Find a component by name. + * @param componentName The name of the component to be found. + * @return The component that was found, or 0 if not found. + */ + SCA_API Component* findComponent(const std::string& componentName); + + /** + * Get all included components + * @return A list of Component name strings + */ + SCA_API std::list<std::string> getComponents() const; + + /** + * Find a component and service by name. + * @param componentServiceName A string of the form + * "componentName"/"serviceName" where the service name is optional + * if there is only one service on the component. + * @return The Service that was found, or 0 if not found. + */ + SCA_API Service* findComponentService(const std::string& componentServiceName); + + /** + * Add a WSDL definition to the composite. + * @param wsdlModel A data object holding all the information about + * the WSDL definition from a WSDL file. + */ + SCA_API void addWSDLDefinition(commonj::sdo::DataObjectPtr wsdlModel); + + /** + * Find a WSDL definition by target namespace. + * @param wsdlNamespace The namespace of the WSDL definitions to find. + */ + SCA_API WSDLDefinition* findWSDLDefinition(const std::string& wsdlNamespace); + + /** + * Get all loaded WSDL namespaces + * @return A list of strings, each of which is a different project namespace + */ + SCA_API std::list<std::string> getWSDLNamespaces() const; + + /** + * Find an Included Composite by its name + * @param compositeName The name of the included composite to find. + */ + SCA_API Composite* findIncludedComposite(const std::string& compositeName); + + /** + * Get all included composites + * @return A list of Composite name strings + */ + SCA_API std::list<std::string> getIncludedComposites() const; + + /** + * Initialize a component of this type. + * @param component The component to initialize. + */ + virtual void initializeComponent(Component* component); + + /** + * Return a cached SDO XSDHelper. + */ + SCA_API commonj::sdo::XSDHelperPtr getXSDHelper(void); + + /** + * Return a cached SDO XMLHelper. + */ + SCA_API commonj::sdo::XMLHelperPtr getXMLHelper(void); + + /** + * Return a data factory which has all the types defined in XSDs and + * WSDL files configured for this composite. + * @return The data factory for this composite. + */ + SCA_API commonj::sdo::DataFactoryPtr getDataFactory(void); + + /** + * Work through the list of wires and connect the source and target uris. + */ + SCA_API void resolveWires(); + + private: + + /** + * Directory of the root of the composite. + */ + std::string root; + + /** + * Cached XSDHelper. + */ + commonj::sdo::XSDHelperPtr xsdHelper; + + /** + * Cached XMLHelper. + */ + commonj::sdo::XMLHelperPtr xmlHelper; + + /** + * Map (by name) of all the components in this composite. + */ + typedef std::map<std::string, Component*> COMPONENT_MAP; + COMPONENT_MAP components; + + /** + * Map of all the composites included by this composite. + * Map the Composite name to the Composite + */ + typedef std::map<std::string, Composite*> INCLUDES; + INCLUDES includes; + + /** + * Vector of all the wires in this composite. + */ + typedef std::vector<Wire*> WIRES; + WIRES wires; + + /** + * Map by namespace of all the wsdl definitions in this composite. + */ + typedef std::map<std::string, WSDLDefinition*> WSDL_MAP; + WSDL_MAP wsdlDefinitions; + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_composite_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReference.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReference.cpp new file mode 100644 index 0000000000..be0bf71a17 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReference.cpp @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/model/CompositeReference.h" +#include "tuscany/sca/model/ReferenceType.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/ComponentType.h" +#include "tuscany/sca/model/ServiceType.h" +#include "tuscany/sca/model/Service.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + + namespace model + { + + // Constructor + CompositeReference::CompositeReference(Composite* composite, const string& name, + Interface* intface, Interface* callbackInterface, bool conversational, ReferenceType::Multiplicity multiplicity) : + Component(composite, name, new ComponentType(composite, name)) + { + logentry(); + loginfo("Composite reference name: %s", name.c_str()); + + // Initialize the component type, service type and service + ComponentType* componentType = getType(); + ServiceType* serviceType = new ServiceType( + componentType, "", intface, callbackInterface); + componentType->addServiceType(serviceType); + + service = new Service(this, serviceType); + addService(service); + } + + // Destructor + CompositeReference::~CompositeReference() + { + logentry(); + } + + } // End namespace model + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReference.h b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReference.h new file mode 100644 index 0000000000..f5e2166e06 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReference.h @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_compositereferencetype_h +#define tuscany_sca_model_compositereferencetype_h + +#include <string> + +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/ReferenceType.h" + +namespace tuscany +{ + namespace sca + { + + namespace model + { + class ComponentType; + class Composite; + class Interface; + class Service; + + /** + * Represents a reference in a composite. + * Composite references represent dependencies that the composite has on services provided elsewhere, + * outside the composite. + */ + class CompositeReference : public Component + { + public: + + /** + * Constructor. + * @param name The name of the reference. + */ + SCA_API CompositeReference(Composite* composite, const std::string& name, + Interface* intface, Interface* callbackInterface, bool conversational, ReferenceType::Multiplicity multiplicity); + + /** + * Destructor. + */ + SCA_API virtual ~CompositeReference(); + + /** + * Returns the service exposed by this composite reference. + * @return The service exposed by this composite reference. + */ + SCA_API Service* getService() const { return service; }; + + /** + * Returns the multiplicity of this composite reference + * @return The multiplicity of the composite reference + */ + SCA_API ReferenceType::Multiplicity getMultiplicity() { return multiplicity; } + + private: + + /** + * The service exposed by this composite reference. + */ + Service* service; + + /** + * The multiplicity of this reference + */ + ReferenceType::Multiplicity multiplicity; + + }; + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_compositereferencetype_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReferenceBinding.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReferenceBinding.cpp new file mode 100644 index 0000000000..24191285c5 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReferenceBinding.cpp @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/model/CompositeReferenceBinding.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + namespace model + { + + // Constructor + CompositeReferenceBinding::CompositeReferenceBinding(Reference* reference) + : ReferenceBinding(reference, ""), uri("") + { + } + + // Destructor + CompositeReferenceBinding::~CompositeReferenceBinding() + { + } + + void CompositeReferenceBinding::configure(ServiceBinding *binding) + { + setTargetServiceBinding(binding); + } + + ServiceProxy* CompositeReferenceBinding::getServiceProxy() + { + return serviceProxy; + } + + void CompositeReferenceBinding::configure(const string& uri) + { + this->uri = uri; + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReferenceBinding.h b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReferenceBinding.h new file mode 100644 index 0000000000..5c64c3b657 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeReferenceBinding.h @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_compositereferencebinding_h +#define tuscany_sca_model_compositereferencebinding_h + +#include <string> + +#include "tuscany/sca/model/ReferenceBinding.h" + +namespace tuscany +{ + namespace sca + { + namespace model + { + /** + * A default composite reference binding + */ + class CompositeReferenceBinding : public ReferenceBinding + { + public: + + /** + * Constructor. + * @param uri The uri of the binding. + */ + SCA_API CompositeReferenceBinding(Reference* reference); + + /** + * Destructor. + */ + SCA_API virtual ~CompositeReferenceBinding(); + + /** + * Returns the type of binding. + */ + virtual std::string getType() { return "http://www.osoa.org/xmlns/sca/1.0#CompositeServiceBinding"; }; + + /** + * Configure this binding from a service binding. + */ + SCA_API virtual void configure(ServiceBinding* serviceBinding); + + /** + * Configure this binding from a URI. + */ + SCA_API virtual void configure(const std::string& uri); + + /** + * Create a proxy representing the reference to the + * client component. + */ + SCA_API virtual ServiceProxy* getServiceProxy(); + + /** + * Returns the binding URI. + * @return The binding URI. + */ + SCA_API virtual const std::string& getURI() const { return uri; }; + + private: + + /** + * The proxy representing the reference to the client + * component. + */ + ServiceProxy* serviceProxy; + + /** + * The binding URI + */ + std::string uri; + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_compositereferencebinding_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeService.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeService.cpp new file mode 100644 index 0000000000..e06dc548c1 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeService.cpp @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/model/CompositeService.h" +#include "tuscany/sca/model/ServiceType.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/ComponentType.h" +#include "tuscany/sca/model/Reference.h" +#include "tuscany/sca/model/Service.h" +#include "tuscany/sca/model/ReferenceType.h" +#include "tuscany/sca/model/ServiceType.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + + namespace model + { + + // Constructor + CompositeService::CompositeService(Composite* composite, const string& name, + Interface* intface, Interface* callbackInterface, bool conversational, + ReferenceType::Multiplicity multiplicity) : + Component(composite, name, new ComponentType(composite, name)) + { + logentry(); + loginfo("Composite service name: %s", name.c_str()); + + // Initialize the component type, reference type and reference + ComponentType* componentType = getType(); + ReferenceType* referenceType = new ReferenceType( + componentType, "", intface, callbackInterface, multiplicity); + componentType->addReferenceType(referenceType); + + reference = new Reference(this, referenceType); + addReference(reference); + } + + // Destructor + CompositeService::~CompositeService() + { + logentry(); + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeService.h b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeService.h new file mode 100644 index 0000000000..8c626ccac3 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/CompositeService.h @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_compositeservicetype_h +#define tuscany_sca_model_compositeservicetype_h + +#include <string> + +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/ReferenceType.h" + +namespace tuscany +{ + namespace sca + { + namespace model + { + class Composite; + class Component; + class Interface; + class Reference; + + /** + * Represents a service in a composite. + * Composite services define the public services provided by the composite, which can be + * accessed from outside the composite. + */ + class CompositeService : public Component + { + + public: + + /** + * Constructor. + * @param componentType The component type on which this service is defined. + * @param name The name of the service. + */ + SCA_API CompositeService(Composite* composite, const std::string& name, + Interface* intface, Interface* callbackInterface, bool conversational, ReferenceType::Multiplicity multiplicity); + + /** + * Destructor. + */ + SCA_API virtual ~CompositeService(); + + /** + * Returns the reference used by this composite service. + * @return The reference used by this composite service. + */ + SCA_API Reference* getReference() const { return reference; }; + + private: + + /** + * The reference used by this composite service. + */ + Reference* reference; + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_compositeservicetype_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Contract.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/Contract.cpp new file mode 100644 index 0000000000..99a1d04dc2 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Contract.cpp @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/model/Contract.h" + + +namespace tuscany +{ + namespace sca + { + + namespace model + { + + // Constructor + Contract::Contract(Interface *intface, Interface* callbackInterface) : + iface(intface), callbackInterface(callbackInterface) + { + logentry(); + } + + // Destructor + Contract::~Contract() + { + logentry(); + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Contract.h b/cpp/sca/runtime/core/src/tuscany/sca/model/Contract.h new file mode 100644 index 0000000000..e1d526f842 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Contract.h @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_contract_h +#define tuscany_sca_model_contract_h + +#include "tuscany/sca/model/Interface.h" + +namespace tuscany +{ + namespace sca + { + namespace model + { + /** + * Interface contracts define one or more business functions. These business functions are + * provided by services and are used by references. Services are defined by the interface which + * they implement. + * This interface will typically be extended to support concrete interface type systems, such as + * CPP classes, Java interfaces, WSDL 1.1 portTypes and WSDL 2.0 interfaces. + */ + class Contract + { + public: + + /** + * Constructor. + */ + SCA_API Contract(Interface* intface, Interface* callbackInterface); + + /** + * Destructor. + */ + SCA_API virtual ~Contract(); + + /** + * Returns the interface for invocations from the requestor to the provider. + * @return The interface for invocations from the requestor to the provider. + */ + SCA_API Interface* getInterface() const { return iface; } + + /** + * Returns the interface for invocations from the provider back to the requestor. + * @return The interface for invocations from the provider back to the requestor. + */ + SCA_API Interface* getCallbackInterface() const { return callbackInterface; } + + private: + + /** + * The interface for invocations from the requestor to the provider. + */ + Interface* iface; + + /** + * The interface for invocations from the provider back to the requestor. + */ + Interface* callbackInterface; + + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_contract_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Interface.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/Interface.cpp new file mode 100644 index 0000000000..f8dd02a74a --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Interface.cpp @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/model/Interface.h" + + +namespace tuscany +{ + namespace sca + { + + namespace model + { + + // Constructor + Interface::Interface(bool remotable, bool conversational) + : remotable(remotable), conversational(conversational) + { + logentry(); + } + + Interface::~Interface() + { + logentry(); + } + + } // End namespace model + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Interface.h b/cpp/sca/runtime/core/src/tuscany/sca/model/Interface.h new file mode 100644 index 0000000000..5ac7736d43 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Interface.h @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_interface_h +#define tuscany_sca_model_interface_h + +#include <string> + +namespace tuscany +{ + namespace sca + { + namespace model + { + + /** + * Information about an interface. Subtypes will hold information + * specific to a type of interface. + */ + class Interface + { + + public: + + /** + * Constructor + */ + SCA_API Interface(bool remotable, bool conversational); + + /** + * Destructor + */ + SCA_API virtual ~Interface(); + + /** + * Returns true if the interface is remotable. + * @return True if the interface is remotable. + */ + SCA_API bool isRemotable() const { return remotable; }; + + /** + * Returns true if the interface is conversational. + * @return True if the interface is conversational. + */ + SCA_API bool isConversational() const { return conversational; }; + + /** + * return the QName of schema type for this interface type + * (e.g. "http://www.osoa.org/xmlns/sca/1.0#WSDLInterface") + */ + SCA_API virtual const std::string& getInterfaceTypeQName() = 0; + + private: + + /** + * True if the interface is remotable + */ + bool remotable; + + /** + * True if the interface is conversational + */ + bool conversational; + + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_interface_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ModelLoader.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/ModelLoader.cpp new file mode 100644 index 0000000000..b8aad705e2 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ModelLoader.cpp @@ -0,0 +1,935 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/export.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/model/ModelLoader.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/Reference.h" +#include "tuscany/sca/model/Service.h" +#include "tuscany/sca/model/CompositeService.h" +#include "tuscany/sca/model/CompositeReference.h" +#include "tuscany/sca/model/ServiceType.h" +#include "tuscany/sca/model/ReferenceType.h" +#include "tuscany/sca/model/WSDLInterface.h" +#include "tuscany/sca/core/SCARuntime.h" +#include "tuscany/sca/util/File.h" + + +using namespace std; +using namespace commonj::sdo; +using namespace tuscany::sca::util; + + +#if defined(WIN32) || defined (_WINDOWS) +#define PATH_SEPARATOR ";" +#else +#define PATH_SEPARATOR ":" +#endif + +namespace tuscany +{ + namespace sca + { + + namespace model + { + // =========== + // Constructor + // =========== + ModelLoader::ModelLoader(SCARuntime* runtime, Composite* system) + : system(system), runtime(runtime) + { + logentry(); + } + + // ========== + // Destructor + // ========== + ModelLoader::~ModelLoader() + { + logentry(); + } + + // ========================================================= + // load: Load the runtime model from the deployed xml files + // This class has the responsibility for translating from + // the SCDL files to the SCA runtime's in memory model. + // ========================================================= + void ModelLoader::load(const string& systemRoot, const string& systemPath) + { + logentry(); + loginfo("System root: %s", systemRoot.c_str()); + loginfo("System path: %s", systemPath.c_str()); + + // Load composite implementations + // Composite implementations can occur anywhere on the given search path + if (systemPath != "") + { + loadComposites(systemRoot + PATH_SEPARATOR + systemPath); + } + else + { + loadComposites(systemRoot); + } + + // Load system composites + // Composites on the the system root path get included + // in the System composite + loadSystem(systemRoot); + + // Resolve the wires in the system composite + system->resolveWires(); + } + + // ======================================================================== + // loadSystem: + // Load all the composite files on the system root path + // Translate the composite information to composite model objects + // ======================================================================== + void ModelLoader::loadSystem(const string& systemRoot) + { + logentry(); + + // Get all the composite files on the system root path + // These composites are included in the system composite + for (string path = systemRoot; path != ""; ) + { + string dir; + Utils::tokeniseString(PATH_SEPARATOR, path, dir, path); + if (dir != "") + { + loginfo("System root directory: %s", dir.c_str()); + Files files(dir, "*.composite", false); + for (unsigned int i=0; i < files.size(); i++) + { + string fileName = files[i].getDirectory() + "/" + files[i].getFileName(); + Composite* composite = compositeFiles[fileName]; + if (composite) + { + // Include the composite in the system composite + system->addInclude(composite); + } + else + { + // We already got an error or warning indicating why the file + // didn't turn into a composite + } + } + } + } + } + + // ===================================================================== + // loadComposites: + // Load all the composites from any directory under the composite search path + // Translate the composite information to composite model objects + // ===================================================================== + void ModelLoader::loadComposites(const string& searchPath) + { + logentry(); + + // Get all the composite files on the composite search path + for (string path = searchPath; path != ""; ) + { + string dir; + Utils::tokeniseString(PATH_SEPARATOR, path, dir, path); + if (dir != "") + { + loginfo("Composite path directory: %s", dir.c_str()); + Files files(dir, "*.composite", true); + for (unsigned int i=0; i < files.size(); i++) + { + loadCompositeFile(files[i]); + } + } + } + + // Complete the mapping of the composites + for (COMPOSITE_DATAOBJECTS::iterator iter = compositeDataObjects.begin(); + iter != compositeDataObjects.end(); + iter++) + { + mapCompositePass2(iter->first, iter->second); + } + } + + // ==================================================================== + // loadCompositeFile: + // This method is called for each .composite file found under the composite search + // path. The location of this composite file will indicate the root of a composite. + // ==================================================================== + Composite* ModelLoader::loadCompositeFile(const File& file) + { + logentry(); + + loginfo("Composite file name: %s", file.getFileName().c_str()); + + Composite* composite = NULL; + try + { + string fileName = file.getDirectory() + "/" + file.getFileName(); + if (compositeFiles[fileName] == NULL) + { + XMLDocumentPtr compositeFile = getXMLHelper()->loadFile(fileName.c_str()); + if (compositeFile->getRootDataObject() == NULL) + { + logerror("Unable to load file: %s", fileName.c_str()); + } + else + { + // Map the SCDL + composite = mapCompositePass1(file, compositeFile->getRootDataObject()); + + // Load the xsd types and wsdl files in the composite + loadTypeMetadata(file.getDirectory(), composite); + } + } + + } catch (SDORuntimeException& ex) + { + logerror("Unable to load composite: %s", ex.getMessageText()); + } + return composite; + } + + // =========== + // mapCompositePass1 + // =========== + Composite* ModelLoader::mapCompositePass1(const File& file, DataObjectPtr root) + { + logentry(); + + const string& compositeRootDir = file.getDirectory(); + const string compositeName = root->getCString("name"); + loginfo("Composite: %s, root directory: %s", compositeName.c_str(), compositeRootDir.c_str()); + + Composite* composite = new Composite(compositeName, compositeRootDir); + compositeModels[compositeName] = composite; + compositeDataObjects[compositeName] = root; + compositeFiles[file.getDirectory() + "/" + file.getFileName()] = composite; + + // ------------ + // Composite services + // ------------ + DataObjectList& compositeServiceList = root->getList("service"); + for (unsigned int i = 0; i < compositeServiceList.size(); i++) + { + addCompositeService(composite, compositeServiceList[i]); + } + + // ----------------- + // Composite references + // ----------------- + DataObjectList& compositeReferenceList = root->getList("reference"); + for (unsigned int cri = 0; cri < compositeReferenceList.size(); cri++) + { + addCompositeReference(composite, compositeReferenceList[cri]); + } + + // ----- + // Wires + // ----- + DataObjectList& wireList = root->getList("wire"); + for (unsigned int l = 0; l < wireList.size(); l++) + { + string source = wireList[l]->getCString("source"); + string target = wireList[l]->getCString("target"); + composite->addWire(source, target); + } + + return composite; + } + + // =========== + // mapCompositePass2 + // =========== + Composite* ModelLoader::mapCompositePass2(const string& compositeName, DataObjectPtr root) + { + logentry(); + loginfo("Composite: %s", compositeName.c_str()); + + Composite* composite = compositeModels[compositeName]; + + // ---------------------------- + // Add components to the composite + // ---------------------------- + DataObjectList& componentList = root->getList("component"); + unsigned int i; + for (i=0; i < componentList.size(); i++) + { + addComponent(composite, componentList[i]); + } + + // Resolve all the wires inside the composite + composite->resolveWires(); + + return composite; + } + + // ================================= + // addComponent: + // ================================= + void ModelLoader::addComponent(Composite* composite, DataObjectPtr componentDO) + { + logentry(); + + // ------------------- + // Get the component implementation + // ------------------- + DataObjectPtr impl = componentDO->getDataObject("implementation"); + if (!impl) + { + string message = "No implementation for component: "; + message = message + componentDO->getCString("name"); + throwException(SystemConfigurationException, message.c_str()); + } + + // Create the component type + ComponentType* componentType; + string componentTypeName; + string componentTypePath; + + string implTypeQname = impl->getType().getURI(); + implTypeQname += "#"; + implTypeQname += impl->getType().getName(); + + if (implTypeQname == "http://www.osoa.org/xmlns/sca/1.0#SCAImplementation") + { + // Handle a composite implementation + Composite* composite = compositeModels[impl->getCString("name")]; + if (!composite) + { + string message = "Composite not found: "; + message = message + impl->getCString("name"); + throwException(SystemConfigurationException, message.c_str()); + } + componentType = composite; + } + else + { + + // Locate extension that handles this implementation type + ImplementationExtension* implExtension = runtime->getImplementationExtension(implTypeQname); + if (implExtension) + { + componentType = implExtension->getImplementation(composite, impl); + + // ----------------------- + // Load the .componentType + // ----------------------- + string typeFileName = composite->getRoot() + "/" + componentType->getName() + ".componentType"; + + // Check that the component type file exists + //TODO We need a better and portable way to do this + string dirName; + string fileName; + Utils::rTokeniseString("/", typeFileName, dirName, fileName); + Files files(dirName, fileName, false); + if (files.size() !=0) + { + try + { + XMLDocumentPtr componentTypeFile = getXMLHelper()->loadFile(typeFileName.c_str()); + if (!componentTypeFile || componentTypeFile->getRootDataObject() == 0) + { + // Component type files are optional + logerror("Unable to load file: %s", typeFileName.c_str()); + } + else + { + //Utils::printDO(componentTypeFile->getRootDataObject()); + //commonj::sdo::SDOUtils::printDataObject(componentTypeFile->getRootDataObject()); + addServiceTypes(composite, componentType, componentTypeFile->getRootDataObject()); + addReferenceTypes(composite, componentType, componentTypeFile->getRootDataObject()); + addPropertyTypes(componentType, componentTypeFile->getRootDataObject()); + } + } catch (SDORuntimeException& ex) + { + logerror("Unable to load file:%s", typeFileName.c_str()); + throwException(SystemConfigurationException, ex); + } + } + } + else + { + logerror("Unsupported implementation type: %s", implTypeQname.c_str()); + + string message = "Implementation type not supported: "; + message = message + implTypeQname; + throwException(SystemConfigurationException, message.c_str()); + } + } + + // First check that references and properties exist, some component types + // will create all used references & properties automatically + DataObjectList& refs = componentDO->getList("reference"); + for (unsigned int i=0; i<refs.size(); i++) + { + string refName = refs[i]->getCString("name"); + if (!componentType->findReferenceType(refName)) + { + // Configuration error: reference is not defined + string message = "Undefined reference: " + refName; + throwException(SystemConfigurationException, message.c_str()); + } + } + + DataObjectList& props = componentDO->getList("property"); + for (unsigned int pi=0; pi<props.size(); pi++) + { + string propName = props[pi]->getCString("name"); + if (!componentType->findPropertyType(propName)) + { + // Configuration error: property is not defined + string message = "Undefined property: " + propName; + throwException(SystemConfigurationException, message.c_str()); + } + } + + + // Create the component + Component* component = new Component(composite, componentDO->getCString("name"), componentType); + composite->addComponent(component); + + // ---------- + // Properties + // ---------- + for (unsigned int pi=0; pi<props.size(); pi++) + { + string propName = props[pi]->getCString("name"); + DataObjectPtr propValue = props[pi]->getDataObject("value"); + + component->setProperty(propName, propValue); + } + + // ---------- + // References + // ---------- + for (unsigned int ri=0; ri<refs.size(); ri++) + { + // ---------------------------------------------------------- + // Add the reference to the composite wires to be resolved later + // ---------------------------------------------------------- + string refName = refs[ri]->getCString("name"); + if (!component->findReference(refName)) + { + // Configuration error: reference is not defined + string message = "Undefined reference: " + refName; + throwException(SystemConfigurationException, message.c_str()); + } + + string src = component->getName() + "/" + refName; + + // Get the reference value + string refValue = refs[ri]->getCString("value"); + + composite->addWire(src, refValue); + } + } + + // ===================================================================== + // addServiceTypes: add the services to the component type + // ===================================================================== + void ModelLoader::addServiceTypes(Composite* composite, ComponentType* componentType, DataObjectPtr componentTypeDO) + { + logentry(); + + DataObjectList& serviceTypes = componentTypeDO->getList("service"); + for (unsigned int i=0; i<serviceTypes.size(); i++) + { + Interface* iface = getInterface(composite, serviceTypes[i]); + ServiceType* serviceType = new ServiceType( + componentType, serviceTypes[i]->getCString("name"), iface, NULL); + componentType->addServiceType(serviceType); + } + } + + // =================================================== + // addReferenceTypes: add the references to the component type + // =================================================== + void ModelLoader::addReferenceTypes(Composite* composite, ComponentType* componentType, DataObjectPtr componentTypeDO) + { + logentry(); + + DataObjectList& refs = componentTypeDO->getList("reference"); + for (unsigned int i=0; i<refs.size(); i++) + { + ReferenceType::Multiplicity multiplicity; + if (refs[i]->isSet("multiplicity")) + { + string s = refs[i]->getCString("multiplicity"); + multiplicity = ReferenceType::getMultiplicityFromString(s); + } + else + { + multiplicity = ReferenceType::ONE_ONE; + } + + Interface* iface = getInterface(composite, refs[i]); + + ReferenceType* referenceType = new ReferenceType( + componentType, refs[i]->getCString("name"), iface, NULL, multiplicity); + componentType->addReferenceType(referenceType); + + } + } + + + // ============== + // getInterface + // ============== + Interface* ModelLoader::getInterface(Composite* composite, DataObjectPtr obj) + { + logentry(); + + // ----------------- + // get the interface + // ----------------- + DataObjectPtr iface = obj->getDataObject("interface"); + if (!iface) + { + string message = "No interface for: "; + message = message + obj->getCString("name"); + throwException(SystemConfigurationException, message.c_str()); + } + + string typeQname = iface->getType().getURI(); + typeQname += "#"; + typeQname += iface->getType().getName(); + + if (typeQname == WSDLInterface::typeQName) + { + // Load a WSDL interface + string qname = iface->getCString("interface"); + + return new WSDLInterface(qname, true, false); + } + else + { + // Locate extension that handles this interface type + InterfaceExtension* ifaceExtension = runtime->getInterfaceExtension(typeQname); + if (ifaceExtension) + { + return ifaceExtension->getInterface(composite, iface); + } + else + { + // log this for now. + logerror("Unsupported interface type: %s", typeQname.c_str()); + return 0; + } + } + + } + + // ============================================== + // addProperties: add properties to the component type + // ============================================== + void ModelLoader::addPropertyTypes(ComponentType* componentType, DataObjectPtr componentTypeDO) + { + logentry(); + + DataObjectList& props = componentTypeDO->getList("property"); + for (unsigned int i=0; i<props.size(); i++) + { + //cout << "Property " << props[i]; + + string name = props[i]->getCString("name"); + string type = props[i]->getCString("type"); + bool many=false; + if (props[i]->isSet("many")) + { + many = props[i]->getBoolean("many"); + } + + //TODO need to add support for complex properties, need the SDO + // folks to help understand how to do this... + DataObjectPtr defaultValue = props[i]->getDataObject("value"); + + componentType->addPropertyType(name, type, many, &defaultValue); + } + } + + // =============================================== + // addCompositeService: add an CompositeService to the composite + // =============================================== + void ModelLoader::addCompositeService(Composite* composite, DataObjectPtr compositeServiceDO) + { + logentry(); + + string compositeServiceName = compositeServiceDO->getCString("name"); + + Interface* iface; + if (compositeServiceDO->getDataObject("interface")) + { + iface = getInterface(composite, compositeServiceDO); + } + else + { + iface = NULL; + } + + ReferenceType::Multiplicity multiplicity; + if (compositeServiceDO->isSet("multiplicity")) + { + string s = compositeServiceDO->getCString("multiplicity"); + multiplicity = ReferenceType::getMultiplicityFromString(s); + } + else + { + multiplicity = ReferenceType::ONE_ONE; + } + + CompositeService* compositeService = new CompositeService( + composite, compositeServiceName, iface, NULL, false, multiplicity); + + composite->addCompositeService(compositeService); + + DataObjectList& refs = compositeServiceDO->getList("reference"); + for (unsigned int i=0; i<refs.size(); i++) + { + // ---------------------------------------------------------- + // Add the reference to the composite wires to be resolved later + // ---------------------------------------------------------- + string targ = refs.getCString(i); + composite->addWire(compositeServiceName, targ); + } + + // Get binding, it will be the first and only binding + DataObjectList& bindings = compositeServiceDO->getList("binding"); + if (bindings.size()==0) + { + string message = "No binding for compositeService: "; + message = message + compositeServiceDO->getCString("name"); + throwException(SystemConfigurationException, message.c_str()); + } + DataObjectPtr binding = bindings[0]; + + // Determine the binding type + string bindingType = binding->getType().getName(); + string bindingTypeQname = binding->getType().getURI(); + bindingTypeQname += "#"; + bindingTypeQname += binding->getType().getName(); + + // Locate the extension that handles this binding type + ReferenceBindingExtension* bindingExtension = runtime->getReferenceBindingExtension(bindingTypeQname); + if (bindingExtension) + { + Reference* reference = compositeService->getReference(); + ReferenceBinding* referenceBinding = bindingExtension->getReferenceBinding(composite, reference, binding); + reference->setBinding(referenceBinding); + } + else + { + logerror("Unsupported binding type: %s", bindingTypeQname.c_str()); + + string message = "Binding type not supported: "; + message = message + bindingTypeQname; + throwException(SystemConfigurationException, message.c_str()); + } + } + + + // ========================================================= + // addCompositeReference: add a CompositeReference to the composite + // ========================================================= + void ModelLoader::addCompositeReference(Composite* composite, DataObjectPtr compositeReferenceDO) + { + logentry(); + + string compositeReferenceName = compositeReferenceDO->getCString("name"); + + Interface* iface; + if (compositeReferenceDO->getDataObject("interface")) + { + iface = getInterface(composite, compositeReferenceDO); + } + else + { + iface = NULL; + } + + CompositeReference* compositeReference = new CompositeReference( + composite, compositeReferenceName, iface, NULL, false, ReferenceType::ONE_ONE); + + composite->addCompositeReference(compositeReference); + + // Get binding, it will be the first and only binding + DataObjectList& bindings = compositeReferenceDO->getList("binding"); + if (bindings.size()==0) + { + string message = "No binding for compositeReference: "; + message = message + compositeReferenceDO->getCString("name"); + throwException(SystemConfigurationException, message.c_str()); + } + DataObjectPtr binding = bindings[0]; + + // Determine the binding type + string bindingType = binding->getType().getName(); + string bindingTypeQname = binding->getType().getURI(); + bindingTypeQname += "#"; + bindingTypeQname += binding->getType().getName(); + + // Locate the extension that handles this binding type + ServiceBindingExtension* bindingExtension = runtime->getServiceBindingExtension(bindingTypeQname); + if (bindingExtension) + { + Service *service = compositeReference->getService(); + ServiceBinding* serviceBinding = bindingExtension->getServiceBinding(composite, service, binding); + service->setBinding(serviceBinding); + } + else + { + logerror("Unsupported binding type: %s", bindingTypeQname.c_str()); + + string message = "Binding type not supported: "; + message = message + bindingTypeQname; + throwException(SystemConfigurationException, message.c_str()); + } + } + + + /// + /// Use the Tuscany.config file in the composite root directory to + /// determine which xsds and wsdls to load into a dataFactory. + /// + void ModelLoader::loadTypeMetadata(const string &compositeRootDir, Composite* composite) + { + logentry(); + + // Load the "Tuscany.config" file, if it exists + Files files(compositeRootDir, "Tuscany.config", false); + if (files.size() !=0) + { + for (unsigned int i=0; i < files.size(); i++) + { + string filename = compositeRootDir + "/" + files[i].getFileName(); + XMLDocumentPtr compositeConfigFile = getXMLHelper()->loadFile(filename.c_str()); + if (compositeConfigFile->getRootDataObject() == 0) + { + logerror("Unable to load file: %s", filename.c_str()); + } + else + { + loginfo("Composite config for: %s, root directory: %s", composite->getName().c_str(), compositeRootDir.c_str()); + + if(compositeConfigFile->getRootDataObject()->isSet("xsd")) + { + DataObjectList& xsds = compositeConfigFile->getRootDataObject()->getList("xsd/file"); + + for (unsigned int i=0; i<xsds.size(); i++) + { + if(xsds[i]->isSet("name")) + { + // Load a xsd file -> set the types in the compositeComponents data factory file + string xsdName = compositeRootDir + "/" +xsds[i]->getCString("name"); + loadXMLSchema(composite, xsdName.c_str()); + } + } + } + + + if( compositeConfigFile->getRootDataObject()->isSet("wsdl")) + { + DataObjectList& wsdls = compositeConfigFile->getRootDataObject()->getList("wsdl/file"); + for (unsigned int j=0; j<wsdls.size(); j++) + { + if(wsdls[i]->isSet("name")) + { + string wsdlName = compositeRootDir + "/" +wsdls[j]->getCString("name"); + // Load a wsdl file -> get the types, then the contents of the wsdl + loadXMLSchema(composite, wsdlName.c_str()); + + // Load the contents of the wsdl files + loadWSDLDefinition(composite, wsdlName.c_str()); + } + } + } + } + } + } + else + { + // The default scheme is to have no Tuscany.config file, then we simply load all + // WSDLs and XSDs that we find under the composite root + + Files xsdFiles(compositeRootDir, "*.xsd", true); + for (unsigned int i=0; i < xsdFiles.size(); i++) + { + // Load a xsd file -> set the types in the compositeComponents data factory file + string xsdName = xsdFiles[i].getDirectory() + "/" + xsdFiles[i].getFileName(); + loadXMLSchema(composite, xsdName.c_str()); + + } + + Files wsdlFiles(compositeRootDir, "*.wsdl", true); + for (unsigned int wi=0; wi < wsdlFiles.size(); wi++) + { + // Load a wsdl file -> get the types, then the contents of the wsdl + string wsdlName = wsdlFiles[wi].getDirectory() + "/" + wsdlFiles[wi].getFileName(); + loadXMLSchema(composite, wsdlName.c_str()); + + // Load the contents of the wsdl files + loadWSDLDefinition(composite, wsdlName.c_str()); + } + } + } + + + /// + /// Use the types from an xsd or wsdl file + /// + void ModelLoader::loadXMLSchema(Composite* composite, const char *fileName) + { + logentry(); + loginfo("Loading XML schema %s", fileName); + // Load a xsd file -> set the types in the data factory associated with + // the composite + try { + composite->getXSDHelper()->defineFile(fileName); + //Utils::printTypes(composite->getXSDHelper()->getDataFactory()); + + } catch (SDORuntimeException& ex) + { + logerror("Unable to load schema: %s", fileName); + throwException(SystemConfigurationException, ex); + } + } + + /// + /// Load the web services definition from a wsdl + /// + void ModelLoader::loadWSDLDefinition(Composite* composite, const char *fileName) + { + logentry(); + + try { + // Load the wsdl file + XMLDocumentPtr doc = getXMLHelper()->loadFile(fileName); + + if (doc!=0 && doc->getRootDataObject()!=0) + { + //Utils::printDO(doc->getRootDataObject()); + + // Add the root WSDL object to the composite + composite->addWSDLDefinition(doc->getRootDataObject()); + + } + else + { + logerror("Unable to load or parse WSDL: %s", fileName); + } + + } catch (SDORuntimeException& ex) + { + logerror("Unable to load or parse WSDL: %s", fileName); + throwException(SystemConfigurationException, ex); + } + } + + ////////////////////////////////////////////////////////////////////////////// + // Methods used to load the model into memory + ////////////////////////////////////////////////////////////////////////////// + + /// + /// Get an XSDHelper that has the appropriate XSDs already loaded + /// + const XSDHelperPtr ModelLoader::getXSDHelper() + { + logentry(); + + if (myXSDHelper == 0) + { + + // Create an xsd helper + myXSDHelper = HelperProvider::getXSDHelper(); + + try { + + // Load the Assembly model schema + string root = runtime->getInstallRoot(); + string filename = root + "/xsd/sca.xsd"; + + myXSDHelper->defineFile(filename.c_str()); + + // Tuscany specific xsd for config files + filename = root + "/xsd/tuscany.xsd"; + myXSDHelper->defineFile(filename.c_str()); + + filename = root + "/xsd/wsdl_11.xsd"; + myXSDHelper->defineFile(filename.c_str()); + + filename = root + "/xsd/wsdl_11_http.xsd"; + myXSDHelper->defineFile(filename.c_str()); + + filename = root + "/xsd/wsdl_11_mime.xsd"; + myXSDHelper->defineFile(filename.c_str()); + + filename = root + "/xsd/wsdl_11_soap.xsd"; + myXSDHelper->defineFile(filename.c_str()); + + filename = root + "/xsd/wsdl_11_soap12.xsd"; + myXSDHelper->defineFile(filename.c_str()); + + // Load any schema from the extensions directory + string extensionsRoot = root + "/extensions"; + + Files files(extensionsRoot, "*.xsd", true); + for (unsigned int i=0; i < files.size(); i++) + { + string extensionSchema = files[i].getDirectory() + "/" + files[i].getFileName(); + myXSDHelper->defineFile(extensionSchema.c_str()); + } + + + } catch (SDORuntimeException& ex) + { + throwException(SystemConfigurationException, ex); + } + } + + return myXSDHelper; + } + + /// + /// Get an XMLHelper to load files + /// + const XMLHelperPtr ModelLoader::getXMLHelper() + { + logentry(); + + if (myXMLHelper == 0) { + + // Create an xml helper + myXMLHelper = HelperProvider::getXMLHelper(getXSDHelper()->getDataFactory()); + + } + + return myXMLHelper; + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ModelLoader.h b/cpp/sca/runtime/core/src/tuscany/sca/model/ModelLoader.h new file mode 100644 index 0000000000..b3b3ec2df6 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ModelLoader.h @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_modelloader_h +#define tuscany_sca_model_modelloader_h + +#include <map> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" +#include "tuscany/sca/core/SCARuntime.h" +#include "tuscany/sca/model/Interface.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/ComponentType.h" +#include "tuscany/sca/util/File.h" + +namespace tuscany +{ + namespace sca + { + namespace model + { + + /** + * Provides methods to load the runtime model from the SCDL file. + */ + class ModelLoader { + + public: + /** + * Constructor. + * @param system The SCA system to load. + */ + ModelLoader(tuscany::sca::SCARuntime* runtime, Composite* system); + + /** + * Destructor. + */ + virtual ~ModelLoader(); + + /** + * Load the model from the configuration information. + * @param systemRoot The location of the system configuration + * @param systemPath The search path for composites + */ + void load(const std::string& systemRoot, const std::string& systemPath); + + private: + void loadSystem(const std::string& systemRoot); + + void loadComposites(const std::string& searchPath); + Composite* loadCompositeFile(const tuscany::sca::util::File& file); + Composite* mapCompositePass1(const tuscany::sca::util::File& file, commonj::sdo::DataObjectPtr rootDO); + Composite* mapCompositePass2(const std::string& compositeName, commonj::sdo::DataObjectPtr rootDO); + + void addComponent(Composite* composite, commonj::sdo::DataObjectPtr componentDO); + void addCompositeService(Composite* composite, commonj::sdo::DataObjectPtr compositeServiceDO); + void addCompositeReference(Composite* composite, commonj::sdo::DataObjectPtr referenceServiceDO); + + void addServiceTypes(Composite* composite, ComponentType* componentType, commonj::sdo::DataObjectPtr componentTypeDO); + void addReferenceTypes(Composite* composite, ComponentType* componentType, commonj::sdo::DataObjectPtr componentTypeDO); + void addPropertyTypes(ComponentType* componentType, commonj::sdo::DataObjectPtr componentTypeDO); + + void loadTypeMetadata(const std::string &compositeRootDir, Composite* composite); + + void loadXMLSchema(Composite* composite, const char *fileName); + void loadWSDLDefinition(Composite* composite, const char *fileName); + + Interface* getInterface(Composite* composite, commonj::sdo::DataObjectPtr obj); + + SCARuntime* runtime; + + commonj::sdo::XMLHelperPtr myXMLHelper; // Used to load scdl files + commonj::sdo::XSDHelperPtr myXSDHelper; // Used to load xsds + + const commonj::sdo::XSDHelperPtr getXSDHelper(void); + const commonj::sdo::XMLHelperPtr getXMLHelper(void); + + /** + * The composite describing the composition of the system + */ + Composite* system; + + /** + * Maps of all the composites installed on the system. + */ + typedef std::map<std::string, Composite*> COMPOSITE_MODELS; + COMPOSITE_MODELS compositeModels; + + typedef std::map<std::string, commonj::sdo::DataObjectPtr> COMPOSITE_DATAOBJECTS; + COMPOSITE_DATAOBJECTS compositeDataObjects; + + typedef std::map<std::string, Composite*> COMPOSITE_FILES; + COMPOSITE_FILES compositeFiles; + + }; + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_modelloader_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Reference.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/Reference.cpp new file mode 100644 index 0000000000..8820379c4e --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Reference.cpp @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/model/Reference.h" +#include "tuscany/sca/model/ReferenceType.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + namespace model + { + + // Constructor + Reference::Reference(Component* component, ReferenceType* referenceType) + : component(component), type(referenceType), binding(0) + { + logentry(); + } + + // Destructor + Reference::~Reference() + { + logentry(); + } + + void Reference::setBinding(ReferenceBinding* binding) + { + this->binding = binding; + } + + ReferenceBinding* Reference::getBinding() const + { + return binding; + } + + void Reference::addTarget(Service* target) + { + logentry(); + + if (type->getMultiplicity() == ReferenceType::ONE_ONE || type->getMultiplicity() == ReferenceType::ZERO_ONE) + { + if (targets.size() > 0) + { + string message = "Duplicate wire for reference: " + type->getName(); + throwException(SystemConfigurationException, message.c_str()); + } + } + + targets.push_back(target); + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Reference.h b/cpp/sca/runtime/core/src/tuscany/sca/model/Reference.h new file mode 100644 index 0000000000..33413b488f --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Reference.h @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_reference_h +#define tuscany_sca_model_reference_h + +#include <vector> + +namespace tuscany +{ + namespace sca + { + namespace model + { + class Component; + class ReferenceType; + class ReferenceBinding; + class Service; + + /** + * An addressable instance of a reference type associated with a particular component. + * Each reference represents a configured version of a logical + * reference type defined in the component type. If the logical reference + * has a multiplicity greater than 1 (0..n or 1..n) then the configured + * reference many have multiple targets. + */ + class Reference + { + public: + /** + * Constructor. + * @param component The component on which the reference is defined. + * @param referenceType The reference type defining the characteristics of the reference. + */ + SCA_API Reference(Component* component, ReferenceType* referenceType); + + /** + * Destructor. + */ + SCA_API virtual ~Reference(); + + /** + * Returns the component on which this reference is defined. + * @return The component on which this reference is defined. + */ + SCA_API Component* getComponent() const { return component; } + + /** + * Returns the reference type defining the characteristics of the reference. + * @return The reference type defining the characteristics of the reference. + */ + SCA_API ReferenceType* getType() const { return type; } + + /** + * Returns the binding supported by the reference. + * @return The binding supported by the reference. + */ + SCA_API ReferenceBinding* getBinding() const; + + /** + * Sets the binding supported by the reference. + * @param binding The binding supported by the reference. + */ + SCA_API void setBinding(ReferenceBinding* binding); + + /** + * Add a target for this reference. There may be more than + * one if the multiplicity is 0..n or 1..n. + * @param target The target of the reference. + */ + SCA_API void addTarget(Service* target); + + typedef std::vector<Service*> TARGETS; + + /** + * Get a vector of all the targets from this reference. + * @return The targets of this reference. + */ + SCA_API const TARGETS& getTargets() const { return targets; } + + private: + + /** + * The component on which this reference is defined. + */ + Component* component; + + /** + * The reference type defining the characteristics of the reference. + */ + ReferenceType* type; + + /** + * The binding supported by this reference + */ + ReferenceBinding* binding; + + /** + * Vector of all the targets wired from this reference. + */ + TARGETS targets; + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_reference_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceBinding.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceBinding.cpp new file mode 100644 index 0000000000..c4e9d875b6 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceBinding.cpp @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/model/ReferenceBinding.h" +#include "tuscany/sca/model/Component.h" +#include "tuscany/sca/model/Reference.h" +#include "tuscany/sca/model/Composite.h" +#include "tuscany/sca/model/Service.h" + + +using namespace std; + +namespace tuscany +{ + namespace sca + { + + namespace model + { + + // Constructor + ReferenceBinding::ReferenceBinding(Reference *reference, const string& uri) : + Binding(uri), reference(reference), targetServiceBinding(0) + { + logentry(); + } + + // Destructor + ReferenceBinding::~ReferenceBinding() + { + logentry(); + } + + void ReferenceBinding::configure(const string& uri) + { + logentry(); + + // Find the target service + Component* component = reference->getComponent(); + Composite* composite = component->getComposite(); + Service* service; + service = composite->findComponentService(uri); + if (!service) + { + logerror("Wire target %s not found", uri.c_str()); + } + else + { + // Configure this binding from the target service binding + configure(service->getBinding()); + } + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceBinding.h b/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceBinding.h new file mode 100644 index 0000000000..197d40aabf --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceBinding.h @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_referencebinding_h +#define tuscany_sca_model_referencebinding_h + +#include <string> + +#include "tuscany/sca/model/Binding.h" + +namespace tuscany +{ + namespace sca + { + class ServiceProxy; + + + namespace model + { + class Reference; + class ServiceBinding; + + /** + * Represents a reference binding. + * Bindings are used by services and references. References use bindings + * to describe the access mechanism used to call an external service (which can + * be a service provided by another SCA composite). Services use bindings to describe + * the access mechanism that clients (which can be a client from another SCA composite) + * have to use to call the service. + * This interface will typically be extended by binding implementations to allow + * specification of binding/transport specific information. + */ + class ReferenceBinding : public Binding + { + public: + + /** + * Constructor to create a new binding. + */ + SCA_API ReferenceBinding(Reference* reference, const std::string& uri); + + /** + * Destructor. + */ + SCA_API virtual ~ReferenceBinding(); + + /** + * Create a proxy representing the reference to the + * client component. + */ + SCA_API virtual ServiceProxy* getServiceProxy() = 0; + + /** + * Configure this binding from a service binding. + */ + SCA_API virtual void configure(ServiceBinding* serviceBinding) = 0; + + /** + * Configure this binding from a URI. + */ + SCA_API virtual void configure(const std::string& uri); + + /** + * Returns the reference. + * @return The reference. + */ + SCA_API Reference* getReference() const { return reference; }; + + /** + * Returns the target service binding. + */ + SCA_API ServiceBinding* getTargetServiceBinding() const { return targetServiceBinding; }; + + protected: + + /** + * Sets the target service binding. + */ + SCA_API void setTargetServiceBinding(ServiceBinding* binding) { targetServiceBinding = binding; }; + + private: + + /** + * The reference configured with the binding. + */ + Reference* reference; + + /** + * The service binding of the target + */ + ServiceBinding* targetServiceBinding; + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_referencebinding_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceType.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceType.cpp new file mode 100644 index 0000000000..732a14cace --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceType.cpp @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/model/ReferenceType.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + namespace model + { + + + // Constructor + ReferenceType::ReferenceType(ComponentType* componentType, const string& name, + Interface* intface, Interface* callbackInterface, Multiplicity multiplicity) + : Contract(intface, callbackInterface), + componentType(componentType), name(name), multiplicity(multiplicity) + { + logentry(); + } + + ReferenceType::~ReferenceType() + { + logentry(); + } + + ReferenceType::Multiplicity ReferenceType::getMultiplicityFromString(const string& multip) + { + logentry(); + + if (multip == "0..1") + { + return ReferenceType::ZERO_ONE; + } + else if (multip == "1..1") + { + return ReferenceType::ONE_ONE; + } + else if (multip == "0..n") + { + return ReferenceType::ZERO_MANY; + } + else if (multip == "1..n") + { + return ReferenceType::ONE_MANY; + } + else + { + return ReferenceType::UNKNOWN; + } + } + + void ReferenceType::setBinding(Binding* binding) + { + this->binding = binding; + } + + Binding* ReferenceType::getBinding() const + { + return binding; + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceType.h b/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceType.h new file mode 100644 index 0000000000..87d38ab330 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ReferenceType.h @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_referencetype_h +#define tuscany_sca_model_referencetype_h + +#include <string> + +#include "tuscany/sca/model/Contract.h" + +namespace tuscany +{ + namespace sca + { + namespace model + { + class ComponentType; + class Interface; + class Binding; + + /** + * Represents an SCA reference. SCA references within an implementation represent + * links to services that the implementation uses that must be provided by other components + * in the SCA system. + */ + class ReferenceType : public Contract + { + public: + + /** + * Multiplicity (how many wires can be connected to this + * reference) + */ + enum Multiplicity + { + ZERO_ONE = 1, + ONE_ONE = 2, + ZERO_MANY = 3, + ONE_MANY = 4, + UNKNOWN = 0, + }; + + /** + * Constructor. + * @param name The name of the reference. + */ + SCA_API ReferenceType(ComponentType* componentType, const std::string& name, + Interface* intface, Interface* callbackInterface, Multiplicity multiplicity); + + /** + * Destructor. + */; + SCA_API virtual ~ReferenceType(); + + /** + * Returns the component type on which this reference is defined. + * @return The component type on which this reference is defined. + */ + SCA_API ComponentType* getComponentType() const { return componentType; } + + /** + * Returns the name of the reference. + * @return The name of the reference. + */ + SCA_API const std::string& getName() const { return name; } + + /** + * Returns the multiplicity allowed for wires connected to this reference. + * @return The multiplicity allowed for wires connected to this reference + */ + SCA_API Multiplicity getMultiplicity() const { return multiplicity; } + + /** + * Returns the binding supported by the reference. + * @return The binding supported by the reference. + */ + SCA_API Binding* getBinding() const; + + /** + * Sets the binding supported by the reference. + * @param binding The binding supported by the reference. + */ + SCA_API void setBinding(Binding* binding); + + /** + * Get the multiplicity corresponding to the given + * string. + */ + static Multiplicity getMultiplicityFromString(const std::string& multip); + + private: + + /** + * The component type on which this reference is defined. + */ + ComponentType *componentType; + + /** + * The name of the reference type. + */ + std::string name; + + /** + * The multiplicity allowed for wires connected to this reference. + */ + Multiplicity multiplicity; + + /** + * The binding supported by this reference type. + */ + Binding* binding; + + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_referencetype_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Service.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/Service.cpp new file mode 100644 index 0000000000..4df1d1efc6 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Service.cpp @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/model/Service.h" + +namespace tuscany +{ + namespace sca + { + namespace model + { + + // Constructor + Service::Service(Component* component, ServiceType* serviceType) + : component(component), type(serviceType), binding(0) + { + logentry(); + } + + // Destructor + Service::~Service() + { + logentry(); + } + + void Service::setBinding(ServiceBinding* binding) + { + this->binding = binding; + } + + ServiceBinding* Service::getBinding() const + { + return binding; + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Service.h b/cpp/sca/runtime/core/src/tuscany/sca/model/Service.h new file mode 100644 index 0000000000..a1f2c5d672 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Service.h @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_service_h +#define tuscany_sca_model_service_h + + +namespace tuscany +{ + namespace sca + { + namespace model + { + class Component; + class ServiceType; + class ServiceBinding; + + /** + * An addressable instance of a service type associated with a particular component. + */ + class Service + { + public: + /** + * Constructor. + * @param component The component on which the service is defined. + * @param serviceType The service type defining the characteristics of the service. + */ + SCA_API Service(Component* component, ServiceType* serviceType); + + /** + * Destructor. + */ + SCA_API virtual ~Service(); + + /** + * Returns the component on which this service is defined. + * @return The component on which this service is defined. + */ + SCA_API Component* getComponent() const { return component; } + + /** + * Returns the service type defining the characteristics of the service. + * @return The service type defining the characteristics of the service. + */ + SCA_API ServiceType* getType() const { return type; } + + /** + * Returns the binding supported by the service. + * @return The binding supported by the service. + */ + SCA_API ServiceBinding* getBinding() const; + + /** + * Sets the binding supported by the service. + * @param binding The binding supported by the service. + */ + SCA_API void setBinding(ServiceBinding* binding); + + private: + + /** + * The component on which this service is defined. + */ + Component* component; + + /** + * The service type defining the characteristics of the service. + */ + ServiceType* type; + + /** + * The binding supported by this service + */ + ServiceBinding* binding; + + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_service_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceBinding.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceBinding.cpp new file mode 100644 index 0000000000..ddc5e6a711 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceBinding.cpp @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/model/ServiceBinding.h" + +using namespace std; + + +namespace tuscany +{ + namespace sca + { + + namespace model + { + + // Constructor + ServiceBinding::ServiceBinding(Service* service, const string& uri) : + Binding(uri), service(service) + { + logentry(); + } + + // Destructor + ServiceBinding::~ServiceBinding() + { + logentry(); + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceBinding.h b/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceBinding.h new file mode 100644 index 0000000000..4e68f492a3 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceBinding.h @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_servicebinding_h +#define tuscany_sca_model_servicebinding_h + +#include <string> + +#include "tuscany/sca/model/Binding.h" + + +namespace tuscany +{ + namespace sca + { + class ServiceWrapper; + + namespace model + { + class Service; + + /** + * Represents a service binding. + * Bindings are used by services and references. References use bindings + * to describe the access mechanism used to call an external service (which can + * be a service provided by another SCA composite). Services use bindings to describe + * the access mechanism that clients (which can be a client from another SCA composite) + * have to use to call the service. + * This interface will typically be extended by binding implementations to allow + * specification of binding/transport specific information. + */ + class ServiceBinding : public Binding + { + public: + + /** + * Constructor to create a new binding. + */ + SCA_API ServiceBinding(Service* service, const std::string& uri); + + /** + * Destructor. + */ + SCA_API virtual ~ServiceBinding(); + + /** + * Create a service wrapper handling the interaction + * with the service configured with this binding. + */ + SCA_API virtual ServiceWrapper* getServiceWrapper() = 0; + + /** + * Returns the service + * @return The service. + */ + SCA_API Service* getService() const { return service; }; + + private: + + /** + * The service configured with the binding. + */ + Service* service; + + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_servicebinding_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceType.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceType.cpp new file mode 100644 index 0000000000..819a832443 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceType.cpp @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/model/ServiceType.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + namespace model + { + + + // Constructor + ServiceType::ServiceType(ComponentType* componentType, const string& name, + Interface* intface, Interface* callbackInterface) + : Contract(intface, callbackInterface), + componentType(componentType), name(name) + { + logentry(); + } + + // Destructor + ServiceType::~ServiceType() + { + logentry(); + } + + void ServiceType::setBinding(Binding* binding) + { + this->binding = binding; + } + + Binding* ServiceType::getBinding() const + { + return binding; + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceType.h b/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceType.h new file mode 100644 index 0000000000..08a76e3b3f --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/ServiceType.h @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_servicetype_h +#define tuscany_sca_model_servicetype_h + +#include <string> + +#include "tuscany/sca/model/Contract.h" + +namespace tuscany +{ + namespace sca + { + namespace model + { + class ComponentType; + class Binding; + class Interface; + + /** + * Represents a service type. Services are used to publish services provided by + * implementations, so that they are addressable by other components. + */ + class ServiceType : public Contract + { + public: + + /** + * Constructor. + * @param componentType The component type on which this service is defined. + * @param name The name of the service. + */ + SCA_API ServiceType(ComponentType* componentType, const std::string& name, + Interface* intface, Interface* callbackInterface); + + /** + * Destructor. + */ + SCA_API virtual ~ServiceType(); + + /** + * Returns the component type on which this service is defined. + * @return The component type on which this service is defined. + */ + SCA_API ComponentType* getComponentType() const { return componentType; } + + /** + * Returns the name of the service type. + * @return The name of the service type + */ + SCA_API const std::string& getName() const { return name; } + + /** + * Returns the binding supported by this service type. + * @return The binding supported by this service type + */ + SCA_API Binding* getBinding() const; + + /** + * Sets the binding supported by this service type. + * @param binding the binding supported by this service type + */ + SCA_API void setBinding(Binding* binding); + + private: + + /** + * The component type on which this service is defined. + */ + ComponentType* componentType; + + /** + * The name of the service type. + */ + std::string name; + + /** + * The binding supported by this service + */ + Binding* binding; + + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_servicetype_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLDefinition.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLDefinition.cpp new file mode 100644 index 0000000000..53971b5a17 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLDefinition.cpp @@ -0,0 +1,633 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <sstream> +#include <set> + +#include "tuscany/sca/model/WSDLDefinition.h" +#include "tuscany/sca/model/WSDLOperation.h" +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/core/Exceptions.h" + +using namespace std; +using namespace commonj::sdo; +using namespace tuscany::sca::util; + +namespace tuscany +{ + namespace sca + { + namespace model + { + + // Constructor + WSDLDefinition::WSDLDefinition(DataObjectPtr wsdlModel) + { + logentry(); + + wsdlModels.insert(wsdlModels.end(), wsdlModel); + mapOperations( wsdlModel ); + } + + WSDLDefinition::~WSDLDefinition() + { + logentry(); + } + + /// + /// The namespace of the service and other definitions defined in this wsdl definition + string WSDLDefinition::getNamespace() + { + logentry(); + return wsdlModels[0]->getCString("targetNamespace"); + } + + void WSDLDefinition::addWSDLModel(DataObjectPtr wsdlModel) + { + logentry(); + wsdlModels.insert(wsdlModels.end(), wsdlModel); + mapOperations( wsdlModel ); + } + + /// + /// Find the operation defined in this wsdl + /// + const WSDLOperation& WSDLDefinition::findOperation(const string& serviceName, + const string& portName, + const string& operationName) + { + logentry(); + + string operationKey = serviceName+"#"+portName; + STR_OPERATION_MAP::const_iterator spIter = servicePortMap.find(operationKey); + if( spIter == servicePortMap.end() ) + { + stringstream errMessage; + errMessage + << "Unable to find Service and Port: \"" + << serviceName << "," << portName + << "\" in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + + OPERATION_MAP::const_iterator opIter = spIter->second.find(operationName); + + if (opIter != spIter->second.end()) + { + return opIter->second; + } + else + { + stringstream errMessage; + errMessage + << "Unable to find operation \"" + << serviceName << ":" << portName << ":" << operationName + << "\" in the WSDL definition"; + + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + } + + /// + /// Find the operation defined in this wsdl + /// + const WSDLOperation& WSDLDefinition::findOperation(const string& portTypeName, + const string& operationName) + { + logentry(); + + STR_OPERATION_MAP::const_iterator ptIter = portTypeMap.find(portTypeName); + if( ptIter == portTypeMap.end() ) + { + stringstream errMessage; + errMessage + << "Unable to find PortType: \"" + << portTypeName + << "\" in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + + OPERATION_MAP::const_iterator opIter = ptIter->second.find(operationName); + + if (opIter != ptIter->second.end()) + { + return opIter->second; + } + else + { + stringstream errMessage; + errMessage + << "Unable to find Operation \"" + << portTypeName << ":" << operationName + << "\" in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + } + + std::list<std::string> WSDLDefinition::getPortTypes() + { + logentry(); + + std::list<std::string> ptList; + STR_OPERATION_MAP::const_iterator ptIter = portTypeMap.begin(); + STR_OPERATION_MAP::const_iterator ptIterEnd = portTypeMap.end(); + + for( ; ptIter != ptIterEnd; ++ptIter ) + { + ptList.push_back( ptIter->first ); + } + + return ptList; + } + + std::list<std::string> WSDLDefinition::getOperations( const std::string &portTypeName ) + { + logentry(); + + std::list<std::string> ptOpList; + STR_OPERATION_MAP::const_iterator ptIter = portTypeMap.find( portTypeName ); + + if( ptIter == portTypeMap.end() ) + { + stringstream errMessage; + errMessage + << "Unable to find PortType: \"" + << portTypeName + << "\" in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + + OPERATION_MAP::const_iterator opIter = ptIter->second.begin(); + OPERATION_MAP::const_iterator opIterEnd = ptIter->second.end(); + + for( ; opIter != opIterEnd; ++opIter ) + { + ptOpList.push_back( opIter->first ); + } + + return ptOpList; + } + + /// + /// Find a service + /// + DataObjectPtr WSDLDefinition::findService(const string& serviceName) + { + logentry(); + + DataObjectPtr service = 0; + + // Find the binding + for (unsigned int m = 0; m < wsdlModels.size(); m++) + { + DataObjectList& serviceList = wsdlModels[m]->getList("service"); + for (unsigned int i=0; i<serviceList.size(); i++) + { + string name(serviceList[i]->getCString("name")); + + if (name.compare(serviceName) == 0) + { + return serviceList[i]; + } + } + } + + return service; + } + + + /// + /// Find a named binding + /// + DataObjectPtr WSDLDefinition::findBinding(const string& bindingName) + { + logentry(); + + DataObjectPtr binding = 0; + string uri; + string name; + + Utils::rTokeniseString("#", bindingName, uri, name); + + // Find the binding + for (unsigned int m = 0; m < wsdlModels.size(); m++) + { + DataObjectList& bindingList = wsdlModels[m]->getList("binding"); + for (unsigned int i=0; i<bindingList.size(); i++) + { + string nameBinding(bindingList[i]->getCString("name")); + + if (nameBinding.compare(name) == 0) + { + return bindingList[i]; + } + } + } + + return binding; + } + + /// + /// Find a named portType + /// + DataObjectPtr WSDLDefinition::findPortType(const string& portTypeName) + { + logentry(); + + DataObjectPtr portType = 0; + string uri; + string name; + + Utils::rTokeniseString("#", portTypeName, uri, name); + + // Find the binding + for (unsigned int m = 0; m < wsdlModels.size(); m++) + { + DataObjectList& portTypeList = wsdlModels[m]->getList("portType"); + for (unsigned int i=0; i<portTypeList.size(); i++) + { + string namePortType(portTypeList[i]->getCString("name")); + + if (namePortType.compare(name) == 0) + { + return portTypeList[i]; + } + } + } + + return portType; + } + + /// + /// Find a named message + /// + DataObjectPtr WSDLDefinition::findMessage(const string& messageName) + { + logentry(); + + DataObjectPtr message = 0; + string uri; + string name; + + Utils::rTokeniseString("#", messageName, uri, name); + + // Find the binding + for (unsigned int m = 0; m < wsdlModels.size(); m++) + { + DataObjectList& messageList = wsdlModels[m]->getList("message"); + for (unsigned int i=0; i<messageList.size(); i++) + { + string nameMessage(messageList[i]->getCString("name")); + + if (nameMessage.compare(name) == 0) + { + return messageList[i]; + } + } + } + + return message; + } + + /// + /// Traverse the WSDL SDO and insert operations into the operationMap + /// + void WSDLDefinition::mapOperations( DataObjectPtr wsdlModel ) + { + logentry(); + + // check for duplicate message/binding/PortType/Service + // with equal name attributes. This cant be enforced by + // the schema, so it has to be enforced at the app level + checkForDuplicates( wsdlModel ); + + DataObjectList& serviceList = wsdlModel->getList("service"); + + // Iterate through the WSDL services + for (unsigned int i=0; i < serviceList.size(); i++) + { + string serviceName( serviceList[i]->getCString("name") ); + + // Iterate through the WSDL service ports + DataObjectList& portList = serviceList[i]->getList("port"); + for (unsigned int j=0; j < portList.size();j++) + { + string portName( portList[j]->getCString("name") ); + string wsBindingName(portList[j]->getCString("binding")); + + // There can only be one soap address and the check must be made + // at the application level since it cant be specified in the xsd +/* + DataObjectList& soapAddressList = portList[j]->getList("address"); + if( soapAddressList.size() != 1 ) + { + // Invalid WSDL + stringstream errMessage; + errMessage + << "service/port/address: \"" + << portName + << "\" cannot be duplicated in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } +*/ + + string targetAddress(portList[j]->getCString("address/location")); + + // get the binding specified in the WSDL service port + DataObjectPtr wsBinding = findBinding(wsBindingName); + if (!wsBinding) + { + // Invalid WSDL + stringstream errMessage; + errMessage + << "Unable to find binding \"" + << wsBindingName + << "\" in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + + // Check if its a SOAP binding, if not go on to the next binding + // doing a find like this will work for SOAP11 and SOAP12 + DataObjectPtr wsBindingSubBinding = wsBinding->getDataObject("binding"); + string bindingURI(wsBindingSubBinding->getType().getURI()); + if (bindingURI.find("http://schemas.xmlsoap.org/wsdl/soap") == string::npos) + { + loginfo("Discarding non-SOAP Binding %s", wsBindingName.c_str() ); + continue; + } + + // Get the port type specified the WSDL binding + string wsBindingPortTypeName(wsBinding->getCString("type")); + DataObjectPtr portType = findPortType(wsBindingPortTypeName); + if (!portType) + { + // Invalid WSDL + stringstream errMessage; + errMessage + << "Unable to find PortType \"" + << wsBindingPortTypeName + << "\" in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + string portTypeName( portType->getCString("name") ); + + // Fill in this map with operation names to WSDLOperations + // then after iterating through the operations, add the map + // to the portTypeMap, keyed off of the portTypeName + OPERATION_MAP operationMap; + + // For each binding and portType operation: + // - get the soap action, style, and use from the binding + // - get the input and/or output message types + // its ok if not all of the PortType operations are not defined in the binding + DataObjectList& bindingOperationList = wsBinding->getList("operation"); + DataObjectList& portTypeOperationList = portType->getList("operation"); + for (unsigned int k=0; k < bindingOperationList.size(); k++) + { + DataObjectPtr bindingOp = bindingOperationList[k]; + string operationName = bindingOp->getCString("name"); + + // Get the corresponding PortType operation + // I know this may not be very efficient, but its a necessary evil + bool foundPortType = false; + DataObjectPtr portTypeOp; + for (unsigned int l=0; + (!foundPortType && l < portTypeOperationList.size()); + l++) + { + //portTypeOp = portTypeOperationList[l]->getDataObject("operation"); + portTypeOp = portTypeOperationList[l]; + if (operationName == portTypeOp->getCString("name") ) + { + foundPortType = true; + } + } + + if( !foundPortType ) + { + // Invalid WSDL + stringstream errMessage; + errMessage + << "Unable to find PortType operation for binding operation: \"" + << operationName + << "\" in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + + string soapAction = ""; + bool documentStyle = true; + bool wrappedStyle = true; + bool useEncodedInput = false; + bool useEncodedOutput = false; + WSDLOperation::soapVersion soapVer = WSDLOperation::SOAP11; + + string opType = bindingOp->getType().getURI(); + if (opType == "http://schemas.xmlsoap.org/wsdl/soap12/") + { + soapVer = WSDLOperation::SOAP12; + } + + // Get the soapAction + soapAction = bindingOp->getCString("operation/soapAction"); + + // Get the style + string style = bindingOp->getCString("operation/style"); + if (style == "") + { + style = wsBinding->getCString("binding/style"); + } + if (style != "document") + { + documentStyle = false; + wrappedStyle = false; + } + + // get the use + string use = bindingOp->getCString("input/body/use"); + if (use == "encoded") + { + useEncodedInput = true; + } + + use = bindingOp->getCString("output/body/use"); + if (use == "encoded") + { + useEncodedOutput = true; + } + + // Get the request message type from the PortType + DataObjectPtr wsMessageIn = + findMessage(portTypeOp->getCString("input/message")); + if (!wsMessageIn) + { + stringstream errMessage; + errMessage + << "unable to find PortType input message \"" + << portTypeOp->getCString("input/message") + << "\" in the wsdl definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + string inputMessageType( wsMessageIn->getCString("name") ); + // If it doesnt have a namespace prefix, add the target namespace + if (inputMessageType.find("#") == string::npos) + { + inputMessageType.insert(0, (getNamespace() + "#") ); + } + + // Get the response message type from the PortType + DataObjectPtr wsMessageOut = + findMessage(portTypeOp->getCString("output/message")); + if (!wsMessageOut) + { + // TODO this is ok for one way operations, right? + stringstream errMessage; + errMessage + << "unable to find PortType output message \"" + << portTypeOp->getCString("output/message") + << "\" in the wsdl definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + string outputMessageType( wsMessageOut->getCString("name") ); + // If it doesnt have a namespace prefix, add the target namespace + if (outputMessageType.find("#") == string::npos) + { + outputMessageType.insert(0, (getNamespace() + "#") ); + } + + WSDLOperation wsdlOp; + wsdlOp.setOperationName(operationName); + wsdlOp.setSoapAction(soapAction); + wsdlOp.setEndpoint(targetAddress); + wsdlOp.setSoapVersion(soapVer); + wsdlOp.setDocumentStyle(documentStyle); + wsdlOp.setWrappedStyle(wrappedStyle); + wsdlOp.setInputEncoded(useEncodedInput); + wsdlOp.setOutputEncoded(useEncodedOutput); + wsdlOp.setInputMessageType(inputMessageType); + wsdlOp.setOutputMessageType(outputMessageType); + wsdlOp.setInputMessage(wsMessageIn); + wsdlOp.setOutputMessage(wsMessageOut); + + operationMap[ operationName ] = wsdlOp; + + } // end bindingOperationList + + portTypeMap[portTypeName] = operationMap; + servicePortMap[(serviceName+"#"+portName)] = operationMap; + + } // end portTypeList + } // end serviceList + } // end method mapOperations + + void WSDLDefinition::checkForDuplicates( DataObjectPtr wsdlModel ) + { + logentry(); + + // check for duplicate message/binding/PortType/service + // with equal name attributes. This cant be enforced by + // the schema, so it has to be enforced at the app level + + std::set<string> namesSet; + + DataObjectList &messageList = wsdlModel->getList("message"); + for( unsigned int i = 0; i < messageList.size(); i++ ) + { + if( namesSet.find( messageList[i]->getCString("name") ) == namesSet.end() ) + { + namesSet.insert( messageList[i]->getCString("name") ); + } + else + { + // Invalid WSDL + stringstream errMessage; + errMessage + << "message/name: \"" + << messageList[i]->getCString("name") + << "\" cannot be duplicated in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + } + + namesSet.clear(); + DataObjectList &bindingList = wsdlModel->getList("binding"); + for( unsigned int i = 0; i < bindingList.size(); i++ ) + { + if( namesSet.find( bindingList[i]->getCString("name") ) == namesSet.end() ) + { + namesSet.insert( bindingList[i]->getCString("name") ); + } + else + { + // Invalid WSDL + stringstream errMessage; + errMessage + << "binding/name: \"" + << bindingList[i]->getCString("name") + << "\" cannot be duplicated in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + } + + namesSet.clear(); + DataObjectList &portTypeList = wsdlModel->getList("portType"); + for( unsigned int i = 0; i < portTypeList.size(); i++ ) + { + if( namesSet.find( portTypeList[i]->getCString("name") ) == namesSet.end() ) + { + namesSet.insert( portTypeList[i]->getCString("name") ); + } + else + { + // Invalid WSDL + stringstream errMessage; + errMessage + << "portType/name: \"" + << portTypeList[i]->getCString("name") + << "\" cannot be duplicated in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + } + + namesSet.clear(); + DataObjectList &serviceList = wsdlModel->getList("service"); + for( unsigned int i = 0; i < serviceList.size(); i++ ) + { + if( namesSet.find( serviceList[i]->getCString("name") ) == namesSet.end() ) + { + namesSet.insert( serviceList[i]->getCString("name") ); + } + else + { + // Invalid WSDL + stringstream errMessage; + errMessage + << "service/name: \"" + << serviceList[i]->getCString("name") + << "\" cannot be duplicated in the WSDL definition"; + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + } + } + + } // end namespace model + } // end namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLDefinition.h b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLDefinition.h new file mode 100644 index 0000000000..baf14fcd6d --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLDefinition.h @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_wsdldefinition_h +#define tuscany_sca_model_wsdldefinition_h + +#include <string> +#include <map> +#include <vector> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" +#include "tuscany/sca/model/WSDLOperation.h" + + +namespace tuscany +{ + namespace sca + { + namespace model + { + + class WSDLOperation; + + /** + * Holds information about a WSDL definition loaded into the runtime. + */ + class WSDLDefinition + { + public: + /** + * Constructor. + * @param wsdlModel The data object representing the WSDL document + * defining a web service. + */ + SCA_API WSDLDefinition(commonj::sdo::DataObjectPtr wsdlModel); + + /** + * Destructor. + */ + SCA_API virtual ~WSDLDefinition(); + + /** + * Returns the target namespace of the WSDL definitions. + * @return The target namespace. + */ + SCA_API std::string getNamespace(void); + + /** + * Add a WSDL model. + */ + SCA_API void addWSDLModel(commonj::sdo::DataObjectPtr wsdlModel); + + /** + * Find an operation in the WSDL definitions. + * @param serviceName The name of the service on which this + * operation is defined. + * @param portName The name of the port in the service to + * use. + * @param operationName The name of the operation to find. + * @return The operation if found. Exception thrown if not found. + */ + SCA_API const WSDLOperation& findOperation(const std::string& serviceName, + const std::string& portName, + const std::string& operationName); + + /** + * Find an operation in the WSDL definitions. + * @param portTypeName The name of the portType on which this + * operation is defined. + * @param operationName The name of the operation to find. + * @return The operation if found. Exception thrown if not found. + */ + SCA_API const WSDLOperation& findOperation(const std::string& portTypeName, + const std::string& operationName); + + /** + * Get all of the PortTypes in this WSDL definition. + * @return A standard list of standard strings, each being a different PortType + */ + SCA_API std::list<std::string> getPortTypes(); + + /** + * Get all of the Operations for a particular PortType in this WSDL definition. + * @param portTypeName Get all of the operations for this portTypeName + * @return A standard list of standard strings, each of which is a different operation + */ + SCA_API std::list<std::string> getOperations( const std::string &portTypeName ); + + private: + + /** + * Find a service in the wsdl definition. + * @param serviceName The name of the service. + * @return A data object describing the service if found, otherwise + * a 0 if not found. + */ + commonj::sdo::DataObjectPtr findService(const std::string& serviceName); + + /** + * Find a binding in the wsdl definition. + * @param bindingName The name of the binding to find. + * @return A data object describing the binding if found, otherwise + * a 0 if not found. + */ + commonj::sdo::DataObjectPtr findBinding(const std::string& bindingName); + + /** + * Find a portType in the wsdl definition. + * @param portTypeName The name of the portType. + * @return A data object describing the portType if found, otherwise + * a 0 if not found. + */ + commonj::sdo::DataObjectPtr findPortType(const std::string& portTypeName); + + /** + * Find a message in the wsdl definition. + * @param messageName The name of the message. + * @return A data object describing the message if found, otherwise + * a 0 if not found. + */ + commonj::sdo::DataObjectPtr findMessage(const std::string& messageName); + + /** + * Given a wsdl represented by an SDO, traverse the SDO and + * extract all of the necessary information to populate the + * operationMap with WSDLOperation objects. + * This method is called each time a wsdl model is added: so + * that would be be in the constructor and in addWSDLModel(). + * @param wsdlModel A wsdl represented by an SDO + */ + void mapOperations( commonj::sdo::DataObjectPtr wsdlModel ); + + /** + * Given a wsdl represented by an SDO, check for duplicate + * message/binding/PortType/service with equal name attributes. + * This cant be enforced by the schema, so it has to be enforced + * at the app level. If a duplicate is found, a + * SystemConfigurationException exception is thrown. + * @param wsdlModel A wsdl represented by an SDO + */ + void checkForDuplicates( commonj::sdo::DataObjectPtr wsdlModel ); + + /** + * The data object representation of the WSDL document. + */ + typedef std::vector<commonj::sdo::DataObjectPtr> MODEL_VECTOR; + MODEL_VECTOR wsdlModels; + + typedef std::map<std::string, WSDLOperation> OPERATION_MAP; + typedef std::map<std::string, OPERATION_MAP> STR_OPERATION_MAP; + STR_OPERATION_MAP portTypeMap; + STR_OPERATION_MAP servicePortMap; + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_wsdldefinition_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLInterface.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLInterface.cpp new file mode 100644 index 0000000000..5fa45a9967 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLInterface.cpp @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/model/WSDLInterface.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + namespace model + { + + const string WSDLInterface::typeQName("http://www.osoa.org/xmlns/sca/1.0#WSDLPortType"); + + // Constructor + WSDLInterface::WSDLInterface( + const string& qname, bool remotable, bool conversational) + : Interface(remotable, conversational) + { + logentry(); + parse(qname); + } + + void WSDLInterface::parse(const string& qname) + { + logentry(); + + // PortType is of the form: <wsdl-namepace-uri>#wsdl.interface(<portType-name>) + string::size_type hash = qname.find("#"); + if (hash != string::npos) + { + // Found a hash + + // Namepace is the part before the # + namespaceURI = qname.substr(0, hash); + + if ( (hash+1) < qname.length()) + { + // Check the next part is wsdl.interface( + int ending = hash+16; + string check = qname.substr(hash+1, 15); + if (check.compare("wsdl.interface(") == 0) + { + // Find the matching ) + int endBracket = qname.find(")",ending); + if (endBracket-1 > ending+1) + { + name = qname.substr(ending, endBracket-ending); + } + else + { + // Nothing between the () + name = ""; + } + } + else + { + // not the correct characters after the #, ignore the rest + name = ""; + } + + } + else + { + // Nothing after the hash + name = ""; + } + } + else + { + // No hash at all + namespaceURI = qname; + name = ""; + } + } + + + WSDLInterface::~WSDLInterface() + { + logentry(); + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLInterface.h b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLInterface.h new file mode 100644 index 0000000000..b071dd221e --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLInterface.h @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_wsdlinterface_h +#define tuscany_sca_model_wsdlinterface_h + +#include <string> + +#include "tuscany/sca/model/Interface.h" + +namespace tuscany +{ + namespace sca + { + namespace model + { + /** + * Holds information about an interface described using a WSDL + * port type. + */ + class WSDLInterface : public Interface + { + + public: + /** + * Constuctor. + * @param interfaceName Name of the WSDL interface. + */ + WSDLInterface(const std::string& qname, bool remotable, bool conversational); + + /** + * Destructor. + */ + virtual ~WSDLInterface(); + + /** + * Returns the WSDL namespace + */ + std::string getNamespaceURI() const { return namespaceURI; } + + /** + * Returns the interface name + */ + std::string getName() const { return name; } + + /** + * return the QName of the schema type for this interface type + * (e.g. "http://www.osoa.org/xmlns/sca/1.0#interface.cpp") + */ + virtual const std::string& getInterfaceTypeQName() { return typeQName; }; + + /** + * The QName of the schema type for this interface type. + */ + SCA_API static const std::string typeQName; + + private: + + /** + * Parse the WSDL qname + */ + void parse(const std::string& qname); + + /** + * WSDL namespace. + */ + std::string namespaceURI; + + /** + * Name of the WSDL interface. + */ + std::string name; + + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_cpp_wsdlinterface_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLMessagePart.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLMessagePart.cpp new file mode 100644 index 0000000000..957d7ab3c1 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLMessagePart.cpp @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <string> + +#include "tuscany/sca/model/WSDLMessagePart.h" +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/util/Utils.h" + +using namespace std; +using namespace tuscany::sca::util; + +namespace tuscany +{ + namespace sca + { + namespace model + { + + + // Default Constructor + WSDLMessagePart::WSDLMessagePart() + { + logentry(); + } + + // Constructor + WSDLMessagePart::WSDLMessagePart(std::string partName, + std::string partType, + std::string partUri) : + partName_(partName), + partType_(partType), + partUri_(partUri) + { + logentry(); + } + + WSDLMessagePart::~WSDLMessagePart() + { + logentry(); + } + + + void WSDLMessagePart::setMessagePartType(const string& inputType) + { + logentry(); + Utils::tokeniseQName(inputType, partUri_, partName_); + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLMessagePart.h b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLMessagePart.h new file mode 100644 index 0000000000..2c381011ba --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLMessagePart.h @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_wsdlmessagepart_h +#define tuscany_sca_model_wsdlmessagepart_h + +#include <map> +#include <list> +#include <string> + +#include "tuscany/sca/export.h" + +namespace tuscany +{ + namespace sca + { + namespace model + { + + /** + * Represents a single WSDL defined message part. + */ + class WSDLMessagePart + { + public: + /** + * Default Constructor + */ + SCA_API WSDLMessagePart(); + + /** + * Constructor. + * @param partName The name of the message part. + * @param partType The type of this message part. + * @param partUri The namespace URI for the message part type. + */ + SCA_API WSDLMessagePart( std::string partName, + std::string partType, + std::string partUri ); + + /** + * Destructor. + */ + SCA_API ~WSDLMessagePart(); + + /** + * Return the name of the WSDL Message part + * @return The name of the WSDL Message part + */ + SCA_API const std::string& getPartName() const {return partName_;} + SCA_API void setPartName(const std::string& partName) {partName_ = partName;} + + /** + * The type of this WSDL Message part + * @return The type of this WSDL Message part + */ + SCA_API const std::string& getPartType() const {return partType_;} + SCA_API void setPartType(const std::string& partType) {partType_ = partType;} + + /** + * Set the WSDL message part type and namespace URI + * @return A prefixed namespace and type of the form prefix:type + */ + SCA_API void setMessagePartType(const std::string& partType); + + /** + * Return the namespace URI of the WSDL Message part + * @return The namespace URI of the WSDL Message part + */ + SCA_API const std::string& getPartUri() const {return partUri_;} + SCA_API void setPartUri(const std::string& partUri) {partUri_ = partUri;} + + private: + /** + * The name of this message part + */ + std::string partName_; + + /** + * The type of this message part + */ + std::string partType_; + + /** + * The namespace URI of the message part type + */ + std::string partUri_; + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_wsdlmessagepart_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLOperation.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLOperation.cpp new file mode 100644 index 0000000000..a61d122a72 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLOperation.cpp @@ -0,0 +1,193 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <map> +#include <list> +#include <string> +#include <sstream> + +#include "tuscany/sca/model/WSDLOperation.h" +#include "tuscany/sca/model/WSDLMessagePart.h" +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/core/Exceptions.h" + +using namespace std; +using namespace commonj::sdo; +using namespace tuscany::sca::util; + +namespace tuscany +{ + namespace sca + { + namespace model + { + + + // Constructor + WSDLOperation::WSDLOperation() + { + logentry(); + } + + WSDLOperation::~WSDLOperation() + { + logentry(); + } + + + void WSDLOperation::setInputMessageType(const string& inputType) + { + logentry(); + Utils::tokeniseQName(inputType, inputMessageUri, inputMessageName); + } + + void WSDLOperation::setOutputMessageType(const string& outputType) + { + logentry(); + Utils::tokeniseQName(outputType, outputMessageUri, outputMessageName); + } + + void WSDLOperation::setInputMessage( commonj::sdo::DataObjectPtr inputMsg ) + { + logentry(); + + DataObjectList &partList = inputMsg->getList("part"); + for( unsigned int i=0; i < partList.size(); i++) + { + string partUri; + string partType; + string partName = partList[0]->getCString("name"); + + Utils::tokeniseQName(partList[0]->getCString("element"), partUri, partType); + WSDLMessagePart part(partName, partType, partUri); + + inputPartMap[partName] = part; + } + } + + void WSDLOperation::setOutputMessage( commonj::sdo::DataObjectPtr outputMsg ) + { + logentry(); + + DataObjectList &partList = outputMsg->getList("part"); + for( unsigned int i=0; i < partList.size(); i++) + { + string partUri; + string partType; + string partName = partList[0]->getCString("name"); + + Utils::tokeniseQName(partList[0]->getCString("element"), partUri, partType); + WSDLMessagePart part(partName, partType, partUri); + + outputPartMap[partName] = part; + } + } + + const std::list<std::string> WSDLOperation::getInputMessagePartNames() const + { + logentry(); + + std::list<std::string> inputPartNames; + + PART_MAP::const_iterator iter = inputPartMap.begin(); + PART_MAP::const_iterator iterEnd = inputPartMap.end(); + + for( ; iter != iterEnd; ++iter ) + { + inputPartNames.push_back( iter->first ); + } + + return inputPartNames; + } + + const std::list<std::string> WSDLOperation::getOutputMessagePartNames() const + { + logentry(); + + std::list<std::string> outputPartNames; + + PART_MAP::const_iterator iter = outputPartMap.begin(); + PART_MAP::const_iterator iterEnd = outputPartMap.end(); + + for( ; iter != iterEnd; ++iter ) + { + outputPartNames.push_back( iter->first ); + } + + return outputPartNames; + } + + const WSDLMessagePart& + WSDLOperation::getInputMessagePart( const std::string &name ) const + { + logentry(); + + PART_MAP::const_iterator iter = inputPartMap.find( name ); + if( iter == inputPartMap.end() ) + { + stringstream errMessage; + errMessage + << "Unable to find input WSDLMessagePart for partName: " << name; + + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + + return iter->second; + } + + const WSDLMessagePart& + WSDLOperation::getOutputMessagePart( const std::string &name ) const + { + logentry(); + + PART_MAP::const_iterator iter = outputPartMap.find( name ); + if( iter == outputPartMap.end() ) + { + stringstream errMessage; + errMessage + << "Unable to find output WSDLMessagePart for partName: " << name; + + throwException(SystemConfigurationException, errMessage.str().c_str()); + } + + return iter->second; + } + + void WSDLOperation::setInputMessagePart( const std::string &partName, + WSDLMessagePart part ) + { + logentry(); + + inputPartMap[partName] = part; + } + + void WSDLOperation::setOutputMessagePart( const std::string &partName, + WSDLMessagePart part ) + { + logentry(); + + outputPartMap[partName] = part; + } + + } // End namespace model + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLOperation.h b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLOperation.h new file mode 100644 index 0000000000..48d848170a --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/WSDLOperation.h @@ -0,0 +1,196 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_wsdloperation_h +#define tuscany_sca_model_wsdloperation_h + +#include <map> +#include <list> +#include <string> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" +#include "tuscany/sca/model/WSDLMessagePart.h" + + +namespace tuscany +{ + namespace sca + { + namespace model + { + + /** + * Represents a single.,bound WSDL defined operation. + * This class includes information from the soapBinding + * in addition to the WSDL definition of the operation. + */ + class WSDLOperation + { + public: + /** + * Constructor. + */ + SCA_API WSDLOperation(); + + /** + * Destructor. + */ + SCA_API virtual ~WSDLOperation(); + + /** + * Return the name of the operation for use when serializing an + * outgoing message. + * @return The name of the element in the request message. + */ + SCA_API const std::string& getOperationName() const {return operationName;} + SCA_API void setOperationName(const std::string& opName) {operationName = opName;} + + /** + * The soap action string for this operation. + * @return The soap action. + */ + SCA_API const std::string& getSoapAction() const {return soapAction;} + SCA_API void setSoapAction(const std::string& soapAct) {soapAction = soapAct;} + + /** + * Return the endpoint address for the target web service. + * @return The endpoint address. + */ + SCA_API const std::string& getEndpoint() const {return endpoint;} + SCA_API void setEndpoint(const std::string& ep) {endpoint = ep;} + + enum soapVersion + { + SOAP11, + SOAP12 + }; + + SCA_API void setSoapVersion(soapVersion ver) {soapVer = ver;} + SCA_API soapVersion getSoapVersion() const {return soapVer;} + + SCA_API void setDocumentStyle(bool docStyle) {documentStyle = docStyle;} + SCA_API bool isDocumentStyle() const {return documentStyle;} + + SCA_API void setWrappedStyle(bool wrapStyle) {wrappedStyle = wrapStyle;} + SCA_API bool isWrappedStyle() const {return wrappedStyle;} + + SCA_API void setInputEncoded(bool enc) {inputEncoded = enc;} + SCA_API bool isInputEncoded() const {return inputEncoded;} + + SCA_API void setOutputEncoded(bool enc) {outputEncoded = enc;} + SCA_API bool isOutputEncoded() const {return outputEncoded;} + + /** + * Input Message URI and Name. To get the message part URI and name, you + * will have to get the WSDLMessagePart by calling getInputMessagePart() + */ + SCA_API void setInputMessageType(const std::string& inputMessageType); + SCA_API const std::string& getInputMessageUri() const {return inputMessageUri;} + SCA_API const std::string& getInputMessageName() const {return inputMessageName;} + + /** + * Output Message URI and Name. To get the message part URI and name, you + * will have to get the WSDLMessagePart by calling getOutputMessagePart() + */ + SCA_API void setOutputMessageType(const std::string& outputMessageType); + SCA_API const std::string& getOutputMessageUri() const {return outputMessageUri;} + SCA_API const std::string& getOutputMessageName() const {return outputMessageName;} + + // TODO We should add an enum for REQUEST_ONLY, RESPONSE_ONLY, REQUEST_RESPONSE + // and a setter/getter. The enum would be checked by getOutputXXX + + /** + * Parse a WSDL message represented by an SDO into the input/outputPartMap + * @param inputMsg a data object which contains the WSDL message + * ie. <wsdl:message name="getAccountReportResponse"> + * <wsdl:part element="tns:getAccountReportResponse" + * name="getAccountReportResponse" /> + * </wsdl:message> + */ + SCA_API void setInputMessage( commonj::sdo::DataObjectPtr inputMsg ); + SCA_API void setOutputMessage( commonj::sdo::DataObjectPtr outputMsg ); + + /** + * Manually set the input/outputPartMap, as opposed to passing in an SDO WSDL message. + * Populates the input/outputPartMap, setInput/OutputMessageType must still be called. + * @param partName the name of the message part + * @param part the message part + */ + SCA_API void setInputMessagePart( const std::string &partName, WSDLMessagePart part ); + SCA_API void setOutputMessagePart( const std::string &partName, WSDLMessagePart part ); + + /** + * Return a message part keyed off of the message part name + * @param name the message part name + * @return the message part in a WSDLMessagePart object + */ + SCA_API const WSDLMessagePart &getInputMessagePart( const std::string &name ) const; + SCA_API const WSDLMessagePart &getOutputMessagePart( const std::string &name ) const; + + /** + * Get all of the message part names + * @return a list of strings, each being a different message part name + */ + SCA_API const std::list<std::string> getInputMessagePartNames() const; + SCA_API const std::list<std::string> getOutputMessagePartNames() const; + + private: + /** + * The name of the operation for use when serializing an + * outgoing message. + */ + std::string operationName; + + /** + * The soap action string for this operation. + */ + std::string soapAction; + + /** + * The endpoint address of the target web service. + */ + std::string endpoint; + + bool documentStyle; + bool wrappedStyle; + bool inputEncoded; + bool outputEncoded; + soapVersion soapVer; + + std::string inputMessageUri; + std::string inputMessageName; + + std::string outputMessageUri; + std::string outputMessageName; + + typedef std::map<std::string, WSDLMessagePart> PART_MAP; + PART_MAP inputPartMap; + PART_MAP outputPartMap; + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_wsdloperation_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Wire.cpp b/cpp/sca/runtime/core/src/tuscany/sca/model/Wire.cpp new file mode 100644 index 0000000000..03f475af09 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Wire.cpp @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/model/Wire.h" + +using namespace tuscany::sca::util; + +namespace tuscany +{ + namespace sca + { + namespace model + { + + // Constructor + Wire::Wire(const std::string& source, const std::string& targ) + { + logentry(); + + Utils::tokeniseUri(source, sourceComponent, sourceReference); + target = targ; + } + + Wire::~Wire() + { + logentry(); + } + + } // End namespace model + + + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/model/Wire.h b/cpp/sca/runtime/core/src/tuscany/sca/model/Wire.h new file mode 100644 index 0000000000..ab136e9109 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/model/Wire.h @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_model_wire_h +#define tuscany_sca_model_wire_h + +#include <string> + +namespace tuscany +{ + namespace sca + { + namespace model + { + /** + * Information about a wire in the model. + */ + class Wire + { + public: + + /** + * Constructor. + * @param source The source of the wire. Either the component and + * reference name (optional) or an entry point. + * @param target The target of the wire. Either a component and service + * service name (optional) or an external sevice. + */ + SCA_API Wire(const std::string& source, const std::string& target); + + /** + * Destructor. + */ + SCA_API virtual ~Wire(); + + /** + * Get the component name defined by the source of the wire. + * @return The component name which is the source of the wire. + */ + SCA_API const std::string& getSourceComponent() const { return sourceComponent; } + + /** + * Get the reference name defined by the source of the wire. + * @return The reference name which is the source of the wire. + */ + SCA_API const std::string& getSourceReference() const { return sourceReference; } + + /** + * Get the target uri defined by the target of the wire. + * @return The target uri which is the source of the wire. + */ + SCA_API const std::string& getTarget() { return target; } + + private: + /** + * The source component of the wire. + */ + std::string sourceComponent; + + /** + * The source reference of the wire. + */ + std::string sourceReference; + + /** + * The target uri of the wire. + */ + std::string target; + }; + + } // End namespace model + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_model_wire_h + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/DefaultLogWriter.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/DefaultLogWriter.cpp new file mode 100644 index 0000000000..48cc0aa1e6 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/DefaultLogWriter.cpp @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <iostream> + +#include "tuscany/sca/util/DefaultLogWriter.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + namespace util + { + DefaultLogWriter::~DefaultLogWriter() + { + } + + void DefaultLogWriter::log(int level, const char* tid, const char* msg) + { + cout << tid << " "; + for (int i=0; i < level; i++) + { + cout << " "; + } + cout << msg <<endl; + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/DefaultLogWriter.h b/cpp/sca/runtime/core/src/tuscany/sca/util/DefaultLogWriter.h new file mode 100644 index 0000000000..80e07d696c --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/DefaultLogWriter.h @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_defaultlogwriter_h +#define tuscany_sca_util_defaultlogwriter_h + +#include "tuscany/sca/util/LogWriter.h" + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * Log writer to write out to standard out. + */ + class DefaultLogWriter : public LogWriter + { + public: + virtual ~DefaultLogWriter(); + + /** + * Will write to the console. + * See LogWriter#log. + */ + virtual void log(int level, const char* tid, const char* msg); + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany +#endif // tuscany_sca_util_defaultlogwriter_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/File.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/File.cpp new file mode 100644 index 0000000000..507c1b38ea --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/File.cpp @@ -0,0 +1,243 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#if defined(WIN32) || defined (_WINDOWS) +#pragma warning(disable: 4786) +#endif + +#include <iostream> +#include <string> + +#if defined(WIN32) || defined (_WINDOWS) +#include <windows.h> +#else +#include <unistd.h> +#include <sys/stat.h> +#include <dirent.h> +#endif + +#include "tuscany/sca/util/File.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/util/Logging.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + namespace util + { + File::File(const string& dir, const string& file, bool isDirectory) + : directory(dir), fileName(file), isDir(isDirectory) + { + } + File::~File() + { + } + + + Files::Files(const string& rootDir, const string& pattern, bool subdirectories, bool directories) + : rootDirectory(rootDir) + { + findFiles(rootDirectory, pattern, subdirectories, directories); + } + + Files::~Files() + { + } + + unsigned int Files::size() + { + return files.size(); + } + + const File& Files::operator[] (unsigned int index) + { + if (size() <= index) + { + throwException(SystemConfigurationException, "Index of of bounds"); + } + + FILES::iterator iter = files.begin(); + for (unsigned int i=0; i<index; i++) + { + iter++; + } + + return *iter; + } + + + void Files::findFiles(const string& rootDir, const string& pattern, bool subdirectories, bool directories) + { + +#if defined(WIN32) || defined (_WINDOWS) + char currentDir[ _MAX_FNAME]; + + GetCurrentDirectory(_MAX_FNAME, currentDir); + + // Set current directory, from which to search. + if (!SetCurrentDirectory(rootDir.c_str())) + { + loginfo("Unable to set current directory to: %s", rootDir.c_str()); + return; + } + char fullDirname[ _MAX_FNAME]; + GetCurrentDirectory(_MAX_FNAME, fullDirname); + + // First, look for all files in this directory that meet the pattern + char search[ _MAX_FNAME]; + strcpy(search, pattern.c_str()); + + // Find the first file in the directory + WIN32_FIND_DATA data; + HANDLE searchHandle = FindFirstFile(search, &data); + + int more = TRUE; + if (searchHandle != INVALID_HANDLE_VALUE) + { + // Found some matching files, so call the function with the details of each one + while (more) + { + // Skip over directories + if (!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) + { + if (!directories) + { + // Add the file to our list + files.push_back(File(fullDirname, data.cFileName)); + } + } + else if (directories) + { + files.push_back(File(fullDirname, data.cFileName, true)); + } + + more = FindNextFile(searchHandle, &data); + } + } + + + if (!subdirectories) + return; + + // Now recurse down all the directories + // Find the first file in the directory + searchHandle = FindFirstFile( "*.*", &data); + more = TRUE; + + if (searchHandle != INVALID_HANDLE_VALUE) + { + // Found some files in the directory. + while (more) + { + // If directory + if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + // Skip over '.' and '..' + if ((strcmp(data.cFileName, ".")) && (strcmp("..", data.cFileName))) + { + // Recurse + findFiles(data.cFileName, pattern, subdirectories, directories); + } + } + + more = FindNextFile(searchHandle, &data); + } + } + SetCurrentDirectory(currentDir); +#else + // Linux + //char fullDirname[MAX_PATH]; + //getcwd(fullDirname, MAX_PATH); + + DIR* root = opendir(rootDir.c_str()); + if (!root) + { + logwarning("Unable to open directory: %s", rootDir.c_str()); + return; + } + + bool exactMatch = true; + string token1, token2; + if (pattern.find('*') != string::npos) + { + exactMatch = false; + Utils::tokeniseString("*", pattern, token1, token2); + } + + struct dirent *entry=0; + while ((entry = readdir(root))) + { + string entryName = rootDir + "/" + entry->d_name; + struct stat statbuf; + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + continue; + if (stat(entryName.c_str(), &statbuf) != 0) + { + logwarning("Unable to stat entry: %s", entryName.c_str()); + } + else + { + string filename = entry->d_name; + + if ((exactMatch && filename == pattern) || + (!exactMatch && + ((filename.find(token1) == 0) + && (filename.length() >= token2.length()) + && (filename.rfind(token2) == (filename.length() - token2.length())) ))) + { + if (S_ISDIR(statbuf.st_mode)) + { + if (directories) + { + files.push_back(File(rootDir, filename, true)); + } + } + else if (S_ISREG(statbuf.st_mode)) + { + if (!directories) + { + // Add the file to our list + files.push_back(File(rootDir, filename)); + } + } + } // end - matching filename + + // recurse if necessary + if (subdirectories && S_ISDIR(statbuf.st_mode)) + { + findFiles(entryName, pattern, subdirectories, directories); + } + } + } + + closedir(root); +#endif + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/File.h b/cpp/sca/runtime/core/src/tuscany/sca/util/File.h new file mode 100644 index 0000000000..944a192cf6 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/File.h @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_file_h +#define tuscany_sca_util_file_h + +#include <string> +#include <vector> + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * File access methods. Provides platform independent + * access to files. + */ + class File + { + public: + /** + * Constructor. + * @param directory Name of the directory in which this file is located. + * Either / or \ can be used interchangeably for separating directory elements. + * @param fileName Name of the file in the dirctory. + */ + File(const std::string& directory, const std::string& fileName, bool isDirectory = false); + + /** + * Destructor. + */ + virtual ~File(); + + /** + * Return the directory in which this file is located. + * @return Name of the parent directory. + */ + const std::string& getDirectory() const {return directory;} + + /** + * Return the name of the file. + * @return Name of the file/directory. + */ + const std::string& getFileName() const {return fileName;} + + /** + * Return if this is a directory + * @return true if a directory + */ + bool isDirectory() const {return isDir;} + + /** + * Return if this is a file + * @return true if a file + */ + bool isFile() const {return !isDir;} + + private: + /** + * Name of the parent directory. + */ + std::string directory; + + /** + * Name of the file/directory + */ + std::string fileName; + + /** + * Is this a directory + */ + bool isDir; + }; + + /** + * Collection of File to provide platform independent access + * to files and directories. + */ + class Files + { + public: + /** + * Constructor which will search a given directory with a pattern and return a + * new instance of this collection class. + * @param rootDirectory The directory in which to search. + * @param pattern A pattern for matching file names. Can include * and ?. + * @param subdirectories Whether subdirectories should be searched too. + * @param directories Whether to directories or files. + */ + Files(const std::string& rootDirectory, + const std::string& pattern, + bool subdirectories = false, + bool directories = false); + + /** + * Destructor. + */ + virtual ~Files(); + + /** + * Return the number of files found. + * @return The number of files found. + */ + unsigned int size(); + + /** + * Return a File at this position in the collection. + * @param index The index into the collection. + * @return The File at this index in the collection. + */ + const File& operator[] (unsigned int index); + + private: + /** + * Search the given directory and pattern for matching files. + * @param rootDirectory The directory in which to search. + * @param pattern A pattern for matching file names. Can include * and ?. + * @param subdirectories Whether subdirectories should be searched too. + * @param directories Whether to directories or files. + */ + void findFiles(const std::string& rootDirectory, const std::string& pattern, + bool subdirectories, bool directories); + + /** + * The top level directory to search. + */ + std::string rootDirectory; + + typedef std::vector<File> FILES; + + /** + * Vector of File. + */ + FILES files; + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany +#endif // tuscany_sca_util_file_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/FileLogWriter.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/FileLogWriter.cpp new file mode 100644 index 0000000000..56eb3146dd --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/FileLogWriter.cpp @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <iostream> + +#include "tuscany/sca/util/FileLogWriter.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + namespace util + { + FileLogWriter::FileLogWriter(const char* logfile) + { + logFile.open(logfile, ios_base::app); + } + + FileLogWriter::~FileLogWriter() + { + logFile.close(); + } + + void FileLogWriter::log(int level, const char*tid, const char* msg) + { + logFile << tid << " "; + for (int i=0; i < level; i++) + { + logFile << " "; + } + logFile << msg <<endl; + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/FileLogWriter.h b/cpp/sca/runtime/core/src/tuscany/sca/util/FileLogWriter.h new file mode 100644 index 0000000000..5925fdca85 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/FileLogWriter.h @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_filelogwriter_h +#define tuscany_sca_util_filelogwriter_h + +#include <iostream> +#include <fstream> + +#include "tuscany/sca/util/LogWriter.h" + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * Log writer to write out to standard out. + */ + class FileLogWriter : public LogWriter + { + public: + FileLogWriter(const char* logfile); + + virtual ~FileLogWriter(); + + /** + * Will write to the console. + * See LogWriter#log. + */ + virtual void log(int level, const char* tid, const char* msg); + private: + std::ofstream logFile; + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany +#endif // tuscany_sca_util_defaultlogwriter_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Library.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/Library.cpp new file mode 100644 index 0000000000..619205ab7e --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Library.cpp @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <sstream> + +#include "tuscany/sca/util/Library.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/core/Exceptions.h" +#include "tuscany/sca/util/Logging.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + namespace util + { + Library::Library() + : hDLL(NULL) + { + } + + Library::Library(const string& libraryName) + : name(libraryName), hDLL(NULL) + { + logentry(); + load(); + } + + Library::Library(const Library& lib) + : name(lib.name), hDLL(NULL) + { + logentry(); + if (lib.hDLL) + { + load(); + } + } + + Library& Library::operator=(const Library& lib) + { + logentry(); + if (&lib != this) + { + unload(); + name = lib.name; + load(); + } + return *this; + } + + Library::~Library() + { + logentry(); + unload(); + } + + + void Library::load() + { + logentry(); + loginfo("Library: %s", name.c_str()); + + string msg; +#if defined(WIN32) || defined (_WINDOWS) + int l = name.length(); + string dllName; + if (l>=4 && name.substr(l-4, 4)==".dll") + { + dllName = name; + } + else + { + dllName = name+".dll"; + } + SetErrorMode(SEM_FAILCRITICALERRORS); + hDLL = LoadLibrary(dllName.c_str()); + if (hDLL == NULL) + { + ostringstream msgs; + msgs << "Unable to load library: " + dllName << ", error: "; + Utils::printLastError(msgs); + msg = msgs.str(); + } +#else + int l = name.length(); + string libName; +#ifdef IS_DARWIN + string suffix = ".dylib"; +#else + string suffix = ".so"; +#endif + unsigned int suffixLength = suffix.length(); + if (l>=suffixLength && name.substr(l-suffixLength, suffixLength)==suffix) + { + libName = name; + } + else + { + int s = name.rfind("/"); + if (s == name.length()) + { + libName = name + suffix; + } + else + { + s++; + libName = name.substr(0, s) + "lib" + name.substr(s, name.length()-s) + suffix; + } + } + hDLL = dlopen(libName.c_str(), RTLD_NOW); + if (hDLL == NULL) + { + msg = "Unable to load library: " + libName + ": " + dlerror(); + } +#endif + if (hDLL == NULL) + { + throwException(SystemConfigurationException, msg.c_str()); + } + } + + void Library::unload() + { + logentry(); + loginfo("Library: %s", name.c_str()); + + if (hDLL != NULL) + { +#if defined(WIN32) || defined (_WINDOWS) + FreeLibrary(hDLL); +#else + dlclose(hDLL); +#endif + hDLL = NULL; + } + } + + void* Library::getSymbol(const string& symbol) + { + logentry(); + loginfo("Symbol: %s", symbol.c_str()); + if (!hDLL) + { + return 0; + } +#if defined(WIN32) || defined (_WINDOWS) + return GetProcAddress(hDLL, symbol.c_str()); +#else + return dlsym(hDLL, symbol.c_str()); +#endif + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Library.h b/cpp/sca/runtime/core/src/tuscany/sca/util/Library.h new file mode 100644 index 0000000000..6b0a17d2b7 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Library.h @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_library_h +#define tuscany_sca_util_library_h + +#if defined(WIN32) || defined (_WINDOWS) +#include <windows.h> +#else +#include <unistd.h> +#include <dlfcn.h> +#endif + +#include <string> + +#include "tuscany/sca/export.h" + + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * Information about shared libraries and methods to + * access these shared libraries. + */ + class SCA_API Library + { + public: + Library(); + + /** + * Constructor. Will load the library. + * @param libraryName Fully qualified name of the library. + */ + Library(const std::string& libraryName); + + /** + * Destructor. Will unload the library. + */ + virtual ~Library(); + + Library(const Library& lib); + Library& operator=(const Library& lib); + + /** + * Find an externalized symbol in the library. + * @param symbol The name of the symbol to be found. + * @return The pointer to the symbol if found, otherwise 0. + */ + void* getSymbol(const std::string& symbol); + private: + /** + * Name of the library. + */ + std::string name; + + /** + * Handle to the loaded library. + */ +#if defined(WIN32) || defined (_WINDOWS) + HINSTANCE hDLL; +#else + void* hDLL; +#endif + + /** + * Load the library. + */ + void load(); + + /** + * Unload the library, if successfully loaded. + */ + void unload(); + + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_util_library_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/LogWriter.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/LogWriter.cpp new file mode 100644 index 0000000000..e551d04eda --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/LogWriter.cpp @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include "tuscany/sca/util/LogWriter.h" + +namespace tuscany +{ + namespace sca + { + namespace util + { + LogWriter::~LogWriter() + { + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/LogWriter.h b/cpp/sca/runtime/core/src/tuscany/sca/util/LogWriter.h new file mode 100644 index 0000000000..5a6a5c38f7 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/LogWriter.h @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_logwriter_h +#define tuscany_sca_util_logwriter_h + +#include "tuscany/sca/export.h" + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * Abstract class for extending logging to other destinations. + */ + class SCA_API LogWriter + { + public: + virtual ~LogWriter(); + + /** + * Log a message. + * @param level The level of logging for this message. + * @param tid The current thread id. + * @param msg The message to log. + */ + virtual void log(int level, const char* tid, const char* msg) = 0; + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany +#endif // tuscany_sca_util_logwriter_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Logger.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/Logger.cpp new file mode 100644 index 0000000000..5745982f42 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Logger.cpp @@ -0,0 +1,276 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <iostream> +#include <stdarg.h> + +#if defined(WIN32) || defined (_WINDOWS) +#include <windows.h> +#include <process.h> +#else +#include <unistd.h> +#include <pthread.h> +#endif + +#include "tuscany/sca/util/Logger.h" +#include "tuscany/sca/util/DefaultLogWriter.h" +#include "tuscany/sca/util/FileLogWriter.h" + +using namespace std; + +namespace tuscany +{ + namespace sca + { + namespace util + { + LogWriter* Logger::logWriter = getLogWriter(); + + LogWriter* Logger::getLogWriter() + { + if (logWriter == 0) + { + setLogWriter(0); + + } + return logWriter; + } + + void Logger::setLogWriter(LogWriter* writer) + { + if (logWriter != writer + && logWriter != 0) + { + delete logWriter; + } + + if (writer == 0) + { + char* loggingVar = 0; + loggingVar = getenv("TUSCANY_SCACPP_LOG"); + if (loggingVar == 0) + logWriter = new DefaultLogWriter; + else + logWriter = new FileLogWriter(loggingVar); + } + else + { + logWriter = writer; + } + } + + int Logger::loggingLevel = setLogging(); + + int Logger::setLogging() + { + char* loggingVar = 0; + loggingVar = getenv("TUSCANY_SCACPP_LOGGING"); + if (loggingVar == 0) + return 0; + else + return atoi(loggingVar); + } + + void Logger::setLogging(int level) + { + loggingLevel = level; + } + + void Logger::log(int level, const char* msg) + { + if (level <= loggingLevel) + { + char tid[21]; + formatThreadID(tid); + logWriter->log(level, tid, msg); + } + } + + void Logger::formatThreadID(char* tid) + { +#if defined(WIN32) || defined (_WINDOWS) + sprintf(tid, "%lu:%lu", (unsigned long)_getpid(), (unsigned long)GetCurrentThreadId()); +#else + sprintf(tid, "%lu:%lu", (unsigned long)getpid(), (unsigned long)pthread_self()); +#endif + } + + void Logger::logArgs(int level, const char* msg, ...) + { + if (level <= loggingLevel) + { + va_list variableArguments; + va_start(variableArguments, msg); + char tid[21]; + formatThreadID(tid); + char messageBuffer[256]; +#if defined(WIN32) || defined (_WINDOWS) + int size = _vsnprintf(messageBuffer, 255, msg, variableArguments); + if (size == -1) + { + size = _vscprintf(msg, variableArguments); + char* bigMessageBuffer = new char[size+1]; + _vsnprintf(bigMessageBuffer, size, msg, variableArguments); + bigMessageBuffer[size] = '\0'; + logWriter->log(level, tid, bigMessageBuffer); + delete bigMessageBuffer; + } +#else + int size = vsnprintf(messageBuffer, 255, msg, variableArguments); + if (size > 255) + { + char* bigMessageBuffer = new char[size+1]; + vsnprintf(bigMessageBuffer, size, msg, variableArguments); + bigMessageBuffer[size] = '\0'; + logWriter->log(level, tid, bigMessageBuffer); + delete bigMessageBuffer; + } +#endif + else + { + messageBuffer[255] = '\0'; + logWriter->log(level, tid, messageBuffer); + } + va_end(variableArguments); + } + } + + void Logger::logArgs0(const char* msg, ...) + { + if (0 <= loggingLevel) + { + va_list variableArguments; + va_start(variableArguments, msg); + char tid[21]; + formatThreadID(tid); + char messageBuffer[256]; +#if defined(WIN32) || defined (_WINDOWS) + int size = _vsnprintf(messageBuffer, 255, msg, variableArguments); + if (size == -1) + { + size = _vscprintf(msg, variableArguments); + char* bigMessageBuffer = new char[size+1]; + _vsnprintf(bigMessageBuffer, size, msg, variableArguments); + bigMessageBuffer[size] = '\0'; + logWriter->log(0, tid, bigMessageBuffer); + delete bigMessageBuffer; + } +#else + int size = vsnprintf(messageBuffer, 255, msg, variableArguments); + if (size > 255) + { + char* bigMessageBuffer = new char[size+1]; + vsnprintf(bigMessageBuffer, size, msg, variableArguments); + bigMessageBuffer[size] = '\0'; + logWriter->log(0, tid, bigMessageBuffer); + delete bigMessageBuffer; + } +#endif + else + { + messageBuffer[255] = '\0'; + logWriter->log(0, tid, messageBuffer); + } + va_end(variableArguments); + } + } + void Logger::logArgs1(const char* msg, ...) + { + if (1 <= loggingLevel) + { + va_list variableArguments; + va_start(variableArguments, msg); + char tid[21]; + formatThreadID(tid); + char messageBuffer[256]; +#if defined(WIN32) || defined (_WINDOWS) + int size = _vsnprintf(messageBuffer, 255, msg, variableArguments); + if (size == -1) + { + size = _vscprintf(msg, variableArguments); + char* bigMessageBuffer = new char[size+1]; + _vsnprintf(bigMessageBuffer, size, msg, variableArguments); + bigMessageBuffer[size] = '\0'; + logWriter->log(1, tid, bigMessageBuffer); + delete bigMessageBuffer; + } +#else + int size = vsnprintf(messageBuffer, 255, msg, variableArguments); + if (size > 255) + { + char* bigMessageBuffer = new char[size+1]; + vsnprintf(bigMessageBuffer, size, msg, variableArguments); + bigMessageBuffer[size] = '\0'; + logWriter->log(1, tid, bigMessageBuffer); + delete bigMessageBuffer; + } +#endif + else + { + messageBuffer[255] = '\0'; + logWriter->log(1, tid, messageBuffer); + } + va_end(variableArguments); + } + } + void Logger::logArgs2(const char* msg, ...) + { + if (2 <= loggingLevel) + { + va_list variableArguments; + va_start(variableArguments, msg); + char tid[21]; + formatThreadID(tid); + char messageBuffer[256]; +#if defined(WIN32) || defined (_WINDOWS) + int size = _vsnprintf(messageBuffer, 255, msg, variableArguments); + if (size == -1) + { + size = _vscprintf(msg, variableArguments); + char* bigMessageBuffer = new char[size+1]; + _vsnprintf(bigMessageBuffer, size, msg, variableArguments); + bigMessageBuffer[size] = '\0'; + logWriter->log(2, tid, bigMessageBuffer); + delete bigMessageBuffer; + } +#else + int size = vsnprintf(messageBuffer, 255, msg, variableArguments); + if (size > 255) + { + char* bigMessageBuffer = new char[size+1]; + vsnprintf(bigMessageBuffer, size, msg, variableArguments); + bigMessageBuffer[size] = '\0'; + logWriter->log(2, tid, bigMessageBuffer); + delete bigMessageBuffer; + } +#endif + else + { + messageBuffer[255] = '\0'; + logWriter->log(2, tid, messageBuffer); + } + va_end(variableArguments); + } + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Logger.h b/cpp/sca/runtime/core/src/tuscany/sca/util/Logger.h new file mode 100644 index 0000000000..eccf3cf02f --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Logger.h @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_logger_h +#define tuscany_sca_util_logger_h + +#include "tuscany/sca/export.h" +#include "tuscany/sca/util/LogWriter.h" + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * Provide a logging interface. + */ + class Logger { + + public: + /** + * Set the log writer to use. + * @param writer The writer to use for all subsequent logging. + */ + SCA_API static void setLogWriter(LogWriter* writer); + + /** + * Set or reset the logging level. Any message with a higher logging + * level than this value will be filtered (i.e. not shown). + * @param level The level of logging to use for all subsequent logging. + */ + SCA_API static void setLogging(int level); + + /** + * Log a message. + * @param level The log level of this message. + * @param msg The message to be logged. + */ + SCA_API static void log(int level, const char* msg); + + /** + * Log a message with variable arguments. + * @param level The log level of this message. + * @param msg The message to be logged. Must include template + * characters as described in printf. + * @param ... Variable arguments. + */ + SCA_API static void logArgs(int level, const char* msg, ...); + SCA_API static void logArgs0(const char* msg, ...); + SCA_API static void logArgs1(const char* msg, ...); + SCA_API static void logArgs2(const char* msg, ...); + + /** + * The currently set logging level + */ + SCA_API static int loggingLevel; + + private: + /** + * The current log writer. + */ + static LogWriter* logWriter; + + /** + * Get the current log writer. + * @return The current log writer. + */ + static LogWriter* getLogWriter(); + + /** + * Retrieves the logging level set as an environment variable. + */ + static int setLogging(); + + /** + * Print the current thread id into the given char buffer. + */ + static void formatThreadID(char* tid); + + /** + * Message buffer + */ + static char messageBuffer[4096]; + + }; + + class LogEntry + { + public: + + LogEntry(const char* func) + : funcName(func) + { + if (Logger::loggingLevel >= 2) + { + Logger::logArgs(2, ">> %s", funcName); + } + } + + ~LogEntry() + { + if (Logger::loggingLevel >= 2) + { + Logger::logArgs(2, "<< %s", funcName); + } + } + + private: + const char *funcName; + + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_util_logger_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Logging.h b/cpp/sca/runtime/core/src/tuscany/sca/util/Logging.h new file mode 100644 index 0000000000..bf1419faa8 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Logging.h @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_logging_h +#define tuscany_sca_util_logging_h + +#include "tuscany/sca/util/Logger.h" + +#if defined(WIN32) || defined (_WINDOWS) +#define logentry() \ +const tuscany::sca::util::LogEntry __LOGENTRY__(__FUNCTION__) +#else +#define logentry() \ +const tuscany::sca::util::LogEntry __LOGENTRY__(__PRETTY_FUNCTION__) +#endif + +#define loginfo \ +if (tuscany::sca::util::Logger::loggingLevel >= 2) tuscany::sca::util::Logger::logArgs2 + +#define logwarning \ +if (tuscany::sca::util::Logger::loggingLevel >= 1) tuscany::sca::util::Logger::logArgs1 + +#define logerror \ +if (tuscany::sca::util::Logger::loggingLevel >= 0) tuscany::sca::util::Logger::logArgs0 + +#endif // tuscany_sca_util_logging_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Mutex.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/Mutex.cpp new file mode 100644 index 0000000000..7b62000717 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Mutex.cpp @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <errno.h> + +#include <sstream> + +#include "tuscany/sca/util/Mutex.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/core/Exceptions.h" + +using namespace std; + + +namespace tuscany +{ + namespace sca + { + namespace util + { + Mutex::Mutex() + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + InitializeCriticalSection(§ion); +#else + int rc = pthread_mutex_init(&mutex, NULL); + if (rc) + { + ostringstream msg; + msg << "Failed to create mutex, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#endif + } + + Mutex::~Mutex() + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + DeleteCriticalSection(§ion); +#else + int rc = pthread_mutex_destroy(&mutex); + if (rc) { + ostringstream msg; + msg << "Failed to destroy mutex, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#endif + } + + void Mutex::lock() + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + EnterCriticalSection(§ion); +#else + int rc = pthread_mutex_lock(&mutex); + if (rc) { + ostringstream msg; + msg << "Failed to lock mutex, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#endif + } + + void Mutex::unlock() + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + LeaveCriticalSection(§ion); +#else + int rc = pthread_mutex_unlock(&mutex); + if (rc) { + ostringstream msg; + msg << "Failed to unlock mutex, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#endif + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Mutex.h b/cpp/sca/runtime/core/src/tuscany/sca/util/Mutex.h new file mode 100644 index 0000000000..d509ba6d73 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Mutex.h @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_mutex_h +#define tuscany_sca_util_mutex_h + +#if defined(WIN32) || defined (_WINDOWS) +#include <windows.h> +#else +#include <pthread.h> +#endif + +#include "tuscany/sca/export.h" + + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * Information about shared libraries and methods to + * access these shared libraries. + */ + class SCA_API Mutex + { + public: + /** + * Constructor. + */ + Mutex(); + + /** + * Destructor. + */ + virtual ~Mutex(); + + /** + * Lock the mutex. + */ + void lock(); + + /** + * Unlock the mutex. + */ + void unlock(); + + private: + + /** + * Handle to the mutex. + */ +#if defined(WIN32) || defined (_WINDOWS) + CRITICAL_SECTION section; +#else + pthread_mutex_t mutex; +#endif + + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_util_mutex_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.cpp new file mode 100644 index 0000000000..c124be572c --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.cpp @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev: 491752 $ $Date: 2007-01-01 22:22:23 -0800 (Mon, 01 Jan 2007) $ */ + +#if defined(WIN32) || defined (_WINDOWS) +#pragma warning(disable: 4786) +#pragma warning(disable: 4251) +#endif + +#include <errno.h> + +#include <sstream> + +#include "tuscany/sca/util/Queue.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/core/Exceptions.h" + +using namespace std; + + +namespace tuscany +{ + namespace sca + { + namespace util + { + Queue::Queue() + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + InitializeCriticalSection(§ion); + hevent = CreateEvent (NULL, TRUE, FALSE, NULL); +#else + int rc = pthread_mutex_init(&mutex, NULL); + if (rc) + { + ostringstream msg; + msg << "Failed to create mutex, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } + + rc = pthread_cond_init(&cond, NULL); + if (rc) + { + ostringstream msg; + msg << "Failed to create condition variable, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#endif + } + + Queue::~Queue() + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + DeleteCriticalSection(§ion); + CloseHandle(hevent); +#else + int rc = pthread_mutex_destroy(&mutex); + if (rc) { + ostringstream msg; + msg << "Failed to destroy mutex, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } + + rc = pthread_cond_destroy(&cond); + if (rc) { + ostringstream msg; + msg << "Failed to destroy condition variable, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#endif + } + + void Queue::enqueue(void* element) + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + EnterCriticalSection(§ion); + + try + { + queue.push(element); + + if (queue.size() == 1) + { + SetEvent(hevent); + } + } + catch (...) + { + LeaveCriticalSection(§ion); + throw; + } + LeaveCriticalSection(§ion); +#else + int rc = pthread_mutex_lock(&mutex); + if (rc) { + ostringstream msg; + msg << "Failed to lock mutex, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } + try + { + queue.push(element); + + if (queue.size() == 1) + { + rc = pthread_cond_signal(&cond); + if (rc) { + ostringstream msg; + msg << "Failed to broadcast condition variable, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } + } + + } + catch(...) + { + pthread_mutex_unlock(&mutex); + throw; + } + rc = pthread_mutex_unlock(&mutex); + if (rc) { + ostringstream msg; + msg << "Failed to unlock mutex, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#endif + } + + void* Queue::dequeue() + { + logentry(); + + void* element = NULL; + +#if defined(WIN32) || defined (_WINDOWS) + + EnterCriticalSection(§ion); + try + { + while (queue.size() == 0) { + LeaveCriticalSection(§ion); + WaitForSingleObject(hevent, INFINITE); + EnterCriticalSection(§ion); + } + + element = queue.front(); + queue.pop(); + + if (queue.size() == 0) + { + ResetEvent(hevent); + } + } + catch(...) + { + LeaveCriticalSection(§ion); + throw; + } + LeaveCriticalSection(§ion); + +#else + int rc = pthread_mutex_lock(&mutex); + if (rc) { + ostringstream msg; + msg << "Failed to lock mutex, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } + try + { + while (queue.size() == 0) { + rc = pthread_cond_wait(&cond, &mutex); + if (rc) { + ostringstream msg; + msg << "Failed to wait for condition variable, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } + } + + element = queue.front(); + queue.pop(); + } + catch(...) + { + pthread_mutex_unlock(&mutex); + throw; + } + rc = pthread_mutex_unlock(&mutex); + if (rc) { + ostringstream msg; + msg << "Failed to unlock mutex, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } + +#endif + + return element; + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.h b/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.h new file mode 100644 index 0000000000..e2ff104631 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.h @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev: 492011 $ $Date: 2007-01-02 18:15:11 -0800 (Tue, 02 Jan 2007) $ */ + +#ifndef tuscany_sca_util_queue_h +#define tuscany_sca_util_queue_h + +#if defined(WIN32) || defined (_WINDOWS) +#include <windows.h> +#else +#include <pthread.h> +#endif + +#include <queue> + +#include "tuscany/sca/export.h" + + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * A thread safe FIFO queue. + */ + class SCA_API Queue + { + public: + /** + * Constructor. + */ + Queue(); + + /** + * Destructor. + */ + virtual ~Queue(); + + /** + * Dequeue an element + */ + void* dequeue(); + + /** + * Enqueue an element + */ + void enqueue(void* element); + + private: + + /** + * The STL queue used to hold elements. + */ + std::queue<void*> queue; + + /** + * Handles to the mutex and condition variable + * used to synchronize access to the queue. + */ +#if defined(WIN32) || defined (_WINDOWS) + CRITICAL_SECTION section; + HANDLE hevent; +#else + pthread_mutex_t mutex; + pthread_cond_t cond; +#endif + + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_util_queue_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/SDOUtils.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/SDOUtils.cpp new file mode 100644 index 0000000000..513409db6a --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/SDOUtils.cpp @@ -0,0 +1,438 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#if defined(WIN32) || defined (_WINDOWS) +#pragma warning(disable: 4786) +#endif + +#include "tuscany/sca/util/SDOUtils.h" + +using namespace std; +using namespace commonj::sdo; + +namespace tuscany +{ + namespace sca + { + namespace util + { + + /** + * Constructor + */ + SDOVisitor::SDOVisitor() + { + } + + /** + * Destructor + */ + SDOVisitor::~SDOVisitor() + { + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, bool boolData) + { + return SDOVisitor::RESULT_NOT_SUPPORTED; + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, char byteData) + { + return SDOVisitor::RESULT_NOT_SUPPORTED; + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, const char* bytesData) + { + return SDOVisitor::RESULT_NOT_SUPPORTED; + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, wchar_t charData) + { + return SDOVisitor::RESULT_NOT_SUPPORTED; + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, const SDODate& dateData) + { + return SDOVisitor::RESULT_NOT_SUPPORTED; + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, long double doubleData) + { + return SDOVisitor::RESULT_NOT_SUPPORTED; + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, float floatData) + { + return SDOVisitor::RESULT_NOT_SUPPORTED; + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, long intData) + { + return SDOVisitor::RESULT_NOT_SUPPORTED; + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, short shortData) + { + return SDOVisitor::RESULT_NOT_SUPPORTED; + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, const string& stringData) + { + return SDOVisitor::RESULT_CONTINUE; + } + + SDOVisitor::RESULT SDOVisitor::visit(DataObjectPtr parent, const Property& property, DataObjectPtr dataObjectData) + { + return SDOVisitor::RESULT_CONTINUE; + } + + const char* SDOUtils::sdoURI = "commonj.sdo"; + + SDOVisitor::RESULT SDOUtils::accept(DataObjectPtr dataObject, SDOVisitor& visitor, SDOVisitor::DEPTH depth) + { + SDOVisitor::RESULT result = SDOVisitor::RESULT_CONTINUE; + + const Type& type = dataObject->getType(); + if (type.isSequencedType()) + { + SequencePtr sequence = dataObject->getSequence(); + int size = sequence->size(); + for (int i = 0; i < size; i++) + { + if (sequence->isText(i)) + { + const string text(sequence->getCStringValue(i)); + result = visitor.visit(dataObject, *(Property*)NULL, (const string&)text); + } + else + { + const Property& seqProperty = sequence->getProperty(i); + const Type& seqType = seqProperty.getType(); + if (seqType.isDataObjectType()) + { + DataObjectPtr dob; + if (seqProperty.isMany()) + { + int index = sequence->getListIndex(i); + dob = dataObject->getList(seqProperty)[index]; + } + else + { + dob = dataObject->getDataObject(seqProperty); + } + + if (dob) + { + result = visitor.visit(dataObject, seqProperty, (DataObjectPtr)dob); + } + } + else + { + Type::Types t = seqProperty.getTypeEnum(); + switch (t) + { + case Type::BooleanType: + { + result = visitor.visit(dataObject, seqProperty, (bool)sequence->getBooleanValue(i)); + } + break; + case Type::ByteType: + { + result = visitor.visit(dataObject, seqProperty, (char)sequence->getByteValue(i)); + } + break; + case Type::CharacterType: + { + result = visitor.visit(dataObject, seqProperty, (wchar_t)sequence->getCharacterValue(i)); + } + break; + case Type::IntType: + { + result = visitor.visit(dataObject, seqProperty, (long)sequence->getIntValue(i)); + } + break; + case Type::ShortType: + { + result = visitor.visit(dataObject, seqProperty, (short)sequence->getShortValue(i)); + } + break; + case Type::DoubleType: + { + result = visitor.visit(dataObject, seqProperty, (long double)sequence->getDoubleValue(i)); + } + break; + case Type::FloatType: + { + result = visitor.visit(dataObject, seqProperty, (float)sequence->getFloatValue(i)); + } + break; + case Type::LongType: + { + result = visitor.visit(dataObject, seqProperty, (long)sequence->getLongValue(i)); + } + break; + case Type::DateType: + { + const SDODate date = sequence->getDateValue(i); + result = visitor.visit(dataObject, seqProperty, (long)(const time_t)date.getTime()); + } + break; + case Type::BigDecimalType: + case Type::BigIntegerType: + case Type::UriType: + case Type::StringType: + { + ; + const string stringData(sequence->getCStringValue(i)); + result = visitor.visit(dataObject, seqProperty, (const string&)stringData); + } + break; + case Type::BytesType: + { + unsigned int len = sequence->getLength(i); + char* byteData = new char[len]; + sequence->getBytesValue(i, byteData, len); + result = visitor.visit(dataObject, seqProperty, byteData); + delete byteData; + } + break; + default: + result = SDOVisitor::RESULT_NOT_SUPPORTED; + break; + } + if (result == SDOVisitor::RESULT_NOT_SUPPORTED) + { + ; + const string stringData(sequence->getCStringValue(i)); + result = visitor.visit(dataObject, seqProperty, (const string&)stringData); + } + } + } + if (result == SDOVisitor::RESULT_STOP) + return result; + } + } + else + { + PropertyList pl = dataObject->getInstanceProperties(); + for (unsigned int i = 0; i < pl.size(); i++) + { + if (dataObject->isSet(pl[i])) + { + if (pl[i].getType().isDataObjectType()) + { + if (pl[i].isMany()) + { + DataObjectList& doList = dataObject->getList(pl[i]); + for (unsigned int li = 0; li < doList.size(); li++) + { + DataObjectPtr dob = doList[li]; + result = visitor.visit(dataObject, pl[i], (DataObjectPtr)dob); + if (result == SDOVisitor::RESULT_STOP) + return result; + } + } + else + { + DataObjectPtr dob = dataObject->getDataObject(pl[i]); + result = visitor.visit(dataObject, pl[i], (DataObjectPtr)dob); + } + } + else + { + if (pl[i].isMany()) + { + Type::Types t = pl[i].getTypeEnum(); + DataObjectList& doList = dataObject->getList(pl[i]); + for (unsigned int li = 0; li < doList.size(); li++) + { + switch (t) + { + case Type::BooleanType: + { + result = visitor.visit(dataObject, pl[i], (bool)doList.getBoolean(li)); + } + break; + case Type::ByteType: + { + result = visitor.visit(dataObject, pl[i], (char)doList.getByte(li)); + } + break; + case Type::CharacterType: + { + result = visitor.visit(dataObject, pl[i], (wchar_t)doList.getCharacter(li)); + } + break; + case Type::IntType: + { + result = visitor.visit(dataObject, pl[i], (long)doList.getInt(li)); + } + break; + case Type::ShortType: + { + result = visitor.visit(dataObject, pl[i], (short)doList.getShort(li)); + } + break; + case Type::DoubleType: + { + result = visitor.visit(dataObject, pl[i], (long double)doList.getDouble(li)); + } + break; + case Type::FloatType: + { + result = visitor.visit(dataObject, pl[i], (float)doList.getFloat(li)); + } + break; + case Type::LongType: + { + result = visitor.visit(dataObject, pl[i], (long)doList.getLong(li)); + } + break; + case Type::DateType: + { + const SDODate date = doList.getDate(li); + result = visitor.visit(dataObject, pl[i], (long)(const time_t)date.getTime()); + } + break; + case Type::BigDecimalType: + case Type::BigIntegerType: + case Type::UriType: + case Type::StringType: + { + const string stringData(doList.getCString(li)); + result = visitor.visit(dataObject, pl[i], (const string&)stringData); + } + break; + case Type::BytesType: + { + unsigned int len = doList.getLength(li); + char* byteData = new char[len]; + doList.getBytes(li, byteData, len); + result = visitor.visit(dataObject, pl[i], byteData); + delete byteData; + } + break; + default: + result = SDOVisitor::RESULT_NOT_SUPPORTED; + break; + } + if (result == SDOVisitor::RESULT_NOT_SUPPORTED) + { + const string stringData(doList.getCString(li)); + result = visitor.visit(dataObject, pl[i], (const string&)stringData); + } + if (result == SDOVisitor::RESULT_STOP) + return result; + } + } + else + { + Type::Types t = pl[i].getTypeEnum(); + switch (t) + { + case Type::BooleanType: + { + result = visitor.visit(dataObject, pl[i], (bool)dataObject->getBoolean(pl[i])); + } + break; + case Type::ByteType: + { + result = visitor.visit(dataObject, pl[i], (char)dataObject->getByte(pl[i])); + } + break; + case Type::CharacterType: + { + result = visitor.visit(dataObject, pl[i], (wchar_t)dataObject->getCharacter(pl[i])); + } + break; + case Type::IntType: + { + result = visitor.visit(dataObject, pl[i], (long)dataObject->getInt(pl[i])); + } + break; + case Type::ShortType: + { + result = visitor.visit(dataObject, pl[i], (short)dataObject->getShort(pl[i])); + } + break; + case Type::DoubleType: + { + result = visitor.visit(dataObject, pl[i], (long double)dataObject->getDouble(pl[i])); + } + break; + case Type::FloatType: + { + result = visitor.visit(dataObject, pl[i], (float)dataObject->getFloat(pl[i])); + } + break; + case Type::LongType: + { + result = visitor.visit(dataObject, pl[i], (long)dataObject->getLong(pl[i])); + } + break; + case Type::DateType: + { + const SDODate date = dataObject->getDate(pl[i]); + result = visitor.visit(dataObject, pl[i], (long)(const time_t)date.getTime()); + } + break; + case Type::BigDecimalType: + case Type::BigIntegerType: + case Type::UriType: + case Type::StringType: + { + const string stringData(dataObject->getCString(pl[i])); + result = visitor.visit(dataObject, pl[i], (const string&)stringData); + } + break; + case Type::BytesType: + { + unsigned int len = dataObject->getLength(pl[i]); + char* byteData = new char[len]; + dataObject->getBytes(pl[i], byteData, len); + result = visitor.visit(dataObject, pl[i], byteData); + delete byteData; + } + break; + default: + result = SDOVisitor::RESULT_NOT_SUPPORTED; + break; + } + if (result == SDOVisitor::RESULT_NOT_SUPPORTED) + { + const string stringData(dataObject->getCString(pl[i])); + result = visitor.visit(dataObject, pl[i], (const string&)stringData); + } + } + } + } + if (result == SDOVisitor::RESULT_STOP) + return result; + } + } + + return SDOVisitor::RESULT_CONTINUE; + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/SDOUtils.h b/cpp/sca/runtime/core/src/tuscany/sca/util/SDOUtils.h new file mode 100644 index 0000000000..5be65c355a --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/SDOUtils.h @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_sdovisitor_h +#define tuscany_sca_util_sdovisitor_h + +#include <string> +#include <vector> +#include <time.h> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * An SDO visitor interface + */ + class SCA_API SDOVisitor + { + public: + /** + * Constructor. + */ + SDOVisitor(); + + /** + * Destructor. + */ + virtual ~SDOVisitor(); + + /** + * Visit depth + */ + enum DEPTH + { + DEPTH_ZERO = 0, + DEPTH_ONE, + DEPTH_INFINITE + }; + + /** + * Visit status + */ + enum RESULT + { + RESULT_CONTINUE = 0, + RESULT_STOP, + RESULT_NOT_SUPPORTED + }; + + /** + * Visit an SDO. + */ + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, bool boolData); + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, char byteData); + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, const char* bytesData); + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, wchar_t charData); + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, const commonj::sdo::SDODate& dateData); + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, long double doubleData); + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, float floatData); + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, long intData); + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, short shortData); + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, const std::string& stringData); + virtual RESULT visit(commonj::sdo::DataObjectPtr parent, const commonj::sdo::Property& property, commonj::sdo::DataObjectPtr dataObjectData); + }; + + /** + * SDO utilities. + */ + class SCA_API SDOUtils + { + public: + + static SDOVisitor::RESULT accept(commonj::sdo::DataObjectPtr dataObject, SDOVisitor& visitor, SDOVisitor::DEPTH depth); + static const char* sdoURI; + + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany +#endif // tuscany_sca_sdovisitor_file_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.cpp new file mode 100644 index 0000000000..064a91409b --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.cpp @@ -0,0 +1,124 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+/* $Rev: 491752 $ $Date: 2007-01-01 22:22:23 -0800 (Mon, 01 Jan 2007) $ */
+
+#if defined(WIN32) || defined (_WINDOWS)
+#pragma warning(disable: 4786)
+#include <process.h>
+#endif
+
+#include <errno.h>
+
+#include <sstream>
+
+#include "tuscany/sca/util/Thread.h"
+#include "tuscany/sca/util/Utils.h"
+#include "tuscany/sca/util/Logging.h"
+#include "tuscany/sca/core/Exceptions.h"
+
+using namespace std;
+
+
+namespace tuscany
+{
+ namespace sca
+ {
+ namespace util
+ {
+
+#if defined(WIN32) || defined (_WINDOWS)
+ unsigned int __stdcall runThread(void *args)
+ {
+ Thread* thread = (Thread*)args;
+ thread->run();
+ return 0;
+ }
+#else
+ void* runThread(void* args)
+ {
+ Thread* thread = (Thread*)args;
+ thread->run();
+ return NULL;
+ }
+#endif
+
+ Thread::Thread()
+#if defined(WIN32) || defined (_WINDOWS)
+ : hthread(0)
+#else
+#endif
+ {
+ logentry();
+ }
+
+ Thread::~Thread()
+ {
+ logentry();
+#if defined(WIN32) || defined (_WINDOWS)
+ if (hthread != 0)
+ {
+ CloseHandle(hthread);
+ }
+#else
+#endif
+ }
+
+ void Thread::start()
+ {
+ logentry();
+#if defined(WIN32) || defined (_WINDOWS)
+ hthread = (HANDLE)_beginthreadex(NULL, 0, runThread, this, 0, NULL);
+ if (hthread == 0)
+ {
+ ostringstream msg;
+ msg << "Failed to create thread, errno: " << __doserrno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+#else
+ int rc =pthread_create(&thread, NULL, runThread, this);
+ if (rc)
+ {
+ ostringstream msg;
+ msg << "Failed to create thread, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+#endif
+ }
+
+ void Thread::join()
+ {
+ logentry();
+
+#if defined(WIN32) || defined (_WINDOWS)
+ WaitForSingleObject(hthread, INFINITE);
+#else
+ int rc =pthread_join(thread, NULL);
+ if (rc)
+ {
+ ostringstream msg;
+ msg << "Failed to join thread, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+#endif
+ }
+
+ } // End namespace util
+ } // End namespace sca
+} // End namespace tuscany
diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.h b/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.h new file mode 100644 index 0000000000..2ca9142994 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.h @@ -0,0 +1,90 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+/* $Rev: 492011 $ $Date: 2007-01-02 18:15:11 -0800 (Tue, 02 Jan 2007) $ */
+
+#ifndef tuscany_sca_util_thread_h
+#define tuscany_sca_util_thread_h
+
+#if defined(WIN32) || defined (_WINDOWS)
+#include <windows.h>
+#else
+#include <pthread.h>
+#endif
+
+#include <queue>
+
+#include "tuscany/sca/export.h"
+
+
+namespace tuscany
+{
+ namespace sca
+ {
+ namespace util
+ {
+ /**
+ * A portable wrapper for native threads.
+ */
+ class SCA_API Thread
+ {
+ public:
+ /**
+ * Constructor.
+ */
+ Thread();
+
+ /**
+ * Destructor.
+ */
+ virtual ~Thread();
+
+ /**
+ * The method that will be run in the thread.
+ */
+ virtual void run() = 0;
+
+ /**
+ * Start the thread.
+ */
+ void start();
+
+ /**
+ * Join the thread.
+ */
+ void join();
+
+ private:
+
+ /**
+ * Native thread handle.
+ */
+#if defined(WIN32) || defined (_WINDOWS)
+ HANDLE hthread;
+#else
+ pthread_t thread;
+#endif
+
+ };
+
+ } // End namespace util
+ } // End namespace sca
+} // End namespace tuscany
+
+#endif // tuscany_sca_util_thread_h
diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/ThreadLocal.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/ThreadLocal.cpp new file mode 100644 index 0000000000..07c99b3cb1 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/ThreadLocal.cpp @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#include <errno.h> + +#include <sstream> + +#include "tuscany/sca/util/ThreadLocal.h" +#include "tuscany/sca/util/Utils.h" +#include "tuscany/sca/util/Logging.h" +#include "tuscany/sca/core/Exceptions.h" + +using namespace std; + + +namespace tuscany +{ + namespace sca + { + namespace util + { + ThreadLocal::ThreadLocal() + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + index = TlsAlloc(); + if (index == TLS_OUT_OF_INDEXES) + { + ostringstream msg; + msg << "Failed to create thread local index, error: " << GetLastError(); + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#else + int rc = pthread_key_create(&key, NULL); + if (rc) + { + ostringstream msg; + msg << "Failed to create thread local key, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#endif + } + + ThreadLocal::~ThreadLocal() + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + if (!TlsFree(index)) + { + ostringstream msg; + msg << "Failed to destroy thread local index, error: " << GetLastError(); + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#else + int rc = pthread_key_delete(key); + if (rc) { + ostringstream msg; + msg << "Failed to destroy thread local key, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#endif + } + + void ThreadLocal::setValue(void* value) + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + if (!TlsSetValue(index, value)) + { + ostringstream msg; + msg << "Failed to set thread local value, error: " << GetLastError(); + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#else + int rc = pthread_setspecific(key, value); + if (rc) { + ostringstream msg; + msg << "Failed to set thread local value, errno: " << errno; + throwException(TuscanyRuntimeException, msg.str().c_str()); + } +#endif + } + + void* ThreadLocal::getValue() const + { + logentry(); +#if defined(WIN32) || defined (_WINDOWS) + return TlsGetValue(index); +#else + return pthread_getspecific(key); +#endif + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/ThreadLocal.h b/cpp/sca/runtime/core/src/tuscany/sca/util/ThreadLocal.h new file mode 100644 index 0000000000..556055f183 --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/ThreadLocal.h @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_threadlocal_h +#define tuscany_sca_util_threadlocal_h + +#if defined(WIN32) || defined (_WINDOWS) +#include <windows.h> +#else +#include <pthread.h> +#endif + +#include "tuscany/sca/export.h" + + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * Information about shared libraries and methods to + * access these shared libraries. + */ + class SCA_API ThreadLocal + { + public: + /** + * Constructor. + */ + ThreadLocal(); + + /** + * Destructor. + */ + virtual ~ThreadLocal(); + + /** + * Set the ThreadLocal value. + */ + void setValue(void *value); + + /** + * Get the ThreadLocal value. + */ + void* getValue() const; + + private: + + /** + * Handle to the thread local key. + */ +#if defined(WIN32) || defined (_WINDOWS) + DWORD index; +#else + pthread_key_t key; +#endif + + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + +#endif // tuscany_sca_util_threadlocal_h diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Utils.cpp b/cpp/sca/runtime/core/src/tuscany/sca/util/Utils.cpp new file mode 100644 index 0000000000..98a929392d --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Utils.cpp @@ -0,0 +1,598 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#if defined(WIN32) || defined (_WINDOWS) +#pragma warning(disable: 4786) +#endif + +#if defined(WIN32) || defined (_WINDOWS) +#include <windows.h> +#else +#include <errno.h> +#endif + +#include "tuscany/sca/util/Utils.h" + +using namespace std; +using namespace commonj::sdo; + +namespace tuscany +{ + namespace sca + { + namespace util + { + void Utils::tokeniseUri(const string& uri, string& token1, string& token2) + { + tokeniseString("/", uri, token1, token2); + } + + void Utils::tokeniseQName(const string& qname, string& uri, string& name) + { + tokeniseString("#", qname, uri, name); + if (name == "") + { + name = uri; + uri = ""; + } + } + + void Utils::tokeniseString( + const string& separator, + const string& str, + string& token1, + string& token2) + { + string::size_type sep = str.find(separator); + if (sep != string::npos) + { + int l = separator.length(); + token1 = str.substr(0, sep); + if ( (sep+l) < str.length()) + { + token2 = str.substr(sep+l); + } + else + { + token2 = ""; + } + } + else + { + token1 = str; + token2 = ""; + } + } + + void Utils::rTokeniseString( + const string& separator, + const string& str, + string& token1, + string& token2) + { + string::size_type sep = str.rfind(separator); + if (sep != string::npos) + { + int l = separator.length(); + token1 = str.substr(0, sep); + if ( (sep+l) < str.length()) + { + token2 = str.substr(sep+l); + } + else + { + token2 = ""; + } + } + else + { + token1 = ""; + token2 = str; + } + } + + void Utils::breakpoint() { + // dummy method used to set breakpoints + } + + ////////////////////////////////////////////////////////////////////////// + // Print a DatObject tree + ////////////////////////////////////////////////////////////////////////// + void Utils::tabs(int inc) + { + for (int ind=0; ind <inc; ind++) + { + cout << " "; + } + } + + const bool Utils::compareProperties(DataObjectPtr dataObject1, const Property& prop1, DataObjectPtr dataObject2, const Property& prop2, string& diff) + { + if(strcmp(prop1.getName(),prop2.getName()) != 0) + { + diff.append("Differing names for Properties:\n"); + diff.append(prop1.getName()); + diff.append("\n"); + diff.append(prop2.getName()); + return false; + } + + const Type& propertyType1 = prop1.getType(); + const Type& propertyType2 = prop2.getType(); + + if(strcmp(propertyType1.getName(), propertyType2.getName()) != 0 || + strcmp(propertyType1.getURI(),propertyType2.getURI()) != 0 ) + { + diff.append("Differing types for Properties:\n"); + diff.append(propertyType1.getName()); + diff.append("#"); + diff.append(propertyType1.getURI()); + diff.append("\n"); + diff.append(propertyType2.getName()); + diff.append("#"); + diff.append(propertyType2.getURI()); + return false; + } + if (dataObject1->isSet(prop1) != dataObject2->isSet(prop2)) + { + diff.append("Property "); + diff.append(prop1.getName()); + diff.append(" is set on one DataObject but not the other"); + return false; + } + + if (dataObject1->isSet(prop1)) + { + + if (prop1.isMany() != prop2.isMany()) + { + diff.append("Property "); + diff.append(prop1.getName()); + diff.append(" is many on one DataObject but not the other"); + return false; + } + if (propertyType1.isDataType() != propertyType2.isDataType()) + { + diff.append("Property "); + diff.append(prop1.getName()); + diff.append(" is dataType on one DataObject but not the other"); + return false; + } + + ////////////////////////////////////////////////////////////////////// + // For a many-valued property get the list of values + ////////////////////////////////////////////////////////////////////// + if (prop1.isMany()) + { + DataObjectList& dol1 = dataObject1->getList(prop1); + DataObjectList& dol2 = dataObject2->getList(prop2); + if (dol1.size() != dol2.size()) + { + diff.append("Property "); + diff.append(prop1.getName()); + diff.append(" is many but has differing number of elements"); + return false; + } + + for (unsigned int j = 0; j <dol1.size(); j++) + { + + if (propertyType1.isDataType()) + { + if( strcmp(dol1.getCString(j), dol2.getCString(j)) != 0) + { + diff.append("Differing value for Property "); + diff.append(prop1.getName()); + diff.append("["); + diff += ((int)j); + diff.append("]:\n"); + diff.append(dol1.getCString(j)); + diff.append("\n"); + diff.append(dol2.getCString(j)); + return false; + } + } + else + { + if(!compareDataObjects(dol1[j], dol2[j], diff)) + { + return false; + } + } + } + } // end IsMany + + + ////////////////////////////////////////////////////////////////////// + // For a primitive data type compare the values + ////////////////////////////////////////////////////////////////////// + else if (propertyType1.isDataType()) + { + if( strcmp(dataObject1->getCString(prop1), dataObject2->getCString(prop2)) != 0) + { + diff.append("Differing value for Property "); + diff.append(prop1.getName()); + diff.append(":\n"); + diff.append(dataObject1->getCString(prop1)); + diff.append("\n"); + diff.append(dataObject2->getCString(prop2)); + return false; + } + } + + ////////////////////////////////////////////////////////////////////// + // For a dataobject compare the DOs + ////////////////////////////////////////////////////////////////////// + else + { + if(!compareDataObjects(dataObject1->getDataObject(prop1), dataObject2->getDataObject(prop2), diff)) + { + return false; + } + } + } + return true; + } + + const bool Utils::compareDataObjects(DataObjectPtr dataObject1, DataObjectPtr dataObject2, string& diff) + { + if (!dataObject1 || !dataObject2) + { + diff.append("Cannot compare null DataObjects"); + return false; + } + + const Type& dataObject1Type = dataObject1->getType(); + const Type& dataObject2Type = dataObject2->getType(); + + if( strcmp(dataObject1Type.getURI(), dataObject2Type.getURI()) != 0 || + strcmp(dataObject1Type.getName(), dataObject2Type.getName()) != 0 ) + { + diff.append("DataObject Types differ:\n"); + diff.append(dataObject1Type.getURI()); + diff.append("#"); + diff.append(dataObject1Type.getName()); + diff.append("\n"); + diff.append(dataObject2Type.getURI()); + diff.append("#"); + diff.append(dataObject2Type.getName()); + return false; + } + + ////////////////////////////////////////////////////////////////////////// + // Iterate over all the properties + ////////////////////////////////////////////////////////////////////////// + PropertyList pl1 = dataObject1->getInstanceProperties(); + PropertyList pl2 = dataObject2->getInstanceProperties(); + if (pl1.size() != pl2.size()) + { + diff.append("Differing number of properties"); + return false; + } + + if (pl1.size() != 0) + { + for (unsigned int i = 0; i < pl1.size(); i++) + { + if(!compareProperties(dataObject1, pl1[i], dataObject2, pl2[i], diff)) + { + return false; + } + } + } + else + { + if(dataObject1->getType().isOpenType() != dataObject2->getType().isOpenType() && + dataObject1->getType().isDataObjectType() != dataObject2->getType().isDataObjectType()) + { + diff.append("DataObject is open & DO type on one but not the other"); + return false; + } + + // Compare elements under an open DataObject + if(dataObject1->getType().isOpenType() && dataObject1->getType().isDataObjectType()) + { + SequencePtr sequence1 = dataObject1->getSequence(); + SequencePtr sequence2 = dataObject2->getSequence(); + + if (sequence1 != NULL && sequence2 != NULL) + { + if (sequence1->size() != sequence1->size()) + { + diff.append("Open DataObjects have differing number of elements"); + return false; + } + + for (unsigned int i = 0; i < sequence1->size(); i++) + { + if (sequence1->isText(i) != sequence2->isText(i)) + { + diff.append("Open DataObjects have differing element types at position "); + diff += ((int) i); + return false; + } + if (sequence1->isText(i)) + { + if( strcmp(sequence1->getCStringValue(i), sequence2->getCStringValue(i)) != 0) + { + diff.append("Differing value for element at position "); + diff += ((int) i); + diff.append(":\n"); + diff.append(sequence1->getCStringValue(i)); + diff.append("\n"); + diff.append(sequence2->getCStringValue(i)); + return false; + } + } + else + { + const Property& p1 = sequence1->getProperty(i); + const Property& p2 = sequence2->getProperty(i); + + if(!compareProperties(dataObject1, p1, dataObject2, p2, diff)) + { + return false; + } + } + } + } + } + } + + return true; + } + + void Utils::printDO(DataObjectPtr dataObject, int increment) + { + int inc=increment; + if (!dataObject) + return; + const Type& dataObjectType = dataObject->getType(); + tabs(inc); + cout << "DataObject type: " << dataObjectType.getURI()<< "#" << dataObjectType.getName() << endl; + inc++; + + ////////////////////////////////////////////////////////////////////////// + // Iterate over all the properties + ////////////////////////////////////////////////////////////////////////// + PropertyList pl = dataObject->getInstanceProperties(); + if (pl.size() != 0) + { + for (unsigned int i = 0; i < pl.size(); i++) + { + tabs(inc); + cout << "Property: " << pl[i].getName() << endl; + + const Type& propertyType = pl[i].getType(); + + tabs(inc); + cout << "Property Type: " << propertyType.getURI()<< "#" << propertyType.getName() << endl; + + if (dataObject->isSet(pl[i])) + { + + ////////////////////////////////////////////////////////////////////// + // For a many-valued property get the list of values + ////////////////////////////////////////////////////////////////////// + if (pl[i].isMany()) + { + inc++; + DataObjectList& dol = dataObject->getList(pl[i]); + for (unsigned int j = 0; j <dol.size(); j++) + { + tabs(inc); + cout << "Value " << j <<endl; + inc++; + + if (propertyType.isDataType()) + { + tabs(inc); + cout<< "Property Value: " << dol.getCString(j) <<endl ; + } + else + printDO(dol[j], inc); + inc--; + } + inc--; + } // end IsMany + + + ////////////////////////////////////////////////////////////////////// + // For a primitive data type print the value + ////////////////////////////////////////////////////////////////////// + else if (propertyType.isDataType()) + { + tabs(inc); + cout<< "Property Value: " << dataObject->getCString(pl[i]) <<endl ; + } + + ////////////////////////////////////////////////////////////////////// + // For a dataobject print the do + ////////////////////////////////////////////////////////////////////// + else + { + inc++; + printDO(dataObject->getDataObject(pl[i]), inc); + inc--; + } + } + else + { + tabs(inc); + cout<< "Property Value: not set" <<endl ; + } + + } + } + else + { + // Print elements under an open DataObject + if(dataObject->getType().isOpenType() && dataObject->getType().isDataObjectType()) + { + SequencePtr sequence = dataObject->getSequence(); + if (sequence != NULL) + { + for (unsigned int i = 0; i < sequence->size(); i++) + { + if (sequence->isText(i)) + { + tabs(inc); + cout<< "Text Value: " << sequence->getCStringValue(i) <<endl ; + } + else { + const Property& p = sequence->getProperty(i); + + tabs(inc); + cout << "Property: " << p.getName() << endl; + + const Type& propertyType = p.getType(); + + tabs(inc); + cout << "Property Type: " << propertyType.getURI()<< "#" << propertyType.getName() << endl; + + if (dataObject->isSet(p)) + { + + ////////////////////////////////////////////////////////////////////// + // For a many-valued property get the list of values + ////////////////////////////////////////////////////////////////////// + if (p.isMany()) + { + inc++; + DataObjectList& dol = dataObject->getList(p); + for (unsigned int j = 0; j <dol.size(); j++) + { + tabs(inc); + cout << "Value " << j <<endl; + inc++; + + if (propertyType.isDataType()) + { + tabs(inc); + cout<< "Property Value: " << dol.getCString(j) <<endl ; + } + else + printDO(dol[j], inc); + inc--; + } + inc--; + } // end IsMany + + + ////////////////////////////////////////////////////////////////////// + // For a primitive data type print the value + ////////////////////////////////////////////////////////////////////// + else if (propertyType.isDataType()) + { + tabs(inc); + cout<< "Property Value: " << dataObject->getCString(p) <<endl ; + } + + ////////////////////////////////////////////////////////////////////// + // For a dataobject print the do + ////////////////////////////////////////////////////////////////////// + else + { + inc++; + printDO(dataObject->getDataObject(p), inc); + inc--; + } + } + else + { + tabs(inc); + cout<< "Property Value: not set" <<endl ; + } + } + } + } + } + } + inc--; + } + + void Utils::printTypes(DataFactoryPtr df) + { + ////////////////////////////////////////////////////////////////////////// + // Retrieve the DataFactory from the mediator + // get the list of Types in the DataFactory and list them + ////////////////////////////////////////////////////////////////////////// + TypeList tl = df->getTypes(); + for (unsigned int i = 0; i < tl.size(); i++) + { + cout << "Type: " << tl[i].getURI()<< "#" << tl[i].getName() << endl; + PropertyList pl = tl[i].getProperties(); + for (unsigned int j = 0; j < pl.size(); j++) + { + cout << "\tProperty: " << pl[j].getName() + << " type: " <<pl[j].getType().getURI()<<"#"<<pl[j].getType().getName()<< endl; + + } + } + + } + + void Utils::printType(const Type& type, int increment) + { + int inc = increment; + tabs(inc); + cout << "Type: " << type.getURI()<< "#" << type.getName() << endl; + inc++; + PropertyList pl = type.getProperties(); + for (unsigned int j = 0; j < pl.size(); j++) + { + tabs(inc); + cout << "\tProperty: " << pl[j].getName() + << " type: " <<pl[j].getType().getURI()<<"#"<<pl[j].getType().getName()<< endl; + inc++; + printType(pl[j].getType(), inc); + inc--; + } + } + + void Utils::printLastError(ostream& os) + { +#if defined(WIN32) || defined (_WINDOWS) + DWORD err = GetLastError(); + LPTSTR buf; + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &buf, + 0, NULL ); + + os << (const char *)buf; + LocalFree(buf); +#else + char buf[256]; + strerror_r(errno, buf, 255); + os << buf; +#endif + } + + } // End namespace util + } // End namespace sca +} // End namespace tuscany + diff --git a/cpp/sca/runtime/core/src/tuscany/sca/util/Utils.h b/cpp/sca/runtime/core/src/tuscany/sca/util/Utils.h new file mode 100644 index 0000000000..bbe1aa812a --- /dev/null +++ b/cpp/sca/runtime/core/src/tuscany/sca/util/Utils.h @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_sca_util_utils_h +#define tuscany_sca_util_utils_h + +#include <string> +#include <ostream> + +#include "commonj/sdo/SDO.h" + +#include "tuscany/sca/export.h" + +namespace tuscany +{ + namespace sca + { + namespace util + { + /** + * Utility methods to parse strings and provide debugging information. + */ + class SCA_API Utils { + + public: + static void tokeniseUri(const std::string& uri, std::string& token1, std::string& token2); + static void tokeniseQName(const std::string& sdoname, std::string& uri, std::string& name); + static void tokeniseString( + const std::string& separator, + const std::string& str, + std::string& token1, + std::string& token2); + + static void rTokeniseString( + const std::string& separator, + const std::string& str, + std::string& token1, + std::string& token2); + + static void breakpoint(); + + static void printLastError(std::ostream& os); + + static void printDO(commonj::sdo::DataObjectPtr dataObject, int increment=0); + static void printTypes(commonj::sdo::DataFactoryPtr df); + static void printType(const commonj::sdo::Type& type, int increment=0); + + static const bool compareDataObjects(commonj::sdo::DataObjectPtr dataObject1, commonj::sdo::DataObjectPtr dataObject2, std::string& diff); + static const bool compareProperties(commonj::sdo::DataObjectPtr dataObject1, const commonj::sdo::Property& prop1, commonj::sdo::DataObjectPtr dataObject2, const commonj::sdo::Property& prop2, std::string& diff); + + private: + static void tabs(int increment=0); + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany +#endif // tuscany_sca_util_utils_h |