001/*
002 *  Copyright 2020 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.plugins.core.ui.resources.vuejs;
017
018import java.io.IOException;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.excalibur.source.Source;
023import org.apache.excalibur.source.SourceResolver;
024
025import org.ametys.core.resources.AbstractResourceHandlerProvider;
026import org.ametys.core.resources.ResourceHandler;
027import org.ametys.core.resources.ResourceHandlerProvider;
028import org.ametys.plugins.core.ui.resources.vuejs.VueJsResourceHandler.LocationParser;
029
030/**
031 * {@link ResourceHandlerProvider} for compiled VueJS files.
032 */
033public class VueJsResourceHandlerProvider extends AbstractResourceHandlerProvider
034{
035    private SourceResolver _resolver;
036    
037    @Override
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        super.service(manager);
041        _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
042    }
043    
044    public int getPriority()
045    {
046        return MIN_PRIORITY + 10_000;
047    }
048    
049    @Override
050    public ResourceHandler getResourceHandler(String source) throws Exception
051    {
052        LocationParser lp = new LocationParser(source);
053        if (!lp.matches())
054        {
055            return null;
056        }
057        
058        try
059        {
060            Source src = _resolver.resolveURI(source);
061            if (!src.exists())
062            {
063                return setup(new VueJsResourceHandler(src));
064            }
065            else
066            {
067                _resolver.release(src);
068            }
069        }
070        catch (IOException e)
071        {
072            // Nothing
073        }
074        
075        return null;
076    }
077}