~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/openstack/common/plugin/pluginmanager.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 OpenStack LLC.
 
2
# All Rights Reserved.
 
3
#
 
4
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
5
#    not use this file except in compliance with the License. You may obtain
 
6
#    a copy of the License at
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
12
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
#    License for the specific language governing permissions and limitations
 
14
#    under the License.
 
15
 
 
16
import imp
 
17
import os
 
18
import pkg_resources
 
19
 
 
20
from nova.openstack.common import cfg
 
21
from nova.openstack.common import log as logging
 
22
from nova.openstack.common.notifier import api as notifier_api
 
23
 
 
24
 
 
25
CONF = cfg.CONF
 
26
LOG = logging.getLogger(__name__)
 
27
 
 
28
 
 
29
class PluginManager(object):
 
30
    """Manages plugin entrypoints and loading.
 
31
 
 
32
    For a service to implement this plugin interface for callback purposes:
 
33
 
 
34
      - Make use of the openstack-common notifier system
 
35
      - Instantiate this manager in each process (passing in
 
36
        project and service name)
 
37
 
 
38
    For an API service to extend itself using this plugin interface,
 
39
    it needs to query the plugin_extension_factory provided by
 
40
    the already-instantiated PluginManager.
 
41
    """
 
42
 
 
43
    def __init__(self, project_name, service_name):
 
44
        """ Construct Plugin Manager; load and initialize plugins.
 
45
 
 
46
        project_name (e.g. 'nova' or 'glance') is used
 
47
        to construct the entry point that identifies plugins.
 
48
 
 
49
        The service_name (e.g. 'compute') is passed on to
 
50
        each plugin as a raw string for it to do what it will.
 
51
        """
 
52
        self._project_name = project_name
 
53
        self._service_name = service_name
 
54
        self.plugins = []
 
55
 
 
56
    def load_plugins(self):
 
57
        self.plugins = []
 
58
 
 
59
        for entrypoint in pkg_resources.iter_entry_points('%s.plugin' %
 
60
                                                          self._project_name):
 
61
            try:
 
62
                pluginclass = entrypoint.load()
 
63
                plugin = pluginclass(self._service_name)
 
64
                self.plugins.append(plugin)
 
65
            except Exception, exc:
 
66
                LOG.error(_("Failed to load plugin %(plug)s: %(exc)s") %
 
67
                          {'plug': entrypoint, 'exc': exc})
 
68
 
 
69
        # Register individual notifiers.
 
70
        for plugin in self.plugins:
 
71
            for notifier in plugin.notifiers:
 
72
                notifier_api.add_driver(notifier)
 
73
 
 
74
    def plugin_extension_factory(self, ext_mgr):
 
75
        for plugin in self.plugins:
 
76
            descriptors = plugin.api_extension_descriptors
 
77
            for descriptor in descriptors:
 
78
                ext_mgr.load_extension(descriptor)