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.data;
017
018import java.io.File;
019import java.io.IOException;
020import java.net.MalformedURLException;
021import java.util.Map;
022
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024import org.apache.commons.lang3.StringUtils;
025import org.apache.excalibur.source.Source;
026import org.apache.excalibur.source.SourceFactory;
027import org.apache.excalibur.source.SourceUtil;
028import org.apache.excalibur.source.impl.FileSource;
029
030import org.ametys.runtime.servlet.RuntimeConfig;
031
032
033/**
034 * SourceFactory handling URIs in Ametys home.
035 */
036public class AmetysHomeSourceFactory extends AbstractLogEnabled implements SourceFactory
037{
038    private static final String __AMETYS_HOME_SOURCE_PREFIX = "ametys-home://";
039    
040    public Source getSource(String location, Map parameters) throws IOException
041    {
042        if (!StringUtils.startsWith(location, __AMETYS_HOME_SOURCE_PREFIX))
043        {
044            throw new MalformedURLException("URI must be like ametys-home://path/to/resource. Location was '" + location + "'");
045        }
046        
047        File ametysHomeDir = RuntimeConfig.getInstance().getAmetysHome();
048        String childLocation = StringUtils.removeStart(location, __AMETYS_HOME_SOURCE_PREFIX);
049        return new FileSource("file", new File(ametysHomeDir, SourceUtil.decodePath(childLocation)));
050    }
051    
052    public void release(Source source)
053    {
054        // empty method
055    }
056}