001/*
002 *  Copyright 2021 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.plugins.workflow.definition;
017
018import java.io.InputStream;
019
020import org.apache.avalon.framework.component.Component;
021import org.apache.avalon.framework.configuration.Configurable;
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.excalibur.source.Source;
028import org.apache.excalibur.source.SourceResolver;
029
030import org.ametys.runtime.plugin.component.AbstractLogEnabled;
031import org.ametys.runtime.plugin.component.PluginAware;
032
033import com.opensymphony.workflow.loader.WorkflowDescriptor;
034import com.opensymphony.workflow.loader.WorkflowLoader;
035
036/**
037 * Object to describe a workflow.
038 */
039public class WorkflowDefinition extends AbstractLogEnabled implements Component, Serviceable, Configurable, PluginAware
040{
041    /** The source resolver */
042    protected SourceResolver _srcResolver;
043    
044    private String _location;
045    private WorkflowDescriptor _descriptor;
046    private String _id;
047    
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _srcResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
051    }
052    
053    public void configure(Configuration configuration) throws ConfigurationException
054    {
055        _location = configuration.getChild("file").getValue();
056        // Lazy loaded
057        _descriptor = null;
058    }
059    
060    public void setPluginInfo(String pluginName, String featureName, String id)
061    {
062        _id = id;
063    }
064    
065    /**
066     * Get the location of the workflow file.
067     * @return the location of the workflow file
068     */
069    public String getLocation()
070    {
071        return _location;
072    }
073    
074    /**
075     * Get or load the workflow descriptor.
076     * @param validate if the descriptor has to be valid
077     * @return the workflow descriptor
078     * @throws Exception if an error occurs
079     */
080    public WorkflowDescriptor getDescriptor(boolean validate) throws Exception
081    {
082        if (_descriptor == null)
083        {
084            getLogger().debug("Loading '{}' for the workflow '{}'.", _location, _id);
085            
086            Source source = null;
087            
088            try
089            {
090                source = _srcResolver.resolveURI(_location);
091                try (InputStream is = source.getInputStream())
092                {
093                    _descriptor = WorkflowLoader.load(is, validate);
094                }
095            }
096            finally
097            {
098                if (source != null)
099                {
100                    _srcResolver.release(source);
101                }
102            }
103            
104            _descriptor.setName(_id);
105        }
106        
107        return _descriptor;
108    }
109}