return (String[]) nameList.toArray(new String[0]);
}
+
+
+ // Migrate stored values from an old profile directory to a new one.
+ // If the destination path does not exists for the origin,
+ // create the path and copy data from the old profile dir to
+ // the new profile dir. The source directory is unmodified.
+ public void migrateProfileData(String newDir, String oldDir, String origin) {
+ origin = cleanFileName(origin);
+
+ // basePath directory
+ File baseDir = new File(newDir);
+ if (!baseDir.exists()) {
+ if (!baseDir.mkdir()) {
+ logger.warning("Unable to create baseDirectory: " + baseDir.getPath());
+ }
+ }
+
+ File originDir = new File(newDir, origin);
+ if (originDir.exists()) {
+ // New dir is already built. No need to migrate.
+ return;
+ }
+
+ File oldOriginDir = new File(oldDir, origin);
+
+ if (oldOriginDir.exists()) {
+ logger.info("Migrating data from " +
+ oldDir + " to " + newDir + " for origin " + origin);
+
+ FileIO oldFiles = new FileIO(oldDir, origin);
+ for (String key: oldFiles.keys()) {
+ logger.info("Migrating key " + key);
+ String val = oldFiles.get(key);
+ this.set(key, val);
+ }
+
+ } else {
+ // create a new directory
+ if (!originDir.mkdir()) {
+ logger.warning(
+ "Error creating data directory: " + originDir.getPath());
+ }
+ }
+ }
}
+
/** Root directory for all FileIO operations */
private static String profileDirectory = null;
+ private static String defaultDirectory = null;
+ private static boolean migrateProfileData = false;
private void configure() {
+ if (profileDirectory != null) {
+ return; // Already loaded
+ }
+
+ String home = System.getProperty("user.home");
+ defaultDirectory = new File(home, ".evergreen").getPath();
+ profileDirectory = Hatch.getProp("data.directory");
+
// Find the profile directory.
// The profile directory + origin string represent the base
// directory for all file I/O for this session.
if (profileDirectory == null) {
+ profileDirectory = defaultDirectory;
+ } else {
+ migrateProfileData = true;
+ }
- // first see if a value is set in the properties file.
- profileDirectory = Hatch.getProp("data.directory");
-
- if (profileDirectory == null) {
- // otherwise set the directory to the users's home
- // directory + .evergreen
- String home = System.getProperty("user.home");
- profileDirectory = new File(home, ".evergreen").getPath();
+ logger.info("Using data directory: " + profileDirectory);
- if (profileDirectory == null) {
- logger.warning("Unable to set profile directory");
- }
- }
-
- logger.info("Using data directory: " + profileDirectory);
+ if (profileDirectory == null) {
+ logger.warning("Unable to set profile directory");
+ return;
}
}
String content = null;
FileIO fileIO = new FileIO(profileDirectory, origin);
+ if (migrateProfileData) {
+ fileIO.migrateProfileData(profileDirectory, defaultDirectory, origin);
+ }
+
switch (action) {
case "printers":