Adding more control over data directory path

Former-commit-id: 263475fc5b3aae04f2530ea78a0456deb18686fe
This commit is contained in:
Steve Springett
2013-05-27 00:10:08 -07:00
parent 9a7fbe44eb
commit dbc862ad39
5 changed files with 15 additions and 39 deletions

View File

@@ -70,14 +70,7 @@ public class Index extends AbstractIndex {
*/
public File getDataDirectory() throws IOException {
final String fileName = Settings.getString(Settings.KEYS.CPE_INDEX);
File exePath = FileUtils.getDataDirectory(fileName, Index.class);
if (exePath.getName().toLowerCase().endsWith(".jar")) {
exePath = exePath.getParentFile();
} else {
exePath = new File(".");
}
File path = new File(exePath.getCanonicalFile() + File.separator + fileName);
path = new File(path.getCanonicalPath());
File path = FileUtils.getDataDirectory(fileName, Index.class);
if (!path.exists()) {
if (!path.mkdirs()) {
throw new IOException("Unable to create CPE Data directory");

View File

@@ -408,16 +408,7 @@ public class CveDB {
*/
public static File getDataDirectory() throws IOException {
final String fileName = Settings.getString(Settings.KEYS.CVE_INDEX);
File exePath = FileUtils.getDataDirectory(fileName, CveDB.class);
if (exePath.getName().toLowerCase().endsWith(".jar")) {
exePath = exePath.getParentFile();
} else {
exePath = new File(".");
}
File path = new File(exePath.getCanonicalFile() + File.separator + fileName);
path = new File(path.getCanonicalPath());
File path = FileUtils.getDataDirectory(fileName, CveDB.class);
if (!path.exists()) {
if (!path.mkdirs()) {
throw new IOException("Unable to create NVD CVE Data directory");

View File

@@ -83,10 +83,18 @@ public final class FileUtils {
public static File getDataDirectory(String configuredFilePath, Class clazz) throws IOException {
File file = new File(configuredFilePath);
if (file.exists() && file.isDirectory() && file.canWrite()) {
return file;
return new File(file.getCanonicalPath());
} else {
String filePath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
return new File(URLDecoder.decode(filePath, "UTF-8"));
final String filePath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
final String decodedPath = URLDecoder.decode(filePath, "UTF-8");
File exePath = new File(decodedPath);
if (exePath.getName().toLowerCase().endsWith(".jar")) {
exePath = exePath.getParentFile();
} else {
exePath = new File(".");
}
File path = new File(exePath.getCanonicalFile() + File.separator + configuredFilePath);
return new File(path.getCanonicalPath());
}
}

View File

@@ -58,15 +58,7 @@ public abstract class BaseIndexTestCase {
protected static File getDataDirectory() throws IOException {
String fileName = Settings.getString(Settings.KEYS.CPE_INDEX);
File exePath = FileUtils.getDataDirectory(fileName, Index.class);
if (exePath.getName().toLowerCase().endsWith(".jar")) {
exePath = exePath.getParentFile();
} else {
exePath = new File(".");
}
File path = new File(exePath.getCanonicalFile() + File.separator + fileName);
path = new File(path.getCanonicalPath());
return path;
return FileUtils.getDataDirectory(fileName, Index.class);
}
public static void ensureIndexExists() throws Exception {

View File

@@ -49,15 +49,7 @@ public abstract class BaseDBTestCase extends TestCase {
protected static File getDataDirectory() throws IOException {
String fileName = Settings.getString(Settings.KEYS.CVE_INDEX);
File exePath = FileUtils.getDataDirectory(fileName, Index.class);
if (exePath.getName().toLowerCase().endsWith(".jar")) {
exePath = exePath.getParentFile();
} else {
exePath = new File(".");
}
File path = new File(exePath.getCanonicalFile() + File.separator + fileName);
path = new File(path.getCanonicalPath());
return path;
return FileUtils.getDataDirectory(fileName, Index.class);
}
public static void ensureDBExists() throws Exception {