001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2024, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.core.model.processor;
016
017import ch.qos.logback.core.Context;
018import ch.qos.logback.core.model.ResourceModel;
019import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
020import ch.qos.logback.core.util.Loader;
021import ch.qos.logback.core.util.OptionHelper;
022
023import java.io.File;
024import java.io.IOException;
025import java.io.InputStream;
026import java.net.MalformedURLException;
027import java.net.URI;
028import java.net.URL;
029
030abstract public class ResourceHandlerBase extends ModelHandlerBase {
031
032    protected String attributeInUse;
033    protected boolean optional;
034
035    protected ResourceHandlerBase(Context context) {
036        super(context);
037    }
038
039    protected InputStream openURL(URL url) {
040        try {
041            return url.openStream();
042        } catch (IOException e) {
043            warnIfRequired("Failed to open [" + url.toString() + "]");
044            return null;
045        }
046    }
047
048    protected boolean checkAttributes(ResourceModel resourceModel) {
049        String fileAttribute = resourceModel.getFile();
050        String urlAttribute = resourceModel.getUrl();
051        String resourceAttribute = resourceModel.getResource();
052
053        int count = 0;
054
055        if (!OptionHelper.isNullOrEmptyOrAllSpaces(fileAttribute)) {
056            count++;
057        }
058        if (!OptionHelper.isNullOrEmptyOrAllSpaces(urlAttribute)) {
059            count++;
060        }
061        if (!OptionHelper.isNullOrEmptyOrAllSpaces(resourceAttribute)) {
062            count++;
063        }
064
065        if (count == 0) {
066            addError("One of \"path\", \"resource\" or \"url\" attributes must be set.");
067            return false;
068        } else if (count > 1) {
069            addError("Only one of \"file\", \"url\" or \"resource\" attributes should be set.");
070            return false;
071        } else if (count == 1) {
072            return true;
073        }
074        throw new IllegalStateException("Count value [" + count + "] is not expected");
075    }
076
077
078    protected String getAttribureInUse() {
079        return this.attributeInUse;
080    }
081
082    protected URL getInputURL(ContextAwarePropertyContainer contextAwarePropertyContainer, ResourceModel resourceModel) {
083        String fileAttribute = resourceModel.getFile();
084        String urlAttribute = resourceModel.getUrl();
085        String resourceAttribute = resourceModel.getResource();
086
087        if (!OptionHelper.isNullOrEmptyOrAllSpaces(fileAttribute)) {
088            this.attributeInUse = contextAwarePropertyContainer.subst(fileAttribute);
089            return filePathAsURL(attributeInUse);
090        }
091
092        if (!OptionHelper.isNullOrEmptyOrAllSpaces(urlAttribute)) {
093            this.attributeInUse = contextAwarePropertyContainer.subst(urlAttribute);
094            return attributeToURL(attributeInUse);
095        }
096
097        if (!OptionHelper.isNullOrEmptyOrAllSpaces(resourceAttribute)) {
098            this.attributeInUse = contextAwarePropertyContainer.subst(resourceAttribute);
099            return resourceAsURL(attributeInUse);
100        }
101        // given preceding checkAttributes() check we cannot reach this line
102        throw new IllegalStateException("A URL stream should have been returned at this stage");
103
104    }
105
106    protected URL filePathAsURL(String path) {
107        URI uri = new File(path).toURI();
108        try {
109            return uri.toURL();
110        } catch (MalformedURLException e) {
111            // impossible to get here
112            e.printStackTrace();
113            return null;
114        }
115    }
116
117    protected URL attributeToURL(String urlAttribute) {
118        try {
119            return new URL(urlAttribute);
120        } catch (MalformedURLException mue) {
121            String errMsg = "URL [" + urlAttribute + "] is not well formed.";
122            addError(errMsg, mue);
123            return null;
124        }
125    }
126
127    protected URL resourceAsURL(String resourceAttribute) {
128        URL url = Loader.getResourceBySelfClassLoader(resourceAttribute);
129        if (url == null) {
130            warnIfRequired("Could not find resource corresponding to [" + resourceAttribute + "]");
131            return null;
132        } else
133            return url;
134    }
135
136    protected void warnIfRequired(String msg) {
137        if (!optional) {
138            addWarn(msg);
139        }
140    }
141}