- Make sure our directory has an absolute path, otherwise we run into inconsistent behaviour
of File.exists() - Throw exception if directory exists and is not a directory - Rename dir instance variable to directory
This commit is contained in:
parent
828ba529dd
commit
ceebcdb96e
1 changed files with 29 additions and 20 deletions
|
@ -27,7 +27,7 @@ import java.util.HashMap;
|
||||||
public class FileRepository extends AbstractRepository {
|
public class FileRepository extends AbstractRepository {
|
||||||
|
|
||||||
// Directory serving sub-repositories and file resources
|
// Directory serving sub-repositories and file resources
|
||||||
private File dir;
|
private File directory;
|
||||||
|
|
||||||
private long lastModified = -1;
|
private long lastModified = -1;
|
||||||
private long lastChecksum = 0;
|
private long lastChecksum = 0;
|
||||||
|
@ -51,7 +51,7 @@ public class FileRepository extends AbstractRepository {
|
||||||
* repository
|
* repository
|
||||||
* @param dir directory
|
* @param dir directory
|
||||||
*/
|
*/
|
||||||
public FileRepository(File dir) {
|
protected FileRepository(File dir) {
|
||||||
this(dir, null);
|
this(dir, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,23 +61,31 @@ public class FileRepository extends AbstractRepository {
|
||||||
* @param dir directory
|
* @param dir directory
|
||||||
* @param parent top-level repository
|
* @param parent top-level repository
|
||||||
*/
|
*/
|
||||||
public FileRepository(File dir, FileRepository parent) {
|
protected FileRepository(File dir, FileRepository parent) {
|
||||||
this.dir = dir;
|
// make sure our directory has an absolute path,
|
||||||
if (!dir.exists()) {
|
// see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4117557
|
||||||
|
if (dir.isAbsolute()) {
|
||||||
|
directory = dir;
|
||||||
|
} else {
|
||||||
|
directory = dir.getAbsoluteFile();
|
||||||
|
}
|
||||||
|
if (!directory.exists()) {
|
||||||
create();
|
create();
|
||||||
|
} else if (!directory.isDirectory()) {
|
||||||
|
throw new IllegalArgumentException("File " + directory + " is not a directory");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parent == null) {
|
if (parent == null) {
|
||||||
name = shortName = dir.getAbsolutePath();
|
name = shortName = directory.getAbsolutePath();
|
||||||
} else {
|
} else {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
shortName = dir.getName();
|
shortName = directory.getName();
|
||||||
name = dir.getAbsolutePath();
|
name = directory.getAbsolutePath();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean exists() {
|
public boolean exists() {
|
||||||
if (dir.exists() && dir.isDirectory()) {
|
if (directory.exists() && directory.isDirectory()) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -85,8 +93,8 @@ public class FileRepository extends AbstractRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void create() {
|
public void create() {
|
||||||
if (!dir.exists() || !dir.isDirectory()) {
|
if (!directory.exists() || !directory.isDirectory()) {
|
||||||
dir.mkdirs();
|
directory.mkdirs();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -104,7 +112,7 @@ public class FileRepository extends AbstractRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
public long lastModified() {
|
public long lastModified() {
|
||||||
return dir.lastModified();
|
return directory.lastModified();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getChecksum() throws IOException {
|
public long getChecksum() throws IOException {
|
||||||
|
@ -131,17 +139,18 @@ public class FileRepository extends AbstractRepository {
|
||||||
* resources
|
* resources
|
||||||
*/
|
*/
|
||||||
public synchronized void update() {
|
public synchronized void update() {
|
||||||
if (!dir.exists()) {
|
if (!directory.exists()) {
|
||||||
repositories = new Repository[0];
|
repositories = new Repository[0];
|
||||||
|
if (resources != null)
|
||||||
resources.clear();
|
resources.clear();
|
||||||
lastModified = 0;
|
lastModified = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dir.lastModified() != lastModified) {
|
if (directory.lastModified() != lastModified) {
|
||||||
lastModified = dir.lastModified();
|
lastModified = directory.lastModified();
|
||||||
|
|
||||||
File[] list = dir.listFiles();
|
File[] list = directory.listFiles();
|
||||||
|
|
||||||
ArrayList newRepositories = new ArrayList(list.length);
|
ArrayList newRepositories = new ArrayList(list.length);
|
||||||
HashMap newResources = new HashMap(list.length);
|
HashMap newResources = new HashMap(list.length);
|
||||||
|
@ -170,16 +179,16 @@ public class FileRepository extends AbstractRepository {
|
||||||
* Called to create a child resource for this repository
|
* Called to create a child resource for this repository
|
||||||
*/
|
*/
|
||||||
protected Resource createResource(String name) {
|
protected Resource createResource(String name) {
|
||||||
return new FileResource(new File(dir, name), this);
|
return new FileResource(new File(directory, name), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return 17 + (37 * dir.hashCode());
|
return 17 + (37 * directory.hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
return obj instanceof FileRepository &&
|
return obj instanceof FileRepository &&
|
||||||
dir.equals(((FileRepository) obj).dir);
|
directory.equals(((FileRepository) obj).directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue