Project

General

Profile

« Previous | Next » 

Revision 4712

Added by daigle almost 16 years ago

Add external (backup) directory discovery methods.

View differences:

src/edu/ucsb/nceas/metacat/util/SystemUtil.java
26 26

  
27 27
package edu.ucsb.nceas.metacat.util;
28 28

  
29
import java.io.IOException;
29 30
import java.util.regex.Matcher;
30 31
import java.util.regex.Pattern;
31 32
import javax.servlet.ServletContext;
......
302 303
	 * directory where metacat will hold backup files.   This functionality 
303 304
	 * is used to populate the configuration utility initially.  The user 
304 305
	 * can change the directory manually, so you can't rely on this method 
305
	 * to give you the actual directory.
306
	 * to give you the actual directory.  Here are the steps taken to discover
307
	 * the directory:
306 308
	 * 
309
	 * -- 1) Look for a saved backup location file in the user home dir - Is there a file named 
310
	 *       <user_home>/.metacat/backup-location for the user that started tomcat? if so, does 
311
	 *       it contain a single line which is a readable directory?  This directory was the backup 
312
	 *       directory used during a previous install.  Return that directory.
313
	 * -- 2) Look for an existing hidden (.metacat) directory in a default system directory.  Get 
314
	 *       the default base directory for the OS.  (See application.windowsBackupBaseDir and 
315
	 *       application.linuxBackupBaseDir in metacat.properties.)  If a directory called 
316
	 *       <base_dir>/metacat/.metacat exists, return <base_dir>/metacat.
317
	 * -- 3) Look for an existing hidden (.metacat) directory in the user directory. If a directory 
318
	 *       called <user_dir>/.metacat exists for the user that started tomcat, return <user_dir>.
319
	 * -- 4) Is the <base_dir> writable by the user that started tomcat?  If so, return <base_dir>
320
	 * -- 5) Does the <user_home> exist?  If so, return <user_home>
321
	 * -- 6) Otherwise, return null
322
	 *    
307 323
	 * @return a string holding the backup directory path
308 324
	 */
309
	public static String discoverExternalDir() {
325
	public static String discoverExternalBaseDir() throws PropertyNotFoundException {
326
		String userHomeDir = getUserHomeDir();
327
		String defaultBaseDir = "";
328
		
329
		try {
330
			// Check if there is a file at <user_home>/.metacat/backup-location.  If so, it
331
			// should contain one line that is a file that points to a writable directory. 
332
			// If that is true, use that value as the external dir.
333
			String storedBackupFileLoc = getUserHomeDir() + FileUtil.getFS() + ".metacat"
334
					+ FileUtil.getFS() + "backup-location";
335
			if (FileUtil.getFileStatus(storedBackupFileLoc) >= FileUtil.EXISTS_READABLE) {
336
				String storedBackupDirLoc = FileUtil.readFileToString(storedBackupFileLoc);
337
				if (FileUtil.isDirectory(storedBackupDirLoc)
338
						&& FileUtil.getFileStatus(storedBackupDirLoc) > FileUtil.EXISTS_READABLE) {
339
					return storedBackupDirLoc;
340
				}
341
			}
342
		} catch (IOException ioe) {
343
			logMetacat.warn("Could not read stored backup location: " + ioe.getMessage());
344
		}
345
		
346
		// Set the default location using the os
310 347
		if (getOsClass() == WIN_OS) {
311
			return "C:\\Program Files\\metacat";
348
			defaultBaseDir = PropertyService.getProperty("application.windowsBackupBaseDir");
349
		} else {
350
			defaultBaseDir = PropertyService.getProperty("application.linuxBackupBaseDir");
312 351
		}
313 352
		
314
		return "/var/metacat";
353
		String defaultBackupDir = defaultBaseDir + FileUtil.getFS() + "metacat";
354
		String defaultBackupHiddenDir = defaultBackupDir + FileUtil.getFS() + ".metacat";
355
		String userHomeBackupHiddenDir = userHomeDir + FileUtil.getFS() + ".metacat";
356
		
357
		// If <base_dir>/metacat/.metacat exists, return <base_dir>
358
		if ((FileUtil.getFileStatus(defaultBackupHiddenDir) >= FileUtil.EXISTS_READ_WRITABLE)) {
359
			return defaultBaseDir;
360
		}
361
		
362
		// Otherwise if <user_dir>/.metacat exists, return <user_dir>
363
		if ((FileUtil.getFileStatus(userHomeBackupHiddenDir) >= FileUtil.EXISTS_READ_WRITABLE)) {
364
			return userHomeDir;
365
		}
366
		
367
		// Otherwise if <base_dir>/metacat exists, return <base_dir>
368
		if ((FileUtil.getFileStatus(defaultBaseDir) >= FileUtil.EXISTS_READ_WRITABLE)) {
369
			return defaultBaseDir;
370
		}
371
		
372
		// Otherwise if <user_dir> exists, return <user_dir>
373
		if ((FileUtil.getFileStatus(userHomeDir) >= FileUtil.EXISTS_READ_WRITABLE)) {
374
			return userHomeDir;
375
		}
376
	
377
		// Otherwise, return userHomeDir
378
		return null;
315 379
	}
380
	
381
	/**
382
	 * Store the location of the backup file location into a file at 
383
	 * <user_home>/.metacat/backup-location
384
	 * 
385
	 * @param externalDir the backup file location.
386
	 */
387
	public static void storeExternalDirLocation(String externalDir) {
388
		if (getUserHomeDir() != null) {
389
			String storedBackupDirLoc = getUserHomeDir() + FileUtil.getFS() + ".metacat"
390
					+ FileUtil.getFS() + "backup-location";
391
			try {
392
				FileUtil.writeFile(storedBackupDirLoc, externalDir);
393
			} catch (IOException ioe) {
394
				logMetacat.error("I/O error while trying to write stored backup directory: "
395
								+ storedBackupDirLoc + " : " + ioe.getMessage());
396
			}
397
		} else {
398
			logMetacat.warn("Could not write out stored backup directory." 
399
					+ " User directory does not exist");
400
		}
401
	}
316 402

  
317 403
	/**
318 404
	 * Get the style skins directory. This is made up of the tomcat directory
......
373 459
		
374 460
		return realPath;
375 461
	}
462
	
463
	/**
464
	 * Get the current user's home directory
465
	 * 
466
	 * @return a string holding the home directory
467
	 */
468
	public static String getUserHomeDir() {
469
		return System.getProperty("user.home");
470
	}
376 471

  
377 472
}

Also available in: Unified diff