| 1 | // Created on: 1997-03-06 |
| 2 | // Created by: Mister rmi |
| 3 | // Copyright (c) 1997-1999 Matra Datavision |
| 4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
| 5 | // |
| 6 | // This file is part of Open CASCADE Technology software library. |
| 7 | // |
| 8 | // This library is free software; you can redistribute it and/or modify it under |
| 9 | // the terms of the GNU Lesser General Public License version 2.1 as published |
| 10 | // by the Free Software Foundation, with special exception defined in the file |
| 11 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
| 12 | // distribution for complete text of the license and disclaimer of any warranty. |
| 13 | // |
| 14 | // Alternatively, this file may be used under the terms of Open CASCADE |
| 15 | // commercial license or contractual agreement. |
| 16 | |
| 17 | #include <Plugin.ixx> |
| 18 | #include <Plugin_MapOfFunctions.hxx> |
| 19 | #include <OSD_SharedLibrary.hxx> |
| 20 | #include <Resource_Manager.hxx> |
| 21 | #include <Plugin_Failure.hxx> |
| 22 | |
| 23 | #include <TCollection_AsciiString.hxx> |
| 24 | |
| 25 | static Standard_Character tc[1000]; |
| 26 | static Standard_PCharacter thePluginId = tc; |
| 27 | |
| 28 | |
| 29 | //======================================================================= |
| 30 | //function : Load |
| 31 | //purpose : |
| 32 | //======================================================================= |
| 33 | Handle(Standard_Transient) Plugin::Load (const Standard_GUID& aGUID, |
| 34 | const Standard_Boolean theVerbose) |
| 35 | { |
| 36 | |
| 37 | aGUID.ToCString(thePluginId); |
| 38 | TCollection_AsciiString pid(thePluginId); |
| 39 | static Plugin_MapOfFunctions theMapOfFunctions; |
| 40 | OSD_Function f; |
| 41 | |
| 42 | if(!theMapOfFunctions.IsBound(pid)) { |
| 43 | |
| 44 | Handle(Resource_Manager) PluginResource = new Resource_Manager("Plugin"); |
| 45 | TCollection_AsciiString theResource(thePluginId); |
| 46 | theResource += ".Location"; |
| 47 | |
| 48 | if(!PluginResource->Find(theResource.ToCString())) { |
| 49 | PluginResource = AdditionalPluginMap(); |
| 50 | if (!PluginResource->Find(theResource.ToCString())) { |
| 51 | Standard_SStream aMsg; aMsg << "could not find the resource:"; |
| 52 | aMsg << theResource.ToCString()<< endl; |
| 53 | if (theVerbose) |
| 54 | cout << "could not find the resource:"<<theResource.ToCString()<< endl; |
| 55 | Plugin_Failure::Raise(aMsg); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | TCollection_AsciiString thePluginLibrary(""); |
| 60 | #ifndef WNT |
| 61 | thePluginLibrary += "lib"; |
| 62 | #endif |
| 63 | thePluginLibrary += PluginResource->Value(theResource.ToCString()); |
| 64 | #ifdef WNT |
| 65 | thePluginLibrary += ".dll"; |
| 66 | #elif defined(__APPLE__) |
| 67 | thePluginLibrary += ".dylib"; |
| 68 | #elif defined (HPUX) || defined(_hpux) |
| 69 | thePluginLibrary += ".sl"; |
| 70 | #else |
| 71 | thePluginLibrary += ".so"; |
| 72 | #endif |
| 73 | OSD_SharedLibrary theSharedLibrary(thePluginLibrary.ToCString()); |
| 74 | if(!theSharedLibrary.DlOpen(OSD_RTLD_LAZY)) { |
| 75 | TCollection_AsciiString error(theSharedLibrary.DlError()); |
| 76 | Standard_SStream aMsg; aMsg << "could not open:"; |
| 77 | aMsg << PluginResource->Value(theResource.ToCString()); |
| 78 | aMsg << "; reason:"; |
| 79 | aMsg << error.ToCString(); |
| 80 | if (theVerbose) |
| 81 | cout << "could not open: " << PluginResource->Value(theResource.ToCString())<< " ; reason: "<< error.ToCString() << endl; |
| 82 | Plugin_Failure::Raise(aMsg); |
| 83 | } |
| 84 | f = theSharedLibrary.DlSymb("PLUGINFACTORY"); |
| 85 | if( f == NULL ) { |
| 86 | TCollection_AsciiString error(theSharedLibrary.DlError()); |
| 87 | Standard_SStream aMsg; aMsg << "could not find the factory in:"; |
| 88 | aMsg << PluginResource->Value(theResource.ToCString()); |
| 89 | aMsg << error.ToCString(); |
| 90 | Plugin_Failure::Raise(aMsg); |
| 91 | } |
| 92 | theMapOfFunctions.Bind(pid,f); |
| 93 | } |
| 94 | else |
| 95 | f = theMapOfFunctions(pid); |
| 96 | |
| 97 | Handle(Standard_Transient) (*fp) (const Standard_GUID&) = NULL; |
| 98 | fp = (Handle(Standard_Transient) (*)(const Standard_GUID&)) f; |
| 99 | Handle(Standard_Transient) theServiceFactory = (*fp) (aGUID); |
| 100 | return theServiceFactory; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | const Handle(Resource_Manager)& Plugin::AdditionalPluginMap() |
| 105 | { |
| 106 | static Handle(Resource_Manager) aMap; |
| 107 | if (aMap.IsNull()) |
| 108 | aMap = new Resource_Manager ("" /*theName*/, Standard_False /*theVerbose*/); |
| 109 | return aMap; |
| 110 | } |