1 56 package org.objectstyle.cayenne.project; 57 58 import java.io.File ; 59 60 import org.apache.log4j.Logger; 61 62 69 public class CayenneUserDir { 70 static final Logger logObj = Logger.getLogger(CayenneUserDir.class); 71 72 protected static CayenneUserDir sharedInstance; 73 74 public static final String CAYENNE_DIR = ".cayenne"; 75 76 82 public static final String ALT_USER_DIR_PROPERTY = "cayenne.userdir"; 83 84 protected File cayenneUserDir; 85 86 public static CayenneUserDir getInstance() { 87 if(sharedInstance == null) { 88 sharedInstance = new CayenneUserDir(); 89 } 90 return sharedInstance; 91 } 92 93 94 97 protected CayenneUserDir() { 98 super(); 99 100 File tmpDir = null; 101 String dirName = System.getProperty(ALT_USER_DIR_PROPERTY); 102 103 if (dirName != null) { 104 tmpDir = new File (dirName); 105 } 106 else { 107 File homeDir = new File (System.getProperty("user.home")); 108 tmpDir = new File (homeDir, CAYENNE_DIR); 109 } 110 111 if (tmpDir.exists() && !tmpDir.isDirectory()) { 112 tmpDir = null; 113 logObj.warn(tmpDir + " is not a directory."); 114 } 115 else if (tmpDir.exists() && !tmpDir.canRead()) { 116 tmpDir = null; 117 logObj.warn(tmpDir + " is not readable."); 118 } 119 else if (!tmpDir.exists()) { 120 tmpDir.mkdirs(); 121 if (!tmpDir.exists()) { 122 tmpDir = null; 123 logObj.warn("Couldn't create " + tmpDir); 124 } 125 } 126 127 cayenneUserDir = tmpDir; 128 } 129 130 134 public File getDirectory() { 135 return cayenneUserDir; 136 } 137 138 142 public boolean canRead() { 143 return cayenneUserDir != null; 144 } 145 146 150 public boolean canWrite() { 151 return cayenneUserDir != null && cayenneUserDir.canWrite(); 152 } 153 154 public File resolveFile(String name) { 155 return new File (cayenneUserDir, name); 156 } 157 } 158 | Popular Tags |