? execution/manifest-subst.mf ? ide/manifest-subst.mf ? javahelp/manifest-subst.mf ? output/manifest-subst.mf ? settings/manifest-subst.mf ? term/manifest-subst.mf ? test/unit/src/META-INF ? ui/manifest-subst.mf ? windows/manifest-subst.mf Index: src/META-INF/services/java.net.URLStreamHandlerFactory =================================================================== RCS file: /cvs/core/src/META-INF/services/java.net.URLStreamHandlerFactory,v retrieving revision 1.2 diff -u -r1.2 java.net.URLStreamHandlerFactory --- src/META-INF/services/java.net.URLStreamHandlerFactory 2 Nov 2002 21:06:46 -0000 1.2 +++ src/META-INF/services/java.net.URLStreamHandlerFactory 25 Mar 2004 12:48:13 -0000 @@ -1 +1,3 @@ org.netbeans.core.NbURLStreamHandlerFactory$Standard +org.netbeans.core.filesystems.NbinstURLStreamHandlerFactory + Index: src/META-INF/services/org.openide.filesystems.URLMapper =================================================================== RCS file: /cvs/core/src/META-INF/services/org.openide.filesystems.URLMapper,v retrieving revision 1.2 diff -u -r1.2 org.openide.filesystems.URLMapper --- src/META-INF/services/org.openide.filesystems.URLMapper 16 Mar 2004 15:28:10 -0000 1.2 +++ src/META-INF/services/org.openide.filesystems.URLMapper 25 Mar 2004 12:48:13 -0000 @@ -1 +1,3 @@ org.netbeans.core.filesystems.ArchiveURLMapper +org.netbeans.core.filesystems.NbinstURLMapper + Index: src/org/netbeans/core/filesystems/NbinstURLMapper.java =================================================================== RCS file: src/org/netbeans/core/filesystems/NbinstURLMapper.java diff -N src/org/netbeans/core/filesystems/NbinstURLMapper.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/netbeans/core/filesystems/NbinstURLMapper.java 25 Mar 2004 12:48:13 -0000 @@ -0,0 +1,95 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.netbeans.core.filesystems; + +import java.io.File; +import java.io.UnsupportedEncodingException; +import java.net.*; + +import org.openide.ErrorManager; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.filesystems.URLMapper; +import org.openide.modules.InstalledFileLocator; + + +/** + * URLMapper for the nbinst URL protocol. + * The mapper handles only the translation from URL into FileObjects. + * The opposite conversion is not needed, it is handled by the default URLMapper. + * The format of the nbinst URL is nbinst://host/path. + * The host part is optional, if presents it specifies the name of the supplying module. + * The path is mandatory and specifies the relative path from the ${netbeans.home}, ${netbeans.user} + * or ${netbeans.dirs}. + * @author Tomas Zezula + */ +public class NbinstURLMapper extends URLMapper { + + public static final String PROTOCOL = "nbinst"; //NOI18N + + /** Creates a new instance of NbInstURLMapper */ + public NbinstURLMapper() { + } + + /** + * Returns FileObjects for given URL + * @param url the URL for which the FileObjects should be find. + * @return FileObject[], never returns null, may return empty array. + */ + public FileObject[] getFileObjects(URL url) { + return decodeURL (url); + } + + /** + * Returns null, the translation into URL is doen by default URLMapper + * @param fo + * @param type + * @return + */ + public URL getURL(FileObject fo, int type) { + return null; + } + + /** + * Resolves the nbinst URL into the array of the FileObjects. + * @param url to be resolved + * @return FileObject[], never returns null, may return empty array. + */ + static FileObject[] decodeURL (URL url) { + assert url != null; + try { + URI uri = new URI (url.toExternalForm()); + String protocol = uri.getScheme(); + if (PROTOCOL.equals(protocol)) { + String module = uri.getHost(); + String path = uri.getPath(); + if (path.length()>0) { + try { + File file = InstalledFileLocator.getDefault().locate(path.substring(1),module,false); + if (file != null) { + return URLMapper.findFileObjects(file.toURL()); + } + } + catch (MalformedURLException mue) { + ErrorManager.getDefault().notify(mue); + } + } + } + } catch (URISyntaxException use) { + ErrorManager.getDefault().notify(use); + } + return new FileObject[0]; + } + +} Index: src/org/netbeans/core/filesystems/NbinstURLStreamHandlerFactory.java =================================================================== RCS file: src/org/netbeans/core/filesystems/NbinstURLStreamHandlerFactory.java diff -N src/org/netbeans/core/filesystems/NbinstURLStreamHandlerFactory.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/netbeans/core/filesystems/NbinstURLStreamHandlerFactory.java 25 Mar 2004 12:48:13 -0000 @@ -0,0 +1,129 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.netbeans.core.filesystems; + + +import java.io.IOException; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.net.URLStreamHandlerFactory; +import java.net.UnknownServiceException; +import org.openide.ErrorManager; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileLock; + + + +/** + * StreamHandlerFactory for nbinst protocol + */ +public class NbinstURLStreamHandlerFactory implements URLStreamHandlerFactory { + + /** + * Creates URLStreamHandler for nbinst protocol + * @param protocol + * @return NbinstURLStreamHandler if the protocol is nbinst otherwise null + */ + public URLStreamHandler createURLStreamHandler(String protocol) { + if (NbinstURLMapper.PROTOCOL.equals(protocol)) { + return new NbinstURLStreamHandler (); + } + return null; + } + + /** + * URLStreamHandler for nbinst protocol + */ + private static class NbinstURLStreamHandler extends URLStreamHandler { + + /** + * Creates URLConnection for URL with nbinst protocol. + * @param u URL for which the URLConnection should be created + * @return URLConnection + * @throws IOException + */ + protected URLConnection openConnection(URL u) throws IOException { + return new NbinstURLConnection (u); + } + } + + /** URLConnection for URL with nbinst protocol. + * + */ + private static class NbinstURLConnection extends URLConnection { + + private FileObject fo; + private InputStream iStream; + private OutputStream oStream; + + /** + * Creates new URLConnection + * @param url the parameter for which the connection should be + * created + */ + public NbinstURLConnection (URL url) { + super (url); + } + + + public void connect() throws IOException { + if (fo == null) { + FileObject[] decoded = NbinstURLMapper.decodeURL(this.url); + if (decoded.length>0) { + fo = decoded[0]; + } + else { + throw new FileNotFoundException("Cannot find: " + url); // NOI18N + } + } + if (fo.isFolder()) { + throw new UnknownServiceException(); + } + } + + public int getContentLength() { + try { + this.connect(); + return (int) this.fo.getSize(); //May cause overflow long->int + } catch (IOException e) { + return -1; + } + } + + + public InputStream getInputStream() throws IOException { + this.connect(); + if (iStream == null) { + iStream = fo.getInputStream(); + } + return iStream; + } + + + public String getHeaderField (String name) { + if ("content-type".equals(name)) { //NOI18N + try { + this.connect(); + return fo.getMIMEType(); + } catch (IOException ioe) { + ErrorManager.getDefault().notify(ioe); + } + } + return super.getHeaderField(name); + } + } +} Index: test/unit/src/org/netbeans/core/filesystems/NbinstURLMapperTest.java =================================================================== RCS file: test/unit/src/org/netbeans/core/filesystems/NbinstURLMapperTest.java diff -N test/unit/src/org/netbeans/core/filesystems/NbinstURLMapperTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/unit/src/org/netbeans/core/filesystems/NbinstURLMapperTest.java 25 Mar 2004 12:48:14 -0000 @@ -0,0 +1,181 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.netbeans.core.filesystems; + + +import java.beans.PropertyVetoException; +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.StringTokenizer; +import org.openide.filesystems.*; +import org.openide.util.Lookup; +import org.openide.modules.InstalledFileLocator; +import org.netbeans.junit.NbTestCase; + + +public class NbinstURLMapperTest extends NbTestCase { + + private static final String FILE_NAME = "test.txt"; //NOI18N + private static final String FOLDER_NAME = "modules"; //NOI18N + + private FileSystem fs; + private int expectedLength; + + public NbinstURLMapperTest (String testName) throws IOException { + super (testName); + } + + + protected void setUp() throws Exception { + super.setUp(); + File f = this.getWorkDir(); + cleanUp (new File (f,FOLDER_NAME)); + Lookup.Result result = Lookup.getDefault().lookup (new Lookup.Template(InstalledFileLocator.class)); + boolean found = false; + for (java.util.Iterator it = result.allInstances().iterator(); it.hasNext();) { + Object locator = it.next(); + if (locator instanceof TestInstalledFileLocator) { + ((TestInstalledFileLocator)locator).setRoot(f); + found = true; + } + } + assertTrue("The TestInstalledFileLocator can not be found in the default lookup.",found); + f = new File (f,FOLDER_NAME); + f.mkdir(); + f = new File (f,FILE_NAME); + f.createNewFile(); + PrintWriter pw = null; + try { + pw = new PrintWriter(new FileWriter(f)); + pw.println(FILE_NAME); + } finally { + if (pw!=null) { + pw.close (); + } + } + this.expectedLength = (int) f.length(); + this.fs = this.mountFs (); + assertNotNull ("Test was not able to mount filesystem.",this.fs); + } + + + protected void tearDown() throws Exception { + this.umountFs (this.fs); + super.tearDown(); + } + + public void testFindFileObject () throws MalformedURLException, IOException { + URL url = new URL ("nbinst:///modules/test.txt"); //NOI18N + FileObject[] fos = URLMapper.findFileObjects (url); + assertTrue ("URLMapper returned null, violation of API contract.",fos!=null); + assertTrue ("The nbinst URL was not resolved.",fos.length == 1); + url = new URL ("nbinst://test-module/modules/test.txt"); + fos = URLMapper.findFileObjects (url); + assertTrue ("URLMapper returned null, violation of API contract.",fos!=null); + assertTrue ("The nbinst URL was not resolved.",fos.length == 1); + url = new URL ("nbinst://foo-module/modules/test.txt"); + fos = URLMapper.findFileObjects (url); + assertTrue ("URLMapper returned null, violation of API contract.",fos!=null); + assertTrue ("The nbinst URL was resolved.",fos.length == 0); + } + + public void testURLConenction () throws MalformedURLException, IOException { + URL url = new URL ("nbinst:///modules/test.txt"); //NOI18N + URLConnection connection = url.openConnection(); + assertEquals ("URLConnection returned wrong content length.",connection.getContentLength(),expectedLength); + BufferedReader in = null; + try { + in = new BufferedReader ( new InputStreamReader (connection.getInputStream())); + String line = in.readLine(); + assertTrue("URLConnection returned invalid InputStream",line.equals(FILE_NAME)); + } finally { + if (in != null) { + in.close (); + } + } + } + + + + + + private FileSystem mountFs () throws IOException { + File f = FileUtil.normalizeFile(this.getWorkDir()); + String parentName; + while ((parentName=f.getParent())!=null) { + f = new File (parentName); + } + try { + LocalFileSystem fs = new LocalFileSystem (); + fs.setRootDirectory (f); + Repository.getDefault().addFileSystem(fs); + return fs; + } catch (PropertyVetoException pve) { + return null; + } + } + + private void umountFs (FileSystem fs) { + assertNotNull ("umountFs called with null FileSystem.",fs); + Repository.getDefault().removeFileSystem(fs); + } + + private void cleanUp (File f) { + if (!f.exists()) { + return; + } + if (f.isDirectory()) { + File[] files = f.listFiles(); + for (int i = 0; i < files.length; i++) { + cleanUp(files[i]); + } + } + f.delete(); + } + + public static class TestInstalledFileLocator extends InstalledFileLocator { + + private File root; + + public TestInstalledFileLocator () { + } + + + public void setRoot (File root) { + this.root = root; + } + + public File locate(String relativePath, String codeNameBase, boolean localized) { + assert relativePath != null; + if (root == null) { + return null; + } + if (codeNameBase!= null && !"test-module".equals(codeNameBase)) { + return null; + } + StringTokenizer tk = new StringTokenizer(relativePath,"/"); + File f = this.root; + while (tk.hasMoreTokens()) { + String part = tk.nextToken(); + f = new File (f,part); + if (!f.exists()) { + return null; + } + } + return f; + } + } + +}