Lines 41-46
Link Here
|
41 |
import java.io.File; |
41 |
import java.io.File; |
42 |
import java.util.logging.Level; |
42 |
import java.util.logging.Level; |
43 |
import java.util.logging.Logger; |
43 |
import java.util.logging.Logger; |
|
|
44 |
import org.openide.util.Lookup; |
45 |
import org.openide.util.lookup.ServiceProvider; |
44 |
|
46 |
|
45 |
/** |
47 |
/** |
46 |
* Provides access to standard file locations. |
48 |
* Provides access to standard file locations. |
Lines 52-73
Link Here
|
52 |
* <li>{@code someClass.getProtectionDomain().getCodeSource().getLocation()} to find resources inside a module class loader. |
54 |
* <li>{@code someClass.getProtectionDomain().getCodeSource().getLocation()} to find resources inside a module class loader. |
53 |
* </ul> |
55 |
* </ul> |
54 |
* </div> |
56 |
* </div> |
55 |
* @since 7.25 |
57 |
* @since 7.26 |
56 |
*/ |
58 |
*/ |
57 |
public class Places { |
59 |
public abstract class Places { |
58 |
|
60 |
|
59 |
/** |
61 |
/** |
60 |
* System property key for {@link #getUserDirectory}. |
62 |
* System property key for {@link #getUserDirectory}. Can be used |
61 |
* Should not be used by most code directly. |
63 |
* from a testing code to influence the location of user directory |
|
|
64 |
* when no {@link Places} implementation if found in {@link Lookup#getDefault()}. |
62 |
*/ |
65 |
*/ |
63 |
public static final String USER_DIR_PROP = "netbeans.user"; |
66 |
public static final String USER_DIR_PROP = "netbeans.user"; |
64 |
|
67 |
|
65 |
private static final String MEMORY = "memory"; |
|
|
66 |
|
67 |
private static final Logger LOG = Logger.getLogger(Places.class.getName()); |
68 |
private static final Logger LOG = Logger.getLogger(Places.class.getName()); |
68 |
|
69 |
|
69 |
private static File cacheDir; |
|
|
70 |
|
71 |
/** |
70 |
/** |
72 |
* Locates the NetBeans user directory. |
71 |
* Locates the NetBeans user directory. |
73 |
* This may be used to persist valuable files for which the system filesystem |
72 |
* This may be used to persist valuable files for which the system filesystem |
Lines 77-94
Link Here
|
77 |
* @return a directory location (need not yet exist), or null if unconfigured |
76 |
* @return a directory location (need not yet exist), or null if unconfigured |
78 |
*/ |
77 |
*/ |
79 |
public static synchronized /*@CheckForNull*/ File getUserDirectory() { |
78 |
public static synchronized /*@CheckForNull*/ File getUserDirectory() { |
|
|
79 |
Places places = Lookup.getDefault().lookup(Places.class); |
80 |
if (places != null) { |
81 |
return places.findUserDirectory(); |
82 |
} |
80 |
String p = System.getProperty(USER_DIR_PROP); |
83 |
String p = System.getProperty(USER_DIR_PROP); |
81 |
return p != null && !p.equals(MEMORY) ? new File(p) : null; |
84 |
return p != null ? 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 |
} |
85 |
} |
93 |
|
86 |
|
94 |
/** |
87 |
/** |
Lines 103-110
Link Here
|
103 |
* @see #getCacheSubfile |
96 |
* @see #getCacheSubfile |
104 |
*/ |
97 |
*/ |
105 |
public static synchronized /*@NonNull*/ File getCacheDirectory() { |
98 |
public static synchronized /*@NonNull*/ File getCacheDirectory() { |
106 |
if (cacheDir != null) { |
99 |
Places places = Lookup.getDefault().lookup(Places.class); |
107 |
return cacheDir; |
100 |
if (places != null) { |
|
|
101 |
File cache = places.findCacheDirectory(); |
102 |
if (cache != null) { |
103 |
return cache; |
104 |
} |
108 |
} |
105 |
} |
109 |
File userdir = getUserDirectory(); |
106 |
File userdir = getUserDirectory(); |
110 |
if (userdir != null) { |
107 |
if (userdir != null) { |
Lines 143-158
Link Here
|
143 |
return f; |
140 |
return f; |
144 |
} |
141 |
} |
145 |
|
142 |
|
146 |
/** |
143 |
/** Constructor for those who believe to know |
147 |
* Configures the NetBeans cache directory. |
144 |
* where {@link #getCacheDirectory} or |
148 |
* This would be called by startup code in the NetBeans Platform, |
145 |
* {@link #getUserDirectory()} is. Register your subclass via |
149 |
* but might also be useful to call from a unit test if tested code calls {@link #getCacheDirectory}. |
146 |
* {@link ServiceProvider} annotation. |
150 |
* @param dir a directory location (need not yet exist) |
|
|
151 |
*/ |
147 |
*/ |
152 |
public static synchronized void setCacheDirectory(/*@NonNull*/ File dir) { |
148 |
protected Places() { |
153 |
cacheDir = dir; |
|
|
154 |
} |
149 |
} |
155 |
|
150 |
|
156 |
private Places() {} |
151 |
/** The cache directory to return from {@link #getCacheDirectory}. |
157 |
|
152 |
* If <code>null</code> the caches will be placed below {@link #getUserDirectory()}. |
|
|
153 |
* @return the file to use for caches or null |
154 |
* @since 7.26 |
155 |
*/ |
156 |
protected abstract File findCacheDirectory(); |
157 |
|
158 |
/** Finds location of a user directory to return from {@link #getUserDirectory}. |
159 |
* @return the user directory or <code>null</code> if no user directory is |
160 |
* supposed to be used |
161 |
* @since 7.26 |
162 |
*/ |
163 |
protected abstract File findUserDirectory(); |
158 |
} |
164 |
} |