Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common Development and |
11 |
* Distribution License("CDDL") (collectively, the "License"). You may not use |
12 |
* this file except in compliance with the License. You can obtain a copy of |
13 |
* the License at http://www.netbeans.org/cddl-gplv2.html or |
14 |
* nbbuild/licenses/CDDL-GPL-2-CP. See the License for the specific language |
15 |
* governing permissions and limitations under the License. When distributing |
16 |
* the software, include this License Header Notice in each file and include |
17 |
* the License file at nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
18 |
* particular file as subject to the "Classpath" exception as provided by |
19 |
* Oracle in the GPL Version 2 section of the License file that accompanied |
20 |
* this code. If applicable, add the following below the License Header, with |
21 |
* the fields enclosed by brackets [] replaced by your own identifying |
22 |
* information: "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL or |
25 |
* only the GPL Version 2, indicate your decision by adding "[Contributor] |
26 |
* elects to include this software in this distribution under the [CDDL or GPL |
27 |
* Version 2] license." If you do not indicate a single choice of license, a |
28 |
* recipient has the option to distribute your version of this file under |
29 |
* either the CDDL, the GPL Version 2 or to extend the choice of license to its |
30 |
* licensees as provided above. However, if you add GPL Version 2 code and |
31 |
* therefore, elected the GPL Version 2 license, then the option applies only |
32 |
* if the new code is made subject to such option by the copyright holder. |
33 |
* |
34 |
* Contributor(s): |
35 |
* |
36 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
37 |
*/ |
38 |
|
39 |
package org.openide.modules; |
40 |
|
41 |
import java.io.File; |
42 |
import java.util.logging.Level; |
43 |
import java.util.logging.Logger; |
44 |
|
45 |
/** |
46 |
* Provides access to standard file locations. |
47 |
* <div class="nonnormative"> |
48 |
* This class should be used for limited purposes only. You might instead want to use:<ul> |
49 |
* <li><a href="@org-openide-filesystems@/org/openide/filesystems/FileUtil.html#getConfigFile(java.lang.String)"><code>FileUtil.getConfigFile</code></a> |
50 |
* to find a file declared in an XML layer or created or overridden in the {@code config} subdirectory of the user directory. |
51 |
* <li>{@link InstalledFileLocator} to find modules installed as part of an NBM. |
52 |
* <li>{@code someClass.getProtectionDomain().getCodeSource().getLocation()} to find resources inside a module class loader. |
53 |
* </ul> |
54 |
* </div> |
55 |
* @since 7.25 |
56 |
*/ |
57 |
public class Places { |
58 |
|
59 |
/** |
60 |
* System property key for {@link #getUserDirectory}. |
61 |
* Should not be used by most code directly. |
62 |
*/ |
63 |
public static final String USER_DIR_PROP = "netbeans.user"; |
64 |
|
65 |
private static final String MEMORY = "memory"; |
66 |
|
67 |
private static final Logger LOG = Logger.getLogger(Places.class.getName()); |
68 |
|
69 |
private static File cacheDir; |
70 |
|
71 |
/** |
72 |
* Locates the NetBeans user directory. |
73 |
* This may be used to persist valuable files for which the system filesystem |
74 |
* ({@code FileUtil.getConfigFile}) is inappropriate due its being virtual. |
75 |
* Each module is responsible for using sufficiently unique filenames within this directory. |
76 |
* The system property {@link #USER_DIR_PROP} is used for compatibility. |
77 |
* @return a directory location (need not yet exist), or null if unconfigured |
78 |
*/ |
79 |
public static synchronized /*@CheckForNull*/ File getUserDirectory() { |
80 |
String p = System.getProperty(USER_DIR_PROP); |
81 |
return p != null && !p.equals(MEMORY) ? new File(p) : null; |
82 |
} |
83 |
|
84 |
/** |
85 |
* Configures the NetBeans user directory. |
86 |
* This would be called by startup code in the NetBeans Platform, |
87 |
* but might also be useful to call from a unit test if tested code calls {@link #getUserDirectory} or {@link #getCacheDirectory}. |
88 |
* @param dir a directory location (need not yet exist); null be passed but means the same as {@code memory} for the system property, interpreted by the module system |
89 |
*/ |
90 |
public static synchronized void setUserDirectory(/*@NullAllowed*/ File dir) { |
91 |
System.setProperty(USER_DIR_PROP, dir != null ? dir.getAbsolutePath() : MEMORY); |
92 |
} |
93 |
|
94 |
/** |
95 |
* Locates the NetBeans cache directory. |
96 |
* This may be used to store pure performance caches - files which could be safely deleted, |
97 |
* since they would be automatically recreated on demand. |
98 |
* Each module is responsible for using sufficiently unique filenames within this directory. |
99 |
* {@code $userdir/var/cache/} is used as a default when {@link #getUserDirectory} is configured. |
100 |
* As a final fallback, a location in the system temporary directory will be returned. |
101 |
* @return a directory location (never null but need not yet exist) |
102 |
* @see #getCacheSubdirectory |
103 |
* @see #getCacheSubfile |
104 |
*/ |
105 |
public static synchronized /*@NonNull*/ File getCacheDirectory() { |
106 |
if (cacheDir != null) { |
107 |
return cacheDir; |
108 |
} |
109 |
File userdir = getUserDirectory(); |
110 |
if (userdir != null) { |
111 |
return new File(new File(userdir, "var"), "cache"); |
112 |
} |
113 |
return new File(System.getProperty("java.io.tmpdir"), "nbcache"); |
114 |
} |
115 |
|
116 |
/** |
117 |
* Convenience method to get a particular subdirectory within {@link #getCacheDirectory}. |
118 |
* The directory will be created if it does not yet exist (but a warning logged if permissions do not allow this). |
119 |
* @param path a subdirectory path such as {@code stuff} or {@code mymodule/stuff} ({@code /} permitted even on Windows) |
120 |
* @return a directory of that name within the general cache directory |
121 |
*/ |
122 |
public static /*@NonNull*/ File getCacheSubdirectory(String path) { |
123 |
File d = new File(getCacheDirectory(), path); |
124 |
if (!d.isDirectory() && !d.mkdirs()) { |
125 |
LOG.log(Level.WARNING, "could not create {0}", d); |
126 |
} |
127 |
return d; |
128 |
} |
129 |
|
130 |
/** |
131 |
* Convenience method to get a particular file within {@link #getCacheDirectory}. |
132 |
* The parent directory will be created if it does not yet exist (but a warning logged if permissions do not allow this); |
133 |
* the file itself will not be automatically created. |
134 |
* @param path a file path such as {@code stuff.ser} or {@code mymodule/stuff.ser} ({@code /} permitted even on Windows) |
135 |
* @return a file of that name within the general cache directory |
136 |
*/ |
137 |
public static /*@NonNull*/ File getCacheSubfile(String path) { |
138 |
File f = new File(getCacheDirectory(), path); |
139 |
File d = f.getParentFile(); |
140 |
if (!d.isDirectory() && !d.mkdirs()) { |
141 |
LOG.log(Level.WARNING, "could not create {0}", d); |
142 |
} |
143 |
return f; |
144 |
} |
145 |
|
146 |
/** |
147 |
* Configures the NetBeans cache directory. |
148 |
* This would be called by startup code in the NetBeans Platform, |
149 |
* but might also be useful to call from a unit test if tested code calls {@link #getCacheDirectory}. |
150 |
* @param dir a directory location (need not yet exist) |
151 |
*/ |
152 |
public static synchronized void setCacheDirectory(/*@NonNull*/ File dir) { |
153 |
cacheDir = dir; |
154 |
} |
155 |
|
156 |
private Places() {} |
157 |
|
158 |
} |