Issue #168414: provide API complement to CacheDirectoryProvider with fallback impl. diff --git a/project.ant/src/org/netbeans/spi/project/support/ant/ExtensibleMetadataProviderImpl.java b/project.ant/src/org/netbeans/spi/project/support/ant/ExtensibleMetadataProviderImpl.java --- a/project.ant/src/org/netbeans/spi/project/support/ant/ExtensibleMetadataProviderImpl.java +++ b/project.ant/src/org/netbeans/spi/project/support/ant/ExtensibleMetadataProviderImpl.java @@ -57,7 +57,7 @@ /** * Relative path from project directory to the required private cache directory. */ - private static final String CACHE_PATH = "nbproject/private/cache"; // NOI18N + private static final String CACHE_PATH = "nbproject/private"; // NOI18N private final AntProjectHelper helper; diff --git a/projectapi/apichanges.xml b/projectapi/apichanges.xml --- a/projectapi/apichanges.xml +++ b/projectapi/apichanges.xml @@ -104,6 +104,23 @@ + + + API complement to CacheDirectoryProvider + + + + + +

+ Added ProjectUtils.getCacheDirectory to complement + the SPI interface CacheDirectoryProvider. +

+
+ + +
+ introduction of SourceGroupModifier, a way to create SourceGroup if missing diff --git a/projectapi/manifest.mf b/projectapi/manifest.mf --- a/projectapi/manifest.mf +++ b/projectapi/manifest.mf @@ -1,7 +1,7 @@ Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.modules.projectapi/1 OpenIDE-Module-Install: org/netbeans/modules/projectapi/Installer.class -OpenIDE-Module-Specification-Version: 1.25 +OpenIDE-Module-Specification-Version: 1.26 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/projectapi/Bundle.properties OpenIDE-Module-Layer: org/netbeans/modules/projectapi/layer.xml diff --git a/projectapi/src/org/netbeans/api/project/ProjectUtils.java b/projectapi/src/org/netbeans/api/project/ProjectUtils.java --- a/projectapi/src/org/netbeans/api/project/ProjectUtils.java +++ b/projectapi/src/org/netbeans/api/project/ProjectUtils.java @@ -42,6 +42,7 @@ package org.netbeans.api.project; import java.beans.PropertyChangeListener; +import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -49,19 +50,19 @@ import java.util.Set; import java.util.prefs.Preferences; import javax.swing.Icon; -import javax.swing.ImageIcon; import org.netbeans.modules.projectapi.AuxiliaryConfigBasedPreferencesProvider; import org.netbeans.modules.projectapi.AuxiliaryConfigImpl; import org.netbeans.spi.project.AuxiliaryConfiguration; import org.netbeans.spi.project.AuxiliaryProperties; +import org.netbeans.spi.project.CacheDirectoryProvider; import org.netbeans.spi.project.SubprojectProvider; import org.netbeans.spi.project.support.GenericSources; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileStateInvalidException; +import org.openide.filesystems.FileUtil; import org.openide.util.ImageUtilities; import org.openide.util.Mutex; import org.openide.util.Parameters; -import org.openide.util.Utilities; /** * Utility methods to get information about {@link Project}s. @@ -263,4 +264,31 @@ return new AuxiliaryConfigImpl(project); } + /** + * Gets a directory in which modules may store arbitrary extra unversioned files + * associated with a project. + * These could be caches of information found in sources, logs or snapshots + * from activities associated with developing the project, etc. + *

+ * If the project supplies a {@link CacheDirectoryProvider}, that will be used + * for the parent directory. Otherwise an unspecified storage area will be used. + * @param project a project + * @param owner a class from the calling module (each module or package will get its own space) + * @return a directory available for storing miscellaneous files + * @throws IOException if no such directory could be created + * @since org.netbeans.modules.projectapi/1 1.26 + */ + public static FileObject getCacheDirectory(Project project, Class owner) throws IOException { + FileObject d; + CacheDirectoryProvider cdp = project.getLookup().lookup(CacheDirectoryProvider.class); + if (cdp != null) { + d = cdp.getCacheDirectory(); + } else { + d = FileUtil.createFolder(FileUtil.getConfigRoot(), + String.format("Projects/extra/%s-%08x", getInformation(project).getName(), // NOI18N + project.getProjectDirectory().getPath().hashCode())); + } + return FileUtil.createFolder(d, AuxiliaryConfigBasedPreferencesProvider.findCNBForClass(owner)); + } + } diff --git a/projectapi/src/org/netbeans/modules/projectapi/AuxiliaryConfigBasedPreferencesProvider.java b/projectapi/src/org/netbeans/modules/projectapi/AuxiliaryConfigBasedPreferencesProvider.java --- a/projectapi/src/org/netbeans/modules/projectapi/AuxiliaryConfigBasedPreferencesProvider.java +++ b/projectapi/src/org/netbeans/modules/projectapi/AuxiliaryConfigBasedPreferencesProvider.java @@ -316,7 +316,7 @@ modified = true; } - private static String findCNBForClass(Class cls) { + public static String findCNBForClass(Class cls) { String absolutePath = null; ClassLoader cl = cls.getClassLoader(); for (ModuleInfo module : Lookup.getDefault().lookupAll(ModuleInfo.class)) { diff --git a/projectapi/test/unit/src/org/netbeans/api/project/ProjectUtilsTest.java b/projectapi/test/unit/src/org/netbeans/api/project/ProjectUtilsTest.java --- a/projectapi/test/unit/src/org/netbeans/api/project/ProjectUtilsTest.java +++ b/projectapi/test/unit/src/org/netbeans/api/project/ProjectUtilsTest.java @@ -41,12 +41,16 @@ package org.netbeans.api.project; +import java.beans.PropertyChangeListener; import java.io.File; +import java.io.IOException; import java.util.Arrays; import java.util.HashSet; import java.util.Set; +import javax.swing.Icon; import javax.swing.event.ChangeListener; import org.netbeans.junit.NbTestCase; +import org.netbeans.spi.project.CacheDirectoryProvider; import org.netbeans.spi.project.SubprojectProvider; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; @@ -209,10 +213,62 @@ public void removeChangeListener(ChangeListener l) {} - public String toString() { + public @Override String toString() { return name; } } + + public void testGetCacheDirectory() throws Exception { + final FileObject pdir = FileUtil.createMemoryFileSystem().getRoot().createFolder("foo"); + final ProjectInformation info = new ProjectInformation() { + public String getName() { + return "foo-project"; + } + public String getDisplayName() { + return getName(); + } + public Icon getIcon() { + return null; + } + public Project getProject() { + return null; + } + public void addPropertyChangeListener(PropertyChangeListener listener) {} + public void removePropertyChangeListener(PropertyChangeListener listener) {} + }; + Project p = new Project() { + public FileObject getProjectDirectory() { + return pdir; + } + public Lookup getLookup() { + return Lookups.fixed(info); + } + }; + FileObject d = ProjectUtils.getCacheDirectory(p, Object.class); + assertEquals(FileUtil.getConfigRoot().getFileSystem(), d.getFileSystem()); + assertEquals("Projects/extra/foo-project-00018cc6/java-lang", d.getPath()); + d = ProjectUtils.getCacheDirectory(p, Object.class); + assertEquals("Projects/extra/foo-project-00018cc6/java-lang", d.getPath()); + final FileObject cache = FileUtil.createMemoryFileSystem().getRoot(); + p = new Project() { + public FileObject getProjectDirectory() { + return pdir; + } + public Lookup getLookup() { + return Lookups.fixed(info, new CacheDirectoryProvider() { + public FileObject getCacheDirectory() throws IOException { + return cache; + } + }); + } + }; + d = ProjectUtils.getCacheDirectory(p, Object.class); + assertEquals("java-lang", d.getNameExt()); + assertEquals(cache, d.getParent()); + d = ProjectUtils.getCacheDirectory(p, Object.class); + assertEquals("java-lang", d.getNameExt()); + assertEquals(cache, d.getParent()); + } }