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.runtime.plugins.admin.configuration; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.component.Component; 024 025import org.ametys.core.ui.Callable; 026import org.ametys.runtime.config.ConfigManager; 027import org.ametys.runtime.config.ConfigParameterCheckerDescriptor; 028import org.ametys.runtime.model.checker.ItemChecker; 029import org.ametys.runtime.plugin.component.AbstractLogEnabled; 030 031/** 032 * Fetch the check values and dispatch the requests to the appropriate parameter checkers. 033 */ 034public class CheckConfigHelper extends AbstractLogEnabled implements Component 035{ 036 /** 037 * Check the configuration values for the requested checkers 038 * @param fieldCheckersInfo a JSON map of the checkers info 039 * @return a map with each failed checker id and the associated message 040 */ 041 @Callable(rights = "Runtime_Rights_Admin_Access", context = "/admin") 042 public Map<String, Object> check(Map<String, Object> fieldCheckersInfo) 043 { 044 Map<String, Object> result = new HashMap<> (); 045 046 // Handle the 'configuration not initialized' case 047 Map<String, Object> oldValues = ConfigManager.getInstance().getOldValues(); 048 049 // Prepare the values for the test 050 Map<String, Object> valuesByParameterChecker = _getValuesByParamCheckerId(fieldCheckersInfo, oldValues); 051 052 // Dispatch the requests 053 for (String fieldCheckerId : valuesByParameterChecker.keySet()) 054 { 055 @SuppressWarnings("unchecked") 056 ItemChecker fieldChecker = (ItemChecker) ((Map<String, Object>) valuesByParameterChecker.get(fieldCheckerId)).get("checker"); 057 @SuppressWarnings("unchecked") 058 List<String> values = (List<String>) ((Map<String, Object>) valuesByParameterChecker.get(fieldCheckerId)).get("values"); 059 try 060 { 061 fieldChecker.check(values); 062 } 063 catch (Throwable t) 064 { 065 getLogger().error("The check '" + fieldCheckerId + "' failed : \n" + t.getMessage(), t); 066 String msg = t.getMessage() != null ? t.getMessage() : "Unknown error"; 067 result.put(fieldCheckerId, msg); 068 } 069 } 070 071 return result; 072 } 073 074 /** 075 * Compute the proper values and {@link ItemChecker} implementations to use for the test and order them by parameter checker id 076 * @param paramCheckersInfo the information concerning the parameter checkers 077 * @param oldValues the map of old values of the configuration 078 * @return the map of values lists ordered by parameter checker 079 */ 080 @SuppressWarnings("unchecked") 081 private Map<String, Object> _getValuesByParamCheckerId(Map<String, Object> paramCheckersInfo, Map<String, Object> oldValues) 082 { 083 Map<String, Object> result = new HashMap<> (); 084 085 ConfigManager configManager = ConfigManager.getInstance(); 086 for (String paramCheckerId : paramCheckersInfo.keySet()) 087 { 088 Map<String, Object> valuesByParamCheckerId = new HashMap<> (); 089 090 // Check the ids of the parameter checkers and build the parameter checkers' list 091 ConfigParameterCheckerDescriptor parameterCheckerDescriptor = configManager.getParameterChecker(paramCheckerId); 092 if (parameterCheckerDescriptor == null) 093 { 094 throw new IllegalArgumentException("The parameter checker '" + paramCheckerId + "' was not found."); 095 } 096 097 ItemChecker parameterChecker = parameterCheckerDescriptor.getParameterChecker(); 098 099 valuesByParamCheckerId.put("checker", parameterChecker); 100 101 List<String> paramNames = (List<String>) ((Map<String, Object>) paramCheckersInfo.get(paramCheckerId)).get("testParamsNames"); 102 List<Object> paramRawValues = (List<Object>) ((Map<String, Object>) paramCheckersInfo.get(paramCheckerId)).get("rawTestValues"); 103 104 List<String> values = new ArrayList<> (); 105 106 // Compute the proper values for the test 107 for (int i = 0; i < paramNames.size(); i++) 108 { 109 String parameterName = paramNames.get(i); 110 Object value = paramRawValues.get(i); 111 112 // Resolve the value 113 String resolvedValue = configManager.resolveValueForParameterChecker(parameterName, value, oldValues); 114 values.add(resolvedValue); 115 } 116 117 valuesByParamCheckerId.put("values", values); 118 119 result.put(paramCheckerId, valuesByParamCheckerId); 120 } 121 122 return result; 123 } 124}