001/*
002 *  Copyright 2010 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;
017
018import java.io.File;
019import java.io.FileInputStream;
020import java.io.FileNotFoundException;
021import java.io.InputStream;
022
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.w3c.dom.Element;
026
027import com.opensymphony.workflow.loader.XMLWorkflowFactory;
028
029/**
030 * A XML factory for finding workflows definitions in
031 * <code>context://WEB-INF/config/workflows.xml"</code>.
032 */
033public class XmlWorkflowFactory extends XMLWorkflowFactory
034{
035    // Path to the XML for the configuration of the workflow.
036    private static final String __CONFIG_FILE = "workflows.xml";
037
038    /** Logger for traces. */
039    protected Logger _logger = LoggerFactory.getLogger(getClass());
040    
041    private String _contextPath;
042
043    /**
044     * Constructor.
045     * @param contextPath The webapp context path 
046     * @throws Exception if an error occurs.
047     */
048    public XmlWorkflowFactory(String contextPath) throws Exception
049    {
050        _contextPath = contextPath;
051    }
052
053    @Override
054    protected InputStream getInputStream(String name)
055    {
056        String filename = null;
057        try
058        {
059            filename = new File(_contextPath, __CONFIG_FILE).getAbsolutePath();
060            return new FileInputStream(filename);
061        }
062        catch (FileNotFoundException e)
063        {
064            _logger.error("Missing workflow configuration file : " + filename, e);
065            return null;
066        }
067    }
068
069    @Override
070    protected String getBaseDir(Element root)
071    {
072        return new File(_contextPath).getPath();
073    }
074}