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