# HG changeset patch # User Vladimir Kvashin # Date 1492167516 -10800 # Fri Apr 14 13:58:36 2017 +0300 # Branch release82 # Node ID afbab96ea297da80fa6485b47ddf520f44bba75c # Parent 7a0974c63e43194629866268eb2add9ff8b35003 fix and test for #270390 - StackOverflowError at java.io.UnixFileSystem.getBooleanAttributes diff -r 7a0974c63e43 -r afbab96ea297 dlight.libs.common/src/org/netbeans/modules/dlight/libs/common/invalid/InvalidFileURLStreamHandler.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dlight.libs.common/src/org/netbeans/modules/dlight/libs/common/invalid/InvalidFileURLStreamHandler.java Fri Apr 14 13:58:36 2017 +0300 @@ -0,0 +1,63 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle 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): + */ +package org.netbeans.modules.dlight.libs.common.invalid; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import org.openide.util.URLStreamHandlerRegistration; + +/** + * + * @author vkvashin + */ +@URLStreamHandlerRegistration(protocol="invalid") +public class InvalidFileURLStreamHandler extends URLStreamHandler { + + public static final String PROTOCOL = "invalid"; //NOI18N + public static final String PROTOCOL_PREFIX = "invalid:/"; //NOI18N + + @Override + protected URLConnection openConnection(URL u) throws IOException { + throw new FileNotFoundException(u.toString()); + } +} diff -r 7a0974c63e43 -r afbab96ea297 dlight.libs.common/src/org/netbeans/modules/dlight/libs/common/invalid/InvalidFileUrlMapper.java --- a/dlight.libs.common/src/org/netbeans/modules/dlight/libs/common/invalid/InvalidFileUrlMapper.java Thu Apr 13 13:01:24 2017 +0300 +++ b/dlight.libs.common/src/org/netbeans/modules/dlight/libs/common/invalid/InvalidFileUrlMapper.java Fri Apr 14 13:58:36 2017 +0300 @@ -40,7 +40,9 @@ package org.netbeans.modules.dlight.libs.common.invalid; import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.net.URL; +import org.netbeans.modules.dlight.libs.common.InvalidFileObjectSupport; import org.netbeans.modules.dlight.libs.common.PathUtilities; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileStateInvalidException; @@ -54,9 +56,16 @@ */ @org.openide.util.lookup.ServiceProvider(service = org.openide.filesystems.URLMapper.class) public class InvalidFileUrlMapper extends URLMapper { - + @Override public FileObject[] getFileObjects(URL url) { + if (url.getProtocol().equals(InvalidFileURLStreamHandler.PROTOCOL)) { + String path = unescapePath(url); + FileObject fo = InvalidFileObjectSupport.getInvalidFileObject(InvalidFileObjectSupport.getDummyFileSystem(), path); + if (fo != null) { + return new FileObject[] { fo }; + } + } return null; } @@ -69,7 +78,12 @@ // even if the file is already created. // So we'll try creating a "real" URL FileSystem fs = fo.getFileSystem(); - String root = fs.getRoot().toURL().toExternalForm(); + String root; + if (fs == InvalidFileObjectSupport.getDummyFileSystem()) { + root = InvalidFileURLStreamHandler.PROTOCOL_PREFIX; + } else { + root = fs.getRoot().toURL().toExternalForm(); + } String path = PathUtilities.escapePathForUseInURL(fo.getPath()); String res; if (root.endsWith("/")) { // NOI18N @@ -84,4 +98,17 @@ } return null; } + + private static String unescapePath(URL url) { + String path = url.getFile(); + if (path.contains("%")) { //NOI18N + try { + return url.toURI().getPath(); + } catch (URISyntaxException ex) { + Exceptions.printStackTrace(ex); + } + } + return path; + } + } diff -r 7a0974c63e43 -r afbab96ea297 dlight.libs.common/test/unit/src/org/netbeans/modules/dlight/InvalidFileObjectSupportTest.java --- a/dlight.libs.common/test/unit/src/org/netbeans/modules/dlight/InvalidFileObjectSupportTest.java Thu Apr 13 13:01:24 2017 +0300 +++ b/dlight.libs.common/test/unit/src/org/netbeans/modules/dlight/InvalidFileObjectSupportTest.java Fri Apr 14 13:58:36 2017 +0300 @@ -62,13 +62,23 @@ */ public class InvalidFileObjectSupportTest { - static { + static { // otherwise we get java.net.MalformedURLException: unknown protocol // even if we register via @URLStreamHandlerRegistration annotation URL.setURLStreamHandlerFactory(Lookup.getDefault().lookup(URLStreamHandlerFactory.class)); } @Test + public void testInvalidFileObjectURL() throws Exception { + // see #270390 - StackOverflowError at java.io.UnixFileSystem.getBooleanAttributes + FileSystem dummyFS = InvalidFileObjectSupport.getDummyFileSystem(); + FileObject invalidFO = InvalidFileObjectSupport.getInvalidFileObject(dummyFS, "/inexistent"); + final URL url = invalidFO.getURL(); + FileObject foundFO = URLMapper.findFileObject(url); + assertEquals("Invalid and found by URL ", invalidFO, foundFO); + } + + @Test public void testInvalidFileObject() throws Exception { File file = File.createTempFile("qwe", "asd"); FileObject origFo = null; @@ -84,8 +94,10 @@ } FileObject invalidFo1 = InvalidFileObjectSupport.getInvalidFileObject(fs, path); URI uri1 = invalidFo1.toURI(); // just to check that there is no assertions + URL url1 = invalidFo1.toURL(); // just to check that there is no assertions FileObject invalidFo2 = InvalidFileObjectSupport.getInvalidFileObject(fs, path); URI uri2 = invalidFo2.toURI(); // just to check that there is no assertions + URL url2 = invalidFo2.toURL(); // just to check that there is no assertions assertTrue(invalidFo1 == invalidFo2); assertFalse(invalidFo1.isValid()); assertEquals(origFo.getName(), invalidFo1.getName());