本文Teamcenter SOA使用C++參考SOA的例子進行編寫,以下程式碼為登入Teamcenter,程式碼工程在Teamcenter四層環境下執行。
SOA的庫檔案、樣例檔案、幫助檔案在Teamcenter的安裝包中可以找到。
執行以下bat檔案開啟vs後再開啟工程,不然偵錯執行會報錯:
1 cd /d %~dp0 2 set UGII_UGMGR_COMMUNICATION=HTTP 3 set UGII_UGMGR_HTTP_URL=http://plmdev/tc 4 set TPR=D:\PLM\Siemens\Teamcenter12\portal 5 set UGII_BASE_DIR=D:\PLM\Siemens\NX 6 set TC_ROOT=D:\PLM\Siemens\Teamcenter12 7 set FMS_HOME=D:\PLM\Siemens\Teamcenter12\tccs 8 9 set JAVA_HOME=%TC_JRE_HOME% 10 set JRE_HOME=%TC_JRE_HOME% 11 12 set PATH=%JAVA_HOME%\bin;%FME_HOME%\bin;%FMS_HOME%\lib;%TPR%;%UGII_BASE_DIR%\NXBIN;%UGII_BASE_DIR%\UGII;%PATH%; 13 14 cd "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE" 15 C: 16 devenv.exe
以下為工程程式碼:
1 //tcnx_projectexe.h 2 #pragma once 3 // Mandatory UF Includes 4 #include <uf.h> 5 #include <uf_object_types.h> 6 #include <uf_ugmgr.h> 7 #include <uf_part.h> 8 #include <uf_disp.h> 9 #include <uf_modl.h> 10 #include <uf_obj.h> 11 #include <uf_assem.h> 12 13 // Internal+External Includes 14 #include <NXOpen/Annotations.hxx> 15 #include <NXOpen/Assemblies_Component.hxx> 16 #include <NXOpen/Assemblies_ComponentAssembly.hxx> 17 #include <NXOpen/Body.hxx> 18 #include <NXOpen/BodyCollection.hxx> 19 #include <NXOpen/Face.hxx> 20 #include <NXOpen/Line.hxx> 21 #include <NXOpen/NXException.hxx> 22 #include <NXOpen/NXObject.hxx> 23 #include <NXOpen/Part.hxx> 24 #include <NXOpen/PartCollection.hxx> 25 #include <NXOpen/Session.hxx> 26 27 #include <NXOpen/PDM_PdmSession.hxx> 28 #include <NXOpen/PDM_SoaConnectionHandle.hxx> 29 #include <teamcenter/soa/client/Connection.hxx> 30 #include <teamcenter/services/core/DatamanagementService.hxx> 31 #include <teamcenter/soa/common/Version.hxx> 32 33 // check mate 34 #include <NXOpen/Validate_ValidationManager.hxx> 35 #include <NXOpen/Validate_Validator.hxx> 36 #include <NXOpen/Validate_ValidatorOptions.hxx> 37 #include <NXOpen/Validate_Parser.hxx> 38 39 // Std C++ Includes 40 #include <iostream> 41 #include <sstream> 42 #include <stdio.h> 43 #include <stdlib.h> 44 45 using namespace NXOpen; 46 using namespace Teamcenter::Soa::Client; 47 using namespace Teamcenter::Services::Core; 48 using std::string; 49 using std::exception; 50 using std::stringstream; 51 using std::endl; 52 using std::cout; 53 using std::cerr; 54 55 #define MAX_UGMGR_NAME_LEN 1024 56 #define CREATION_DATE 1 57 #define MODIFICATION_DATE 2 58 static int indent_level = 0; 59 60 #define CHECK( func_ ) \ 61 ifail = (func_); \ 62 if (ifail != 0) {\ 63 printf("ERROR: %s returned %d", # func_, ifail); \ 64 return ifail;} 65 66 #define PRINT( content_ ) \ 67 { int ii; \ 68 for (ii = 0; ii < indent_level; ii++) \ 69 { printf(" "); } \ 70 printf content_; \ 71 printf("\n"); }
1 //tcnx_projectexe.cpp 2 #include "tcnx_projectexe.h" 3 #include "teamcenter/clientx/AppXSession.hxx" 4 #include "teamcenter/hello/HomeFolder.hxx" 5 #include <teamcenter/soa/client/model/User.hxx> 6 #include <teamcenter/soa/client/RuntimeException.hxx> 7 8 using namespace Teamcenter::ClientX; 9 using namespace Teamcenter::Services::Core; 10 using namespace Teamcenter::Soa::Common; 11 using namespace Teamcenter::Soa::Client; 12 using namespace Teamcenter::Soa::Client::Model; 13 using namespace Teamcenter::Schemas::Soa::_2006_03::Exceptions; 14 using namespace Teamcenter::Hello; 15 16 17 //=================== 18 // Entry Point 19 //=================== 20 #ifdef WIN32 21 int _tmain(int argc, _TCHAR* argv[]) 22 #else 23 int main(int argc, char* argv[]) 24 #endif 25 { 26 try{ 27 28 #ifdef _UFUGMGR 29 int _errCode = 0; 30 const char** consolePara = (const char**)(argv); 31 logical is_active; 32 _errCode = UF_is_ugmanager_active(&is_active);// 判斷ugmanager環境是否已經初始化 33 if (!is_active) 34 _errCode = UF_UGMGR_initialize(argc, consolePara);// 初始化ugmanager環境 35 36 //do_it(); 37 //_errCode = invokePdmServer(); 38 39 _errCode = UF_UGMGR_terminate(); 40 return _errCode; 41 #else 42 if (argc > 1){ 43 if (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "-h") == 0){ 44 cout << "usage: Hello [-host http://server:port/tc] " << endl; 45 return 0; 46 } 47 } 48 49 std::string serverHost = "http://plmdev:7001/tc"; 50 if (argc > 2 && strcmp(argv[1], "-host") == 0) 51 serverHost = argv[2]; 52 53 string _username, _password; 54 std::vector<std::string> _tempStr; 55 for (int idx = 0; idx < argc; idx++){ 56 _tempStr.push_back(argv[idx]); 57 if (string(argv[idx]).find("-u=") != std::string::npos){ 58 _username = string(argv[idx]).substr(string(argv[idx]).find_first_of("=") + 1); 59 } 60 else if (string(argv[idx]).find("-p=") != std::string::npos){ 61 _password = string(argv[idx]).substr(string(argv[idx]).find_first_of("=") + 1); 62 } 63 } 64 65 AppXSession session(serverHost, _username, _password); 66 HomeFolder home; 67 68 try{ 69 User* user = session.login(); 70 if (!user) 71 return 0; 72 73 home.listHomeFolder(user); 74 75 session.logout(); 76 } 77 catch (Teamcenter::Soa::Client::RuntimeException& e){ 78 cout << e.getMessage() << endl; 79 cout << "The application will terminate." << endl; 80 } 81 return 0; 82 #endif 83 84 } 85 catch (const NXException& e1){ 86 cerr << "NXException: " << e1.ErrorCode() << endl; 87 cerr << e1.Message() << endl; 88 } 89 catch (const exception& e2){ 90 cerr << "Exception: " << e2.what() << endl; 91 } 92 catch (...){ 93 cerr << "Unknown Exception: " << endl; 94 } 95 }
1 //AppXSession.hpp 2 #ifndef TEAMCENTER_CLIENTX_SESSION_HXX 3 #define TEAMCENTER_CLIENTX_SESSION_HXX 4 5 #include <string> 6 #include <vector> 7 #include <map> 8 9 #include "AppXCredentialManager.hxx" 10 #include "AppXExceptionHandler.hxx" 11 #include "AppXPartialErrorListener.hxx" 12 #include "AppXModelEventListener.hxx" 13 #include "AppXRequestListener.hxx" 14 #include "AppXMemoryAllocator.hxx" 15 16 #include <teamcenter/soa/client/Connection.hxx> 17 #include <teamcenter/soa/client/model/User.hxx> 18 19 20 namespace Teamcenter 21 { 22 namespace ClientX 23 { 24 class AppXSession; 25 26 class AppXSession 27 { 28 public: 29 30 AppXSession(const std::string& host, std::string& _username, std::string& _password); 31 32 static Teamcenter::Soa::Client::Connection* getConnection(); 33 34 Teamcenter::Soa::Client::Model::User* login(); 35 36 void logout(); 37 38 static void printObjects(const std::vector<Teamcenter::Soa::Client::ModelObject* >& objects); 39 void dumpMemoryInfo(const char* pSectionTitle); 40 41 virtual ~AppXSession(); 42 43 private: 44 45 static void getUsers(const std::vector<Teamcenter::Soa::Client::ModelObject* >& objects); 46 47 static Teamcenter::Soa::Client::Connection* connection; 48 49 static Teamcenter::ClientX::AppXCredentialManager* credentialManager; 50 static Teamcenter::ClientX::AppXExceptionHandler* exceptionHandler; 51 static Teamcenter::ClientX::AppXPartialErrorListener* errorListener; 52 static Teamcenter::ClientX::AppXModelEventListener* modelEventListener; 53 static Teamcenter::ClientX::AppXRequestListener* requestListener; 54 static Teamcenter::ClientX::AppXMemoryAllocator* memoryAllocator; 55 56 FILE* m_pMemLogFile; 57 bool m_bTrackMemory; 58 59 std::string _username; 60 std::string _password; 61 }; 62 } 63 } //end namespace 64 #endif
1 //AppXSession.cpp 2 #include "AppXSession.hxx" 3 4 #include <typeinfo> 5 #include <iostream> 6 #ifdef WIN32 7 #include <tchar.h> 8 #endif 9 #include <teamcenter/services/core/DatamanagementService.hxx> 10 #include <teamcenter/soa/common/DateTime.hxx> 11 #include <teamcenter/soa/common/MemoryManager.hxx> 12 #include <teamcenter/soa/client/RuntimeException.hxx> 13 #include <teamcenter/schemas/soa/_2006_03/exceptions/ServiceException.hxx> 14 #include <teamcenter/soa/client/NotLoadedException.hxx> 15 #include <teamcenter/soa/client/model/WorkspaceObject.hxx> 16 #include <teamcenter/soa/client/model/User.hxx> 17 #include <teamcenter/services/core/SessionService.hxx> 18 19 using namespace std; 20 using namespace Teamcenter::ClientX; 21 using namespace Teamcenter::Services::Core; 22 using namespace Teamcenter::Soa::Common; 23 using namespace Teamcenter::Soa::Client; 24 using namespace Teamcenter::Soa::Client::Model; 25 using namespace Teamcenter::Schemas::Soa::_2006_03::Exceptions; 26 27 Connection* AppXSession::connection = NULL; 28 AppXCredentialManager* AppXSession::credentialManager = NULL; 29 AppXExceptionHandler* AppXSession::exceptionHandler = NULL; 30 AppXPartialErrorListener* AppXSession::errorListener = NULL; 31 AppXModelEventListener* AppXSession::modelEventListener = NULL; 32 AppXRequestListener* AppXSession::requestListener = NULL; 33 AppXMemoryAllocator* AppXSession::memoryAllocator = NULL; 34 35 36 AppXSession::AppXSession(const std::string& host, std::string& _username, std::string& _password) 37 { 38 this->_username = _username; 39 this->_password = _password; 40 41 m_bTrackMemory = false; 42 m_pMemLogFile = NULL; 43 44 memoryAllocator = new AppXMemoryAllocator(); 45 Teamcenter::Soa::Common::MemoryManager::initializeMemoryManager(memoryAllocator, m_bTrackMemory); 46 47 credentialManager = new AppXCredentialManager(_username, _password); 48 exceptionHandler = new AppXExceptionHandler(); 49 errorListener = new AppXPartialErrorListener(); 50 modelEventListener = new AppXModelEventListener(); 51 requestListener = new AppXRequestListener(); 52 53 Connection::Protocol proto; 54 std::string envNameTccs; 55 if (host.find("http") == 0){ 56 proto = Connection::HTTP; 57 } 58 else if (host.find("tccs") == 0){ 59 proto = Connection::TCCS; 60 int envNameStart = host.find('/') + 2; 61 envNameTccs = (host.substr(envNameStart, host.size() - envNameStart)); 62 } 63 else{ 64 proto = Connection::IIOP; 65 } 66 67 connection = new Connection(host, credentialManager, proto, Connection::REST, false); 68 if (proto == Connection::TCCS) 69 connection->setOption(Teamcenter::Soa::Client::Connection::TCCS_ENV_NAME, envNameTccs); 70 71 connection->setExceptionHandler(exceptionHandler); 72 connection->getModelManager()->addPartialErrorListener(errorListener); 73 connection->getModelManager()->addModelEventListener(modelEventListener); 74 Connection::addRequestListener(requestListener); 75 } 76 77 void AppXSession::dumpMemoryInfo(const char* pSectionTitle) 78 { 79 if (m_pMemLogFile) 80 { 81 MemoryInfo memInfo; 82 Teamcenter::Soa::Common::MemoryManager::getMemoryManager()->getSoaMemoryInfo(memInfo); 83 84 fprintf(m_pMemLogFile, "\n\n============================================="); 85 fprintf(m_pMemLogFile, "\n%s", pSectionTitle); 86 fprintf(m_pMemLogFile, "\n============================================="); 87 fprintf(m_pMemLogFile, "\nCurrent SOA CDM memory size : %f KB", memInfo.currentAllocSize); 88 fprintf(m_pMemLogFile, "\nPeak CDM memory size : %f KB", memInfo.PeakMemSize); 89 fprintf(m_pMemLogFile, "\nLargest Memory request : %u bytes", memInfo.LargestMemSizeRequested); 90 } 91 } 92 93 Connection* AppXSession::getConnection() 94 { 95 return connection; 96 } 97 98 User* AppXSession::login() 99 { 100 SessionService* sessionService = SessionService::getService(connection); 101 try{ 102 CredentialManager::Credentials credentials = credentialManager->promptForCredentials(); 103 while (true) 104 { 105 try{ 106 dumpMemoryInfo("MEMORY SIZE BEFORE LOGIN"); 107 Teamcenter::Services::Core::_2006_03::Session::LoginResponse out = sessionService->login(credentials.username, credentials.securetoken, &credentials.group, &credentials.role, NULL, credentials.descriminator); 108 dumpMemoryInfo("MEMORY SIZE AFTER LOGIN"); 109 return out.user; 110 } 111 catch (InvalidCredentialsException& e){ 112 credentials = credentialManager->getCredentials(e); 113 } 114 } 115 } 116 catch (CanceledOperationException& /*e*/) {} 117 return NULL; 118 } 119 120 AppXSession::~AppXSession() 121 { 122 if (m_pMemLogFile){ 123 fclose(m_pMemLogFile); 124 } 125 } 126 127 void AppXSession::logout() 128 { 129 SessionService* sessionService = SessionService::getService(connection); 130 try 131 { 132 dumpMemoryInfo("MEMORY SIZE BEFORE LOGOUT"); 133 sessionService->logout(); 134 dumpMemoryInfo("MEMORY SIZE AFTER LOGOUT & BEFORE DELETING CONNECTION"); 135 delete connection; 136 delete credentialManager; 137 delete exceptionHandler; 138 delete errorListener; 139 delete modelEventListener; 140 141 if (requestListener){ 142 Connection::removeRequestListener(requestListener); 143 delete requestListener; 144 } 145 146 connection = NULL; 147 credentialManager = NULL; 148 exceptionHandler = NULL; 149 errorListener = NULL; 150 modelEventListener = NULL; 151 requestListener = NULL; 152 dumpMemoryInfo("MEMORY SIZE AFTER DELETING CONNECTION"); 153 Teamcenter::Soa::Common::MemoryManager::getMemoryManager()->dumptofile("./HelloTeamcenterMemDump.txt"); 154 } 155 catch (ServiceException& /*e*/){} 156 } 157 158 void AppXSession::printObjects(const std::vector<ModelObject* >& objects) 159 { 160 getUsers(objects); 161 162 std::string name; 163 std::string modified; 164 std::string ownerName; 165 166 cout << "\nName\t\t\t\tOwner\t\t\t\tLast Modified" << endl; 167 cout << "====\t\t\t\t=====\t\t\t\t=============" << endl; 168 for (size_t i = 0; i < objects.size(); i++) 169 { 170 WorkspaceObject* wo; 171 ModelObject* obj = objects[i]; 172 try{ 173 wo = dynamic_cast<WorkspaceObject*>(obj); 174 } 175 catch (std::bad_cast&){ 176 continue; 177 } 178 179 User* owner = NULL; 180 try{ 181 owner = dynamic_cast<User*>(wo->get_owning_user()); 182 183 DateTime lastModified = wo->get_last_mod_date(); 184 name = wo->get_object_string(); 185 ownerName = owner->get_user_name(); 186 modified = lastModified.toString(); 187 188 cout << name << "\t\t\t" << ownerName << "\t\t\t" << modified << endl; 189 } 190 catch (NotLoadedException& e){ 191 cout << e.getMessage() << endl; 192 cout << "The Object Property Policy ($TC_DATA/soa/policies/Default.xml) is not configured with this property." << endl; 193 } 194 } 195 } 196 197 void AppXSession::getUsers(const std::vector<ModelObject* >& objects) 198 { 199 DatamanagementService* dmService = DatamanagementService::getService(AppXSession::getConnection()); 200 201 std::vector<ModelObject* > unKnownUsers; 202 for (size_t i = 0; i < objects.size(); i++) 203 { 204 WorkspaceObject* wo; 205 try{ 206 wo = dynamic_cast<WorkspaceObject*>(objects[i]); 207 } 208 catch (std::bad_cast&){ 209 continue; 210 } 211 212 User* owner = NULL; 213 try{ 214 owner = dynamic_cast<User*>(wo->get_owning_user()); 215 owner->get_user_name(); 216 } 217 catch (NotLoadedException& /*e*/){ 218 if (owner) 219 unKnownUsers.push_back(dynamic_cast<ModelObject*>(owner)); 220 } 221 } 222 std::vector< std::string> attributes; 223 attributes.push_back("user_name"); 224 dmService->getProperties(unKnownUsers, attributes); 225 }
1 //AppXRequestListener.hpp 2 #ifndef TEAMCENTER_CLIENTX_APPXREQUESTLISTENER_HXX 3 #define TEAMCENTER_CLIENTX_APPXREQUESTLISTENER_HXX 4 5 #include <string> 6 #include <vector> 7 8 #include <teamcenter/soa/client/RequestListener.hxx> 9 10 11 namespace Teamcenter 12 { 13 namespace ClientX 14 { 15 16 class AppXRequestListener : public Teamcenter::Soa::Client::RequestListener 17 { 18 public: 19 20 virtual void serviceRequest(const Teamcenter::Soa::Client::ServiceInfo& info); 21 22 virtual void serviceResponse(const Teamcenter::Soa::Client::ServiceInfo& info); 23 }; 24 } 25 } //end namespace 26 #endif
1 //AppXRequestListener.cpp 2 #include "AppXRequestListener.hxx" 3 #include <iostream> 4 5 using namespace std; 6 using namespace Teamcenter::ClientX; 7 using namespace Teamcenter::Soa::Client; 8 9 void AppXRequestListener::serviceRequest ( const ServiceInfo& /*info*/ ) 10 { 11 // will log the service name when done 12 } 13 14 void AppXRequestListener::serviceResponse( const ServiceInfo& info ) 15 { 16 cout << info.id << ": " << info.service << "." << info.operation << endl; 17 }
1 //AppXPartialErrorListener.hpp 2 #ifndef TEAMCENTER_CLIENTX_SOAPARTIALERRORLISTENER_HXX 3 #define TEAMCENTER_CLIENTX_SOAPARTIALERRORLISTENER_HXX 4 5 #include <string> 6 #include <vector> 7 8 #include <teamcenter/soa/client/ErrorStack.hxx> 9 #include <teamcenter/soa/client/PartialErrorListener.hxx> 10 11 12 namespace Teamcenter 13 { 14 namespace ClientX 15 { 16 class AppXPartialErrorListener; 17 18 class AppXPartialErrorListener : public Teamcenter::Soa::Client::PartialErrorListener 19 { 20 public: 21 22 virtual void handlePartialErrors(const std::vector< Teamcenter::Soa::Client::ErrorStack >& stacks); 23 }; 24 } 25 } //end namespace 26 #endif
1 //AppXPartialErrorListener.cpp 2 #include "AppXPartialErrorListener.hxx" 3 #include <iostream> 4 #include <teamcenter/soa/client/ModelObject.hxx> 5 6 using namespace std; 7 using namespace Teamcenter::ClientX; 8 using namespace Teamcenter::Soa::Client; 9 10 11 void AppXPartialErrorListener::handlePartialErrors(const std::vector< Teamcenter::Soa::Client::ErrorStack >& stacks) 12 { 13 if (stacks.size() == 0) return; 14 15 cout << "" << endl; 16 cout << "*****" << endl; 17 cout << "Partial Errors caught in Teamcenter::ClientX::AppXPartialErrorListener." << endl; 18 19 for (size_t i = 0; i < stacks.size(); i++) 20 { 21 cout << "Partial Error for "; 22 23 if (stacks[i].hasAssociatedObject()){ 24 cout << "object " << stacks[i].getAssociatedObject()->getUid() << endl; 25 } 26 else if (stacks[i].hasClientId()){ 27 cout << "client id " << stacks[i].getClientId() << endl; 28 } 29 else if (stacks[i].hasClientIndex()) 30 cout << "client index " << stacks[i].getClientIndex() << endl; 31 32 for (int j = 0; j < stacks[i].getErrorValueCount(); j++) 33 { 34 ErrorValue error = stacks[i].getErrorValue(j); 35 cout << " Code: " << error.code << "\tSeverity: " << 36 error.level << "\t" << error.message << endl; 37 } 38 } 39 }
1 //AppXModelEventListener.hpp 2 #ifndef TEAMCENTER_CLIENTX_SOAMODELEVENTLISTENER_HXX 3 #define TEAMCENTER_CLIENTX_SOAMODELEVENTLISTENER_HXX 4 5 #include <string> 6 #include <vector> 7 8 #include <teamcenter/soa/client/ModelObject.hxx> 9 #include <teamcenter/soa/client/ModelEventListener.hxx> 10 11 namespace Teamcenter 12 { 13 namespace ClientX 14 { 15 class AppXModelEventListener; 16 17 class AppXModelEventListener : public Teamcenter::Soa::Client::ModelEventListener 18 { 19 public: 20 AppXModelEventListener(); 21 ~AppXModelEventListener(); 22 23 virtual void localObjectChange(const std::vector<Teamcenter::Soa::Client::ModelObject* >& objects); 24 virtual void localObjectDelete(const std::vector< std::string>& uids); 25 }; 26 } 27 } //end namespace 28 #endif
1 //AppXModelEventListener.cpp 2 #include <typeinfo> 3 #include "AppXModelEventListener.hxx" 4 5 #include <iostream> 6 #include <teamcenter/soa/client/NotLoadedException.hxx> 7 #include <teamcenter/soa/client/model/WorkspaceObject.hxx> 8 9 using namespace std; 10 using namespace Teamcenter::ClientX; 11 using namespace Teamcenter::Soa::Client; 12 using namespace Teamcenter::Soa::Client::Model; 13 using namespace Teamcenter::Soa::Common; 14 15 16 AppXModelEventListener::AppXModelEventListener() :ModelEventListener() 17 { 18 } 19 20 AppXModelEventListener::~AppXModelEventListener() 21 { 22 } 23 24 void AppXModelEventListener::localObjectChange(const vector<Teamcenter::Soa::Client::ModelObject* >& objects) 25 { 26 if (objects.size() == 0) return; 27 28 cout << "" << endl; 29 cout << "Modified Objects handled in Teamcenter::ClientX::AppXModelEventListener.localObjectChange" << endl; 30 cout << "The following objects have been updated in the client data model:" << endl; 31 32 for (size_t i = 0; i < objects.size(); i++) 33 { 34 ModelObject* object = objects[i]; 35 std::string uid = object->getUid(); 36 std::string type = object->getType()->getName(); 37 std::string name = ""; 38 39 try 40 { 41 Teamcenter::Soa::Client::Model::WorkspaceObject* wo = dynamic_cast<Teamcenter::Soa::Client::Model::WorkspaceObject*>(object); 42 if (wo) 43 { 44 try{ 45 name = wo->get_object_string(); 46 } 47 catch (NotLoadedException& /*e*/) {} // just ignore 48 } 49 } 50 catch (std::bad_cast&) {} 51 cout << " " << uid << " " << type << " " << name << endl; 52 } 53 } 54 55 void AppXModelEventListener::localObjectDelete(const std::vector< std::string >& uids) 56 { 57 if (uids.size() == 0) 58 return; 59 60 cout << "" << endl; 61 cout << "Deleted Objects handled in com.teamcenter.clientx.AppXModelEventListener.localObjectDelete" << endl; 62 cout << "The following objects have been deleted from the server and removed from the client data model:" << endl; 63 64 for (size_t i = 0; i < uids.size(); i++){ 65 cout << " " + uids[i] << endl; 66 } 67 }
1 //AppXMemoryAllocator.hpp 2 #ifndef TEAMCENTER_CLIENTX_APPXMEMALLOCATORIMPL_HXX 3 #define TEAMCENTER_CLIENTX_APPXMEMALLOCATORIMPL_HXX 4 5 #include <string> 6 #include <exception> 7 #include <list> 8 #include <algorithm> 9 #include <iostream> //for dump() 10 11 #include <teamcenter/soa/common/MemoryAllocator.hxx> 12 13 namespace Teamcenter 14 { 15 namespace ClientX 16 { 17 class AppXMemoryAllocator; 18 19 class AppXMemoryAllocator : public Teamcenter::Soa::Common::MemoryAllocator 20 { 21 public: 22 AppXMemoryAllocator(); 23 24 ~AppXMemoryAllocator(); 25 26 void* allocate(size_t size); 27 28 void deallocate(void *ptr); 29 30 virtual void* placementallocate(size_t size, void* _where); 31 32 virtual void placementdeallocate(void *ptr, void* _where); 33 34 std::string getAllocatorID(); 35 }; 36 } 37 } //end namespace 38 39 #endif //TEAMCENTER_SOA_CLIENT_MEMORYMANAGERIMPL_HXX
1 //AppXMemoryAllocator.cpp 2 #include <string> 3 #ifdef _WIN32 4 #pragma warning(push) 5 #pragma warning(disable:4018) //signed/unsigned mismatch 6 #pragma warning(disable:4290) // exception spec ignored 7 #endif 8 9 #include <exception> 10 #include <list> 11 #include <algorithm> 12 #include <iostream> //for dump() 13 #include <new> 14 15 #include "AppXMemoryAllocator.hxx" 16 #include <teamcenter/soa/common/MemoryAllocator.hxx> 17 18 19 using namespace std; 20 using namespace Teamcenter::Soa::Common; 21 22 23 Teamcenter::ClientX::AppXMemoryAllocator::AppXMemoryAllocator() :MemoryAllocator() 24 { 25 } 26 27 Teamcenter::ClientX::AppXMemoryAllocator::~AppXMemoryAllocator() 28 { 29 } 30 31 void* Teamcenter::ClientX::AppXMemoryAllocator::allocate(size_t size) 32 { 33 void* p = NULL; 34 p = malloc(size); 35 36 return p; 37 } 38 39 void Teamcenter::ClientX::AppXMemoryAllocator::deallocate(void *p) 40 { 41 if (p) 42 free(p); 43 } 44 45 void* Teamcenter::ClientX::AppXMemoryAllocator::placementallocate(size_t, void* _where) 46 { 47 return _where; 48 } 49 50 void Teamcenter::ClientX::AppXMemoryAllocator::placementdeallocate(void*, void*) 51 { 52 } 53 54 std::string Teamcenter::ClientX::AppXMemoryAllocator::getAllocatorID() 55 { 56 return "Teamcenter::Soa::Client::AppXMemoryAllocator";//Think about a better id. 57 }
1 //AppXExceptionHandler.hpp 2 #ifndef TEAMCENTER_CLIENTX_SOAEXCEPTIONHANDLER_HXX 3 #define TEAMCENTER_CLIENTX_SOAEXCEPTIONHANDLER_HXX 4 5 #include <string> 6 7 #include <teamcenter/schemas/soa/_2006_03/exceptions/InternalServerException.hxx> 8 #include <teamcenter/soa/client/ExceptionHandler.hxx> 9 #include <teamcenter/soa/client/CanceledOperationException.hxx> 10 11 12 namespace Teamcenter 13 { 14 namespace ClientX 15 { 16 class AppXExceptionHandler; 17 18 class AppXExceptionHandler : public Teamcenter::Soa::Client::ExceptionHandler 19 { 20 public: 21 22 virtual void handleException(Teamcenter::Schemas::Soa::_2006_03::Exceptions::InternalServerException& ise); 23 virtual void handleException(Teamcenter::Soa::Client::CanceledOperationException& coe); 24 }; 25 } 26 } //end namespace 27 #endif
1 //AppXExceptionHandler.cpp 2 #include "AppXExceptionHandler.hxx" 3 4 #include <iostream> 5 6 #include <teamcenter/soa/client/RuntimeException.hxx> 7 #include <teamcenter/schemas/soa/_2006_03/exceptions/ConnectionException.hxx> 8 #include <teamcenter/schemas/soa/_2006_03/exceptions/ProtocolException.hxx> 9 10 using namespace std; 11 using namespace Teamcenter::ClientX; 12 using namespace Teamcenter::Soa::Client; 13 using namespace Teamcenter::Schemas::Soa::_2006_03::Exceptions; 14 15 16 void AppXExceptionHandler::handleException(InternalServerException& ise) 17 { 18 cout << "" << endl; 19 cout << "*****" << endl; 20 cout << "Exception caught in Teamcenter::ClientX::AppXExceptionHandler.handleException(InternalServerException)." << endl; 21 22 ConnectionException *ce = dynamic_cast<ConnectionException *>(&ise); 23 ProtocolException *pe = dynamic_cast<ProtocolException *>(&ise); 24 25 if (ce) 26 { 27 cout << endl << "The server returned an connection error." << endl << ise.getMessage() << 28 endl << "Do you wish to retry the last service request?[y/n]"; 29 } 30 else if (pe) 31 { 32 cout << endl << "The server returned an protocol error." << endl << ise.getMessage() << 33 endl << "This is most likely the result of a programming error." << 34 endl << "Do you wish to retry the last service request?[y/n]"; 35 } 36 else 37 { 38 cout << endl << "The server returned an internal server error." << endl << ise.getMessage() << 39 endl << "This is most likely the result of a programming error." << 40 endl << "A RuntimeException will be thrown." << endl; 41 throw RuntimeException(RuntimeException::InternalError, ise.getMessage().c_str()); 42 } 43 44 std::string retry; 45 cin >> retry; 46 47 if (retry == "y" || retry == "yes") 48 return; 49 50 throw RuntimeException(RuntimeException::InternalError, "The user has opted not to retry the last request"); 51 } 52 53 void AppXExceptionHandler::handleException(CanceledOperationException& coe) 54 { 55 cout << "" << endl; 56 cout << "*****" << endl; 57 cout << "Exception caught in Teamcenter::ClientX::AppXExceptionHandler.handleException(CanceledOperationException)." << endl; 58 59 throw RuntimeException(RuntimeException::InternalError, coe.getMessage().c_str()); 60 }
1 //AppXCredentialManager.hpp 2 #ifndef TEAMCENTER_CLIENTX_SOACREDENTIALMANAGER_HXX 3 #define TEAMCENTER_CLIENTX_SOACREDENTIALMANAGER_HXX 4 5 #include <string> 6 7 #include <teamcenter/schemas/soa/_2006_03/exceptions/InvalidCredentialsException.hxx> 8 #include <teamcenter/schemas/soa/_2006_03/exceptions/InvalidUserException.hxx> 9 #include <teamcenter/soa/client/CredentialManager.hxx> 10 11 12 namespace Teamcenter 13 { 14 namespace ClientX 15 { 16 class AppXCredentialManager; 17 18 class AppXCredentialManager : public Teamcenter::Soa::Client::CredentialManager 19 { 20 public: 21 22 AppXCredentialManager(std::string& _username, std::string& _password); 23 24 virtual Teamcenter::Soa::Client::CredentialManager::CredentialsType getCredentialType(); 25 26 virtual Teamcenter::Soa::Client::CredentialManager::Credentials getCredentials(Teamcenter::Schemas::Soa::_2006_03::Exceptions::InvalidCredentialsException e); 27 28 virtual Teamcenter::Soa::Client::CredentialManager::Credentials getCredentials(Teamcenter::Schemas::Soa::_2006_03::Exceptions::InvalidUserException e); 29 30 virtual void setGroupRole(const std::string& group, const std::string& role); 31 32 virtual void setUserPassword(const std::string& user, const std::string& password, const std::string& discriminator); 33 Teamcenter::Soa::Client::CredentialManager::Credentials promptForCredentials(); 34 35 private: 36 Teamcenter::Soa::Client::CredentialManager::Credentials credentials; 37 38 private: 39 std::string _username; 40 std::string _password; 41 }; 42 } 43 } //end namespace 44 #endif
1 //AppXCredentialManager.cpp 2 #include "AppXCredentialManager.hxx" 3 #include <iostream> 4 #include <teamcenter/soa/client/CanceledOperationException.hxx> 5 6 using namespace std; 7 using namespace Teamcenter::ClientX; 8 using namespace Teamcenter::Soa::Client; 9 using namespace Teamcenter::Schemas::Soa::_2006_03::Exceptions; 10 11 12 AppXCredentialManager::AppXCredentialManager(std::string& _username, std::string& _password) 13 :credentials("", "", "", "", "SoaAppX") 14 { 15 this->_username = _username; 16 this->_password = _password; 17 } 18 19 CredentialManager::CredentialsType AppXCredentialManager::getCredentialType() 20 { 21 return CredentialManager::USERPASSWORD; 22 } 23 24 CredentialManager::Credentials AppXCredentialManager::getCredentials(InvalidCredentialsException e) 25 { 26 cout << e.getMessage() << endl; 27 return promptForCredentials(); 28 } 29 30 CredentialManager::Credentials AppXCredentialManager::getCredentials(InvalidUserException e) 31 { 32 // Have not logged in yet, shoult not happen but just in case 33 if (credentials.username.length() == 0) 34 return promptForCredentials(); 35 36 // Return cached credentials 37 return credentials; 38 } 39 40 void AppXCredentialManager::setGroupRole(const std::string& group, const std::string& role) 41 { 42 credentials.group = group; 43 credentials.role = role; 44 } 45 46 void AppXCredentialManager::setUserPassword(const std::string& user, const std::string& password, const std::string& discriminator) 47 { 48 credentials.username = user; 49 credentials.securetoken = password; 50 credentials.descriminator = discriminator; 51 } 52 53 CredentialManager::Credentials AppXCredentialManager::promptForCredentials() 54 { 55 credentials.username = _username; 56 credentials.securetoken = _password; 57 return credentials; 58 }
1 //HomeFolder.hpp 2 #ifndef TEAMCENTER_HELLO_HOMEFOLDER_HXX 3 #define TEAMCENTER_HELLO_HOMEFOLDER_HXX 4 5 #include <string> 6 #include <teamcenter/soa/client/model/User.hxx> 7 8 9 namespace Teamcenter 10 { 11 namespace Hello 12 { 13 class HomeFolder; 14 15 class HomeFolder 16 { 17 public: 18 19 void listHomeFolder(Teamcenter::Soa::Client::Model::User* user); 20 }; 21 } 22 } //end namespace 23 #endif
1 //HomeFolder.cpp 2 #include "HomeFolder.hxx" 3 #include "../clientx/AppXSession.hxx" 4 5 #include <teamcenter/services/core/DatamanagementService.hxx> 6 #include <teamcenter/soa/client/ModelObject.hxx> 7 #include <teamcenter/soa/client/model/Folder.hxx> 8 #include <teamcenter/soa/client/NotLoadedException.hxx> 9 10 #include <iostream> 11 #include <vector> 12 13 using namespace std; 14 using namespace Teamcenter::ClientX; 15 using namespace Teamcenter::Hello; 16 using namespace Teamcenter::Services::Core; 17 using namespace Teamcenter::Soa::Client; 18 using namespace Teamcenter::Soa::Common; 19 using namespace Teamcenter::Soa::Client::Model; 20 21 22 void HomeFolder::listHomeFolder(User* user) 23 { 24 Folder* home; 25 vector< ModelObject* > contents; 26 DatamanagementService* dmService = DatamanagementService::getService(AppXSession::getConnection()); 27 28 try 29 { 30 home = user->get_home_folder(); 31 } 32 catch (NotLoadedException& e) 33 { 34 cout << e.getMessage() << endl; 35 cout << "The Object Property Policy ($TC_DATA/soa/policies/Default.xml) is not configured with this property." << endl; 36 return; 37 } 38 39 try 40 { 41 vector < ModelObject* > objects; 42 vector < string > attributes; 43 objects.push_back(dynamic_cast<ModelObject*>(home)); 44 attributes.push_back("contents"); 45 46 dmService->getProperties(objects, attributes); 47 48 for (size_t i = 0; i < home->count_contents(); i++) 49 contents.push_back(dynamic_cast<ModelObject*>(home->get_contents_at(i))); 50 } 51 catch (NotLoadedException& /*e*/){} 52 53 cout << "" << endl; 54 cout << "Home Folder:" << endl; 55 AppXSession::printObjects(contents); 56 }
程式偵錯執行動圖:
黃河遠上白雲間,一片孤城萬仞山。
羌笛何須怨楊柳,春風不度玉門關。
詩人初到涼州,面對黃河、邊城的遼闊景象,又耳聽著《折楊柳》曲,有感而發,寫成了這首表現戍守邊疆的士兵思念家鄉情懷的詩作。
詩的前兩句描繪了西北邊地廣漠壯闊的風光。首句抓住自下(遊)向上(遊)、由近及遠眺望黃河的特殊感受,描繪出「黃河遠上白雲間」的動人畫面:洶湧澎湃波浪滔滔的黃河竟像一條絲帶迤邐飛上雲端。寫得真是神思飛躍,氣象開闊。詩人的另一名句「黃河入海流」,其觀察角度與此正好相反,是自上而下的目送;而李白的「黃河之水天上來」,雖也寫觀望上游,但視線運動卻又由遠及近,與此句不同。「黃河入海流」和「黃河之水天上來」,同是著意渲染黃河一瀉千里的氣派,表現的是動態美。而「黃河遠上白雲間」,方向與河的流向相反,意在突出其源遠流長的閒遠儀態,表現的是一種靜態美。同時展示了邊地廣漠壯闊的風光,不愧為千古奇句。