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.appender;
18  
19  import java.io.Serializable;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.concurrent.TimeUnit;
23  
24  import org.apache.logging.log4j.core.Filter;
25  import org.apache.logging.log4j.core.Layout;
26  import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
27  import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
28  import org.apache.logging.log4j.core.net.Advertiser;
29  
30  /**
31   * Abstract File Appender.
32   */
33  public abstract class AbstractFileAppender<M extends OutputStreamManager> extends AbstractOutputStreamAppender<M> {
34  
35      /**
36       * Builds FileAppender instances.
37       * 
38       * @param <B>
39       *            The type to build
40       */
41      public abstract static class Builder<B extends Builder<B>> extends AbstractOutputStreamAppender.Builder<B> {
42  
43          @PluginBuilderAttribute
44          @Required
45          private String fileName;
46  
47          @PluginBuilderAttribute
48          private boolean append = true;
49  
50          @PluginBuilderAttribute
51          private boolean locking;
52  
53          @PluginBuilderAttribute
54          private boolean advertise;
55  
56          @PluginBuilderAttribute
57          private String advertiseUri;
58  
59          @PluginBuilderAttribute
60          private boolean createOnDemand;
61  
62          @PluginBuilderAttribute
63          private String filePermissions;
64  
65          @PluginBuilderAttribute
66          private String fileOwner;
67  
68          @PluginBuilderAttribute
69          private String fileGroup;
70  
71          public String getAdvertiseUri() {
72              return advertiseUri;
73          }
74  
75          public String getFileName() {
76              return fileName;
77          }
78  
79          public boolean isAdvertise() {
80              return advertise;
81          }
82  
83          public boolean isAppend() {
84              return append;
85          }
86  
87          public boolean isCreateOnDemand() {
88              return createOnDemand;
89          }
90  
91          public boolean isLocking() {
92              return locking;
93          }
94  
95          public String getFilePermissions() {
96              return filePermissions;
97          }
98  
99          public String getFileOwner() {
100             return fileOwner;
101         }
102 
103         public String getFileGroup() {
104             return fileGroup;
105         }
106 
107         public B withAdvertise(final boolean advertise) {
108             this.advertise = advertise;
109             return asBuilder();
110         }
111 
112         public B withAdvertiseUri(final String advertiseUri) {
113             this.advertiseUri = advertiseUri;
114             return asBuilder();
115         }
116 
117         public B withAppend(final boolean append) {
118             this.append = append;
119             return asBuilder();
120         }
121 
122         public B withFileName(final String fileName) {
123             this.fileName = fileName;
124             return asBuilder();
125         }
126 
127         public B withCreateOnDemand(final boolean createOnDemand) {
128             this.createOnDemand = createOnDemand;
129             return asBuilder();
130         }
131 
132         public B withLocking(final boolean locking) {
133             this.locking = locking;
134             return asBuilder();
135         }
136 
137         public B withFilePermissions(final String filePermissions) {
138             this.filePermissions = filePermissions;
139             return asBuilder();
140         }
141 
142         public B withFileOwner(final String fileOwner) {
143             this.fileOwner = fileOwner;
144             return asBuilder();
145         }
146 
147         public B withFileGroup(final String fileGroup) {
148             this.fileGroup = fileGroup;
149             return asBuilder();
150         }
151 
152     }
153     
154     private final String fileName;
155 
156     private final Advertiser advertiser;
157 
158     private final Object advertisement;
159 
160     private AbstractFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
161             final M manager, final String filename, final boolean ignoreExceptions,
162             final boolean immediateFlush, final Advertiser advertiser) {
163 
164         super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
165         if (advertiser != null) {
166             final Map<String, String> configuration = new HashMap<>(layout.getContentFormat());
167             configuration.putAll(manager.getContentFormat());
168             configuration.put("contentType", layout.getContentType());
169             configuration.put("name", name);
170             advertisement = advertiser.advertise(configuration);
171         } else {
172             advertisement = null;
173         }
174         this.fileName = filename;
175         this.advertiser = advertiser;
176     }
177 
178     /**
179      * Returns the file name this appender is associated with.
180      * @return The File name.
181      */
182     public String getFileName() {
183         return this.fileName;
184     }
185 
186     @Override
187     public boolean stop(final long timeout, final TimeUnit timeUnit) {
188         setStopping();
189         super.stop(timeout, timeUnit, false);
190         if (advertiser != null) {
191             advertiser.unadvertise(advertisement);
192         }
193         setStopped();
194         return true;
195     }
196 }