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    private boolean deleteOrphanLockFiles = true;
042
043    @Override
044    public void prepareOnStartup(GenericFileOperations<File> operations, GenericFileEndpoint<File> endpoint) {
045        if (deleteOrphanLockFiles) {
046
047            String dir = endpoint.getConfiguration().getDirectory();
048            File file = new File(dir);
049
050            LOG.debug("Prepare on startup by deleting orphaned lock files from: {}", dir);
051
052            StopWatch watch = new StopWatch();
053            deleteLockFiles(file, endpoint.isRecursive());
054
055            // log anything that takes more than a second
056            if (watch.taken() > 1000) {
057                LOG.info("Prepared on startup by deleting orphaned lock files from: {} took {} millis to complete.", dir, watch.taken());
058            }
059        }
060    }
061
062    @Override
063    public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations,
064                                            GenericFile<File> file, Exchange exchange) throws Exception {
065
066        if (!markerFile) {
067            // if not using marker file then we assume acquired
068            return true;
069        }
070
071        String lockFileName = getLockFileName(file);
072        LOG.trace("Locking the file: {} using the lock file name: {}", file, lockFileName);
073
074        // create a plain file as marker filer for locking (do not use FileLock)
075        boolean acquired = FileUtil.createNewFile(new File(lockFileName));
076
077        // store read-lock state
078        exchange.setProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_ACQUIRED), acquired);
079        exchange.setProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_NAME), lockFileName);
080
081        return acquired;
082    }
083
084    @Override
085    public void releaseExclusiveReadLockOnAbort(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
086        doReleaseExclusiveReadLock(operations, file, exchange);
087    }
088
089    @Override
090    public void releaseExclusiveReadLockOnRollback(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
091        doReleaseExclusiveReadLock(operations, file, exchange);
092    }
093
094    @Override
095    public void releaseExclusiveReadLockOnCommit(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
096        doReleaseExclusiveReadLock(operations, file, exchange);
097    }
098
099    protected void doReleaseExclusiveReadLock(GenericFileOperations<File> operations,
100                                              GenericFile<File> file, Exchange exchange) throws Exception {
101        if (!markerFile) {
102            // if not using marker file then nothing to release
103            return;
104        }
105
106        boolean acquired = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_ACQUIRED), false, Boolean.class);
107
108        // only release the file if camel get the lock before
109        if (acquired) {
110            String lockFileName = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_NAME), String.class);
111            File lock = new File(lockFileName);
112
113            if (lock.exists()) {
114                LOG.trace("Unlocking file: {}", lockFileName);
115                boolean deleted = FileUtil.deleteFile(lock);
116                LOG.trace("Lock file: {} was deleted: {}", lockFileName, deleted);
117            }
118        }
119    }
120
121    @Override
122    public void setTimeout(long timeout) {
123        // noop
124    }
125
126    @Override
127    public void setCheckInterval(long checkInterval) {
128        // noop
129    }
130
131    @Override
132    public void setReadLockLoggingLevel(LoggingLevel readLockLoggingLevel) {
133        // noop
134    }
135
136    @Override
137    public void setMarkerFiler(boolean markerFile) {
138        this.markerFile = markerFile;
139    }
140
141    @Override
142    public void setDeleteOrphanLockFiles(boolean deleteOrphanLockFiles) {
143        this.deleteOrphanLockFiles = deleteOrphanLockFiles;
144    }
145
146    private static void deleteLockFiles(File dir, boolean recursive) {
147        File[] files = dir.listFiles();
148        if (files == null || files.length == 0) {
149            return;
150        }
151
152        for (File file : files) {
153            if (file.getName().startsWith(".")) {
154                // files starting with dot should be skipped
155                continue;
156            } else if (file.getName().endsWith(FileComponent.DEFAULT_LOCK_FILE_POSTFIX)) {
157                LOG.warn("Deleting orphaned lock file: " + file);
158                FileUtil.deleteFile(file);
159            } else if (recursive && file.isDirectory()) {
160                deleteLockFiles(file, true);
161            }
162        }
163    }
164
165    private static String getLockFileName(GenericFile<File> file) {
166        return file.getAbsoluteFilePath() + FileComponent.DEFAULT_LOCK_FILE_POSTFIX;
167    }
168
169    private static String asReadLockKey(GenericFile file, String key) {
170        // use the copy from absolute path as that was the original path of the file when the lock was acquired
171        // for example if the file consumer uses preMove then the file is moved and therefore has another name
172        // that would no longer match
173        String path = file.getCopyFromAbsoluteFilePath() != null ? file.getCopyFromAbsoluteFilePath() : file.getAbsoluteFilePath();
174        return path + "-" + key;
175    }
176
177}