forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.go
80 lines (63 loc) · 2.12 KB
/
support.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
79
80
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package peer
import (
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/resourcesconfig"
)
var supportFactory SupportFactory
// SupportFactory is a factory of Support interfaces
type SupportFactory interface {
// NewSupport returns a Support interface
NewSupport() Support
}
// Support gives access to peer resources and avoids calls to static methods
type Support interface {
// GetApplicationConfig returns the configtxapplication.SharedConfig for the channel
// and whether the Application config exists
GetApplicationConfig(cid string) (channelconfig.Application, bool)
// ChaincodeByName returns the definition (and whether they exist)
// for a chaincode in a specific channel
ChaincodeByName(chainname, ccname string) (resourcesconfig.ChaincodeDefinition, bool)
}
type supportImpl struct {
}
func (s *supportImpl) GetApplicationConfig(cid string) (channelconfig.Application, bool) {
cc := GetChannelConfig(cid)
if cc == nil {
return nil, false
}
return cc.ApplicationConfig()
}
func (s *supportImpl) ChaincodeByName(chainname, ccname string) (resourcesconfig.ChaincodeDefinition, bool) {
rc := GetResourcesConfig(chainname)
if rc == nil {
return nil, false
}
return rc.ChaincodeRegistry().ChaincodeByName(ccname)
}
type SupportFactoryImpl struct {
}
func (c *SupportFactoryImpl) NewSupport() Support {
return &supportImpl{}
}
// RegisterSupportFactory should be called to specify
// which factory should be used to serve GetSupport calls
func RegisterSupportFactory(ccfact SupportFactory) {
supportFactory = ccfact
}
// GetSupport returns a new Support instance by calling the factory
func GetSupport() Support {
if supportFactory == nil {
panic("The factory must be set first via RegisterSupportFactory")
}
return supportFactory.NewSupport()
}
// init is called when this package is loaded; this way by default,
// all calls to GetSupport will be satisfied by SupportFactoryImpl,
// unless RegisterSupportFactory is called again
func init() {
RegisterSupportFactory(&SupportFactoryImpl{})
}