KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > mktemp > TempLocator


1 /**
2  * $Id: TempLocator.java 187 2007-03-25 17:59:16Z ssmc $
3  * Copyright 2002-2004,2007 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://antxtras.sf.net/ EMAIL- jware[at]users[dot]sourceforge[dot]net
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.mktemp;
30
31 import java.io.File JavaDoc;
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 /**
44  * Task that locates suitable scratch or temporary directories into which scratch files can
45  * be safely dumped. Helper for other mktemp package classes.
46  * <p>
47  * <b>Examples:</b><pre>
48  * &lt;<b>tempdir</b> haltiferror="no" subdirectory="&#46;antx-build"
49           pathproperty="scratch&#46;root"/&gt;
50  *
51  * &lt;<b>tempdir</b> subdirectory="&#46;antx-builds" pathproperty="scratch&#46;root"/&gt;
52  * &lt;tstamp&gt;
53  * &lt;format property="itid" pattern="..."/&gt;
54  * &lt;/tstamp&gt;
55  * &lt;property name="debug&#46;root" value="${scratch&#46;root}/debug_${itid}"/&gt;
56  * &lt;property name="opt&#46;root" value="${scratch&#46;root}/opt_${itid}"/&gt;
57  * </pre>
58  *
59  * @since JWare/AntX 0.1
60  * @author ssmc, &copy;2002-2004,2007 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
61  * @version 0.5.1
62  * @.safety guarded
63  * @.group impl,helper
64  * @see MkTempObject
65  **/

66
67 public final class TempLocator extends AssertableTask
68     implements RecoveryEnabled, AntLibFriendly
69 {
70     /**
71      * Tries to determine the OS-specific temporary directory. Not as nice
72      * as the JWare TempLocator but fixed to work with Ant's own tempfile
73      * factory as a fallback.
74      **/

75     private static final File JavaDoc TMPDIR;
76     static {
77         File JavaDoc f=null;
78         try {
79             f = File.createTempFile("qat",null);
80             f.deleteOnExit();
81             f = f.getParentFile();
82             f = f.getCanonicalFile();
83         } catch(Exception JavaDoc anyX) {//hmm...this is bad...ok...make ant do it...
84
FileUtils fu= FileUtils.getFileUtils();
85             f = fu.createTempFile("qat",".tmp",null);
86             try {
87                 f = f.getParentFile().getCanonicalFile();
88             } catch (Exception JavaDoc ioX) {
89                 f = new File JavaDoc("_will_die_on_first_use");
90             }
91         }
92         TMPDIR= f;
93     }
94
95     /**
96      * Returns the OS-specific temp directory. Never returns <i>null</i>.
97      **/

98     public final static File JavaDoc getSystemTempDir()
99     {
100         return TMPDIR;//?expose
101
}
102
103
104     /**
105      * Initializes a new TempLocator task.
106      **/

107     public TempLocator()
108     {
109         super(AntX.mktemp);
110
111     }
112
113
114     /**
115      * Sets the property into which the temp directory's path is copied.
116      * @param property the property's name (non-null)
117      * @since JWare/AntX 0.3
118      **/

119     public void setPathProperty(String JavaDoc property)
120     {
121         require_(property!=null,"setProp- nonzro name");
122         m_updateProperty = property;
123     }
124
125
126     /**
127      * Returns the name of the property into which the temp directory's
128      * path will be stored. Returns <i>null</i> if never set.
129      **/

130     public final String JavaDoc getPathProperty()
131     {
132         return m_updateProperty;
133     }
134
135
136
137     /**
138      * Sets the property into which a URL representation of the
139      * temp directory's path is copied.
140      * @param urlproperty the property's name (non-null)
141      * @since JWare/AntX 0.5
142      **/

143     public void setUrlProperty(String JavaDoc urlproperty)
144     {
145         require_(urlproperty!=null,"setUrlProp- nonzro name");
146         m_updateUrlProperty = urlproperty;
147     }
148
149
150
151     /**
152      * Returns the name of the property into which a URL representation
153      * of the temp directory's path will be stored. Returns <i>null</i>
154      * if never set explicitly.
155      * @since JWare/AntX 0.5
156      **/

157     public final String JavaDoc getUrlPathProperty()
158     {
159         return m_updateUrlProperty;
160     }
161
162
163
164     /**
165      * Sets name of preferrred sub-directory within the OS-specific temp
166      * directory. When executed, this templocator will try to create a
167      * writable directory with this name in the temp directory.
168      * @param directory the sub-directory's name (non-null)
169      **/

170     public void setSubdirectory(String JavaDoc directory)
171     {
172         require_(directory!=null, "setroot- nonzro dirname");
173         m_unverifiedTmpDir = new File JavaDoc(getSystemTempDir(),directory);
174     }
175
176
177     /**
178      * Returns the preferred sub-directory as a file. Whether this
179      * sub-directory actually exists depends on whether this task has
180      * been executed at least once. Returns <i>null</i> if a
181      * preferred subdirectory has never been set, <em>or</em> if
182      * the preferred subdirectory has been created and verified.
183      * @see #getVerifiedRoot
184      **/

185     public File JavaDoc getUnverifiedRoot()
186     {
187         return m_unverifiedTmpDir;
188     }
189
190
191     /**
192      * Returns the temporary directory created by this task. If
193      * non-null then this directory has been verified by this task
194      * (exists, is-a directory, and is writable).
195      * @see #getUnverifiedRoot
196      **/

197     public File JavaDoc getVerifiedRoot()
198     {
199         return m_verifiedTmpDir;
200     }
201
202
203     /**
204      * Sets whether this task will throw a build exception if it's
205      * unable to create a preferred sub-directory. Defaults to
206      * <i>true</i>.
207      **/

208     public void setHaltIfError(boolean haltIfError)
209     {
210         m_haltIfError = haltIfError;
211     }
212
213
214     /**
215      * Returns <i>true</i> if this task will fail if it's unable to
216      * create a preferred temp sub-directory. Defaults to <i>true</i>.
217      * @see #setHaltIfError
218      **/

219     public boolean isHaltIfError()
220     {
221         return m_haltIfError;
222     }
223
224
225     /**
226      * Locates the runtime platform's temporary directory and stores its
227      * location in the specified {@linkplain #setPathProperty property}. If
228      * a preferred temp sub-directory has been requested, this method will
229      * try to ensure this directory exists and is read/writable (creating
230      * it if it has to)-- the property is set to the appropriate
231      * subdirectory's path.
232      * @throws BuildException if fail-on-error and unable to create
233      * preferred sub-directory
234      **/

235     public void execute() throws BuildException
236     {
237         File JavaDoc rootDir = getSystemTempDir();
238
239         if (getUnverifiedRoot()!=null) {
240             File JavaDoc preferredRootDir=getUnverifiedRoot();
241             try {
242                 preferredRootDir.mkdirs();
243                 if (!preferredRootDir.isDirectory() || !preferredRootDir.canWrite()) {
244                     String JavaDoc 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 JavaDoc rtX) {
254                 if (isHaltIfError()) {
255                     throw rtX;
256                 }//NB:ELSE-otherwise defaults back to systemTmpDir...
257
}
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 JavaDoc urlPath = AntXFixture.fileUtils().toURI(rootDir.getPath());
269             getProject().setNewProperty(getUrlPathProperty(),urlPath);
270         }
271     }
272
273
274     private String JavaDoc m_updateProperty;
275     private String JavaDoc m_updateUrlProperty;
276     private File JavaDoc m_verifiedTmpDir, m_unverifiedTmpDir;
277     private boolean m_haltIfError=true;//NB:default barfs if cant make what user asked for
278
}
279
280 /* end-of-TempLocator.java */
281
Popular Tags