plugin_init.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:21k
- /*
- * ===========================================================================
- * PRODUCTION $Log: plugin_init.cpp,v $
- * PRODUCTION Revision 1000.5 2004/06/01 20:56:32 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.42
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: plugin_init.cpp,v 1000.5 2004/06/01 20:56:32 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
- * Anatoliy Kuznetsov
- *
- * File Description:
- * CGBenchPluginInit -- single standard autoloading plugin for Genome
- * Workbench
- */
- #include <ncbi_pch.hpp>
- #include "plugin_init.hpp"
- #include <bdb/bdb_blobcache.hpp>
- #include <corelib/ncbiapp.hpp>
- #include <corelib/ncbireg.hpp>
- #include <corelib/ncbitime.hpp>
- #include <corelib/ncbi_process.hpp>
- #include <gui/core/data_store.hpp>
- #include <gui/core/doc_manager.hpp>
- #include <gui/core/plugin_exception.hpp>
- #include <gui/core/plugin_handle.hpp>
- #include <gui/core/plugin_registry.hpp>
- #include <gui/utils/system_path.hpp>
- #include <gui/core/version.hpp>
- #include <gui/plugin/PluginCommand.hpp>
- #include <gui/plugin/PluginCommandSet.hpp>
- #include <gui/plugin/PluginInfo.hpp>
- #include <gui/plugin/PluginValueConstraint.hpp>
- #include <gui/utils/message_box.hpp>
- #include <objmgr/object_manager.hpp>
- #include <objtools/data_loaders/lds/lds_dataloader.hpp>
- #include <objtools/data_loaders/genbank/gbloader.hpp>
- #include <objtools/data_loaders/genbank/readers/id1/reader_id1_cache.hpp>
- #include <objtools/lds/admin/lds_admin.hpp>
- #include <objtools/lds/lds.hpp>
- #include <objtools/lds/lds_reader.hpp>
- BEGIN_NCBI_SCOPE
- USING_SCOPE(objects);
- void CGBenchPluginInit::GetInfo(CPluginInfo& info)
- {
- info.Reset();
- // version info macro
- info.SetInfo(CPluginVersion::eMajor, CPluginVersion::eMinor, 0,
- string(__DATE__) + " " + string(__TIME__),
- "CGBenchPluginInit", "", "", "");
- // command info
- info.SetCommands().AddAlgoCommand(eAlgoCommand_run);
- info.SetAutorun(true);
- }
- void CGBenchPluginInit::RunCommand(CPluginMessage& msg)
- {
- CPluginReply& reply = msg.SetReply();
- //
- // first, set up our stock data loaders
- // these must be present for sequence ID resolution to work correctly
- //
- // Create GenBank data loader and register it with the OM.
- // The last argument "eDefault" informs the OM that the loader must
- // be included in scopes during the CScope::AddDefaults() call.
- try {
- x_InitGenBank();
- }
- catch (CException& e) {
- LOG_POST(Error << "error creating GenBank data loader: " << e.what());
- string msg("Error connecting to GenBank:n");
- msg += e.GetMsg();
- msg += "nGenBank will not be available.";
- NcbiMessageBox(msg);
- }
- #ifndef _DEBUG
- catch (...) {
- LOG_POST(Error << "unknown error creating GenBank data loader");
- NcbiMessageBox("An unknown error has occurred while trying to "
- "connect to GenBank");
- }
- #endif
- //
- // initialize the local data storage
- //
- try {
- x_InitLDS();
- }
- catch(CException& e) {
- LOG_POST(Error << "error initializing local data storage: "
- << e.what());
- string msg("Error initializing local data storage:n");
- msg += e.GetMsg();
- msg += "nLDS will not be available.";
- NcbiMessageBox(msg);
- }
- #ifndef _DEBUG
- catch (...) {
- LOG_POST(Error << "unknown error creating GenBank data loader");
- NcbiMessageBox("An unknown error has occurred while trying to "
- "initialize the local data stores.");
- }
- #endif
- //
- // PLANNED:
- //
- // - establish named pipe for gbench-agent (our mime droid)
- // - check NCBI to see if there are any gbench updates
- // - evaluate history to see what the user has looked at recently and see
- // if there are any recent changes in those sequences / organisms
- // - check the user's cubby at NCBI to see if there are any updates in any
- // watched files
- //
- reply.SetStatus(eMessageStatus_success);
- }
- //
- // Create our GenBank connection
- //
- void CGBenchPluginInit::x_InitGenBank()
- {
- CNcbiApplication* app = CNcbiApplication::Instance();
- _ASSERT(app);
- CNcbiRegistry& reg = app->GetConfig();
- // general genbank loader options
- int gc_size = reg.GetInt("GBLOADER", "GcSize", 10000,
- CNcbiRegistry::eErrPost);
- int priority = reg.GetInt("GBLOADER", "Priority", 99,
- CNcbiRegistry::eErrPost);
- // caching options
- string cache_path = CSystemPath::ResolvePath("<home>", "cache");
- cache_path = reg.GetString("GBLOADER", "CachePath", cache_path,
- CNcbiRegistry::eErrPost);
- int cache_age = reg.GetInt("GBLOADER", "CacheAge", 5,
- CNcbiRegistry::eErrPost);
- if (cache_age == 0) {
- cache_age = 5; // keep objects for 5 days (default)
- }
- // ID resolution time in hours
- int id_resolution_time = reg.GetInt("GBLOADER",
- "IdResolutionTime", 24,
- CNcbiRegistry::eErrPost);
- if (id_resolution_time > 24 * 3)
- id_resolution_time = 24 * 3; // correct the unreasonable value
- bool disable_cache = reg.GetBool("GBLOADER", "DisableCache", false,
- CNcbiRegistry::eErrPost);
- auto_ptr<CCachedId1Reader> id1_reader;
- if (!cache_path.empty() && !disable_cache) {
- try {
- auto_ptr<CBDB_Cache> bc(new CBDB_Cache());
- try {
- ICache::TTimeStampFlags flags =
- ICache::fTimeStampOnRead |
- ICache::fExpireLeastFrequentlyUsed |
- ICache::fPurgeOnStartup;
- bc->SetTimeStampPolicy(flags, cache_age*24*60*60);
- bc->Open(cache_path.c_str(), "blobs", CBDB_Cache::ePidLock);
- // Cache cleaning
- // Objects age should be assigned in days, negative value
- // means cleaning is disabled
- if (cache_age > 0) {
- CTime time_stamp(CTime::eCurrent);
- time_t age = time_stamp.GetTimeT();
- age -= 60 * 60 * 24 * cache_age;
- bc->Purge(age);
- }
- } catch (CPIDGuardException& ex) {
- switch (ex.GetErrCode())
- {
- case CPIDGuardException::eStillRunning:
- // We probably(!) have another program competing for cache,
- // So this copy is not getting access
- LOG_POST(Error << "GenBank Cache is in use by another process");
- LOG_POST(Error << "Local caching will be disabled.");
- throw;
- default:
- throw;
- } // switch
- }
- auto_ptr<CBDB_Cache> idc(new CBDB_Cache());
- try {
- ICache::TTimeStampFlags flags =
- ICache::fTimeStampOnCreate|
- ICache::fCheckExpirationAlways;
- idc->SetTimeStampPolicy(flags, id_resolution_time * 60 * 60);
- idc->Open(cache_path.c_str(), "idc", CBDB_Cache::ePidLock);
- } catch (CPIDGuardException& ex) {
- switch (ex.GetErrCode())
- {
- case CPIDGuardException::eStillRunning:
- // We probably(!) have another program competing for cache,
- // So this copy is not getting access
- LOG_POST(Error << "GenBank Cache is in use by another process");
- LOG_POST(Error << "Local caching will be disabled.");
- throw;
- default:
- throw;
- } // switch
- }
- CRef<CBDB_CacheHolder>
- cache_holder(new CBDB_CacheHolder(bc.release(),
- idc.release()));
- CDataStore::PutObject("BDB_Cache", *cache_holder);
- id1_reader.reset(new CCachedId1Reader(5,
- cache_holder->GetBlobCache(),
- cache_holder->GetIdCache()));
- LOG_POST(Info << "ID1 cache enabled at " << cache_path);
- }
- catch(CException& e) {
- LOG_POST(Error << "ID1 cache initialization failed in "
- << cache_path << ": "
- << e.what());
- }
- #ifndef _DEBUG
- catch(...) {
- LOG_POST(Error << "ID1 cache initialization failed in "
- << cache_path);
- }
- #endif
- } else {
- LOG_POST(Info << "ID1 cache disabled.");
- }
- CRef<CGBDataLoader> loader
- (new CGBDataLoader("GenBank", id1_reader.release(), gc_size));
- if ( !loader ) {
- NCBI_THROW(CPluginException, eUnknownError,
- "CGBenchPluginInit(): "
- "can't create GenBank data loader");
- }
- CDocManager::GetObjectManager()
- .RegisterDataLoader(*(loader.Release()),
- CObjectManager::eDefault, priority);
- LOG_POST(Info << "registered GenBank data loader");
- }
- //
- // Create our local data storage connections
- //
- void CGBenchPluginInit::x_InitLDS()
- {
- CNcbiApplication* app = CNcbiApplication::Instance();
- _ASSERT(app);
- const CNcbiRegistry& reg = app->GetConfig();
- CRef<CLDS_DatabaseHolder> dbh(new CLDS_DatabaseHolder());
- // Discover all LDS_* sections in the application registry
- //
- list<string> sections;
- list<string> lds_sections;
- reg.EnumerateSections(§ions);
- ITERATE (list<string>, it, sections) {
- if (it->find("LDS") == 0) {
- lds_sections.push_back(*it);
- }
- }
- vector<string> plugin_aliases;
- ITERATE(list<string>, it, lds_sections) {
- string lds_section_name = *it;
- string lds_path =
- reg.GetString(lds_section_name, "Path", "", CNcbiRegistry::eErrPost);
- string lds_alias =
- reg.GetString(lds_section_name, "Alias", "Default", CNcbiRegistry::eReturn);
- if (lds_path.empty()) {
- LOG_POST(Error << "LDS: database " << lds_section_name
- << " " << lds_alias << " ignored. No path.");
- continue;
- }
- // Check if the database already open
- {{
- CLDS_Database* db1 = dbh->GetDatabase(lds_alias);
- if (db1) {
- LOG_POST(Error << "LDS: database " << lds_section_name
- << " alias=" << lds_alias
- << " ignored. Alias already exists.");
- continue;
- }
- }}
- bool recurse_sub_dir =
- reg.GetBool(lds_section_name, "SubDir", true, 0, CNcbiRegistry::eErrPost);
- CLDS_Management::ERecurse recurse =
- (recurse_sub_dir) ? CLDS_Management::eRecurseSubDirs :
- CLDS_Management::eDontRecurse;
- bool crc32 =
- reg.GetBool(lds_section_name, "ControlSum", true, 0, CNcbiRegistry::eErrPost);
- // if ControlSum key is true (default), try an laternative "CRC32" key
- // (more straightforward synonym for the same setting)
- if (crc32) {
- crc32 = reg.GetBool(lds_section_name, "CRC32", true, 0, CNcbiRegistry::eErrPost);
- }
- CLDS_Management::EComputeControlSum control_sum =
- (crc32) ? CLDS_Management::eComputeControlSum :
- CLDS_Management::eNoControlSum;
- bool is_created;
- CLDS_Database *ldb =
- CLDS_Management::OpenCreateDB(lds_path, "lds.db",
- &is_created, recurse,
- control_sum);
- if (!lds_alias.empty()) {
- ldb->SetAlias(lds_alias);
- }
- dbh->AddDatabase(ldb);
- if (!is_created) {
- CLDS_Management mgmt(*ldb);
- mgmt.SyncWithDir(lds_path, recurse, control_sum);
- }
- plugin_aliases.push_back(lds_alias);
-
- // Adding NON DEFAULT LDS dataloader
- // To be activated later in the LDS derived scopes by the dload_basic plugin
- try {
- string dl_name = string("LDS_dataloader_") + lds_alias;
- CRef<CLDS_DataLoader> loader(new CLDS_DataLoader(*ldb, dl_name));
- if ( !loader ) {
- NCBI_THROW(CPluginException, eUnknownError,
- "CGBenchPluginInit(): "
- "can't create LDS data loader");
- }
- CDocManager::GetObjectManager()
- .RegisterDataLoader(*(loader.Release()),
- CObjectManager::eNonDefault, 80);
- LOG_POST(Info << "LDS: registered data loader: " << dl_name);
- }
- catch (CException& e) {
- LOG_POST(Error << "error creating LDS data loader: "
- << e.what() << " Alias:" << lds_alias);
- string msg("Error creating LDS data loader:n");
- msg += e.GetMsg();
- msg += "nLocal data storage will not be available.";
- msg += "Alias: " + lds_alias;
- NcbiMessageBox(msg);
- }
- //break; // TODO: This break is temporary
- } // ITERATE
- //
- // twiddle the plugin registry to handle our aliases
- //
- if (plugin_aliases.size() > 0) {
- CPluginHandle handle = CPluginRegistry::GetPlugin("CLDS_Loader");
- if (handle) {
- //
- // this is a (dangerous!) hack
- // DON"T CHANGE THE MENU ITEM!!!!
- //
- // search for the load data command
- CPluginInfo& info = const_cast<CPluginInfo&> (handle.GetInfo());
- NON_CONST_ITERATE (CPluginInfo::TCommands::TData, cmd_iter,
- info.SetCommands().SetData()) {
- CPluginCommand& cmd = **cmd_iter;
- if ( cmd.GetCommand() != eDataCommand_load ) {
- continue;
- }
- string default_lds_alias = *(plugin_aliases.begin());
- //
- // found it. Add an argument for our aliases
- //
- cmd.AddDefaultArgument("alias", "Directory to load from",
- CPluginArg::eString, default_lds_alias);
- CRef<CPluginValueConstraint> cnst
- (CPluginValueConstraint::CreateSet());
- ITERATE(vector<string>, alias, plugin_aliases) {
- *cnst, *alias;
- }
- cmd.SetConstraint("alias", *cnst);
- break;
- }
- }
- }
- CDataStore::PutObject("LDS_Database", *dbh);
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: plugin_init.cpp,v $
- * Revision 1000.5 2004/06/01 20:56:32 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.42
- *
- * Revision 1.42 2004/05/21 22:27:47 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.41 2004/05/03 13:04:16 dicuccio
- * Made log-post message for cache use conflict more user friendly
- *
- * Revision 1.40 2004/04/09 20:14:30 dicuccio
- * Don't add the trace chromatogram loader by default
- *
- * Revision 1.39 2004/03/25 14:31:28 dicuccio
- * Added TRACE chromatogram data loader
- *
- * Revision 1.38 2004/02/27 17:33:11 kuznets
- * Removed CCacheHolder, it now lives in BDB library.
- *
- * Revision 1.37 2004/02/27 15:34:34 kuznets
- * Id1 reader initialization: replaced old local cache with new ICache
- * implementation (the old one is supposed to be phased out from the toolkit)
- *
- * Revision 1.36 2004/02/17 20:35:27 rsmith
- * moved core/settings.[ch]pp and core/system_path.[ch]pp to config and utils, respectively.
- *
- * Revision 1.35 2004/02/17 17:19:57 dicuccio
- * Moved all LOCAL_CACHE entries into GBLOADER section of registry
- *
- * Revision 1.34 2004/01/27 18:42:58 dicuccio
- * Added flag to diable local cache. CLeaned up processing of indexed local
- * directories
- *
- * Revision 1.33 2004/01/05 18:09:43 vasilche
- * Fixed path to genbank loader and readers.
- *
- * Revision 1.32 2003/12/09 15:46:54 dicuccio
- * Use CException::GetMsg() instead of what()
- *
- * Revision 1.31 2003/12/02 14:41:42 dicuccio
- * Restored autorun
- *
- * Revision 1.30 2003/11/26 17:16:18 dicuccio
- * Use the correct class name for registering the initialization plugin
- *
- * Revision 1.29 2003/11/24 15:43:12 dicuccio
- * Added new tip-of-the-day dialog. Changed CVersion to CPluginVersion
- *
- * Revision 1.28 2003/11/04 17:49:23 dicuccio
- * Changed calling parameters for plugins - pass CPluginMessage instead of paired
- * CPluginCommand/CPluginReply
- *
- * Revision 1.27 2003/10/29 15:10:12 kuznets
- * Fixed crash on trying to change the lds pluging arguments when gbench.ini does
- * not have any LDS instances.
- *
- * Revision 1.26 2003/10/28 14:22:07 kuznets
- * Working on support of multiple LDS instances.
- *
- * Revision 1.25 2003/10/27 19:58:21 kuznets
- * Added application registry scan for multiple LDS instances
- * (not enabled yet)
- *
- * Revision 1.24 2003/10/24 12:41:14 kuznets
- * Added cache locking. Removed explicit local cache directory check
- * (lives in cache implementation now)
- *
- * Revision 1.23 2003/10/23 13:33:57 dicuccio
- * Modify plugin arguments for LDS loader plugin to add a drop-down box for data
- * alias
- *
- * Revision 1.22 2003/10/21 16:21:22 kuznets
- * Enabled Id resolution cache for (cached id1 reader)
- *
- * Revision 1.21 2003/10/20 18:19:51 kuznets
- * Changed cache ownership scheme.
- * Now cache is dumped into a common reusable gbench data store (CDataStore)
- * (it was owned by the ID1 reader before)
- *
- * Revision 1.20 2003/10/16 15:49:49 dicuccio
- * Properly set the plugin reply status
- *
- * Revision 1.19 2003/10/14 16:25:04 dicuccio
- * Moved GenBank data loader initialization into a separate function - easiter to
- * follow the code this way
- *
- * Revision 1.18 2003/10/09 19:56:47 kuznets
- * Added registry controlled alias to LDS database
- *
- * Revision 1.17 2003/10/08 18:21:26 kuznets
- * Reflecting changes in LDS library API
- *
- * Revision 1.16 2003/10/07 18:28:20 dicuccio
- * FIxed access to auto_ptr<> after release
- *
- * Revision 1.15 2003/10/07 13:40:08 dicuccio
- * Code clean-up. Use auto_ptr<> to manage memory locally. Added default cache
- * path if no path is found.
- *
- * Revision 1.14 2003/10/06 20:22:13 kuznets
- * Added support for sub directories and option to disable CRC32 for files:
- * [LDS]
- * SubDir
- * ControlSum (alt.name CRC32)
- *
- * Revision 1.13 2003/10/06 16:31:21 kuznets
- * Cache purge re-enabled.
- *
- * Revision 1.12 2003/10/06 15:58:05 dicuccio
- * Various changes:
- * - Disabled cache purge (temporarily)
- * - Ensure the cache directory exists at app start-up
- * - Changed default priority of local data store
- * - Changed garbage collection trigger size
- *
- * Revision 1.11 2003/10/06 15:15:08 kuznets
- * Added cache cleaning code (cache keeps objects for 5 days (default value))
- *
- * Revision 1.10 2003/10/06 14:33:32 kuznets
- * Implemented local cache for the id1 reader
- *
- * Revision 1.9 2003/08/19 14:35:20 kuznets
- * LDS dataloader re-included. Now as a NON DEFAULT dataloader.
- * (To be later activated in the LDS derived scopes)
- *
- * Revision 1.8 2003/08/19 13:44:27 kuznets
- * LDS dataloader excluded from the list of default loaders.
- *
- * Revision 1.7 2003/08/15 19:38:28 dicuccio
- * Added setting of GenBank data loader settings in .ini file
- *
- * Revision 1.6 2003/08/05 15:24:42 kuznets
- * Redefined default priorities for OM data loaders (GenBank