org.apacheextras.camel.component.jcifs.strategy.SmbChangedExclusiveReadLockStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camel-jcifs Show documentation
Show all versions of camel-jcifs Show documentation
Camel JCIFS (SMB) component
/**************************************************************************************
https://camel-extra.github.io
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
http://www.gnu.org/licenses/lgpl-3.0-standalone.html
***************************************************************************************/
package org.apacheextras.camel.component.jcifs.strategy;
import java.util.Date;
import java.util.List;
import jcifs.smb.SmbFile;
import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileEndpoint;
import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy;
import org.apache.camel.component.file.GenericFileOperations;
import org.apache.camel.util.CamelLogger;
import org.apache.camel.util.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @see org.apache.camel.component.file.remote.strategy.SftpChangedExclusiveReadLockStrategy
*/
public class SmbChangedExclusiveReadLockStrategy implements GenericFileExclusiveReadLockStrategy {
private static final Logger LOG = LoggerFactory.getLogger(SmbChangedExclusiveReadLockStrategy.class);
private long timeout;
private long checkInterval = 5000;
private LoggingLevel readLockLoggingLevel = LoggingLevel.WARN;
private long minLength = 1;
private long minAge;
@Override
public void prepareOnStartup(GenericFileOperations tGenericFileOperations, GenericFileEndpoint tGenericFileEndpoint) throws Exception {
// noop
}
public boolean acquireExclusiveReadLock(GenericFileOperations operations, GenericFile file, Exchange exchange) throws Exception {
boolean exclusive = false;
LOG.trace("Waiting for exclusive read lock to file: " + file);
long lastModified = Long.MIN_VALUE;
long length = Long.MIN_VALUE;
StopWatch watch = new StopWatch();
long startTime = (new Date()).getTime();
while (!exclusive) {
// timeout check
if (timeout > 0) {
long delta = watch.taken();
if (delta > timeout) {
CamelLogger.log(LOG, readLockLoggingLevel,
"Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file);
// we could not get the lock within the timeout period, so return false
return false;
}
}
long newLastModified = 0;
long newLength = 0;
LOG.trace("Using full directory listing to update file information for {}.", file);
// fast option not available (smb listFiles only handles directories), so list the directory and filter the file name
List files = operations.listFiles(file.getParent());
LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size());
for (SmbFile f : files) {
// use same attribute sources as org.apacheextras.camel.component.jcifs.SmbConsumer#asGenericFile()
if (f.getName().equals(file.getFileNameOnly())) {
newLastModified = f.getLastModified();
newLength = f.getContentLength();
}
}
LOG.trace("Previous last modified: " + lastModified + ", new last modified: " + newLastModified);
LOG.trace("Previous length: " + length + ", new length: " + newLength);
long newOlderThan = startTime + watch.taken() - minAge;
LOG.trace("New older than threshold: {}", newOlderThan);
if (newLength >= minLength && ((minAge == 0 && newLastModified == lastModified && newLength == length) || (minAge != 0 && newLastModified < newOlderThan))) {
LOG.trace("Read lock acquired.");
exclusive = true;
} else {
// set new base file change information
lastModified = newLastModified;
length = newLength;
boolean interrupted = sleep();
if (interrupted) {
// we were interrupted while sleeping, we are likely being shutdown so return false
return false;
}
}
}
return exclusive;
}
private boolean sleep() {
LOG.trace("Exclusive read lock not granted. Sleeping for " + checkInterval + " millis.");
try {
Thread.sleep(checkInterval);
return false;
} catch (InterruptedException e) {
LOG.debug("Sleep interrupted while waiting for exclusive read lock, so breaking out");
return true;
}
}
@Override
public void releaseExclusiveReadLockOnAbort(GenericFileOperations operations, GenericFile file, Exchange exchange) throws Exception {
// noop
}
@Override
public void releaseExclusiveReadLockOnRollback(GenericFileOperations operations, GenericFile file, Exchange exchange) throws Exception {
// noop
}
@Override
public void releaseExclusiveReadLockOnCommit(GenericFileOperations operations, GenericFile file, Exchange exchange) throws Exception {
// noop
}
public long getTimeout() {
return timeout;
}
@Override
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public long getCheckInterval() {
return checkInterval;
}
@Override
public void setCheckInterval(long checkInterval) {
this.checkInterval = checkInterval;
}
@Override
public void setReadLockLoggingLevel(LoggingLevel readLockLoggingLevel) {
this.readLockLoggingLevel = readLockLoggingLevel;
}
public long getMinLength() {
return minLength;
}
public void setMinLength(long minLength) {
this.minLength = minLength;
}
public long getMinAge() {
return minAge;
}
public void setMinAge(long minAge) {
this.minAge = minAge;
}
@Override
public void setMarkerFiler(boolean markerFiler) {
// noop - not supported by smb
}
@Override
public void setDeleteOrphanLockFiles(boolean deleteOrphanLockFiles) {
// noop - not supported by smb
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy