github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/bccsp/factory/factory.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain 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, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package factory 17 18 import ( 19 "fmt" 20 "sync" 21 22 "github.com/hyperledger/fabric/bccsp" 23 "github.com/op/go-logging" 24 ) 25 26 var ( 27 // Default BCCSP 28 defaultBCCSP bccsp.BCCSP 29 30 // when InitFactories has not been called yet (should only happen 31 // in test cases), use this BCCSP temporarily 32 bootBCCSP bccsp.BCCSP 33 34 // BCCSP Factories 35 bccspMap map[string]bccsp.BCCSP 36 37 // factories' Sync on Initialization 38 factoriesInitOnce sync.Once 39 bootBCCSPInitOnce sync.Once 40 41 // Factories' Initialization Error 42 factoriesInitError error 43 44 logger = logging.MustGetLogger("BCCSP_FACTORY") 45 ) 46 47 // BCCSPFactory is used to get instances of the BCCSP interface. 48 // A Factory has name used to address it. 49 type BCCSPFactory interface { 50 51 // Name returns the name of this factory 52 Name() string 53 54 // Get returns an instance of BCCSP using opts. 55 Get(opts *FactoryOpts) (bccsp.BCCSP, error) 56 } 57 58 // GetDefault returns a non-ephemeral (long-term) BCCSP 59 func GetDefault() bccsp.BCCSP { 60 if defaultBCCSP == nil { 61 logger.Warning("Before using BCCSP, please call InitFactories(). Falling back to bootBCCSP.") 62 bootBCCSPInitOnce.Do(func() { 63 var err error 64 f := &SWFactory{} 65 bootBCCSP, err = f.Get(&DefaultOpts) 66 if err != nil { 67 panic("BCCSP Internal error, failed initialization with DefaultOpts!") 68 } 69 }) 70 return bootBCCSP 71 } 72 return defaultBCCSP 73 } 74 75 // GetBCCSP returns a BCCSP created according to the options passed in input. 76 func GetBCCSP(name string) (bccsp.BCCSP, error) { 77 return bccspMap[name], nil 78 } 79 80 // InitFactories must be called before using factory interfaces 81 // It is acceptable to call with config = nil, in which case 82 // some defaults will get used 83 // Error is returned only if defaultBCCSP cannot be found 84 func InitFactories(config *FactoryOpts) error { 85 factoriesInitOnce.Do(func() { 86 // Take some precautions on default opts 87 if config == nil { 88 config = &DefaultOpts 89 } 90 91 if config.ProviderName == "" { 92 config.ProviderName = "SW" 93 } 94 95 if config.SwOpts == nil { 96 config.SwOpts = DefaultOpts.SwOpts 97 } 98 99 // Initialize factories map 100 bccspMap = make(map[string]bccsp.BCCSP) 101 102 // Software-Based BCCSP 103 if config.SwOpts != nil { 104 f := &SWFactory{} 105 err := initBCCSP(f, config) 106 if err != nil { 107 factoriesInitError = fmt.Errorf("[%s]", err) 108 } 109 } 110 111 // PKCS11-Based BCCSP 112 if config.Pkcs11Opts != nil { 113 f := &PKCS11Factory{} 114 err := initBCCSP(f, config) 115 if err != nil { 116 factoriesInitError = fmt.Errorf("%s\n[%s]", factoriesInitError, err) 117 } 118 } 119 120 var ok bool 121 defaultBCCSP, ok = bccspMap[config.ProviderName] 122 if !ok { 123 factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName) 124 } 125 }) 126 127 return factoriesInitError 128 } 129 130 func initBCCSP(f BCCSPFactory, config *FactoryOpts) error { 131 csp, err := f.Get(config) 132 if err != nil { 133 return fmt.Errorf("Could not initialize BCCSP %s [%s]", f.Name(), err) 134 } 135 136 logger.Debugf("Initialize BCCSP [%s]", f.Name()) 137 bccspMap[f.Name()] = csp 138 return nil 139 }