001/*
002 *  Copyright 2025 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.web.analytics;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.excalibur.source.Source;
026import org.apache.excalibur.source.SourceFactory;
027import org.apache.excalibur.source.SourceResolver;
028
029import org.ametys.runtime.plugin.component.AbstractLogEnabled;
030
031/**
032 * Resolve the protocol wa://tracking seeking xsl file to track site
033 */
034public class WebAnalyticsSourceFactory extends AbstractLogEnabled implements SourceFactory, Serviceable
035{
036    /** Scheme for site tracking */
037    protected static final String __WA_SCHEME = "wa";
038
039    /** URL for site tracking */
040    protected static final String __WA_URL = __WA_SCHEME + "://tracking";
041
042    /** The source resolver */
043    protected SourceResolver _sourceResolver;
044    
045    /** The web analytics helper */
046    protected WebAnalyticsHelper _waHelper;
047    
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
051        _waHelper = (WebAnalyticsHelper) manager.lookup(WebAnalyticsHelper.ROLE);
052    }
053    
054    public Source getSource(String location, Map parameters) throws IOException, MalformedURLException
055    {
056        if (__WA_URL.equals(location))
057        {
058            WebAnalyticsProvider selectedWAProvider = _waHelper.getSelectedProvider();
059            return _getSource(location, selectedWAProvider.getXSLTPath());
060        }
061        
062        getLogger().error("Can't find xsl for location '" + location + "'. Only " + __WA_URL + " is accepted for the moment.");
063        return null;
064    }
065
066    private Source _getSource(String location, String path) throws IOException
067    {
068        if (getLogger().isInfoEnabled())
069        {
070            getLogger().info("For protocol '" + location + "', try to resolve the source with URI '" + path + "'");
071        }
072        return _sourceResolver.resolveURI(path);
073    }
074    
075    public void release(Source source)
076    {
077        // Nothing to do
078        
079    }
080    
081}