examples/core/plugin/main.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled
5  applications.
6 
7  TerraLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License,
10  or (at your option) any later version.
11 
12  TerraLib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with TerraLib. See COPYING. If not, write to
19  TerraLib Team at <terralib-team@terralib.org>.
20  */
21 
22 /*!
23  \file terralib/examples/core/plugin/main.cpp
24 
25  \brief Examples for the Terralib Plugin Module
26 
27  \author Matheus Cavassan Zaglia
28 */
29 
30 // TerraLib
31 #include <terralib/core/plugin.h>
32 #include <terralib/core/utils.h>
33 
34 // STL
35 #include <cstdlib>
36 #include <iostream>
37 
39 {
40  // prepare PluginEngineManager with all known plugin engines.
41  std::unique_ptr<te::core::AbstractPluginEngine> cppengine(
43 
44  te::core::PluginEngineManager::instance().insert(std::move(cppengine));
45 }
46 
48 {
49  // plugins will be loaded with C++ plugin engine
50  te::core::AbstractPluginEngine& plugin_engine =
52 
53  // find fisrt plugin manifest file, read the manifest, load plugin and start
54  // it
55  std::string p1_manifest_file =
56  te::core::FindInTerraLibPath("example/plugins/plugin1.teplg.json");
57 
58  te::core::PluginInfo p1_info =
59  te::core::JSONPluginInfoSerializer(p1_manifest_file);
60 
61  std::unique_ptr<te::core::AbstractPlugin> p1(plugin_engine.load(p1_info));
62 
63  p1->startup();
64 
65  // find second plugin manifest file, read the manifest, load plugin and start
66  // it
67  std::string p3_manifest_file =
68  te::core::FindInTerraLibPath("example/plugins/plugin3.teplg.json");
69 
70  te::core::PluginInfo p3_info =
71  te::core::JSONPluginInfoSerializer(p3_manifest_file);
72 
73  std::unique_ptr<te::core::AbstractPlugin> p3(plugin_engine.load(p3_info));
74 
75  p3->startup();
76 
77  // stop all plugins and unload them
78  p3->shutdown();
79  plugin_engine.unload(std::move(p3));
80 
81  p1->shutdown();
82  plugin_engine.unload(std::move(p1));
83 }
84 
86 {
87  // Load all the config files for the plugins.
88  std::vector<te::core::PluginInfo> v_pInfo;
89 
90  v_pInfo.push_back(te::core::JSONPluginInfoSerializer(
91  te::core::FindInTerraLibPath("example/plugins/plugin1.teplg.json")));
92  v_pInfo.push_back(te::core::JSONPluginInfoSerializer(
93  te::core::FindInTerraLibPath("example/plugins/plugin2.teplg.json")));
94  v_pInfo.push_back(te::core::JSONPluginInfoSerializer(
95  te::core::FindInTerraLibPath("example/plugins/plugin3.teplg.json")));
96  v_pInfo.push_back(te::core::JSONPluginInfoSerializer(
97  te::core::FindInTerraLibPath("example/plugins/plugin4.teplg.json")));
98 
99  // Insert all the plugins stored in the vector from a given PluginInfo.
100  v_pInfo = te::core::plugin::TopologicalSort(v_pInfo);
101 
102  for(const te::core::PluginInfo& pinfo : v_pInfo)
103  {
106  }
107 
108  // get all the loaded plugins
109  std::vector<te::core::PluginInfo> pVec =
111 
112  // unload it in the reverse order
113  for (auto plugin = pVec.rbegin(); plugin != pVec.rend(); ++plugin)
114  {
115  te::core::PluginManager::instance().stop(plugin->name);
117  }
119 }
120 
121 int main(int argc, char* argv[])
122 {
123  try
124  {
129  }
130  catch(boost::exception& e)
131  {
132  std::string* err = boost::get_error_info<te::ErrorDescription>(e);
133  std::cerr << *err << std::endl;
134  }
135 
136  return EXIT_SUCCESS;
137 }
TECOREEXPORT void FinalizePluginSystem()
void insert(std::unique_ptr< AbstractPluginEngine > engine)
Register a new plugin engine.
Include files for Core Plugin Library.
TECOREEXPORT std::vector< PluginInfo > TopologicalSort(const std::vector< PluginInfo > &v_pinfo)
void stop(const std::string &plugin_name)
Stop a loaded plugin.
void insert(const PluginInfo &pinfo)
Adds plugin with its plugin information to the list of unloaded plugins.
int main(int argc, char *argv[])
Basic information about a plugin.
void load(const std::string &plugin_name, const bool start=true)
It tries to load the informed plugin.
virtual std::unique_ptr< AbstractPlugin > load(const PluginInfo &pinfo)=0
Load the informed plugin.
static PluginManager & instance()
Access the singleton.
Include files for Core Utility Library.
void WithoutPluginManager()
void InitPluginSystem()
AbstractPluginEngine & get(const std::string &engine_id) const
Find a plugin engine with the given id.
virtual void unload(std::unique_ptr< AbstractPlugin > plugin)=0
Unload the informed plugin.
TECOREEXPORT PluginInfo JSONPluginInfoSerializer(const std::string &file_name)
A plugin finder that search for plugins in some special directories defined by compile time macros...
Definition: Serializers.cpp:44
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
TECOREEXPORT void InitializePluginSystem()
void clear()
Stop and unload all plugins, then clear the internal list of plugins.
std::vector< PluginInfo > getLoadedPlugins() const
Return the list of plugins that are loaded.
void unload(const std::string &plugin_name)
Try to unload a given plugin.
void WithPluginManager()
static PluginEngineManager & instance()
Access the singleton.