diff options
Diffstat (limited to 'sched/src/com/android')
16 files changed, 275 insertions, 70 deletions
diff --git a/sched/src/com/android/sched/util/file/CannotCreateFileException.java b/sched/src/com/android/sched/util/file/CannotCreateFileException.java index bf5b1fe0..b637a540 100644 --- a/sched/src/com/android/sched/util/file/CannotCreateFileException.java +++ b/sched/src/com/android/sched/util/file/CannotCreateFileException.java @@ -16,25 +16,38 @@ package com.android.sched.util.file; +import com.android.sched.util.location.HasLocation; import com.android.sched.util.location.Location; -import java.io.IOException; - import javax.annotation.CheckForNull; import javax.annotation.Nonnull; /** * Exception when a file or directory can not be created. */ -public class CannotCreateFileException extends IOException { +public class CannotCreateFileException extends WithLocationException { private static final long serialVersionUID = 1L; public CannotCreateFileException(@Nonnull Location location) { - this(location, null); + super(location, null); } public CannotCreateFileException(@Nonnull Location location, @CheckForNull Throwable cause) { - super(location.getDescription() + " can not be created", cause); + super(location, cause); + } + + public CannotCreateFileException(@Nonnull HasLocation locationProvider) { + super(locationProvider, null); + } + + public CannotCreateFileException(@Nonnull HasLocation locationProvider, + @CheckForNull Throwable cause) { + super(locationProvider, cause); + } + + @Override + protected String createMessage(@Nonnull String description) { + return description + " can not be created"; } } diff --git a/sched/src/com/android/sched/util/file/CannotDeleteFileException.java b/sched/src/com/android/sched/util/file/CannotDeleteFileException.java index 0779bda2..7691d6c5 100644 --- a/sched/src/com/android/sched/util/file/CannotDeleteFileException.java +++ b/sched/src/com/android/sched/util/file/CannotDeleteFileException.java @@ -16,25 +16,38 @@ package com.android.sched.util.file; +import com.android.sched.util.location.HasLocation; import com.android.sched.util.location.Location; -import java.io.IOException; - import javax.annotation.CheckForNull; import javax.annotation.Nonnull; /** * Exception thrown when deletion of a file or a directory fails. */ -public class CannotDeleteFileException extends IOException { +public class CannotDeleteFileException extends WithLocationException { private static final long serialVersionUID = 1L; public CannotDeleteFileException(@Nonnull Location location) { - this(location, null); + super(location, null); + } + + public CannotDeleteFileException(@Nonnull Location location, + @CheckForNull Throwable cause) { + super(location, cause); + } + + public CannotDeleteFileException(@Nonnull HasLocation locationProvider) { + super(locationProvider, null); + } + + public CannotDeleteFileException(@Nonnull HasLocation locationProvider, + @CheckForNull Throwable cause) { + super(locationProvider, cause); } - public CannotDeleteFileException( - @Nonnull Location location, @CheckForNull Throwable cause) { - super(location.getDescription() + " can not be deleted", cause); + @Override + protected String createMessage(@Nonnull String description) { + return description + " can not be deleted"; } } diff --git a/sched/src/com/android/sched/util/file/CannotReadException.java b/sched/src/com/android/sched/util/file/CannotReadException.java index d34fecfb..9b00a931 100644 --- a/sched/src/com/android/sched/util/file/CannotReadException.java +++ b/sched/src/com/android/sched/util/file/CannotReadException.java @@ -16,25 +16,38 @@ package com.android.sched.util.file; +import com.android.sched.util.location.HasLocation; import com.android.sched.util.location.Location; -import java.io.IOException; - import javax.annotation.CheckForNull; import javax.annotation.Nonnull; /** * Exception when a file can not be read. */ -public class CannotReadException extends IOException { +public class CannotReadException extends WithLocationException { private static final long serialVersionUID = 1L; public CannotReadException(@Nonnull Location location) { - this(location, null); + super(location, null); } public CannotReadException(@Nonnull Location location, @CheckForNull Throwable cause) { - super(location.getDescription() + " can not be read", cause); + super(location, cause); + } + + public CannotReadException(@Nonnull HasLocation locationProvider) { + super(locationProvider, null); + } + + public CannotReadException(@Nonnull HasLocation locationProvider, + @CheckForNull Throwable cause) { + super(locationProvider, cause); + } + + @Override + protected String createMessage(@Nonnull String description) { + return description + " can not be read"; } } diff --git a/sched/src/com/android/sched/util/file/CannotSetPermissionException.java b/sched/src/com/android/sched/util/file/CannotSetPermissionException.java index 3f15f1d8..59c203b6 100644 --- a/sched/src/com/android/sched/util/file/CannotSetPermissionException.java +++ b/sched/src/com/android/sched/util/file/CannotSetPermissionException.java @@ -18,19 +18,22 @@ package com.android.sched.util.file; import com.android.sched.util.file.FileOrDirectory.ChangePermission; import com.android.sched.util.file.FileOrDirectory.Permission; +import com.android.sched.util.location.HasLocation; import com.android.sched.util.location.Location; -import java.io.IOException; - import javax.annotation.CheckForNull; import javax.annotation.Nonnull; /** * Exception when a file or directory can not be set to the expected permission. */ -public class CannotSetPermissionException extends IOException { +public class CannotSetPermissionException extends WithLocationException { private static final long serialVersionUID = 1L; + private final int permission; + @Nonnull + private final ChangePermission change; + public CannotSetPermissionException(@Nonnull Location location, int permission, @Nonnull ChangePermission change) { this(location, permission, change, null); @@ -38,10 +41,29 @@ public class CannotSetPermissionException extends IOException { public CannotSetPermissionException(@Nonnull Location location, int permission, @Nonnull ChangePermission change, @CheckForNull Throwable cause) { - super(location.getDescription() + " can not be set " + - ((permission == Permission.READ) ? "readable" : - ((permission == Permission.WRITE) ? "writable" : - ((permission == Permission.EXECUTE) ? "executable" : "???"))) + - ((change == ChangePermission.EVERYBODY) ? " for everybody" : ""), cause); + super(location, cause); + this.permission = permission; + this.change = change; + } + + public CannotSetPermissionException(@Nonnull HasLocation locationProvider, int permission, + @Nonnull ChangePermission change) { + this(locationProvider, permission, change, null); + } + + public CannotSetPermissionException(@Nonnull HasLocation locationProvider, int permission, + @Nonnull ChangePermission change, @CheckForNull Throwable cause) { + super(locationProvider, cause); + this.permission = permission; + this.change = change; + } + + @Override + protected String createMessage(@Nonnull String description) { + return description + " can not be set " + + ((permission == Permission.READ) ? "readable" : + ((permission == Permission.WRITE) ? "writable" : + ((permission == Permission.EXECUTE) ? "executable" : "???"))) + + ((change == ChangePermission.EVERYBODY) ? " for everybody" : ""); } } diff --git a/sched/src/com/android/sched/util/file/FileAlreadyExistsException.java b/sched/src/com/android/sched/util/file/FileAlreadyExistsException.java index 4e1f0705..8c622eee 100644 --- a/sched/src/com/android/sched/util/file/FileAlreadyExistsException.java +++ b/sched/src/com/android/sched/util/file/FileAlreadyExistsException.java @@ -16,25 +16,38 @@ package com.android.sched.util.file; +import com.android.sched.util.location.HasLocation; import com.android.sched.util.location.Location; -import java.io.IOException; - import javax.annotation.CheckForNull; import javax.annotation.Nonnull; /** * Exception when a file or directory of that name already exists. */ -public class FileAlreadyExistsException extends IOException { +public class FileAlreadyExistsException extends WithLocationException { private static final long serialVersionUID = 1L; public FileAlreadyExistsException(@Nonnull Location location) { - this(location, null); + super(location, null); + } + + public FileAlreadyExistsException(@Nonnull Location location, + @CheckForNull Throwable cause) { + super(location, cause); + } + + public FileAlreadyExistsException(@Nonnull HasLocation locationProvider) { + super(locationProvider, null); + } + + public FileAlreadyExistsException(@Nonnull HasLocation locationProvider, + @CheckForNull Throwable cause) { + super(locationProvider, cause); } - public FileAlreadyExistsException( - @Nonnull Location location, @CheckForNull Throwable cause) { - super(location.getDescription() + " already exists", cause); + @Override + protected String createMessage(@Nonnull String description) { + return description + " already exists"; } } diff --git a/sched/src/com/android/sched/util/file/NoSuchFileException.java b/sched/src/com/android/sched/util/file/NoSuchFileException.java index 16926e4f..abaaf0c7 100644 --- a/sched/src/com/android/sched/util/file/NoSuchFileException.java +++ b/sched/src/com/android/sched/util/file/NoSuchFileException.java @@ -16,24 +16,36 @@ package com.android.sched.util.file; +import com.android.sched.util.location.HasLocation; import com.android.sched.util.location.Location; -import java.io.IOException; - import javax.annotation.CheckForNull; import javax.annotation.Nonnull; /** * Exception when a file or directory does not exist. */ -public class NoSuchFileException extends IOException { +public class NoSuchFileException extends WithLocationException { private static final long serialVersionUID = 1L; public NoSuchFileException(@Nonnull Location location) { - this(location, null); + super(location, null); } public NoSuchFileException(@Nonnull Location location, @CheckForNull Throwable cause) { - super(location.getDescription() + " does not exist", cause); + super(location, cause); + } + + public NoSuchFileException(@Nonnull HasLocation locationProvider) { + super(locationProvider, null); + } + + public NoSuchFileException(@Nonnull HasLocation location, @CheckForNull Throwable cause) { + super(location, cause); + } + + @Override + protected String createMessage(@Nonnull String description) { + return description + " does not exist"; } } diff --git a/sched/src/com/android/sched/util/file/NotDirectoryException.java b/sched/src/com/android/sched/util/file/NotDirectoryException.java index c47ff69c..a0c3dab4 100644 --- a/sched/src/com/android/sched/util/file/NotDirectoryException.java +++ b/sched/src/com/android/sched/util/file/NotDirectoryException.java @@ -16,6 +16,7 @@ package com.android.sched.util.file; +import com.android.sched.util.location.HasLocation; import com.android.sched.util.location.Location; import javax.annotation.CheckForNull; @@ -27,11 +28,24 @@ import javax.annotation.Nonnull; public class NotDirectoryException extends NotFileOrDirectoryException { private static final long serialVersionUID = 1L; - public NotDirectoryException(@Nonnull Location expected) { - this(expected, null); + public NotDirectoryException(@Nonnull Location location) { + super(location, null); } - public NotDirectoryException(@Nonnull Location expected, @CheckForNull Throwable cause) { - super(expected.getDescription() + " is not actually a directory", cause); + public NotDirectoryException(@Nonnull Location location, @CheckForNull Throwable cause) { + super(location, cause); + } + + public NotDirectoryException(@Nonnull HasLocation locationProvider) { + super(locationProvider, null); + } + + public NotDirectoryException(@Nonnull HasLocation location, @CheckForNull Throwable cause) { + super(location, cause); + } + + @Override + protected String createMessage(@Nonnull String description) { + return description + " is not actually a directory"; } } diff --git a/sched/src/com/android/sched/util/file/NotFileException.java b/sched/src/com/android/sched/util/file/NotFileException.java index 87a85782..9d5cfc57 100644 --- a/sched/src/com/android/sched/util/file/NotFileException.java +++ b/sched/src/com/android/sched/util/file/NotFileException.java @@ -16,6 +16,7 @@ package com.android.sched.util.file; +import com.android.sched.util.location.HasLocation; import com.android.sched.util.location.Location; import javax.annotation.CheckForNull; @@ -27,11 +28,24 @@ import javax.annotation.Nonnull; public class NotFileException extends NotFileOrDirectoryException { private static final long serialVersionUID = 1L; - public NotFileException(@Nonnull Location expected) { - this(expected, null); + public NotFileException(@Nonnull Location location) { + super(location, null); } - public NotFileException(@Nonnull Location expected, @CheckForNull Throwable cause) { - super(expected.getDescription() + " is not actually a file", cause); + public NotFileException(@Nonnull Location location, @CheckForNull Throwable cause) { + super(location, cause); + } + + public NotFileException(@Nonnull HasLocation locationProvider) { + super(locationProvider, null); + } + + public NotFileException(@Nonnull HasLocation location, @CheckForNull Throwable cause) { + super(location, cause); + } + + @Override + protected String createMessage(@Nonnull String description) { + return description + " is not actually a file"; } } diff --git a/sched/src/com/android/sched/util/file/NotFileOrDirectoryException.java b/sched/src/com/android/sched/util/file/NotFileOrDirectoryException.java index 37b08999..43e55b40 100644 --- a/sched/src/com/android/sched/util/file/NotFileOrDirectoryException.java +++ b/sched/src/com/android/sched/util/file/NotFileOrDirectoryException.java @@ -16,7 +16,8 @@ package com.android.sched.util.file; -import java.io.IOException; +import com.android.sched.util.location.HasLocation; +import com.android.sched.util.location.Location; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; @@ -24,14 +25,15 @@ import javax.annotation.Nonnull; /** * Exception when a path is not from the expected file or directory kind. */ -public abstract class NotFileOrDirectoryException extends IOException { +public abstract class NotFileOrDirectoryException extends WithLocationException { private static final long serialVersionUID = 1L; - public NotFileOrDirectoryException(@Nonnull String string) { - super(string); + public NotFileOrDirectoryException(@Nonnull Location location, @CheckForNull Throwable cause) { + super(location, cause); } - public NotFileOrDirectoryException(@Nonnull String string, @CheckForNull Throwable cause) { - super(string, cause); + public NotFileOrDirectoryException(@Nonnull HasLocation locationProvider, + @CheckForNull Throwable cause) { + super(locationProvider, cause); } } diff --git a/sched/src/com/android/sched/util/file/WithLocationException.java b/sched/src/com/android/sched/util/file/WithLocationException.java new file mode 100644 index 00000000..4d5a08f5 --- /dev/null +++ b/sched/src/com/android/sched/util/file/WithLocationException.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed 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 com.android.sched.util.file; + +import com.android.sched.util.location.HasLocation; +import com.android.sched.util.location.Location; + +import java.io.IOException; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; + +/** + * Common superclass for exceptions which are related to a specific location. + */ +public abstract class WithLocationException extends IOException { + private static final long serialVersionUID = 1L; + + @CheckForNull + private final Location location; + @CheckForNull + private final HasLocation locationProvider; + + @Nonnull + protected abstract String createMessage(@Nonnull String description); + + protected WithLocationException(@Nonnull Location location, @CheckForNull Throwable cause) { + super("", cause); + assert location != null; + this.location = location; + this.locationProvider = null; + } + + protected WithLocationException(@Nonnull HasLocation locationProvider, + @CheckForNull Throwable cause) { + super("", cause); + assert locationProvider != null; + this.location = null; + this.locationProvider = locationProvider; + } + + @Override + @Nonnull + public String getMessage() { + if (location != null) { + return createMessage(location.getDescription()); + } + if (locationProvider != null) { + return createMessage(locationProvider.getLocation().getDescription()); + } + throw new AssertionError(); + } +} diff --git a/sched/src/com/android/sched/util/file/WrongPermissionException.java b/sched/src/com/android/sched/util/file/WrongPermissionException.java index e4faea0f..f046d90e 100644 --- a/sched/src/com/android/sched/util/file/WrongPermissionException.java +++ b/sched/src/com/android/sched/util/file/WrongPermissionException.java @@ -17,28 +17,47 @@ package com.android.sched.util.file; import com.android.sched.util.file.FileOrDirectory.Permission; +import com.android.sched.util.location.HasLocation; import com.android.sched.util.location.Location; -import java.io.IOException; - import javax.annotation.CheckForNull; import javax.annotation.Nonnull; /** * Exception when a file or directory have not the expected permission. */ -public class WrongPermissionException extends IOException { +public class WrongPermissionException extends WithLocationException { private static final long serialVersionUID = 1L; + private final int permission; + public WrongPermissionException(@Nonnull Location location, int permission) { - this(location, permission, null); + super(location, null); + this.permission = permission; } public WrongPermissionException(@Nonnull Location location, int permission, @CheckForNull Throwable cause) { - super(location.getDescription() + " is not " + - ((permission == Permission.READ) ? "readable" : - ((permission == Permission.WRITE) ? "writable" : - ((permission == Permission.EXECUTE) ? "executable" : "???"))), cause); + super(location, cause); + this.permission = permission; + } + + public WrongPermissionException(@Nonnull HasLocation locationProvider, int permission) { + super(locationProvider, null); + this.permission = permission; + } + + public WrongPermissionException(@Nonnull HasLocation location, int permission, + @CheckForNull Throwable cause) { + super(location, cause); + this.permission = permission; + } + + @Override + protected String createMessage(@Nonnull String description) { + return description + " is not " + + ((permission == Permission.READ) ? "readable" : + ((permission == Permission.WRITE) ? "writable" : + ((permission == Permission.EXECUTE) ? "executable" : "???"))); } } diff --git a/sched/src/com/android/sched/vfs/CachedDirectFS.java b/sched/src/com/android/sched/vfs/CachedDirectFS.java index de2deeec..83512be1 100644 --- a/sched/src/com/android/sched/vfs/CachedDirectFS.java +++ b/sched/src/com/android/sched/vfs/CachedDirectFS.java @@ -288,7 +288,7 @@ public class CachedDirectFS extends BaseVFS<CachedParentVDir, CachedParentVFile> File path = getNativeFile(file.getPath()); if (!path.delete() || path.exists()) { - throw new CannotDeleteFileException(file.getLocation()); + throw new CannotDeleteFileException(file); } file.deleteFromCache(); @@ -297,8 +297,8 @@ public class CachedDirectFS extends BaseVFS<CachedParentVDir, CachedParentVFile> @Override @Nonnull - synchronized CachedParentVFile createVFile(@Nonnull CachedParentVDir parent, @Nonnull String name) - throws CannotCreateFileException { + synchronized CachedParentVFile createVFile(@Nonnull CachedParentVDir parent, + @Nonnull String name) throws CannotCreateFileException { assert !isClosed(); try { @@ -324,7 +324,8 @@ public class CachedDirectFS extends BaseVFS<CachedParentVDir, CachedParentVFile> @Override @Nonnull - synchronized CachedParentVDir createVDir(@Nonnull CachedParentVDir parent, @Nonnull String name) + synchronized CachedParentVDir createVDir(@Nonnull CachedParentVDir parent, + @Nonnull String name) throws CannotCreateFileException { assert !isClosed(); diff --git a/sched/src/com/android/sched/vfs/CaseInsensitiveFS.java b/sched/src/com/android/sched/vfs/CaseInsensitiveFS.java index 1188bf43..1c81dfa3 100644 --- a/sched/src/com/android/sched/vfs/CaseInsensitiveFS.java +++ b/sched/src/com/android/sched/vfs/CaseInsensitiveFS.java @@ -379,11 +379,11 @@ public class CaseInsensitiveFS extends BaseVFS<CaseInsensitiveVDir, CaseInsensit BaseVFile encoded = vfs.getRootDir().getVFile(encode(file.getPath())); vfs.delete(encoded); } catch (NotDirectoryException e) { - throw new CannotDeleteFileException(file.getLocation()); + throw new CannotDeleteFileException(file); } catch (NotFileException e) { - throw new CannotDeleteFileException(file.getLocation()); + throw new CannotDeleteFileException(file); } catch (NoSuchFileException e) { - throw new CannotDeleteFileException(file.getLocation()); + throw new CannotDeleteFileException(file); } } diff --git a/sched/src/com/android/sched/vfs/DirectFS.java b/sched/src/com/android/sched/vfs/DirectFS.java index a9537f6c..b280a404 100644 --- a/sched/src/com/android/sched/vfs/DirectFS.java +++ b/sched/src/com/android/sched/vfs/DirectFS.java @@ -223,7 +223,7 @@ public class DirectFS extends BaseVFS<ParentVDir, ParentVFile> implements VFS { File path = getNativeFile(file.getPath()); if (!path.delete() || path.exists()) { - throw new CannotDeleteFileException(file.getLocation()); + throw new CannotDeleteFileException(file); } } diff --git a/sched/src/com/android/sched/vfs/InMemoryVDir.java b/sched/src/com/android/sched/vfs/InMemoryVDir.java index 4ace1fcd..ad6f186d 100644 --- a/sched/src/com/android/sched/vfs/InMemoryVDir.java +++ b/sched/src/com/android/sched/vfs/InMemoryVDir.java @@ -83,7 +83,8 @@ abstract class InMemoryVDir extends BaseVDir { @Override @Nonnull - public synchronized BaseVDir createVDir(@Nonnull String name) throws CannotCreateFileException { + public synchronized BaseVDir createVDir(@Nonnull String name) + throws CannotCreateFileException { try { return getVDir(name); } catch (NoSuchFileException e) { @@ -98,7 +99,8 @@ abstract class InMemoryVDir extends BaseVDir { @Override @Nonnull - public synchronized BaseVFile createVFile(@Nonnull String name) throws CannotCreateFileException { + public synchronized BaseVFile createVFile(@Nonnull String name) + throws CannotCreateFileException { try { return getVFile(name); } catch (NoSuchFileException e) { diff --git a/sched/src/com/android/sched/vfs/MessageDigestFS.java b/sched/src/com/android/sched/vfs/MessageDigestFS.java index 294eb6db..d01c1e00 100644 --- a/sched/src/com/android/sched/vfs/MessageDigestFS.java +++ b/sched/src/com/android/sched/vfs/MessageDigestFS.java @@ -197,7 +197,7 @@ public class MessageDigestFS extends BaseVFS<BaseVDir, MessageDigestVFile> imple } } catch (IOException e) { throw new WrongVFSFormatException(this, vfs.getLocation(), new CannotReadException( - digestFile.getLocation())); + digestFile)); } } finally { if (in != null) { |