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.workspace;
017
018import java.io.File;
019import java.io.IOException;
020import java.net.MalformedURLException;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.excalibur.source.Source;
027import org.apache.excalibur.source.SourceFactory;
028import org.apache.excalibur.source.SourceResolver;
029import org.apache.excalibur.source.impl.FileSource;
030
031/**
032 * SourceFactory handling resources URIs for workspaces.
033 * Workspace resources can be found of File System, in a JAR, ...
034 */
035public class WorkspaceSourceFactory implements SourceFactory, Serviceable
036{
037    private static final String __PROTOCOL_SEPARATOR = "://"; 
038    
039    private SourceResolver _resolver;
040    
041    public Source getSource(String location, Map parameters) throws IOException
042    {
043        int protocolIndex = location.indexOf(__PROTOCOL_SEPARATOR);
044        
045        if (protocolIndex == -1)
046        {
047            throw new MalformedURLException("URI must be like workspace:<workspace name>://path/to/resource. Location was '" + location + "'");
048        }
049        
050        int index = location.indexOf(':');
051        
052        if (index == protocolIndex)
053        {
054            throw new MalformedURLException("URI must be like workspace:<workspace name>://path/to/resource. Location was '" + location + "'");
055        }
056        
057        // 2 = __PROTOCOL_SEPARATOR.length - 1 (pour garder le / du début de chaine)
058        String path = location.substring(protocolIndex + 2); 
059        
060        String workspaceName = location.substring(index + 1, protocolIndex);
061        
062        String resourceURI = WorkspaceManager.getInstance().getBaseURI(workspaceName);
063        
064        if (resourceURI == null)
065        {
066            File pluginLocation = WorkspaceManager.getInstance().getLocation(workspaceName);
067            return new FileSource("file", new File(pluginLocation, path));
068        }
069        else
070        {
071            return _resolver.resolveURI("resource:/" + resourceURI + path);
072        }
073    }
074    
075    public void release(Source source)
076    {
077        // empty method
078    }
079
080    public void service(ServiceManager manager) throws ServiceException
081    {
082        _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
083    }
084}