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