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