1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
public class FilesListingFactoryBean implements FactoryBean, InitializingBean {
private String parentDirectory;
private List<File> children;
@Override
public Object getObject() throws Exception {
return children;
}
@Override
public Class getObjectType() {
return List.class;
}
@Override
public boolean isSingleton() {
return false;
}
public void setParentDirectory(String parentDirectory) {
this.parentDirectory = parentDirectory;
}
@Override
public void afterPropertiesSet() throws Exception {
if (parentDirectory == null) {
throw new IllegalArgumentException("parentDirectory must be set");
}
File file = new File(parentDirectory);
if (!file.exists() || !file.isDirectory()) {
throw new IllegalArgumentException(
"parentDirectory must point to a valid directory");
}
children = Arrays.asList(file.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isFile() && f.canRead();
}
}));
}
} |