001/* 002 * Copyright 2016 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.datasource; 017 018import java.util.Arrays; 019import java.util.Collection; 020import java.util.Comparator; 021import java.util.Map; 022import java.util.stream.Stream; 023 024/** 025 * Interface for entities that use data sources 026 */ 027public interface DataSourceConsumer 028{ 029 /** 030 * The type of use of a data source 031 */ 032 public enum TypeOfUse 033 { 034 /** The data source is in use and in a blocking way : the application must start in safe-mode if not available */ 035 BLOCKING, 036 /** The data source is in use but in a non-blocking way */ 037 NON_BLOCKING, 038 /** The data source is not used */ 039 NOT_USED; 040 041 /** 042 * Merge several types of use to keep only the most important 043 * @param typesOfUse The types of use to merge. Cannot be null but can be empty. 044 * @return The most important type of use 045 */ 046 public static TypeOfUse merge(Collection<TypeOfUse> typesOfUse) 047 { 048 return _merge(typesOfUse.stream()); 049 } 050 051 /** 052 * Merge several types of use to keep only the most important 053 * @param typesOfUse The types of use to merge. Cannot be null but can be empty. 054 * @return The most important type of use 055 */ 056 public static TypeOfUse merge(TypeOfUse... typesOfUse) 057 { 058 return _merge(Arrays.stream(typesOfUse)); 059 } 060 061 private static TypeOfUse _merge(Stream<TypeOfUse> typesOfUse) 062 { 063 return typesOfUse.filter(r -> r != null) 064 .min(Comparator.naturalOrder()) 065 .orElse(TypeOfUse.NOT_USED); 066 } 067 } 068 069 /** 070 * Determines if a data source is used 071 * @param id The id of data source to check 072 * @return true if the data source is currently in use 073 */ 074 public TypeOfUse isInUse(String id); 075 076 /** 077 * Retrieve the ids of the used data sources 078 * @return the set of ids of the data source(s) used by this data source customer 079 */ 080 public Map<String, TypeOfUse> getUsedDataSourceIds(); 081}