github.com/slava-ustovytski/docker@v1.8.2-rc1/volume/drivers/extpoint.go (about)

     1  package volumedrivers
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/docker/docker/pkg/plugins"
     8  	"github.com/docker/docker/volume"
     9  )
    10  
    11  // currently created by hand. generation tool would generate this like:
    12  // $ extpoint-gen Driver > volume/extpoint.go
    13  
    14  var drivers = &driverExtpoint{extensions: make(map[string]volume.Driver)}
    15  
    16  type driverExtpoint struct {
    17  	extensions map[string]volume.Driver
    18  	sync.Mutex
    19  }
    20  
    21  func Register(extension volume.Driver, name string) bool {
    22  	drivers.Lock()
    23  	defer drivers.Unlock()
    24  	if name == "" {
    25  		return false
    26  	}
    27  	_, exists := drivers.extensions[name]
    28  	if exists {
    29  		return false
    30  	}
    31  	drivers.extensions[name] = extension
    32  	return true
    33  }
    34  
    35  func Unregister(name string) bool {
    36  	drivers.Lock()
    37  	defer drivers.Unlock()
    38  	_, exists := drivers.extensions[name]
    39  	if !exists {
    40  		return false
    41  	}
    42  	delete(drivers.extensions, name)
    43  	return true
    44  }
    45  
    46  func Lookup(name string) (volume.Driver, error) {
    47  	drivers.Lock()
    48  	defer drivers.Unlock()
    49  	ext, ok := drivers.extensions[name]
    50  	if ok {
    51  		return ext, nil
    52  	}
    53  	pl, err := plugins.Get(name, "VolumeDriver")
    54  	if err != nil {
    55  		return nil, fmt.Errorf("Error looking up volume plugin %s: %v", name, err)
    56  	}
    57  
    58  	d := NewVolumeDriver(name, pl.Client)
    59  	drivers.extensions[name] = d
    60  	return d, nil
    61  }