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.datasource; 017 018import java.util.ArrayList; 019import java.util.Collections; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026 027import org.ametys.core.datasource.DataSourceConsumerExtensionPoint; 028import org.ametys.core.datasource.LDAPDataSourceManager; 029import org.ametys.core.datasource.SQLDataSourceManager; 030import org.ametys.core.datasource.AbstractDataSourceManager.DataSourceDefinition; 031import org.ametys.runtime.i18n.I18nizableText; 032import org.ametys.runtime.parameter.ParameterCheckerTestFailureException; 033import org.ametys.runtime.plugins.admin.notificator.AbstractConfigurableAdministratorNotificator; 034import org.ametys.runtime.plugins.admin.notificator.Notification; 035 036/** 037 * Notificator for unused datasources which have a invalid configuration 038 */ 039public class InvalidDataSourceNotificator extends AbstractConfigurableAdministratorNotificator implements Serviceable 040{ 041 private SQLDataSourceManager _sqlDataSourceManager; 042 private LDAPDataSourceManager _ldapDataSourceManager; 043 private DataSourceConsumerExtensionPoint _dataSourceConsumerEP; 044 045 @Override 046 public void service(ServiceManager smanager) throws ServiceException 047 { 048 _sqlDataSourceManager = (SQLDataSourceManager) smanager.lookup(SQLDataSourceManager.ROLE); 049 _ldapDataSourceManager = (LDAPDataSourceManager) smanager.lookup(LDAPDataSourceManager.ROLE); 050 _dataSourceConsumerEP = (DataSourceConsumerExtensionPoint) smanager.lookup(DataSourceConsumerExtensionPoint.ROLE); 051 } 052 053 @Override 054 public List<Notification> getNotifications() 055 { 056 List<Notification> notifications = new ArrayList<>(); 057 Map<String, DataSourceDefinition> sqlDataSources = _sqlDataSourceManager.getDataSourceDefinitions(true, true, false); 058 for (String id : sqlDataSources.keySet()) 059 { 060 DataSourceDefinition datasource = sqlDataSources.get(id); 061 if (!_dataSourceConsumerEP.isInUse(id)) 062 { 063 _checkSqlDatasource(datasource, notifications); 064 } 065 } 066 Map<String, DataSourceDefinition> ldapDataSources = _ldapDataSourceManager.getDataSourceDefinitions(true, true, false); 067 for (String id : ldapDataSources.keySet()) 068 { 069 DataSourceDefinition datasource = ldapDataSources.get(id); 070 if (!_dataSourceConsumerEP.isInUse(id)) 071 { 072 _checkLdapDatasource(datasource, notifications); 073 } 074 } 075 return notifications; 076 } 077 078 private Notification _getNotification(String datasourceName) 079 { 080 I18nizableText message = new I18nizableText(_message.getCatalogue(), _message.getKey(), Collections.singletonList(datasourceName)); 081 return new Notification(_type, new I18nizableText(_title.getCatalogue(), _title.getKey()), message, _iconGlyph, _action); 082 } 083 084 private void _checkSqlDatasource(DataSourceDefinition datasource, List<Notification> notifications) 085 { 086 try 087 { 088 _sqlDataSourceManager.checkParameters (datasource.getParameters()); 089 } 090 catch (ParameterCheckerTestFailureException e) 091 { 092 notifications.add(_getNotification(datasource.getName().getLabel())); 093 } 094 } 095 096 private void _checkLdapDatasource(DataSourceDefinition datasource, List<Notification> notifications) 097 { 098 try 099 { 100 _ldapDataSourceManager.checkParameters (datasource.getParameters()); 101 } 102 catch (ParameterCheckerTestFailureException e) 103 { 104 notifications.add(_getNotification(datasource.getName().getLabel())); 105 } 106 } 107 108}