001/**
002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC
003 * and GZip files. (http://jwat.org/)
004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jwat.arc;
019
020import java.net.InetAddress;
021import java.net.UnknownHostException;
022import java.text.DateFormat;
023import java.text.SimpleDateFormat;
024import java.util.Date;
025
026/**
027 * Default ARC file naming implementation used for writing to multiple files.
028 * Automatically add '.gz' to the extension.
029 * Format: prefix-date-sequenceNr-hostname.extension
030 *
031 * @author nicl
032 */
033public class ArcFileNamingDefault implements ArcFileNaming {
034
035    /** <code>DateFormat</code> to the following format 'yyyyMMddHHmmss'. */
036    protected DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
037
038    /** Prefix component. */
039    protected String filePrefix;
040
041    /** Date component. */
042    protected Date date;
043
044    /** Date component converted into a human readable string. */
045    protected String dateStr;
046
047    /** Host name component. */
048    protected String hostname;
049
050    /** Extension component (including leading "."). */
051    protected String extension;
052
053    /**
054     * Construct file naming instance.
055     * @param filePrefix prefix or null, will default to "JWAT"
056     * @param date date or null, if you want current date
057     * @param hostname host name or null, if you want to use default local host name
058     * @param extension extension or null, will default to ".arc"
059     */
060    public ArcFileNamingDefault(String filePrefix, Date date, String hostname, String extension) {
061        if (filePrefix != null) {
062            this.filePrefix = filePrefix;
063        } else {
064            this.filePrefix = "JWAT";
065        }
066        if (date != null) {
067            this.date = date;
068        } else {
069            this.date = new Date();
070        }
071        if (hostname != null ) {
072            this.hostname = hostname;
073        } else {
074            try {
075                this.hostname = InetAddress.getLocalHost().getHostName().toLowerCase();
076            } catch (UnknownHostException e) {
077                this.hostname = "unknown";
078            }
079        }
080        if (extension != null) {
081            this.extension = extension;
082        } else {
083            this.extension = ".arc";
084        }
085        dateStr = dateFormat.format(this.date);
086    }
087
088    @Override
089    public boolean supportMultipleFiles() {
090        return true;
091    }
092
093    @Override
094    public String getFilename(int sequenceNr, boolean bCompressed) {
095        String filename = filePrefix + "-" + dateStr + "-" + String.format("%05d", sequenceNr++) + "-" + hostname + extension;
096        if (bCompressed) {
097            filename += ".gz";
098        }
099        return filename;
100    }
101
102}