|
What this is
Other links
The source code/*
* 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;
import java.io.*;
import java.net.*;
import java.util.*;
import org.openide.ErrorManager;
import org.openide.filesystems.FileUtil;
import org.openide.util.*;
/**
* Proxying stream handler factory. Currently searches Lookup for registered
* factories and delegates to them. But #20838 suggests using JNDI instead,
* in which case registering them via Lookup would be deprecated.
* @author Jesse Glick
*/
final class NbURLStreamHandlerFactory implements URLStreamHandlerFactory, LookupListener {
private Lookup.Result r = null;
private URLStreamHandlerFactory[] handlers = null;
public NbURLStreamHandlerFactory() {}
public URLStreamHandler createURLStreamHandler(String protocol) {
if (protocol.equals("jar") || protocol.equals("file") || // NOI18N
protocol.equals("http") || protocol.equals("resource")) { // NOI18N
// Well-known handlers in JRE. Do not try to initialize lookup, etc.
return null;
}
URLStreamHandlerFactory[] _handlers;
synchronized (this) {
if (r == null) {
r = Lookup.getDefault().lookup(new Lookup.Template(URLStreamHandlerFactory.class));
r.addLookupListener(this);
resultChanged(null);
}
_handlers = (URLStreamHandlerFactory[])handlers.clone();
}
for (int i = 0; i < _handlers.length; i++) {
URLStreamHandler h = _handlers[i].createURLStreamHandler(protocol);
if (h != null) {
return h;
}
}
return null;
}
public void resultChanged(LookupEvent ev) {
Collection c = r.allInstances();
synchronized (this) {
handlers = (URLStreamHandlerFactory[])c.toArray(new URLStreamHandlerFactory[c.size()]);
}
}
/** Implements standard protocols: nbfs, nbres, nbresloc.
* Registered into NbURLStreamHandlerFactory via lookup.
*/
public static final class Standard implements URLStreamHandlerFactory {
public URLStreamHandler createURLStreamHandler(String protocol) {
if (protocol.equals("nbfs")) { // NOI18N
return FileUtil.nbfsURLStreamHandler();
} else if (protocol.equals(NbResourceStreamHandler.PROTOCOL_SYSTEM_RESOURCE) ||
protocol.equals(NbResourceStreamHandler.PROTOCOL_LOCALIZED_SYSTEM_RESOURCE)) {
return new NbResourceStreamHandler();
} else {
return null;
}
}
}
/** Stream handler for internal resource-based URLs.
* Copied with modifications from org.openide.execution - that version is now
* deprecated and handles only deprecated protocols.
* @author Jesse Glick
*/
private static final class NbResourceStreamHandler extends URLStreamHandler {
public NbResourceStreamHandler() {}
public static final String PROTOCOL_SYSTEM_RESOURCE = "nbres"; // NOI18N
public static final String PROTOCOL_LOCALIZED_SYSTEM_RESOURCE = "nbresloc"; // NOI18N
public URLConnection openConnection(URL u) throws IOException {
if (u.getProtocol().equals(PROTOCOL_SYSTEM_RESOURCE)) {
return new Connection(u, false);
} else if (u.getProtocol().equals(PROTOCOL_LOCALIZED_SYSTEM_RESOURCE)) {
return new Connection(u, true);
} else {
throw new IOException("Bad protocol: " + u.getProtocol()); // NOI18N
}
}
private static class Connection extends URLConnection {
private final boolean localized;
// A real connection to delegate to. Non-null if successfully connected.
private URLConnection real;
private IOException exception = null;
public Connection(URL u, boolean localized) {
super(u);
this.localized = localized;
}
/** Tries to get a URL from this resource from the proper classloader,
* localizing first if requested.
* Also opens the URL to make a connection; this connection,
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.