github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/config/application.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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  
    17  package config
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/hyperledger/fabric/common/config/msp"
    23  )
    24  
    25  const (
    26  	// ApplicationGroupKey is the group name for the Application config
    27  	ApplicationGroupKey = "Application"
    28  )
    29  
    30  // ApplicationGroup represents the application config group
    31  type ApplicationGroup struct {
    32  	*Proposer
    33  	*ApplicationConfig
    34  	mspConfig *msp.MSPConfigHandler
    35  }
    36  
    37  type ApplicationConfig struct {
    38  	*standardValues
    39  
    40  	applicationGroup *ApplicationGroup
    41  	applicationOrgs  map[string]ApplicationOrg
    42  }
    43  
    44  // NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper
    45  func NewApplicationGroup(mspConfig *msp.MSPConfigHandler) *ApplicationGroup {
    46  	ag := &ApplicationGroup{
    47  		mspConfig: mspConfig,
    48  	}
    49  	ag.Proposer = NewProposer(ag)
    50  
    51  	return ag
    52  }
    53  
    54  func (ag *ApplicationGroup) NewGroup(name string) (ValueProposer, error) {
    55  	return NewApplicationOrgGroup(name, ag.mspConfig), nil
    56  }
    57  
    58  // Allocate returns a new instance of the ApplicationConfig
    59  func (ag *ApplicationGroup) Allocate() Values {
    60  	return NewApplicationConfig(ag)
    61  }
    62  
    63  func NewApplicationConfig(ag *ApplicationGroup) *ApplicationConfig {
    64  	sv, err := NewStandardValues(&(struct{}{}))
    65  	if err != nil {
    66  		logger.Panicf("Programming error: %s", err)
    67  	}
    68  
    69  	return &ApplicationConfig{
    70  		applicationGroup: ag,
    71  
    72  		// Currently there are no config values
    73  		standardValues: sv,
    74  	}
    75  }
    76  
    77  func (ac *ApplicationConfig) Validate(tx interface{}, groups map[string]ValueProposer) error {
    78  	ac.applicationOrgs = make(map[string]ApplicationOrg)
    79  	var ok bool
    80  	for key, value := range groups {
    81  		ac.applicationOrgs[key], ok = value.(*ApplicationOrgGroup)
    82  		if !ok {
    83  			return fmt.Errorf("Application sub-group %s was not an ApplicationOrgGroup, actually %T", key, value)
    84  		}
    85  	}
    86  	return nil
    87  }
    88  
    89  func (ac *ApplicationConfig) Commit() {
    90  	ac.applicationGroup.ApplicationConfig = ac
    91  }
    92  
    93  // Organizations returns a map of org ID to ApplicationOrg
    94  func (ac *ApplicationConfig) Organizations() map[string]ApplicationOrg {
    95  	return ac.applicationOrgs
    96  }