forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pluginfactory.go
78 lines (64 loc) · 2.05 KB
/
pluginfactory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/
package plugin
import (
"errors"
"fmt"
"os"
"plugin"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp"
)
const (
// PluginFactoryName is the factory name for BCCSP plugins
PluginFactoryName = "PLUGIN"
)
// PluginOpts contains the options for the PluginFactory
type PluginOpts struct {
// Path to plugin library
Library string
// Config map for the plugin library
Config map[string]interface{}
}
// PluginFactory is the factory for BCCSP plugins
type PluginFactory struct{}
// Name returns the name of this factory
func (f *PluginFactory) Name() string {
return PluginFactoryName
}
// Get returns an instance of BCCSP using Opts.
func (f *PluginFactory) Get(pluginOpts *PluginOpts) (bccsp.BCCSP, error) {
// check for valid config
if pluginOpts == nil {
return nil, errors.New("Invalid config. It must not be nil.")
}
// Library is required property
if pluginOpts.Library == "" {
return nil, errors.New("Invalid config: missing property 'Library'")
}
// make sure the library exists
if _, err := os.Stat(pluginOpts.Library); err != nil {
return nil, fmt.Errorf("Could not find library '%s' [%s]", pluginOpts.Library, err)
}
// attempt to load the library as a plugin
plug, err := plugin.Open(pluginOpts.Library)
if err != nil {
return nil, fmt.Errorf("Failed to load plugin '%s' [%s]", pluginOpts.Library, err)
}
// lookup the required symbol 'New'
sym, err := plug.Lookup("New")
if err != nil {
return nil, fmt.Errorf("Could not find required symbol 'CryptoServiceProvider' [%s]", err)
}
// check to make sure symbol New meets the required function signature
new, ok := sym.(func(config map[string]interface{}) (bccsp.BCCSP, error))
if !ok {
return nil, fmt.Errorf("Plugin does not implement the required function signature for 'New'")
}
return new(pluginOpts.Config)
}