blob: b98ad863d3785758b6c9934ad2073de5d1d3ec21 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.oodt.cas.crawl.action;
//JDK imports
import java.io.File;
import java.util.logging.Level;
//Apache imports
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.Validate;
//OODT imports
import org.apache.oodt.cas.crawl.structs.exceptions.CrawlerActionException;
import org.apache.oodt.cas.metadata.Metadata;
/**
* Deletes a product file as a {@link CrawlerAction} response.
*
* @author bfoster (Brian Foster)
* @author mattmann (Chris Mattmann)
* @author luca (Luca Cinquini)
*/
public class DeleteFile extends CrawlerAction {
private File file;
private String fileExtension;
@Override
public boolean performAction(File product, Metadata productMetadata)
throws CrawlerActionException {
try {
File fileToDelete = product;
if (file != null) {
fileToDelete = file;
} else if (fileExtension != null) {
fileToDelete = new File(product.getAbsolutePath() + "." + fileExtension);
}
LOG.log(Level.INFO, "Deleting file " + fileToDelete.getAbsolutePath());
if (fileToDelete.isDirectory()) {
// the following method will throw an exception if the directory cannot be deleted
FileUtils.deleteDirectory(fileToDelete);
return true;
} else {
return fileToDelete.delete();
}
} catch (Exception e) {
LOG.log(Level.SEVERE, "Error while deleting file for product '"
+ product + "' : " + e.getMessage(), e);
return false;
}
}
@Override
public void validate() throws CrawlerActionException {
super.validate();
try {
Validate.isTrue(file == null || fileExtension == null,
"Must specify either file or fileExtension");
} catch (Exception e) {
throw new CrawlerActionException(e);
}
}
public void setFile(File file) {
this.file = file;
}
public void setFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
}
}