TemporaryDirectory.java revision 4fefecee9d4a5d2a4510f516b4015607b19e8d09
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package dalvik.system;
18
19import java.io.File;
20import java.util.logging.Logger;
21
22/**
23 * Utility class to handle the setup of the core library's concept of
24 * what the "default temporary directory" is. Application code may
25 * call into this class with an appropriate base directory during its
26 * startup, as a reasonably easy way to get the standard property
27 * <code>java.io.tmpdir</code> to point at something useful.
28 *
29 * @deprecated this is an internal Dalvik class that is not appropriate for
30 *      general use. It will be removed from the public API in a future release.
31 */
32public class TemporaryDirectory {
33    /** system property name for the temporary directory */
34    private static final String PROPERTY = "java.io.tmpdir";
35
36    /** final path component name for the temporary directory */
37    private static final String PATH_NAME = "tmp";
38
39    /** whether a temporary directory has been configured yet */
40    private static boolean configured = false;
41
42    /**
43     * Convenience method which is equivalent to
44     * <code>setupDirectory(new File(baseDir))</code>.
45     *
46     * @param baseDir the base directory of the temporary directory
47     */
48    public static void setUpDirectory(String baseDir) {
49        setUpDirectory(new File(baseDir));
50    }
51
52    /**
53     * Sets up the temporary directory, but only if one isn't already
54     * defined for this process, and only if it is possible (e.g., the
55     * directory already exists and is read-write, or the directory
56     * can be created). This call will do one of three things:
57     *
58     * <ul>
59     * <li>return without error and without doing anything, if a
60     * previous call to this method succeeded</li>
61     * <li>return without error, having either created a temporary
62     * directory under the given base or verified that such a directory
63     * already exists</li>
64     * <li>throw <code>UnsupportedOperationException</code> if the
65     * directory could not be created or accessed</li>
66     * </ul>
67     *
68     * @param baseDir the base directory of the temporary directory
69     */
70    public static synchronized void setUpDirectory(File baseDir) {
71        if (configured) {
72            Logger.global.info("Already set to: " +
73                    System.getProperty(PROPERTY));
74            return;
75        }
76
77        File dir = new File(baseDir, PATH_NAME);
78        String absolute = dir.getAbsolutePath();
79
80        if (dir.exists()) {
81            if (!dir.isDirectory()) {
82                throw new UnsupportedOperationException(
83                        "Name is used by a non-directory file: " +
84                        absolute);
85            } else if (!(dir.canRead() && dir.canWrite())) {
86                throw new UnsupportedOperationException(
87                        "Existing directory is not readable and writable: " +
88                        absolute);
89            }
90        } else {
91            if (!dir.mkdirs()) {
92                throw new UnsupportedOperationException(
93                        "Failed to create directory: " + absolute);
94            }
95        }
96
97        System.setProperty(PROPERTY, absolute);
98        configured = true;
99    }
100}
101