This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 41266
Collapse All | Expand All

(-)src/META-INF/services/java.net.URLStreamHandlerFactory (+2 lines)
Line 1 Link Here
1
org.netbeans.core.NbURLStreamHandlerFactory$Standard
1
org.netbeans.core.NbURLStreamHandlerFactory$Standard
2
org.netbeans.core.filesystems.NbinstURLStreamHandlerFactory
3
(-)src/META-INF/services/org.openide.filesystems.URLMapper (+2 lines)
Line 1 Link Here
1
org.netbeans.core.filesystems.ArchiveURLMapper
1
org.netbeans.core.filesystems.ArchiveURLMapper
2
org.netbeans.core.filesystems.NbinstURLMapper
3
(-)src/org/netbeans/core/filesystems/NbinstURLMapper.java (+93 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.core.filesystems;
15
16
import java.io.File;
17
import java.io.UnsupportedEncodingException;
18
import java.net.URL;
19
import java.net.URLDecoder;
20
import org.openide.ErrorManager;
21
import org.openide.filesystems.FileObject;
22
import org.openide.filesystems.FileUtil;
23
import org.openide.filesystems.URLMapper;
24
import org.openide.modules.InstalledFileLocator;
25
26
27
/**
28
 * URLMapper for the nbinst URL protocol.
29
 * The mapper handles only the translation from URL into FileObjects.
30
 * The opposite conversion is not needed, it is handled by the default URLMapper.
31
 * The format of the nbinst URL is nbinst://host/path.
32
 * The host part is optional, if presents it specifies the name of the supplying module.
33
 * The path is mandatory and specifies the relative path from the ${netbeans.home}, ${netbeans.user}
34
 * or ${netbeans.dirs}.
35
 * @author  Tomas Zezula
36
 */
37
public class NbinstURLMapper extends URLMapper {
38
    
39
    public static final String PROTOCOL = "nbinst";     //NOI18N
40
    
41
    /** Creates a new instance of NbInstURLMapper */
42
    public NbinstURLMapper() {
43
    }
44
45
    /**
46
     * Returns FileObjects for given URL
47
     * @param url the URL for which the FileObjects should be find.
48
     * @return FileObject[], never returns null, may return empty array.
49
     */
50
    public FileObject[] getFileObjects(URL url) {
51
        return decodeURL (url);
52
    }
53
54
    /**
55
     * Returns null, the translation into URL is doen by default URLMapper
56
     * @param fo
57
     * @param type
58
     * @return
59
     */
60
    public URL getURL(FileObject fo, int type) {
61
        return null;
62
    }
63
64
    /**
65
     * Resolves the nbinst URL into the array of the FileObjects.
66
     * @param url to be resolved
67
     * @return FileObject[], never returns null, may return empty array.
68
     */
69
    static FileObject[] decodeURL (URL url) {
70
        assert url != null;
71
        String protocol = url.getProtocol();
72
        if (PROTOCOL.equals(protocol)) {
73
            String module = url.getHost();
74
            if (module.length() == 0) {
75
                module = null;
76
            }
77
            String path = url.getPath();
78
            if (path.length()>0) {
79
                try {
80
                path = URLDecoder.decode(path.substring(1),"UTF-8");        //NOI18N
81
                File file = InstalledFileLocator.getDefault().locate(path,module,false);
82
                if (file != null) {
83
                    return FileUtil.fromFile(file);
84
                }
85
                } catch (UnsupportedEncodingException e) {
86
                    ErrorManager.getDefault().notify(e);
87
                }
88
            }
89
        }
90
        return new FileObject[0];
91
    }
92
93
}
(-)src/org/netbeans/core/filesystems/NbinstURLStreamHandlerFactory.java (+166 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
package org.netbeans.core.filesystems;
14
15
16
import java.io.IOException;
17
import java.io.FileNotFoundException;
18
import java.io.InputStream;
19
import java.io.OutputStream;
20
import java.net.URL;
21
import java.net.URLConnection;
22
import java.net.URLStreamHandler;
23
import java.net.URLStreamHandlerFactory;
24
import java.net.UnknownServiceException;
25
import org.openide.ErrorManager;
26
import org.openide.filesystems.FileObject;
27
import org.openide.filesystems.FileLock;
28
29
30
31
/**
32
 * StreamHandlerFactory for nbinst protocol
33
 */
34
public class NbinstURLStreamHandlerFactory implements URLStreamHandlerFactory {
35
36
    /**
37
     * Creates URLStreamHandler for nbinst protocol
38
     * @param protocol
39
     * @return NbinstURLStreamHandler if the protocol is nbinst otherwise null
40
     */
41
    public URLStreamHandler createURLStreamHandler(String protocol) {
42
        if (NbinstURLMapper.PROTOCOL.equals(protocol)) {
43
            return new NbinstURLStreamHandler ();
44
        }
45
        return null;
46
    }
47
48
    /**
49
     * URLStreamHandler for nbinst protocol
50
     */
51
    private static class NbinstURLStreamHandler extends URLStreamHandler {
52
53
        /**
54
         * Creates URLConnection for URL with nbinst protocol.
55
         * @param u URL for which the URLConnection should be created
56
         * @return URLConnection
57
         * @throws IOException
58
         */
59
        protected URLConnection openConnection(URL u) throws IOException {
60
            return new NbinstURLConnection (u);
61
        }
62
    }
63
64
    /** URLConnection for URL with nbinst protocol.
65
     *
66
     */
67
    private static class NbinstURLConnection extends URLConnection {
68
69
        private FileObject fo;
70
        private InputStream iStream;
71
        private OutputStream oStream;
72
73
        /**
74
         * Creates new URLConnection
75
         * @param url the parameter for which the connection should be
76
         * created
77
         */
78
        public NbinstURLConnection (URL url) {
79
            super (url);
80
        }
81
82
83
        public void connect() throws IOException {
84
            if (fo != null) {
85
                return;
86
            }
87
            FileObject[] decoded = NbinstURLMapper.decodeURL(this.url);
88
            if (decoded.length>0) {
89
                fo = decoded[0];
90
            }
91
            else {
92
                throw new FileNotFoundException("Cannot find: " + url); // NOI18N
93
            }
94
        }
95
96
        public int getContentLength() {
97
            try {
98
                this.connect();
99
                return (int) this.fo.getSize();     //May cause overflow long->int
100
            } catch (IOException e) {
101
                return -1;
102
            }
103
        }
104
105
106
        public InputStream getInputStream() throws IOException {
107
            this.connect();
108
            if (fo.isFolder()) {
109
                throw new UnknownServiceException ();
110
            }
111
            if (iStream == null) {
112
                iStream = fo.getInputStream();
113
            }
114
            return iStream;
115
        }
116
117
        public OutputStream getOutputStream() throws IOException {
118
            this.connect();
119
            if (fo.isFolder()) {
120
                throw new UnknownServiceException ();
121
            }
122
            FileLock flock = fo.lock();
123
            if (oStream == null) {
124
                oStream = new LockOS (fo.getOutputStream(flock), flock);
125
            }
126
            return oStream;
127
        }
128
129
130
        public String getHeaderField (String name) {
131
            if ("content-type".equals(name)) {                  //NOI18N
132
                try {
133
                    this.connect();
134
                    return fo.getMIMEType();
135
                } catch (IOException ioe) {
136
                    ErrorManager.getDefault().notify(ioe);
137
                }
138
            }
139
            return super.getHeaderField(name);
140
        }
141
    }
142
143
    /** Stream that also closes the lock, if closed.
144
     */
145
    private static class LockOS extends java.io.BufferedOutputStream {
146
        /** lock */
147
        private FileLock flock;
148
149
        /**
150
        * @param os is an OutputStream for writing in
151
        * @param lock is a lock for the stream
152
        */
153
        public LockOS (OutputStream os, FileLock lock)
154
        throws IOException {
155
            super(os);
156
            flock = lock;
157
        }
158
159
        /** overriden */
160
        public void close()
161
        throws IOException {
162
            flock.releaseLock();
163
            super.close();
164
        }
165
    }
166
}
(-)test/cfg-unit.xml (+1 lines)
Lines 130-135 Link Here
130
        <testset dir="unit/src">
130
        <testset dir="unit/src">
131
            <patternset>
131
            <patternset>
132
                <!-- Add your own code tests -->
132
                <!-- Add your own code tests -->
133
                <include name="org/netbeans/core/filesystems/NbinstURLMapperTest.class"/>
133
            </patternset>
134
            </patternset>
134
        </testset>
135
        </testset>
135
    </testbag>
136
    </testbag>
(-)test/unit/src/org/netbeans/core/filesystems/NbinstURLMapperTest.java (+165 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
package org.netbeans.core.filesystems;
14
15
16
import java.beans.PropertyVetoException;
17
import java.io.*;
18
import java.net.MalformedURLException;
19
import java.net.URL;
20
import java.net.URLConnection;
21
import java.util.StringTokenizer;
22
import org.openide.filesystems.*;
23
import org.openide.util.Lookup;
24
import org.openide.modules.InstalledFileLocator;
25
import org.netbeans.junit.NbTestCase;
26
27
28
public class NbinstURLMapperTest extends NbTestCase {
29
30
    private static final String FILE_NAME = "test.txt";     //NOI18N
31
    private static final String FOLDER_NAME = "modules";    //NOI18N
32
33
    private FileSystem fs;
34
    private int expectedLength;
35
36
    public NbinstURLMapperTest (String testName) throws IOException {
37
        super (testName);
38
    }
39
40
41
    protected void setUp() throws Exception {
42
        super.setUp();
43
        File f = this.getWorkDir();
44
        Lookup.Result result = Lookup.getDefault().lookup (new Lookup.Template(InstalledFileLocator.class));
45
        for (java.util.Iterator it = result.allInstances().iterator(); it.hasNext();) {
46
            Object locator = it.next();
47
            if (locator instanceof TestInstalledFileLocator) {
48
                ((TestInstalledFileLocator)locator).setRoot(f);
49
            }
50
        }
51
        f = new File (f,FOLDER_NAME);
52
        f.mkdir();
53
        f = new File (f,FILE_NAME);
54
        f.createNewFile();
55
        PrintWriter pw = null;
56
        try {
57
            pw = new PrintWriter(new FileWriter(f));
58
            pw.println(FILE_NAME);
59
        } finally {
60
            if (pw!=null) {
61
                pw.close ();
62
            }
63
        }
64
        this.expectedLength = (int) f.length();
65
        this.fs = this.mountFs ();
66
        assertNotNull (this.fs);
67
    }
68
69
70
    protected void tearDown() throws Exception {
71
        this.umountFs (this.fs);
72
        File f = new File (this.getWorkDir(),FOLDER_NAME);
73
        cleanUp (f);
74
        super.tearDown();
75
    }
76
77
    public void testFindFileObject () throws MalformedURLException, IOException {
78
        URL url = new URL (null,"nbinst:///modules/test.txt");  //NOI18N
79
        FileObject[] fos = URLMapper.findFileObjects (url);
80
        assertTrue (fos!=null);
81
        assertTrue (fos.length == 1);
82
    }
83
84
    public void testURLConenction () throws MalformedURLException, IOException {
85
        URL url = new URL ("nbinst:///modules/test.txt");                //NOI18N
86
        URLConnection connection = url.openConnection();
87
        assertTrue (connection.getContentLength()==expectedLength);
88
        BufferedReader in = null;
89
        try {
90
            in = new BufferedReader  ( new InputStreamReader (connection.getInputStream()));
91
            String line = in.readLine();
92
            assertTrue(line.equals(FILE_NAME));
93
        } finally {
94
            if (in != null) {
95
                in.close ();
96
            }
97
        }
98
    }
99
100
101
102
103
104
    private FileSystem mountFs () throws IOException {
105
        File f = FileUtil.normalizeFile(this.getWorkDir());
106
        String parentName;
107
        while ((parentName=f.getParent())!=null) {
108
            f = new File (parentName);
109
        }
110
        try {
111
            LocalFileSystem fs = new LocalFileSystem ();
112
            fs.setRootDirectory (f);
113
            Repository.getDefault().addFileSystem(fs);
114
            return fs;
115
        } catch (PropertyVetoException pve) {
116
            return null;
117
        }
118
    }
119
120
    private void umountFs (FileSystem fs) {
121
        assertNotNull (fs);
122
        Repository.getDefault().removeFileSystem(fs);
123
    }
124
125
    private void cleanUp (File f) {
126
        if (f.isDirectory()) {
127
            File[] files = f.listFiles();
128
            for (int i = 0; i < files.length; i++) {
129
                cleanUp(files[i]);
130
            }
131
        }
132
        f.delete();
133
    }
134
135
    public static class TestInstalledFileLocator extends InstalledFileLocator {
136
137
        private File root;
138
139
        public TestInstalledFileLocator () {
140
        }
141
142
143
        public void setRoot (File root) {
144
            this.root = root;
145
        }
146
147
        public File locate(String relativePath, String codeNameBase, boolean localized) {
148
            assert relativePath != null;
149
            if (root == null) {
150
                return null;
151
            }
152
            StringTokenizer tk = new StringTokenizer(relativePath,"/");
153
            File f = this.root;
154
            while (tk.hasMoreTokens()) {
155
                String part = tk.nextToken();
156
                f = new File (f,part);
157
                if (!f.exists()) {
158
                    return null;
159
                }
160
            }
161
            return f;
162
        }
163
    }
164
165
}

Return to bug 41266