001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconsole.base;
020
021import java.net.URL;
022import java.util.Locale;
023import java.util.ResourceBundle;
024import org.jgrapes.core.Channel;
025import org.jgrapes.core.Component;
026import org.jgrapes.core.annotation.Handler;
027import org.jgrapes.io.IOSubchannel;
028import org.jgrapes.webconsole.base.events.AddPageResources;
029import org.jgrapes.webconsole.base.events.ConsoleReady;
030import org.jgrapes.webconsole.base.events.PageResourceRequest;
031
032/**
033 * Base class for implementing components that add resources to
034 * the `<HEAD>` section of the web console page.
035 * 
036 * A derived class must implement a handler for {@link ConsoleReady}
037 * that generates an {@link AddPageResources} event. This
038 * will, in turn, result in a {@link PageResourceRequest} that must
039 * be handled by the derived class' 
040 * {@link #onResourceRequest(PageResourceRequest, IOSubchannel)} method.
041 * 
042 * @see AddPageResources
043 */
044public abstract class PageResourceProvider extends Component {
045
046    /**
047     * Creates a new component.
048     * 
049     * @param channel the channel to listen on
050     */
051    public PageResourceProvider(Channel channel) {
052        super(channel);
053    }
054
055    /**
056     * Provides a resource bundle for localization.
057     * The default implementation looks up a bundle using the
058     * package name plus "l10n" as base name.
059     * 
060     * @return the resource bundle
061     */
062    protected ResourceBundle resourceBundle(Locale locale) {
063        return ResourceBundle.getBundle(
064            getClass().getPackage().getName() + ".l10n", locale,
065            getClass().getClassLoader(),
066            ResourceBundle.Control.getNoFallbackControl(
067                ResourceBundle.Control.FORMAT_DEFAULT));
068    }
069
070    /**
071     * A default handler for resource requests. Searches for
072     * a file with the requested resource URI in the component's 
073     * class path and sets its {@link URL} as result if found.
074     * 
075     * @param event the resource request event
076     * @param channel the channel that the request was received on
077     */
078    @Handler
079    public final void onResourceRequest(
080            PageResourceRequest event, IOSubchannel channel) {
081        URL resourceUrl = this.getClass().getResource(
082            event.resourceUri().getPath());
083        if (resourceUrl == null) {
084            return;
085        }
086        event.setResult(new ResourceByUrl(event, resourceUrl));
087        event.stop();
088    }
089
090}