001/*
002 *  Copyright 2012 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.plugin;
017
018import java.io.File;
019import java.io.IOException;
020import java.net.MalformedURLException;
021import java.util.Map;
022import java.util.regex.Matcher;
023import java.util.regex.Pattern;
024
025import org.apache.avalon.framework.logger.AbstractLogEnabled;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.avalon.framework.service.Serviceable;
029import org.apache.excalibur.source.Source;
030import org.apache.excalibur.source.SourceFactory;
031import org.apache.excalibur.source.SourceNotFoundException;
032import org.apache.excalibur.source.SourceResolver;
033import org.apache.excalibur.source.impl.FileSource;
034
035
036/**
037 * SourceFactory handling resources URIs for plugins.
038 * Plugin resources can be found of File System, in a JAR, ...
039 */
040public class PluginSourceFactory extends AbstractLogEnabled implements SourceFactory, Serviceable
041{
042    private static final Pattern __SOURCE_PATTERN = Pattern.compile("^[\\w]+:(" + PluginsManager.PLUGIN_NAME_REGEXP + ")://(.*)$");
043    
044    private SourceResolver _resolver;
045    
046    public Source getSource(String location, Map parameters) throws IOException
047    {
048        Matcher m = __SOURCE_PATTERN.matcher(location);
049        if (!m.matches())
050        {
051            throw new MalformedURLException("URI must be like protocol:<plugin name>://path/to/resource. Location was '" + location + "'");
052        }
053        
054        String pluginName = m.group(1);
055        String path = m.group(2); 
056        
057        String resourceURI = PluginsManager.getInstance().getResourceURI(pluginName);
058        
059        if (resourceURI == null)
060        {
061            File pluginLocation = PluginsManager.getInstance().getPluginLocation(pluginName);
062            if (pluginLocation == null)
063            {
064                String errorMessage = "The plugin '" + pluginName + "' does not exist.";
065                if (getLogger().isWarnEnabled())
066                {
067                    getLogger().warn(errorMessage);
068                }
069                
070                throw new SourceNotFoundException(errorMessage);
071            }
072             
073            return new FileSource("file", new File(pluginLocation, path));
074        }
075        else
076        {
077            return _resolver.resolveURI(resourceURI + '/' + path);
078        }
079    }
080    
081    public void release(Source source)
082    {
083        // empty method
084    }
085
086    public void service(ServiceManager manager) throws ServiceException
087    {
088        _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
089    }
090}