1 19 20 package org.apache.cayenne.project; 21 22 import java.io.File ; 23 24 31 public class CayenneUserDir { 32 33 protected static CayenneUserDir sharedInstance; 34 35 public static final String CAYENNE_DIR = ".cayenne"; 36 37 43 public static final String ALT_USER_DIR_PROPERTY = "cayenne.userdir"; 44 45 protected File cayenneUserDir; 46 47 public static CayenneUserDir getInstance() { 48 if(sharedInstance == null) { 49 sharedInstance = new CayenneUserDir(); 50 } 51 return sharedInstance; 52 } 53 54 55 58 protected CayenneUserDir() { 59 super(); 60 61 File tmpDir = null; 62 String dirName = System.getProperty(ALT_USER_DIR_PROPERTY); 63 64 if (dirName != null) { 65 tmpDir = new File (dirName); 66 } 67 else { 68 File homeDir = new File (System.getProperty("user.home")); 69 tmpDir = new File (homeDir, CAYENNE_DIR); 70 } 71 72 if (tmpDir.exists() && !tmpDir.isDirectory()) { 73 tmpDir = null; 74 } 75 else if (tmpDir.exists() && !tmpDir.canRead()) { 76 tmpDir = null; 77 } 78 else if (!tmpDir.exists()) { 79 tmpDir.mkdirs(); 80 if (!tmpDir.exists()) { 81 tmpDir = null; 82 } 83 } 84 85 cayenneUserDir = tmpDir; 86 } 87 88 92 public File getDirectory() { 93 return cayenneUserDir; 94 } 95 96 100 public boolean canRead() { 101 return cayenneUserDir != null; 102 } 103 104 108 public boolean canWrite() { 109 return cayenneUserDir != null && cayenneUserDir.canWrite(); 110 } 111 112 public File resolveFile(String name) { 113 return new File (cayenneUserDir, name); 114 } 115 } 116 | Popular Tags |