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.plugins.core.impl.datasource; 017 018import java.util.Collection; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.ametys.core.datasource.DataSourceConsumer; 023import org.ametys.runtime.config.Config; 024import org.ametys.runtime.config.ConfigManager; 025import org.ametys.runtime.model.ElementDefinition; 026import org.ametys.runtime.model.type.ModelItemTypeConstants; 027import org.ametys.runtime.parameter.Validator; 028import org.ametys.runtime.plugin.component.AbstractLogEnabled; 029 030/** 031 * Implementation of {@link DataSourceConsumer} allowing to know whether a data source is used in the configuration or not. 032 * It also allows to retrieve the data source ids that are currently in use. 033 */ 034public class ConfigurationDataSourceConsumer extends AbstractLogEnabled implements DataSourceConsumer 035{ 036 @Override 037 public TypeOfUse isInUse(String id) 038 { 039 Map<String, TypeOfUse> usedDataSourceIds = getUsedDataSourceIds(); 040 return usedDataSourceIds.containsKey(id) ? usedDataSourceIds.get(id) : TypeOfUse.NOT_USED; 041 } 042 043 @Override 044 public Map<String, TypeOfUse> getUsedDataSourceIds() 045 { 046 Map<String, TypeOfUse> usedDataSourceIds = new HashMap<>(); 047 Config config = Config.getInstance(); 048 if (config != null) 049 { 050 Map<String, Object> values = new HashMap<> (); 051 try 052 { 053 values = config.getValues(); 054 } 055 catch (Exception e) 056 { 057 getLogger().error("Cannot read the configuration file.", e); 058 } 059 060 ConfigManager configManager = ConfigManager.getInstance(); 061 Collection<ElementDefinition> definitions = configManager.getConfigurationParameters(); 062 for (ElementDefinition definition : definitions) 063 { 064 if (ModelItemTypeConstants.DATASOURCE_ELEMENT_TYPE_ID.equals(definition.getType().getId()) && !configManager.evaluateDisableConditions(definition.getDisableConditions(), values)) 065 { 066 // A data source is not considered used when it is disabled 067 // Type of use depends on mandatory property 068 usedDataSourceIds.put(config.getValue(definition.getName()), _isMandatory(definition) ? TypeOfUse.BLOCKING : TypeOfUse.NON_BLOCKING); 069 } 070 } 071 } 072 073 return usedDataSourceIds; 074 } 075 076 private boolean _isMandatory(ElementDefinition def) 077 { 078 Validator validator = def.getValidator(); 079 if (validator != null) 080 { 081 return validator.getConfiguration().containsKey("mandatory") && (Boolean) validator.getConfiguration().get("mandatory"); 082 } 083 084 return false; 085 } 086}