plugin_handle_impl.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:13k
- /*
- * ===========================================================================
- * PRODUCTION $Log: plugin_handle_impl.cpp,v $
- * PRODUCTION Revision 1000.5 2004/06/01 20:44:26 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.16
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: plugin_handle_impl.cpp,v 1000.5 2004/06/01 20:44:26 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Mike DiCuccio
- *
- * File Description:
- *
- */
- #include <ncbi_pch.hpp>
- #include <gui/core/plugin.hpp>
- #include <gui/core/plugin_factory.hpp>
- #include <gui/core/plugin_exception.hpp>
- #include <gui/core/plugin_registry.hpp>
- #include <gui/plugin/PluginInfo.hpp>
- #include <gui/plugin/PluginLibInfo.hpp>
- #include <gui/plugin/PluginArgSet.hpp>
- #include <gui/plugin/PluginMessage.hpp>
- #include "plugin_handle_impl.hpp"
- #include <ctype.h>
- #include <corelib/ncbiapp.hpp>
- #include <corelib/ncbienv.hpp>
- #include <corelib/ncbifile.hpp>
- #include <corelib/ncbistr.hpp>
- #define DEFAULT_HELP_BASE
- "http://graceland:6224/projects/IEB-mj-docs/wiki/moin.cgi/GbenchPluginHelp"
- BEGIN_NCBI_SCOPE
- USING_SCOPE(objects);
- CPluginHandle_Impl::CPluginHandle_Impl(const CPluginLibInfo& info)
- : m_Info(&info),
- m_Factory(NULL)
- {
- }
- CPluginHandle_Impl::~CPluginHandle_Impl()
- {
- }
- // retrieve a plugin handle representing this implementation
- CPluginHandle CPluginHandle_Impl::GetHandle(void) const
- {
- return CPluginHandle(const_cast<CPluginHandle_Impl&>(*this));
- }
- //
- //
- //
- // accessor functions
- //
- //
- //
- CPluginCommandSet::E_Choice CPluginHandle_Impl::GetCommand(void) const
- {
- return m_Info->GetInfo().GetCommands().Which();
- }
- bool CPluginHandle_Impl::IsLoaded(void) const
- {
- return m_Factory != NULL;
- }
- bool CPluginHandle_Impl::IsEnabled(void) const
- {
- return m_Info->CanGetEnabled() && m_Info->GetEnabled();
- }
- const CPluginInfo& CPluginHandle_Impl::GetInfo(void) const
- {
- return m_Info->GetInfo();
- }
- const CPluginLibInfo& CPluginHandle_Impl::GetLibInfo(void) const
- {
- return *m_Info;
- }
- const string& CPluginHandle_Impl::GetClassName(void) const
- {
- return m_Info->GetInfo().GetClass_name();
- }
- const string CPluginHandle_Impl::GetHelpFile(void) const
- {
- string s_help_expr = "";
- if (m_Info->GetInfo().CanGetHelp_file()) {
- s_help_expr = m_Info->GetInfo().GetHelp_file();
- } else {
- // Get class name with underscores removed
- NStr::Replace(m_Info->GetInfo().GetClass_name(),
- "_", "", s_help_expr);
- }
- // If the help file is a simple URL, return it.
- if (s_help_expr.find("http://", 0) == 0) {
- return s_help_expr;
- }
- // Look up config
- CNcbiApplication *my_app = CNcbiApplication::Instance();
- _ASSERT(my_app);
- // Non-absolute path implies config lookup
- CNcbiRegistry& registry = my_app->GetConfig();
- // If there's a variable in the config whose name
- // is the expression, use the value of that variable
- string s_help_path =
- registry.GetString("Help", "HELP_BASE", kEmptyStr);
- s_help_path =
- registry.GetString("Help", "HELP_BASE", DEFAULT_HELP_BASE);
- // Help path isn't empty, so turn it into a URL
- if (s_help_path != kEmptyStr) {
- s_help_expr = s_help_path + "/" + s_help_expr;
- }
- return s_help_expr;
- }
- const string& CPluginHandle_Impl::GetLibrary(void) const
- {
- return m_Info->GetLibrary();
- }
- const string& CPluginHandle_Impl::GetMenuItem(void) const
- {
- if (m_Info->GetInfo().CanGetMenu_item()) {
- return m_Info->GetInfo().GetMenu_item();
- }
- static string s_str;
- return s_str;
- }
- const string& CPluginHandle_Impl::GetToolTip(void) const
- {
- if (m_Info->GetInfo().CanGetTooltip()) {
- return m_Info->GetInfo().GetTooltip();
- }
- static string s_str;
- return s_str;
- }
- int CPluginHandle_Impl::GetVerMajor(void) const
- {
- return m_Info->GetInfo().GetVer_major();
- }
- int CPluginHandle_Impl::GetVerMinor(void) const
- {
- return m_Info->GetInfo().GetVer_minor();
- }
- int CPluginHandle_Impl::GetVerRevision(void) const
- {
- return m_Info->GetInfo().GetVer_revision();
- }
- const string& CPluginHandle_Impl::GetVerBuildDate(void) const
- {
- return m_Info->GetInfo().GetVer_build_date();
- }
- CRef<CPluginMessage> CPluginHandle_Impl::CreateMessage(EAlgoCommand cmd)
- {
- CRef<CPluginMessage> msg(new CPluginMessage());
- msg->SetDestination(GetClassName());
- FillDefaults(cmd, msg->SetRequest().SetAlgo());
- return msg;
- }
- CRef<CPluginMessage> CPluginHandle_Impl::CreateMessage(EDataCommand cmd)
- {
- CRef<CPluginMessage> msg(new CPluginMessage());
- msg->SetDestination(GetClassName());
- FillDefaults(cmd, msg->SetRequest().SetData());
- return msg;
- }
- CRef<CPluginMessage> CPluginHandle_Impl::CreateMessage(EViewCommand cmd)
- {
- CRef<CPluginMessage> msg(new CPluginMessage());
- msg->SetDestination(GetClassName());
- FillDefaults(cmd, msg->SetRequest().SetView());
- return msg;
- }
- //
- // determine if a given command subtype is supported
- //
- bool CPluginHandle_Impl::HasCommandSubtype(EAlgoCommand cmd) const
- {
- if ( GetCommand() != CPluginCommandSet::e_Algo ) {
- return false;
- }
- ITERATE (CPluginCommandSet::TAlgo, iter, m_Info->GetCommands().GetAlgo()) {
- if ( (*iter)->GetCommand() == cmd) {
- return true;
- }
- }
- return false;
- }
- bool CPluginHandle_Impl::HasCommandSubtype(EDataCommand cmd) const
- {
- if ( GetCommand() != CPluginCommandSet::e_Data ) {
- return false;
- }
- ITERATE (CPluginCommandSet::TData, iter, m_Info->GetCommands().GetData()) {
- if ( (*iter)->GetCommand() == cmd) {
- return true;
- }
- }
- return false;
- }
- bool CPluginHandle_Impl::HasCommandSubtype(EViewCommand cmd) const
- {
- if ( GetCommand() != CPluginCommandSet::e_View ) {
- return false;
- }
- ITERATE (CPluginCommandSet::TView, iter, m_Info->GetCommands().GetView()) {
- if ( (*iter)->GetCommand() == cmd) {
- return true;
- }
- }
- return false;
- }
- //
- // fill in the default set of arguments for a given command
- //
- template <class Command>
- inline bool s_FillDefaults(Command cmd, CPluginCommand& args,
- const list< CRef<CPluginCommand> >& cmd_list)
- {
- ITERATE (list< CRef<CPluginCommand> >, iter, cmd_list) {
- const CPluginCommand& pcmd = **iter;
- if ( pcmd.CanGetCommand() && pcmd.GetCommand() == cmd) {
-
- args.SetCommand(pcmd.GetCommand());
- if (pcmd.CanGetContext()) {
- args.SetContext(pcmd.GetContext());
- }
- if (pcmd.CanGetArgs()) {
- args.SetArgs(const_cast<CPluginArgSet&>(pcmd.GetArgs()));
- }
- return true;
- }
- }
- return false;
- }
- bool CPluginHandle_Impl::FillDefaults(EAlgoCommand cmd,
- CPluginCommand& args) const
- {
- if (GetCommand() != CPluginCommandSet::e_Algo) {
- return false;
- }
- return s_FillDefaults(cmd, args, x_GetWorkingSet().GetCommands().GetAlgo());
- }
- bool CPluginHandle_Impl::FillDefaults(EDataCommand cmd,
- CPluginCommand& args) const
- {
- if (GetCommand() != CPluginCommandSet::e_Data) {
- return false;
- }
- return s_FillDefaults(cmd, args, x_GetWorkingSet().GetCommands().GetData());
- }
- bool CPluginHandle_Impl::FillDefaults(EViewCommand cmd,
- CPluginCommand& args) const
- {
- if (GetCommand() != CPluginCommandSet::e_View) {
- return false;
- }
- return s_FillDefaults(cmd, args, x_GetWorkingSet().GetCommands().GetView());
- }
- CPluginLibInfo& CPluginHandle_Impl::x_GetWorkingSet(void) const
- {
- if ( !m_WorkingInfo ) {
- m_WorkingInfo.Reset(new CPluginLibInfo());
- m_WorkingInfo->Assign(*m_Info);
- }
- return *m_WorkingInfo;
- }
- //
- // Execute()
- // This is the main interface function; it is forwarded to the actual plugin.
- //
- void CPluginHandle_Impl::Execute(CPluginMessage& msg)
- {
- // !!! temporary implementation !!!
- string context;
- if (msg.CanGetContext()) {
- context = msg.GetContext();
- }
- CRef<CPluginBase> plugin(x_GetPlugin(context));
- plugin->Execute(msg);
- }
- //
- // FinalizeArgs()
- // This is the main interface function; it is forwarded to the actual plugin.
- //
- void CPluginHandle_Impl::FinalizeArgs(CPluginMessage& msg)
- {
- // !!! temporary implementation !!!
- string context;
- if (msg.CanGetContext()) {
- context = msg.GetContext();
- }
- CRef<CPluginBase> plugin(x_GetPlugin(context));
- plugin->FinalizeArgs(msg);
- }
- void CPluginHandle_Impl::x_SetFactory(CPluginFactoryBase* factory) const
- {
- m_Factory = factory;
- }
- CPluginBase* CPluginHandle_Impl::x_GetPlugin(const string& ctx_name)
- {
- if ( !m_Factory ) {
- CPluginRegistry::x_LoadLibrary(GetLibrary());
- }
- if ( !m_Factory ) {
- NCBI_THROW(CPluginException, ePluginLoadFailed,
- "Failed to load plugin library " + GetLibrary());
- }
- // retrieve a plugin based on its context
- TPluginDict::iterator iter = m_PluginDict.find(ctx_name);
- if (iter == m_PluginDict.end()) {
- m_PluginDict[ctx_name].Reset(m_Factory->GetPlugin());
- iter = m_PluginDict.find(ctx_name);
- }
- return iter->second;
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: plugin_handle_impl.cpp,v $
- * Revision 1000.5 2004/06/01 20:44:26 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.16
- *
- * Revision 1.16 2004/05/25 17:08:50 dicuccio
- * Added CreateMessage() API to generate a new plugin message for a given plugin
- * call
- *
- * Revision 1.15 2004/05/21 22:27:40 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.14 2004/04/14 20:14:43 jcherry
- * Removed invalid "flag" (CNcbiRegistry::eReturn) from calls to
- * CNcbiRegistry::GetString() (was being ignored and provoking an
- * error message)
- *
- * Revision 1.13 2004/02/13 04:03:02 ucko
- * #include <ctype.h> rather than <cctype>, which some compilers (MIPSpro
- * anyway) lack.
- *
- * Revision 1.12 2004/02/12 23:03:02 mjohnson
- * Added documentation lookup.
- *
- * Revision 1.11 2004/02/03 21:23:48 dicuccio
- * Added standard accessors for the working set of plugin args
- *
- * Revision 1.10 2004/01/15 22:50:17 jcherry
- * Added FinalizeArgs() for plugins
- *
- * Revision 1.9 2003/12/23 20:23:21 jcherry
- * Make argument values persist across invocations of plugin
- *
- * Revision 1.8 2003/11/06 20:04:07 dicuccio
- * Refactored FillDefaults(). Added option to have cached set of arguments
- *
- * Revision 1.7 2003/11/04 17:18:41 dicuccio
- * Changed calling API for plugins - take CPluginMessage directly instead of
- * paired command/reply
- *
- * Revision 1.6 2003/10/23 16:16:36 dicuccio
- * Exposed CPluginLibInfo
- *
- * Revision 1.5 2003/08/05 17:07:15 dicuccio
- * Changed calling semantics for the message queue - pass by reference, not
- * CConstRef<>
- *
- * Revision 1.4 2003/08/02 12:51:07 dicuccio
- * Enabled the context dictionary inside of plugin handles
- *
- * Revision 1.3 2003/07/14 11:01:06 shomrat
- * Plugin messageing system related changes
- *
- * Revision 1.2 2003/07/11 12:37:37 dicuccio
- * Made many string members of CPluginInfo optional; added checks for safe
- * retrieval
- *
- * Revision 1.1 2003/06/25 17:02:54 dicuccio
- * Split CPluginHandle into a handle (pointer-to-implementation) and
- * implementation file. Lots of #include file clean-ups.
- *
- * ===========================================================================
- */