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.action.impl;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.core.migration.MigrationException;
025import org.ametys.core.migration.action.Action;
026import org.ametys.core.migration.action.data.ActionData;
027import org.ametys.core.migration.action.data.impl.JavaActionData;
028import org.ametys.core.migration.version.Version;
029import org.ametys.runtime.plugin.component.AbstractLogEnabled;
030
031/**
032 * SQL action : A script will be executed
033 */
034public class JavaAction extends AbstractLogEnabled implements Action, Serviceable
035{
036    /** service Manager */
037    protected ServiceManager _manager;
038    
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        _manager = manager;
042    }
043    
044    public void doAction(ActionData actionData) throws MigrationException
045    {
046        getLogger().debug("Start initialization for : {}", actionData.toString());
047        
048        if (!(actionData instanceof JavaActionData))
049        {
050            throw new MigrationException("Java Upgrade can only be created for a Java upgrade, this is not the case for upgrade : " + actionData.toString());
051        }
052        
053        JavaActionData javaActionData = (JavaActionData) actionData;
054        try
055        {
056            JavaActionComponent action = (JavaActionComponent) _manager.lookup(javaActionData.getRole());
057            action.doAction(javaActionData);
058        }
059        catch (ClassCastException | ServiceException e)
060        {
061            throw new MigrationException("In component '" + javaActionData.getVersion().getComponentId() + "', the role for the version can not be found or is not a JavaActionComponent.", e);
062        }
063        
064        getLogger().debug("End upgrade for : {}", actionData.toString());
065    }
066
067    
068    public ActionData generateActionData(String id, Version version, String comment, String from, String type, String pluginName, Configuration configuration) throws MigrationException, ConfigurationException
069    {
070        return new JavaActionData(id, version, comment, from, type, pluginName, configuration);
071    }
072}