github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/exchangers/exchangers.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package exchangers
    20  
    21  import (
    22  	"fmt"
    23  
    24  	"github.com/cs3org/reva/v2/pkg/mentix/entity"
    25  )
    26  
    27  // Collection is an interface for exchanger collections.
    28  type Collection interface {
    29  	entity.Collection
    30  
    31  	// Exchangers returns a vector of exchangers within the collection.
    32  	Exchangers() []Exchanger
    33  }
    34  
    35  type entityCollectionWrapper struct {
    36  	entities []entity.Entity
    37  }
    38  
    39  func (collection *entityCollectionWrapper) Entities() []entity.Entity {
    40  	return collection.entities
    41  }
    42  
    43  // AsEntityCollection transforms an exchanger collection into an entity collection.
    44  func AsEntityCollection(collection Collection) entity.Collection {
    45  	wrapper := entityCollectionWrapper{}
    46  	for _, exchanger := range collection.Exchangers() {
    47  		wrapper.entities = append(wrapper.entities, exchanger)
    48  	}
    49  	return &wrapper
    50  }
    51  
    52  // StartExchangers starts the given exchangers.
    53  func StartExchangers(collection Collection) error {
    54  	for _, exchanger := range collection.Exchangers() {
    55  		if err := exchanger.Start(); err != nil {
    56  			return fmt.Errorf("unable to start exchanger '%v': %v", exchanger.GetName(), err)
    57  		}
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  // StopExchangers stops the given exchangers.
    64  func StopExchangers(collection Collection) {
    65  	for _, exchanger := range collection.Exchangers() {
    66  		exchanger.Stop()
    67  	}
    68  }
    69  
    70  // GetRequestExchangers gets all exchangers from a vector that implement the RequestExchanger interface.
    71  func GetRequestExchangers(collection Collection) []RequestExchanger {
    72  	var reqExchangers []RequestExchanger
    73  	for _, exporter := range collection.Exchangers() {
    74  		if reqExchanger, ok := exporter.(RequestExchanger); ok {
    75  			reqExchangers = append(reqExchangers, reqExchanger)
    76  		}
    77  	}
    78  	return reqExchangers
    79  }