Issue #136333: use an API/SPI split to provide a fallback AuxiliaryConfiguration implementation. 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 @@ -49,8 +49,11 @@ import java.util.Set; import javax.swing.Icon; import javax.swing.ImageIcon; +import org.netbeans.modules.projectapi.AuxiliaryConfigImpl; +import org.netbeans.spi.project.AuxiliaryConfiguration; 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.util.Mutex; import org.openide.util.Utilities; @@ -215,4 +218,23 @@ } + /** + * Find a way of storing extra configuration in a project. + * If the project's {@linkplain Project#getLookup lookup} does not provide an instance, + * a fallback implementation is used. + *

+ * The current fallback implementation uses {@linkplain FileObject#setAttribute file attributes} + * for "nonsharable" configuration, and a specially named file in the project directory + * for "sharable" configuration. For compatibility purposes (in case a project adds an + * {@link AuxiliaryConfiguration} instance to its lookup where before it had none), + * the fallback storage is read (but not written) even if there is an instance in project lookup. + *

+ * @param project a project + * @return an auxiliary configuration handle + * @since XXX + */ + public static AuxiliaryConfiguration getAuxiliaryConfiguration(Project project) { + return new AuxiliaryConfigImpl(project); + } + } diff --git a/projectapi/src/org/netbeans/modules/projectapi/AuxiliaryConfigImpl.java b/projectapi/src/org/netbeans/modules/projectapi/AuxiliaryConfigImpl.java new file mode 100644 --- /dev/null +++ b/projectapi/src/org/netbeans/modules/projectapi/AuxiliaryConfigImpl.java @@ -0,0 +1,272 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2008 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.projectapi; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringReader; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.parsers.DocumentBuilderFactory; +import org.netbeans.api.project.Project; +import org.netbeans.api.project.ProjectManager; +import org.netbeans.spi.project.AuxiliaryConfiguration; +import org.openide.filesystems.FileObject; +import org.openide.util.Mutex; +import org.openide.xml.XMLUtil; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSSerializer; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class AuxiliaryConfigImpl implements AuxiliaryConfiguration { + + private static final Logger LOG = Logger.getLogger(AuxiliaryConfigImpl.class.getName()); + private static final String AUX_CONFIG_ATTR_BASE = AuxiliaryConfiguration.class.getName(); + private static final String AUX_CONFIG_FILENAME = ".netbeans.xml"; // NOI18N + + private final Project project; + + public AuxiliaryConfigImpl(Project proj) { + this.project = proj; + } + + public Element getConfigurationFragment(final String elementName, final String namespace, final boolean shared) { + return ProjectManager.mutex().readAccess(new Mutex.Action() { + public Element run() { + AuxiliaryConfiguration delegate = project.getLookup().lookup(AuxiliaryConfiguration.class); + if (delegate != null) { + Element fragment = delegate.getConfigurationFragment(elementName, namespace, shared); + if (fragment != null) { + return fragment; + } + } + FileObject dir = project.getProjectDirectory(); + if (shared) { + FileObject config = dir.getFileObject(AUX_CONFIG_FILENAME); + if (config != null) { + try { + InputStream is = config.getInputStream(); + try { + InputSource input = new InputSource(is); + input.setSystemId(config.getURL().toString()); + Element root = XMLUtil.parse(input, false, true, /*XXX*/null, null).getDocumentElement(); + return findElement(root, elementName, namespace); + } finally { + is.close(); + } + } catch (Exception x) { + LOG.log(Level.INFO, "Cannot parse" + config, x); + } + } + } else { + String attrName = AUX_CONFIG_ATTR_BASE + "." + namespace + "#" + elementName; + Object attr = dir.getAttribute(attrName); + if (attr instanceof String) { + try { + Element fragment = XMLUtil.parse(new InputSource(new StringReader((String) attr)), false, true, + /*XXX need utility method*/null, null).getDocumentElement(); + // XXX check for correct namespace and name + return fragment; + } catch (SAXException x) { + LOG.log(Level.INFO, "Cannot parse value " + attr + " of " + attrName + " on " + dir + ": " + x.getMessage()); + } catch (IOException x) { + assert false : x; + } + } + } + return null; + } + }); + } + + public void putConfigurationFragment(final Element fragment, final boolean shared) throws IllegalArgumentException { + ProjectManager.mutex().writeAccess(new Mutex.Action() { + public Void run() { + String elementName = fragment.getLocalName(); + String namespace = fragment.getNamespaceURI(); + if (namespace == null) { + throw new IllegalArgumentException(); + } + AuxiliaryConfiguration delegate = project.getLookup().lookup(AuxiliaryConfiguration.class); + if (delegate != null) { + delegate.putConfigurationFragment(fragment, shared); + removeFallbackImpl(elementName, namespace, shared); + return null; + } + FileObject dir = project.getProjectDirectory(); + try { + if (shared) { + Document doc; + FileObject config = dir.getFileObject(AUX_CONFIG_FILENAME); + if (config != null) { + InputStream is = config.getInputStream(); + try { + InputSource input = new InputSource(is); + input.setSystemId(config.getURL().toString()); + doc = XMLUtil.parse(input, false, true, /*XXX*/ null, null); + } finally { + is.close(); + } + } else { + config = dir.createData(AUX_CONFIG_FILENAME); + doc = XMLUtil.createDocument("auxiliary-configuration", "XXX", null, null); + } + Element root = doc.getDocumentElement(); + Element newFragment = (Element) doc.importNode(fragment, true); + Element oldFragment = findElement(root, elementName, namespace); + if (oldFragment != null) { + root.replaceChild(newFragment, oldFragment); + } else { + root.appendChild(newFragment); + } + OutputStream os = config.getOutputStream(); + try { + XMLUtil.write(doc, os, "UTF-8"); + } finally { + os.close(); + } + } else { + String attrName = AUX_CONFIG_ATTR_BASE + "." + namespace + "#" + elementName; + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + DOMImplementationLS ls = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0"); // NOI18N + assert ls != null : "No DOM 3 LS supported in " + doc.getClass().getName(); + // JAXP bug #6710755: cannot directly serialize fragment in JDK 5, must add to a document. + doc.appendChild(doc.importNode(fragment, true)); + LSSerializer serializer = ls.createLSSerializer(); + serializer.getDomConfig().setParameter("xml-declaration", false); + dir.setAttribute(attrName, serializer.writeToString(doc)); + } + } catch (Exception x) { + LOG.log(Level.WARNING, "Cannot save configuration to " + dir, x); + } + return null; + } + }); + } + + private boolean removeFallbackImpl(final String elementName, final String namespace, final boolean shared) { + FileObject dir = project.getProjectDirectory(); + try { + if (shared) { + FileObject config = dir.getFileObject(AUX_CONFIG_FILENAME); + if (config != null) { + try { + Document doc; + InputStream is = config.getInputStream(); + try { + InputSource input = new InputSource(is); + input.setSystemId(config.getURL().toString()); + doc = XMLUtil.parse(input, false, true, /*XXX*/ null, null); + } finally { + is.close(); + } + Element root = doc.getDocumentElement(); + Element toRemove = findElement(root, elementName, namespace); + if (toRemove != null) { + root.removeChild(toRemove); + if (root.getElementsByTagName("*").getLength() > 0) { + OutputStream os = config.getOutputStream(); + try { + XMLUtil.write(doc, os, "UTF-8"); + } finally { + os.close(); + } + } else { + config.delete(); + } + return true; + } + } catch (SAXException x) { + LOG.log(Level.INFO, "Cannot parse" + config, x); + } + } + } else { + String attrName = AUX_CONFIG_ATTR_BASE + "." + namespace + "#" + elementName; + if (dir.getAttribute(attrName) != null) { + dir.setAttribute(attrName, null); + return true; + } + } + } catch (IOException x) { + LOG.warning("Cannot remove configuration from " + dir); + } + return false; + } + + public boolean removeConfigurationFragment(final String elementName, final String namespace, final boolean shared) throws IllegalArgumentException { + return ProjectManager.mutex().writeAccess(new Mutex.Action() { + public Boolean run() { + AuxiliaryConfiguration delegate = project.getLookup().lookup(AuxiliaryConfiguration.class); + boolean result = false; + if (delegate != null) { + result |= delegate.removeConfigurationFragment(elementName, namespace, shared); + } + result |= removeFallbackImpl(elementName, namespace, shared); + return result; + } + }); + } + + private static Element findElement(Element parent, String name, String namespace) { + Element result = null; + NodeList l = parent.getChildNodes(); + int len = l.getLength(); + for (int i = 0; i < len; i++) { + if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { + Element el = (Element) l.item(i); + if (name.equals(el.getLocalName()) && namespace.equals(el.getNamespaceURI())) { + if (result == null) { + result = el; + } else { + return null; + } + } + } + } + return result; + } + +} diff --git a/projectapi/src/org/netbeans/spi/project/AuxiliaryConfiguration.java b/projectapi/src/org/netbeans/spi/project/AuxiliaryConfiguration.java --- a/projectapi/src/org/netbeans/spi/project/AuxiliaryConfiguration.java +++ b/projectapi/src/org/netbeans/spi/project/AuxiliaryConfiguration.java @@ -41,6 +41,7 @@ package org.netbeans.spi.project; +import org.netbeans.api.project.ProjectUtils; import org.w3c.dom.Element; /** @@ -57,6 +58,10 @@ * unless it is explicitly given permission to read and/or write other fragments * owned by another module. XML namespaces should be used to scope the data * to avoid accidental clashes. + *

+ *

+ * Do not look for this object directly in project lookup. + * Instead use {@link ProjectUtils#getAuxiliaryConfiguration}. *

* @see org.netbeans.api.project.Project#getLookup * @author Jesse Glick diff --git a/projectapi/test/unit/src/org/netbeans/modules/projectapi/AuxiliaryConfigImplTest.java b/projectapi/test/unit/src/org/netbeans/modules/projectapi/AuxiliaryConfigImplTest.java new file mode 100644 --- /dev/null +++ b/projectapi/test/unit/src/org/netbeans/modules/projectapi/AuxiliaryConfigImplTest.java @@ -0,0 +1,96 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2008 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.projectapi; + +import javax.xml.parsers.DocumentBuilderFactory; +import org.netbeans.api.project.Project; +import org.netbeans.api.project.ProjectUtils; +import org.netbeans.junit.NbTestCase; +import org.netbeans.spi.project.AuxiliaryConfiguration; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.util.Lookup; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +public class AuxiliaryConfigImplTest extends NbTestCase { + + public AuxiliaryConfigImplTest(String name) { + super(name); + } + + public void testFallbackAuxiliaryConfiguration() throws Exception { + final FileObject prjdir = FileUtil.createMemoryFileSystem().getRoot(); + class Prj implements Project { + public FileObject getProjectDirectory() { + return prjdir; + } + public Lookup getLookup() { + return Lookup.EMPTY; + } + } + Project prj = new Prj(); + AuxiliaryConfiguration ac = ProjectUtils.getAuxiliaryConfiguration(prj); + assertNotNull(ac); + String namespace = "http://nowhere.net/test"; + assertNull(ac.getConfigurationFragment("x", namespace, true)); + ac.putConfigurationFragment(makeElement("x", namespace), true); + Element e = ac.getConfigurationFragment("x", namespace, true); + assertNotNull(e); + assertEquals("x", e.getLocalName()); + assertEquals(namespace, e.getNamespaceURI()); + assertNull(ac.getConfigurationFragment("x", namespace, false)); + ac.removeConfigurationFragment("x", namespace, true); + assertNull(ac.getConfigurationFragment("x", namespace, true)); + ac.putConfigurationFragment(makeElement("y", namespace), false); + prj = new Prj(); + ac = ProjectUtils.getAuxiliaryConfiguration(prj); + e = ac.getConfigurationFragment("y", namespace, false); + assertNotNull(e); + assertEquals("y", e.getLocalName()); + assertEquals(namespace, e.getNamespaceURI()); + } + + private static Element makeElement(String name, String namespace) throws Exception { + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + return doc.createElementNS(namespace, name); + } + +}