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.camel.component.file.strategy;
018
019import java.io.File;
020
021import org.apache.camel.Exchange;
022import org.apache.camel.LoggingLevel;
023import org.apache.camel.component.file.FileComponent;
024import org.apache.camel.component.file.GenericFile;
025import org.apache.camel.component.file.GenericFileEndpoint;
026import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy;
027import org.apache.camel.component.file.GenericFileOperations;
028import org.apache.camel.util.FileUtil;
029import org.apache.camel.util.StopWatch;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033/**
034 * Acquires read lock to the given file using a marker file so other Camel consumers wont acquire the same file.
035 * This is the default behavior in Camel 1.x.
036 */
037public class MarkerFileExclusiveReadLockStrategy implements GenericFileExclusiveReadLockStrategy<File> {
038    private static final Logger LOG = LoggerFactory.getLogger(MarkerFileExclusiveReadLockStrategy.class);
039
040    private boolean markerFile = true;
041
042    @Override
043    public void prepareOnStartup(GenericFileOperations<File> operations, GenericFileEndpoint<File> endpoint) {
044        String dir = endpoint.getConfiguration().getDirectory();
045        File file = new File(dir);
046
047        LOG.debug("Prepare on startup by deleting orphaned lock files from: {}", dir);
048
049        StopWatch watch = new StopWatch();
050        deleteLockFiles(file, endpoint.isRecursive());
051
052        // log anything that takes more than a second
053        if (watch.taken() > 1000) {
054            LOG.info("Prepared on startup by deleting orphaned lock files from: {} took {} millis to complete.", dir, watch.taken());
055        }
056    }
057
058    @Override
059    public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations,
060                                            GenericFile<File> file, Exchange exchange) throws Exception {
061
062        if (!markerFile) {
063            // if not using marker file then we assume acquired
064            return true;
065        }
066
067        String lockFileName = getLockFileName(file);
068        LOG.trace("Locking the file: {} using the lock file name: {}", file, lockFileName);
069
070        // create a plain file as marker filer for locking (do not use FileLock)
071        boolean acquired = FileUtil.createNewFile(new File(lockFileName));
072
073        // store read-lock state
074        exchange.setProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_ACQUIRED), acquired);
075        exchange.setProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_NAME), lockFileName);
076
077        return acquired;
078    }
079
080    @Override
081    public void releaseExclusiveReadLock(GenericFileOperations<File> operations,
082                                         GenericFile<File> file, Exchange exchange) throws Exception {
083        if (!markerFile) {
084            // if not using marker file then nothing to release
085            return;
086        }
087
088        boolean acquired = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_ACQUIRED), false, Boolean.class);
089
090        // only release the file if camel get the lock before
091        if (acquired) {
092            String lockFileName = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_NAME), String.class);
093            File lock = new File(lockFileName);
094
095            if (lock.exists()) {
096                LOG.trace("Unlocking file: {}", lockFileName);
097                boolean deleted = FileUtil.deleteFile(lock);
098                LOG.trace("Lock file: {} was deleted: {}", lockFileName, deleted);
099            }
100        }
101    }
102
103    @Override
104    public void setTimeout(long timeout) {
105        // noop
106    }
107
108    @Override
109    public void setCheckInterval(long checkInterval) {
110        // noop
111    }
112
113    @Override
114    public void setReadLockLoggingLevel(LoggingLevel readLockLoggingLevel) {
115        // noop
116    }
117
118    @Override
119    public void setMarkerFiler(boolean markerFile) {
120        this.markerFile = markerFile;
121    }
122
123    private static void deleteLockFiles(File dir, boolean recursive) {
124        File[] files = dir.listFiles();
125        if (files == null || files.length == 0) {
126            return;
127        }
128
129        for (File file : files) {
130            if (file.getName().startsWith(".")) {
131                // files starting with dot should be skipped
132                continue;
133            } else if (file.getName().endsWith(FileComponent.DEFAULT_LOCK_FILE_POSTFIX)) {
134                LOG.warn("Deleting orphaned lock file: " + file);
135                FileUtil.deleteFile(file);
136            } else if (recursive && file.isDirectory()) {
137                deleteLockFiles(file, true);
138            }
139        }
140    }
141
142    private static String getLockFileName(GenericFile<File> file) {
143        return file.getAbsoluteFilePath() + FileComponent.DEFAULT_LOCK_FILE_POSTFIX;
144    }
145
146    private static String asReadLockKey(GenericFile file, String key) {
147        // use the copy from absolute path as that was the original path of the file when the lock was acquired
148        // for example if the file consumer uses preMove then the file is moved and therefore has another name
149        // that would no longer match
150        String path = file.getCopyFromAbsoluteFilePath() != null ? file.getCopyFromAbsoluteFilePath() : file.getAbsoluteFilePath();
151        return path + "-" + key;
152    }
153
154}