github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/component/all/all.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // The all package facilitates the registration of Juju components into
     5  // the relevant machinery. It is intended as the one place in Juju where
     6  // the components (horizontal design layers) and the machinery
     7  // (vertical/architectural layers) intersect. This approach helps
     8  // alleviate interdependence between the components and the machinery.
     9  //
    10  // This is done in an independent package to avoid circular imports.
    11  package all
    12  
    13  import (
    14  	"github.com/juju/errors"
    15  	"github.com/juju/utils/set"
    16  )
    17  
    18  type component interface {
    19  	registerForServer() error
    20  	registerForClient() error
    21  }
    22  
    23  var components = []component{
    24  	&payloads{},
    25  	&resources{},
    26  }
    27  
    28  // RegisterForServer registers all the parts of the components with the
    29  // Juju machinery for use as a server (e.g. jujud, jujuc).
    30  func RegisterForServer() error {
    31  	for _, c := range components {
    32  		if err := c.registerForServer(); err != nil {
    33  			return errors.Trace(err)
    34  		}
    35  	}
    36  	return nil
    37  }
    38  
    39  // RegisterForServer registers all the parts of the components with the
    40  // Juju machinery for use as a client (e.g. juju).
    41  func RegisterForClient() error {
    42  	for _, c := range components {
    43  		if err := c.registerForClient(); err != nil {
    44  			return errors.Trace(err)
    45  		}
    46  	}
    47  	return nil
    48  }
    49  
    50  // registered tracks which parts of each component have been registered.
    51  var registered = map[string]set.Strings{}
    52  
    53  // markRegistered helps components track which things they've
    54  // registered. If the part has already been registered then false is
    55  // returned, indicating that marking failed. This way components can
    56  // ensure a part is registered only once.
    57  func markRegistered(component, part string) bool {
    58  	parts, ok := registered[component]
    59  	if !ok {
    60  		parts = set.NewStrings()
    61  		registered[component] = parts
    62  	}
    63  	if parts.Contains(part) {
    64  		return false
    65  	}
    66  	parts.Add(part)
    67  	return true
    68  }