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.util.Loader;
020import ch.qos.logback.core.util.OptionHelper;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.InputStream;
025import java.net.MalformedURLException;
026import java.net.URI;
027import java.net.URL;
028
029abstract public class ResourceHandlerBase extends ModelHandlerBase {
030
031    protected String attributeInUse;
032    protected boolean optional;
033
034    protected ResourceHandlerBase(Context context) {
035        super(context);
036    }
037
038    protected InputStream openURL(URL url) {
039        try {
040            return url.openStream();
041        } catch (IOException e) {
042            warnIfRequired("Failed to open [" + url.toString() + "]");
043            return null;
044        }
045    }
046
047    protected boolean checkAttributes(ResourceModel resourceModel) {
048        String fileAttribute = resourceModel.getFile();
049        String urlAttribute = resourceModel.getUrl();
050        String resourceAttribute = resourceModel.getResource();
051
052        int count = 0;
053
054        if (!OptionHelper.isNullOrEmptyOrAllSpaces(fileAttribute)) {
055            count++;
056        }
057        if (!OptionHelper.isNullOrEmptyOrAllSpaces(urlAttribute)) {
058            count++;
059        }
060        if (!OptionHelper.isNullOrEmptyOrAllSpaces(resourceAttribute)) {
061            count++;
062        }
063
064        if (count == 0) {
065            addError("One of \"path\", \"resource\" or \"url\" attributes must be set.");
066            return false;
067        } else if (count > 1) {
068            addError("Only one of \"file\", \"url\" or \"resource\" attributes should be set.");
069            return false;
070        } else if (count == 1) {
071            return true;
072        }
073        throw new IllegalStateException("Count value [" + count + "] is not expected");
074    }
075
076
077    protected String getAttribureInUse() {
078        return this.attributeInUse;
079    }
080
081    protected URL getInputURL(ModelInterpretationContext mic, ResourceModel resourceModel) {
082        String fileAttribute = resourceModel.getFile();
083        String urlAttribute = resourceModel.getUrl();
084        String resourceAttribute = resourceModel.getResource();
085
086        if (!OptionHelper.isNullOrEmptyOrAllSpaces(fileAttribute)) {
087            this.attributeInUse = mic.subst(fileAttribute);
088            return filePathAsURL(attributeInUse);
089        }
090
091        if (!OptionHelper.isNullOrEmptyOrAllSpaces(urlAttribute)) {
092            this.attributeInUse = mic.subst(urlAttribute);
093            return attributeToURL(attributeInUse);
094        }
095
096        if (!OptionHelper.isNullOrEmptyOrAllSpaces(resourceAttribute)) {
097            this.attributeInUse = mic.subst(resourceAttribute);
098            return resourceAsURL(attributeInUse);
099        }
100        // given preceding checkAttributes() check we cannot reach this line
101        throw new IllegalStateException("A URL stream should have been returned at this stage");
102
103    }
104
105    protected URL filePathAsURL(String path) {
106        URI uri = new File(path).toURI();
107        try {
108            return uri.toURL();
109        } catch (MalformedURLException e) {
110            // impossible to get here
111            e.printStackTrace();
112            return null;
113        }
114    }
115
116    protected URL attributeToURL(String urlAttribute) {
117        try {
118            return new URL(urlAttribute);
119        } catch (MalformedURLException mue) {
120            String errMsg = "URL [" + urlAttribute + "] is not well formed.";
121            addError(errMsg, mue);
122            return null;
123        }
124    }
125
126    protected URL resourceAsURL(String resourceAttribute) {
127        URL url = Loader.getResourceBySelfClassLoader(resourceAttribute);
128        if (url == null) {
129            warnIfRequired("Could not find resource corresponding to [" + resourceAttribute + "]");
130            return null;
131        } else
132            return url;
133    }
134
135    protected void warnIfRequired(String msg) {
136        if (!optional) {
137            addWarn(msg);
138        }
139    }
140}