diff options
Diffstat (limited to 'tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util')
25 files changed, 3591 insertions, 0 deletions
diff --git a/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/DefaultLogWriter.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/DefaultLogWriter.cpp new file mode 100644 index 0000000000..48cc0aa1e6 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/DefaultLogWriter.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/DefaultLogWriter.h new file mode 100644 index 0000000000..80e07d696c --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/File.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/File.cpp new file mode 100644 index 0000000000..507c1b38ea --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/File.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/File.h new file mode 100644 index 0000000000..944a192cf6 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/FileLogWriter.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/FileLogWriter.cpp new file mode 100644 index 0000000000..56eb3146dd --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/FileLogWriter.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/FileLogWriter.h new file mode 100644 index 0000000000..5925fdca85 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Library.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Library.cpp new file mode 100644 index 0000000000..366fcdf1f3 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Library.cpp @@ -0,0 +1,181 @@ +/* + * 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) +#else +#include "tuscany_sca_config.h" +#endif + +#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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Library.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Library.h new file mode 100644 index 0000000000..6b0a17d2b7 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/LogWriter.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/LogWriter.cpp new file mode 100644 index 0000000000..e551d04eda --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/LogWriter.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/LogWriter.h new file mode 100644 index 0000000000..5a6a5c38f7 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Logger.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Logger.cpp new file mode 100644 index 0000000000..9d55c5fb43 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Logger.cpp @@ -0,0 +1,272 @@ +/* + * 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]; + int size = vsnprintf(messageBuffer, 255, msg, variableArguments); +#if defined(WIN32) || defined (_WINDOWS) + 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 + 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]; + int size = vsnprintf(messageBuffer, 255, msg, variableArguments); +#if defined(WIN32) || defined (_WINDOWS) + 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 + 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]; + int size = vsnprintf(messageBuffer, 255, msg, variableArguments); +#if defined(WIN32) || defined (_WINDOWS) + 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 + 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]; + int size = vsnprintf(messageBuffer, 255, msg, variableArguments); +#if defined(WIN32) || defined (_WINDOWS) + 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 + 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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Logger.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Logger.h new file mode 100644 index 0000000000..eccf3cf02f --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Logging.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Logging.h new file mode 100644 index 0000000000..bf1419faa8 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Mutex.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Mutex.cpp new file mode 100644 index 0000000000..8a9494d7de --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Mutex.cpp @@ -0,0 +1,109 @@ +/* + * 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) +#else +#include "tuscany_sca_config.h" +#endif + +#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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Mutex.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Mutex.h new file mode 100644 index 0000000000..d509ba6d73 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Queue.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Queue.cpp new file mode 100644 index 0000000000..099e3cece8 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Queue.cpp @@ -0,0 +1,224 @@ +/* + * 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) +#else +#include "tuscany_sca_config.h" +#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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Queue.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Queue.h new file mode 100644 index 0000000000..e2ff104631 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/SDOUtils.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/SDOUtils.cpp new file mode 100644 index 0000000000..f2ccda9d69 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/SDOUtils.cpp @@ -0,0 +1,436 @@ +/* + * 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; + } + + 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()) + { + Sequence* 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::IntegerType: + { + result = visitor.visit(dataObject, seqProperty, (long)sequence->getIntegerValue(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, (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::IntegerType: + { + result = visitor.visit(dataObject, pl[i], (long)doList.getInteger(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], (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::IntegerType: + { + result = visitor.visit(dataObject, pl[i], (long)dataObject->getInteger(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], (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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/SDOUtils.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/SDOUtils.h new file mode 100644 index 0000000000..8b5b3b8130 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/SDOUtils.h @@ -0,0 +1,105 @@ +/* + * 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); + + }; + + } // End namespace util + } // End namespace sca +} // End namespace tuscany +#endif // tuscany_sca_sdovisitor_file_h diff --git a/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Thread.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Thread.cpp new file mode 100644 index 0000000000..d70ce28190 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Thread.cpp @@ -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: 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>
+#else
+#include "tuscany_sca_config.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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Thread.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Thread.h new file mode 100644 index 0000000000..2ca9142994 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/ThreadLocal.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/ThreadLocal.cpp new file mode 100644 index 0000000000..33aa1f7605 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/ThreadLocal.cpp @@ -0,0 +1,120 @@ +/* + * 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) +#else +#include "tuscany_sca_config.h" +#endif + +#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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/ThreadLocal.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/ThreadLocal.h new file mode 100644 index 0000000000..556055f183 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Utils.cpp b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Utils.cpp new file mode 100644 index 0000000000..98a929392d --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Utils.h b/tags/native-sca-1.0.incubating-M3-RC1/runtime/core/src/tuscany/sca/util/Utils.h new file mode 100644 index 0000000000..bbe1aa812a --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/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 |