| 1 28 29 package com.idaremedia.antx.mktemp; 30 31 import java.io.File ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.util.FileUtils; 36 37 import com.idaremedia.antx.AntX; 38 import com.idaremedia.antx.AntXFixture; 39 import com.idaremedia.antx.AssertableTask; 40 import com.idaremedia.antx.apis.AntLibFriendly; 41 import com.idaremedia.antx.parameters.RecoveryEnabled; 42 43 66 67 public final class TempLocator extends AssertableTask 68 implements RecoveryEnabled, AntLibFriendly 69 { 70 75 private static final File TMPDIR; 76 static { 77 File f=null; 78 try { 79 f = File.createTempFile("qat",null); 80 f.deleteOnExit(); 81 f = f.getParentFile(); 82 f = f.getCanonicalFile(); 83 } catch(Exception anyX) { FileUtils fu= FileUtils.getFileUtils(); 85 f = fu.createTempFile("qat",".tmp",null); 86 try { 87 f = f.getParentFile().getCanonicalFile(); 88 } catch (Exception ioX) { 89 f = new File ("_will_die_on_first_use"); 90 } 91 } 92 TMPDIR= f; 93 } 94 95 98 public final static File getSystemTempDir() 99 { 100 return TMPDIR; } 102 103 104 107 public TempLocator() 108 { 109 super(AntX.mktemp); 110 111 } 112 113 114 119 public void setPathProperty(String property) 120 { 121 require_(property!=null,"setProp- nonzro name"); 122 m_updateProperty = property; 123 } 124 125 126 130 public final String getPathProperty() 131 { 132 return m_updateProperty; 133 } 134 135 136 137 143 public void setUrlProperty(String urlproperty) 144 { 145 require_(urlproperty!=null,"setUrlProp- nonzro name"); 146 m_updateUrlProperty = urlproperty; 147 } 148 149 150 151 157 public final String getUrlPathProperty() 158 { 159 return m_updateUrlProperty; 160 } 161 162 163 164 170 public void setSubdirectory(String directory) 171 { 172 require_(directory!=null, "setroot- nonzro dirname"); 173 m_unverifiedTmpDir = new File (getSystemTempDir(),directory); 174 } 175 176 177 185 public File getUnverifiedRoot() 186 { 187 return m_unverifiedTmpDir; 188 } 189 190 191 197 public File getVerifiedRoot() 198 { 199 return m_verifiedTmpDir; 200 } 201 202 203 208 public void setHaltIfError(boolean haltIfError) 209 { 210 m_haltIfError = haltIfError; 211 } 212 213 214 219 public boolean isHaltIfError() 220 { 221 return m_haltIfError; 222 } 223 224 225 235 public void execute() throws BuildException 236 { 237 File rootDir = getSystemTempDir(); 238 239 if (getUnverifiedRoot()!=null) { 240 File preferredRootDir=getUnverifiedRoot(); 241 try { 242 preferredRootDir.mkdirs(); 243 if (!preferredRootDir.isDirectory() || !preferredRootDir.canWrite()) { 244 String ermsg = uistrs().get("mktemp.cant.make.tmpdir", 245 rootDir.getPath(), preferredRootDir.getName()); 246 log(ermsg, Project.MSG_ERR); 247 throw new BuildException(ermsg); 248 } 249 m_verifiedTmpDir = preferredRootDir; 250 m_unverifiedTmpDir = null; 251 rootDir = preferredRootDir; 252 253 } catch(RuntimeException rtX) { 254 if (isHaltIfError()) { 255 throw rtX; 256 } } 258 } 259 else if (getVerifiedRoot()!=null) { 260 rootDir = getVerifiedRoot(); 261 } 262 263 if (getPathProperty()!=null) { 264 getProject().setNewProperty(getPathProperty(),rootDir.getPath()); 265 } 266 267 if (getUrlPathProperty()!=null) { 268 String urlPath = AntXFixture.fileUtils().toURI(rootDir.getPath()); 269 getProject().setNewProperty(getUrlPathProperty(),urlPath); 270 } 271 } 272 273 274 private String m_updateProperty; 275 private String m_updateUrlProperty; 276 private File m_verifiedTmpDir, m_unverifiedTmpDir; 277 private boolean m_haltIfError=true;} 279 280 281 | Popular Tags |