001/*
002 *  Copyright 2020 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.migration.handler.impl;
017
018import java.util.List;
019
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.commons.lang3.StringUtils;
026import org.slf4j.LoggerFactory;
027
028import org.ametys.core.migration.MigrationException;
029import org.ametys.core.migration.NotMigrableInSafeModeException;
030import org.ametys.core.migration.configuration.VersionConfiguration;
031import org.ametys.core.migration.configuration.impl.JavaVersionConfiguration;
032import org.ametys.core.migration.handler.VersionHandler;
033import org.ametys.core.migration.version.Version;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035
036/**
037 * Version Handler which use a java class as version storage
038 */
039public class JavaVersionHandler extends AbstractLogEnabled implements VersionHandler, Serviceable
040{
041    /** Service Manager */
042    protected ServiceManager _manager;
043    
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _manager = manager;
047    }
048
049    public List<Version> getCurrentVersions(String componentId, VersionConfiguration configuration) throws MigrationException, NotMigrableInSafeModeException
050    {
051        getLogger().debug("Get versions for component id: " + componentId);
052        if (!(configuration instanceof JavaVersionConfiguration))
053        {
054            throw new MigrationException("The version configuration object should be an instance of JavaVersionConfiguration");
055        }
056        
057        JavaVersionConfiguration javaVersionConfiguration = (JavaVersionConfiguration) configuration;
058
059        JavaVersionHandlerComponent handler = javaVersionConfiguration.getHandler();
060        
061        return handler.getVersions(componentId, javaVersionConfiguration, LoggerFactory.getLogger(getLogger().getName() + "." + componentId));
062    }
063
064    public void addVersion(Version newVersion) throws MigrationException
065    {
066        throw new UnsupportedOperationException("addVersion is not supported by JavaVersionHandler, the code should user another VersionHandler implementation to store the versions (such as the SQLVersionHandler)");
067    }
068
069    public VersionConfiguration getConfiguration(String componentId, Configuration versionConfiguration) throws ConfigurationException, NotMigrableInSafeModeException
070    {
071        getLogger().debug("Get configuration for component id: " + componentId);
072        String role = versionConfiguration.getAttribute("role", null);
073        if (StringUtils.isBlank(role))
074        {
075            throw new ConfigurationException("In component '" + componentId + "', the role for the version can not be found.");
076        }
077        
078        try
079        {
080            JavaVersionHandlerComponent versionStorage = (JavaVersionHandlerComponent) _manager.lookup(role);
081            getLogger().debug("End get configuration for component id: " + componentId);
082            return new JavaVersionConfiguration(componentId, versionConfiguration, versionStorage);
083        }
084        catch (ClassCastException | ServiceException e)
085        {
086            throw new ConfigurationException("In component '" + componentId + "', the role for the version can not be found or is not a JavaVersionHandlerComponent.", e);
087        }
088    }
089
090}