1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.model.processor;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.model.ResourceModel;
19  import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
20  import ch.qos.logback.core.util.Loader;
21  import ch.qos.logback.core.util.OptionHelper;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.net.MalformedURLException;
27  import java.net.URI;
28  import java.net.URL;
29  
30  abstract public class ResourceHandlerBase extends ModelHandlerBase {
31  
32      protected String attributeInUse;
33      protected boolean optional;
34  
35      protected ResourceHandlerBase(Context context) {
36          super(context);
37      }
38  
39      protected InputStream openURL(URL url) {
40          try {
41              return url.openStream();
42          } catch (IOException e) {
43              warnIfRequired("Failed to open [" + url.toString() + "]");
44              return null;
45          }
46      }
47  
48      protected boolean checkAttributes(ResourceModel resourceModel) {
49          String fileAttribute = resourceModel.getFile();
50          String urlAttribute = resourceModel.getUrl();
51          String resourceAttribute = resourceModel.getResource();
52  
53          int count = 0;
54  
55          if (!OptionHelper.isNullOrEmptyOrAllSpaces(fileAttribute)) {
56              count++;
57          }
58          if (!OptionHelper.isNullOrEmptyOrAllSpaces(urlAttribute)) {
59              count++;
60          }
61          if (!OptionHelper.isNullOrEmptyOrAllSpaces(resourceAttribute)) {
62              count++;
63          }
64  
65          if (count == 0) {
66              addError("One of \"path\", \"resource\" or \"url\" attributes must be set.");
67              return false;
68          } else if (count > 1) {
69              addError("Only one of \"file\", \"url\" or \"resource\" attributes should be set.");
70              return false;
71          } else if (count == 1) {
72              return true;
73          }
74          throw new IllegalStateException("Count value [" + count + "] is not expected");
75      }
76  
77  
78      protected String getAttribureInUse() {
79          return this.attributeInUse;
80      }
81  
82      protected URL getInputURL(ContextAwarePropertyContainer contextAwarePropertyContainer, ResourceModel resourceModel) {
83          String fileAttribute = resourceModel.getFile();
84          String urlAttribute = resourceModel.getUrl();
85          String resourceAttribute = resourceModel.getResource();
86  
87          if (!OptionHelper.isNullOrEmptyOrAllSpaces(fileAttribute)) {
88              this.attributeInUse = contextAwarePropertyContainer.subst(fileAttribute);
89              return filePathAsURL(attributeInUse);
90          }
91  
92          if (!OptionHelper.isNullOrEmptyOrAllSpaces(urlAttribute)) {
93              this.attributeInUse = contextAwarePropertyContainer.subst(urlAttribute);
94              return attributeToURL(attributeInUse);
95          }
96  
97          if (!OptionHelper.isNullOrEmptyOrAllSpaces(resourceAttribute)) {
98              this.attributeInUse = contextAwarePropertyContainer.subst(resourceAttribute);
99              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 }