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.core.config.Node;
30  import org.apache.logging.log4j.core.config.plugins.Plugin;
31  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
32  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
33  import org.apache.logging.log4j.core.util.ExtensionLanguageMapping;
34  import org.apache.logging.log4j.core.util.FileUtils;
35  import org.apache.logging.log4j.core.util.IOUtils;
36  import org.apache.logging.log4j.core.util.NetUtils;
37  
38  /**
39   * Container for the language and body of a script file along with the file location.
40   */
41  @Plugin(name = "ScriptFile", category = Node.CATEGORY, printObject = true)
42  public class ScriptFile extends AbstractScript {
43  
44      private final Path filePath;
45      private final boolean isWatched;
46  
47  
48      public ScriptFile(final String name, final Path filePath, final String language, final boolean isWatched, final String scriptText) {
49          super(name, language, scriptText);
50          this.filePath = filePath;
51          this.isWatched = isWatched;
52      }
53  
54      public Path getPath() {
55          return this.filePath;
56      }
57  
58      public boolean isWatched() {
59          return isWatched;
60      }
61  
62      @PluginFactory
63      public static ScriptFile createScript(
64              // @formatter:off
65              @PluginAttribute("name") String name,
66              @PluginAttribute("language") String language,
67              @PluginAttribute("path") final String filePathOrUri,
68              @PluginAttribute("isWatched") final Boolean isWatched,
69              @PluginAttribute("charset") final Charset charset) {
70              // @formatter:on
71          if (filePathOrUri == null) {
72              LOGGER.error("No script path provided for ScriptFile");
73              return null;
74          }
75          if (name == null) {
76              name = filePathOrUri;
77          }
78          final URI uri = NetUtils.toURI(filePathOrUri);
79          final File file = FileUtils.fileFromUri(uri);
80          if (language == null && file != null) {
81              final String fileExtension = FileUtils.getFileExtension(file);
82              if (fileExtension != null) {
83                  final ExtensionLanguageMapping mapping = ExtensionLanguageMapping.getByExtension(fileExtension);
84                  if (mapping != null) {
85                      language = mapping.getLanguage();
86                  }
87              }
88          }
89          if (language == null) {
90              LOGGER.info("No script language supplied, defaulting to {}", DEFAULT_LANGUAGE);
91              language = DEFAULT_LANGUAGE;
92          }
93  
94          final Charset actualCharset = charset == null ? Charset.defaultCharset() : charset;
95          String scriptText;
96          try (final Reader reader = new InputStreamReader(
97                  file != null ? new FileInputStream(file) : uri.toURL().openStream(), actualCharset)) {
98              scriptText = IOUtils.toString(reader);
99          } catch (final IOException e) {
100             LOGGER.error("{}: language={}, path={}, actualCharset={}", e.getClass().getSimpleName(),
101                     language, filePathOrUri, actualCharset);
102             return null;
103         }
104         final Path path = file != null ? Paths.get(file.toURI()) : Paths.get(uri);
105         if (path == null) {
106             LOGGER.error("Unable to convert {} to a Path", uri.toString());
107             return null;
108         }
109         return new ScriptFile(name, path, language, isWatched == null ? Boolean.FALSE : isWatched, scriptText);
110     }
111 
112     @Override
113     public String toString() {
114         final StringBuilder sb = new StringBuilder();
115         if (!(getName().equals(filePath.toString()))) {
116             sb.append("name=").append(getName()).append(", ");
117         }
118         sb.append("path=").append(filePath);
119         if (getLanguage() != null) {
120             sb.append(", language=").append(getLanguage());
121         }
122         sb.append(", isWatched=").append(isWatched);
123         return sb.toString();
124     }
125 }