View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.script;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.IOException;
22  import java.io.InputStreamReader;
23  import java.io.Reader;
24  import java.net.URI;
25  import java.nio.charset.Charset;
26  import java.nio.file.Path;
27  import java.nio.file.Paths;
28  
29  import org.apache.logging.log4j.Logger;
30  import org.apache.logging.log4j.core.config.Node;
31  import org.apache.logging.log4j.core.config.plugins.Plugin;
32  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
33  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
34  import org.apache.logging.log4j.core.util.ExtensionLanguageMapping;
35  import org.apache.logging.log4j.core.util.FileUtils;
36  import org.apache.logging.log4j.core.util.IOUtils;
37  import org.apache.logging.log4j.core.util.NetUtils;
38  import org.apache.logging.log4j.status.StatusLogger;
39  
40  /**
41   * Container for the language and body of a script file along with the file location.
42   */
43  @Plugin(name = "ScriptFile", category = Node.CATEGORY, printObject = true)
44  public class ScriptFile extends AbstractScript {
45  
46      private static final Logger LOGGER = StatusLogger.getLogger();
47      private final Path filePath;
48      private final boolean isWatched;
49  
50  
51      public ScriptFile(String name, Path filePath, String language, boolean isWatched, String scriptText) {
52          super(name, language, scriptText);
53          this.filePath = filePath;
54          this.isWatched = isWatched;
55      }
56  
57      public Path getPath() {
58          return this.filePath;
59      }
60  
61      public boolean isWatched() {
62          return isWatched;
63      }
64  
65      @PluginFactory
66      public static ScriptFile createScript(
67              // @formatter:off
68              @PluginAttribute("name") String name,
69              @PluginAttribute("language") String language,
70              @PluginAttribute("path") final String filePathOrUri,
71              @PluginAttribute("isWatched") final Boolean isWatched,
72              @PluginAttribute("charset") final Charset charset) {
73              // @formatter:on
74          if (filePathOrUri == null) {
75              LOGGER.error("No script path provided for ScriptFile");
76              return null;
77          }
78          if (name == null) {
79              name = filePathOrUri;
80          }
81          final URI uri = NetUtils.toURI(filePathOrUri);
82          final File file = FileUtils.fileFromUri(uri);
83          if (language == null && file != null) {
84              String fileExtension = FileUtils.getFileExtension(file);
85              if (fileExtension != null) {
86                  ExtensionLanguageMapping mapping = ExtensionLanguageMapping.getByExtension(fileExtension);
87                  if (mapping != null) {
88                      language = mapping.getLanguage();
89                  }
90              }
91          }
92          if (language == null) {
93              LOGGER.info("No script language supplied, defaulting to {}", DEFAULT_LANGUAGE);
94              language = DEFAULT_LANGUAGE;
95          }
96  
97          final Charset actualCharset = charset == null ? Charset.defaultCharset() : charset;
98          String scriptText;
99          try (final Reader reader = new InputStreamReader(
100                 file != null ? new FileInputStream(file) : uri.toURL().openStream(), actualCharset)) {
101             scriptText = IOUtils.toString(reader);
102         } catch (IOException e) {
103             LOGGER.error("{}: language={}, path={}, actualCharset={}", e.getClass().getSimpleName(),
104                     language, filePathOrUri, actualCharset);
105             return null;
106         }
107         Path path = file != null ? Paths.get(file.toURI()) : Paths.get(uri);
108         if (path == null) {
109             LOGGER.error("Unable to convert {} to a Path", uri.toString());
110             return null;
111         }
112         return new ScriptFile(name, path, language, isWatched == null ? Boolean.FALSE : isWatched, scriptText);
113     }
114 
115     @Override
116     public String toString() {
117         StringBuilder sb = new StringBuilder();
118         if (!(getName().equals(filePath.toString()))) {
119             sb.append("name=").append(getName()).append(", ");
120         }
121         sb.append("path=").append(filePath);
122         if (getLanguage() != null) {
123             sb.append(", language=").append(getLanguage());
124         }
125         sb.append(", isWatched=").append(isWatched);
126         return sb.toString();
127     }
128 }