001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender;
018
019import java.io.Serializable;
020import java.util.HashMap;
021import java.util.Map;
022import java.util.concurrent.TimeUnit;
023
024import org.apache.logging.log4j.core.Filter;
025import org.apache.logging.log4j.core.Layout;
026import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
027import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
028import org.apache.logging.log4j.core.net.Advertiser;
029
030/**
031 * Abstract File Appender.
032 */
033public abstract class AbstractFileAppender<M extends OutputStreamManager> extends AbstractOutputStreamAppender<M> {
034
035    /**
036     * Builds FileAppender instances.
037     * 
038     * @param <B>
039     *            The type to build
040     */
041    public abstract static class Builder<B extends Builder<B>> extends AbstractOutputStreamAppender.Builder<B> {
042
043        @PluginBuilderAttribute
044        @Required
045        private String fileName;
046
047        @PluginBuilderAttribute
048        private boolean append = true;
049
050        @PluginBuilderAttribute
051        private boolean locking;
052
053        @PluginBuilderAttribute
054        private boolean advertise;
055
056        @PluginBuilderAttribute
057        private String advertiseUri;
058
059        @PluginBuilderAttribute
060        private boolean createOnDemand;
061
062        @PluginBuilderAttribute
063        private String filePermissions;
064
065        @PluginBuilderAttribute
066        private String fileOwner;
067
068        @PluginBuilderAttribute
069        private String fileGroup;
070
071        public String getAdvertiseUri() {
072            return advertiseUri;
073        }
074
075        public String getFileName() {
076            return fileName;
077        }
078
079        public boolean isAdvertise() {
080            return advertise;
081        }
082
083        public boolean isAppend() {
084            return append;
085        }
086
087        public boolean isCreateOnDemand() {
088            return createOnDemand;
089        }
090
091        public boolean isLocking() {
092            return locking;
093        }
094
095        public String getFilePermissions() {
096            return filePermissions;
097        }
098
099        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}