Java源码示例:java.net.URLStreamHandlerFactory
示例1
/**
* Release references to any user provided factories that have been loaded
* using the provided class loader. Called during web application stop to
* prevent memory leaks.
*
* @param classLoader The class loader to release
*/
public static void release(ClassLoader classLoader) {
if (instance == null) {
return;
}
List<URLStreamHandlerFactory> factories = instance.userFactories;
for (URLStreamHandlerFactory factory : factories) {
ClassLoader factoryLoader = factory.getClass().getClassLoader();
while (factoryLoader != null) {
if (classLoader.equals(factoryLoader)) {
// Implementation note: userFactories is a
// CopyOnWriteArrayList, so items are removed with
// List.remove() instead of usual Iterator.remove()
factories.remove(factory);
break;
}
factoryLoader = factoryLoader.getParent();
}
}
}
示例2
/**
* Creates a new URLClassPath for the given URLs. The URLs will be
* searched in the order specified for classes and resources. A URL
* ending with a '/' is assumed to refer to a directory. Otherwise,
* the URL is assumed to refer to a JAR file.
*
* @param urls the directory and JAR file URLs to search for classes
* and resources
* @param factory the URLStreamHandlerFactory to use when creating new URLs
* @param acc the context to be used when loading classes and resources, may
* be null
*/
public URLClassPath(URL[] urls,
URLStreamHandlerFactory factory,
AccessControlContext acc) {
for (int i = 0; i < urls.length; i++) {
path.add(urls[i]);
}
push(urls);
if (factory != null) {
jarHandler = factory.createURLStreamHandler("jar");
}
if (DISABLE_ACC_CHECKING)
this.acc = null;
else
this.acc = acc;
}
示例3
/**
* Creates a new URLClassPath for the given URLs. The URLs will be
* searched in the order specified for classes and resources. A URL
* ending with a '/' is assumed to refer to a directory. Otherwise,
* the URL is assumed to refer to a JAR file.
*
* @param urls the directory and JAR file URLs to search for classes
* and resources
* @param factory the URLStreamHandlerFactory to use when creating new URLs
* @param acc the context to be used when loading classes and resources, may
* be null
*/
public URLClassPath(URL[] urls,
URLStreamHandlerFactory factory,
AccessControlContext acc) {
for (int i = 0; i < urls.length; i++) {
path.add(urls[i]);
}
push(urls);
if (factory != null) {
jarHandler = factory.createURLStreamHandler("jar");
}
if (DISABLE_ACC_CHECKING)
this.acc = null;
else
this.acc = acc;
}
示例4
/**
* Creates a new URLClassPath for the given URLs. The URLs will be
* searched in the order specified for classes and resources. A URL
* ending with a '/' is assumed to refer to a directory. Otherwise,
* the URL is assumed to refer to a JAR file.
*
* @param urls the directory and JAR file URLs to search for classes
* and resources
* @param factory the URLStreamHandlerFactory to use when creating new URLs
* @param acc the context to be used when loading classes and resources, may
* be null
*/
public URLClassPath(URL[] urls,
URLStreamHandlerFactory factory,
AccessControlContext acc) {
for (int i = 0; i < urls.length; i++) {
path.add(urls[i]);
}
push(urls);
if (factory != null) {
jarHandler = factory.createURLStreamHandler("jar");
}
if (DISABLE_ACC_CHECKING)
this.acc = null;
else
this.acc = acc;
}
示例5
@BeforeClass
public static void setupMavenUrlHandler() {
try {
new URL("mvn:foo/bar");
} catch (MalformedURLException e) {
// handles mvn local repository
String mvnLocalRepo = System.getProperty("maven.repo.local");
if (mvnLocalRepo != null) {
System.setProperty("org.ops4j.pax.url.mvn.localRepository", mvnLocalRepo);
}
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if (ServiceConstants.PROTOCOL.equals(protocol)) {
return new Handler();
} else {
return null;
}
}
});
}
}
示例6
/**
* Creates a new URLClassPath for the given URLs. The URLs will be
* searched in the order specified for classes and resources. A URL
* ending with a '/' is assumed to refer to a directory. Otherwise,
* the URL is assumed to refer to a JAR file.
*
* @param urls the directory and JAR file URLs to search for classes
* and resources
* @param factory the URLStreamHandlerFactory to use when creating new URLs
* @param acc the context to be used when loading classes and resources, may
* be null
*/
public URLClassPath(URL[] urls,
URLStreamHandlerFactory factory,
AccessControlContext acc) {
for (int i = 0; i < urls.length; i++) {
path.add(urls[i]);
}
push(urls);
if (factory != null) {
jarHandler = factory.createURLStreamHandler("jar");
}
if (DISABLE_ACC_CHECKING)
this.acc = null;
else
this.acc = acc;
}
示例7
/**
* Creates a new URLStreamHandler instance with the specified protocol.
* Will return null if the protocol is not <code>jndi</code>.
*
* @param protocol the protocol (must be "jndi" here)
* @return a URLStreamHandler for the jndi protocol, or null if the
* protocol is not JNDI
*/
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if (protocol.equals("jndi")) {
return new DirContextURLStreamHandler();
} else if (protocol.equals("classpath")) {
return new ClasspathURLStreamHandler();
} else {
for (URLStreamHandlerFactory factory : userFactories) {
URLStreamHandler handler =
factory.createURLStreamHandler(protocol);
if (handler != null) {
return handler;
}
}
return null;
}
}
示例8
/**
* Creates a new URLClassPath for the given URLs. The URLs will be
* searched in the order specified for classes and resources. A URL
* ending with a '/' is assumed to refer to a directory. Otherwise,
* the URL is assumed to refer to a JAR file.
*
* @param urls the directory and JAR file URLs to search for classes
* and resources
* @param factory the URLStreamHandlerFactory to use when creating new URLs
* @param acc the context to be used when loading classes and resources, may
* be null
*/
public URLClassPath(URL[] urls,
URLStreamHandlerFactory factory,
AccessControlContext acc) {
for (int i = 0; i < urls.length; i++) {
path.add(urls[i]);
}
push(urls);
if (factory != null) {
jarHandler = factory.createURLStreamHandler("jar");
}
if (DISABLE_ACC_CHECKING)
this.acc = null;
else
this.acc = acc;
}
示例9
@BeforeClass
public static void setupMavenUrlHandler() {
try {
new URL("mvn:foo/bar");
} catch (MalformedURLException e) {
// handles mvn local repository
String mvnLocalRepo = System.getProperty("maven.repo.local");
if (mvnLocalRepo != null) {
System.setProperty("org.ops4j.pax.url.mvn.localRepository", mvnLocalRepo);
}
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if (ServiceConstants.PROTOCOL.equals(protocol)) {
return new Handler();
} else {
return null;
}
}
});
}
}
示例10
/**
* Creates a new URLClassPath for the given URLs. The URLs will be
* searched in the order specified for classes and resources. A URL
* ending with a '/' is assumed to refer to a directory. Otherwise,
* the URL is assumed to refer to a JAR file.
*
* @param urls the directory and JAR file URLs to search for classes
* and resources
* @param factory the URLStreamHandlerFactory to use when creating new URLs
* @param acc the context to be used when loading classes and resources, may
* be null
*/
public URLClassPath(URL[] urls,
URLStreamHandlerFactory factory,
AccessControlContext acc) {
ArrayList<URL> path = new ArrayList<>(urls.length);
ArrayDeque<URL> unopenedUrls = new ArrayDeque<>(urls.length);
for (URL url : urls) {
path.add(url);
unopenedUrls.add(url);
}
this.path = path;
this.unopenedUrls = unopenedUrls;
if (factory != null) {
jarHandler = factory.createURLStreamHandler("jar");
} else {
jarHandler = null;
}
if (DISABLE_ACC_CHECKING)
this.acc = null;
else
this.acc = acc;
}
示例11
/**
* Creates a new URLClassPath for the given URLs. The URLs will be
* searched in the order specified for classes and resources. A URL
* ending with a '/' is assumed to refer to a directory. Otherwise,
* the URL is assumed to refer to a JAR file.
*
* @param urls the directory and JAR file URLs to search for classes
* and resources
* @param factory the URLStreamHandlerFactory to use when creating new URLs
* @param acc the context to be used when loading classes and resources, may
* be null
*/
public URLClassPath(URL[] urls,
URLStreamHandlerFactory factory,
AccessControlContext acc) {
for (int i = 0; i < urls.length; i++) {
path.add(urls[i]);
}
push(urls);
if (factory != null) {
jarHandler = factory.createURLStreamHandler("jar");
}
if (DISABLE_ACC_CHECKING)
this.acc = null;
else
this.acc = acc;
}
示例12
/**
* Creates a new URLClassPath for the given URLs. The URLs will be
* searched in the order specified for classes and resources. A URL
* ending with a '/' is assumed to refer to a directory. Otherwise,
* the URL is assumed to refer to a JAR file.
*
* @param urls the directory and JAR file URLs to search for classes
* and resources
* @param factory the URLStreamHandlerFactory to use when creating new URLs
* @param acc the context to be used when loading classes and resources, may
* be null
*/
public URLClassPath(URL[] urls,
URLStreamHandlerFactory factory,
AccessControlContext acc) {
for (int i = 0; i < urls.length; i++) {
path.add(urls[i]);
}
push(urls);
if (factory != null) {
jarHandler = factory.createURLStreamHandler("jar");
}
if (DISABLE_ACC_CHECKING)
this.acc = null;
else
this.acc = acc;
}
示例13
/**
* Creates a new URLStreamHandler instance with the specified protocol.
* Will return null if the protocol is not <code>jndi</code>.
*
* @param protocol the protocol (must be "jndi" here)
* @return a URLStreamHandler for the jndi protocol, or null if the
* protocol is not JNDI
*/
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if (protocol.equals("jndi")) {
return new DirContextURLStreamHandler();
} else if (protocol.equals("classpath")) {
return new ClasspathURLStreamHandler();
} else {
for (URLStreamHandlerFactory factory : userFactories) {
URLStreamHandler handler =
factory.createURLStreamHandler(protocol);
if (handler != null) {
return handler;
}
}
return null;
}
}
示例14
/**
* Returns an URL stream handler for an URL specified as a <code>String</code>.
*
* @param spec the <code>String</code> to parse as an URL
* @param urlHandlerFact an URL stream handler factory
* @return an URL stream handler if one was found for the protocol of the URL
*/
public static URLStreamHandler getURLHandler(String spec, URLStreamHandlerFactory urlHandlerFact)
{
URLStreamHandlerFactory urlHandlerFactory = urlHandlerFact;//getURLHandlerFactory(urlHandlerFact);
URLStreamHandler handler = null;
if (urlHandlerFactory != null)
{
String protocol = getURLProtocol(spec);
if (protocol != null)
{
handler = urlHandlerFactory.createURLStreamHandler(protocol);
}
}
return handler;
}
示例15
/**
* Creates a URL from the specified protocol name, host name, port number,
* and file name.
* <p/>
* No validation of the inputs is performed by this method.
*
* @param protocol the name of the protocol to use
* @param host the name of the host
* @param port the port number
* @param file the file on the host
* @return URL created using specified protocol, host, and file
* @throws MalformedURLException if an unknown protocol is specified
*/
public static URL createURL(String protocol,
String host,
int port,
String file) throws MalformedURLException {
URLStreamHandlerFactory factory = _factories.get(protocol);
// If there is no URLStreamHandlerFactory registered for the
// scheme/protocol, then we just use the regular URL constructor.
if (factory == null) {
return new URL(protocol, host, port, file);
}
// If there is a URLStreamHandlerFactory associated for the
// scheme/protocol, then we create a URLStreamHandler. And, then use
// then use the URLStreamHandler to create a URL.
URLStreamHandler handler = factory.createURLStreamHandler(protocol);
return new URL(protocol, host, port, file, handler);
}
示例16
@BeforeClass
public static void setupURLStreamHandlerFactory() {
// Allows for mocking URL connections
URLStreamHandlerFactory urlStreamHandlerFactory = mock(URLStreamHandlerFactory.class);
URL.setURLStreamHandlerFactory(urlStreamHandlerFactory);
httpUrlStreamHandler = new HttpUrlStreamHandler();
when(urlStreamHandlerFactory.createURLStreamHandler("http")).thenReturn(httpUrlStreamHandler);
when(urlStreamHandlerFactory.createURLStreamHandler("https")).thenReturn(httpUrlStreamHandler);
}
示例17
@Test
public void testUserFactory() throws Exception {
URLStreamHandlerFactory factory = new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
return null;
}
};
TomcatURLStreamHandlerFactory.getInstance().addUserFactory(factory);
TomcatURLStreamHandlerFactory.release(factory.getClass().getClassLoader());
}
示例18
@Override
public void resultChanged(LookupEvent ev) {
Collection<? extends URLStreamHandlerFactory> c = r.allInstances();
synchronized (this) {
handlers = c.toArray(new URLStreamHandlerFactory[c.size()]);
}
}
示例19
@BeforeClass
public static void onlyOnce() throws Throwable {
mockHttpURLConnection = mock(HttpURLConnection.class);
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(final String s) {
return s.equals(TEST_PROTOCOL) ? new URLStreamHandler() {
@Override
protected URLConnection openConnection(final URL url) throws IOException {
return mockHttpURLConnection;
}
} : null;
}
});
}
示例20
public URLStreamHandler createURLStreamHandler(final String protocol) {
final Collection<URLStreamHandlerFactory> factories = this.factories;
synchronized (factories) {
for (final URLStreamHandlerFactory f : factories) {
final URLStreamHandler handler = f.createURLStreamHandler(protocol);
if (handler != null) {
return handler;
}
}
}
return null;
}
示例21
/**
* Sets the URL stream handler factory for the environment. This
* allows specification of the factory used in creating underlying
* stream handlers. This can be called once per JVM instance.
*
* @param factory
* The URL stream handler factory.
*/
public static void setURLStreamHandlerFactory ( URLStreamHandlerFactory factory ) {
synchronized ( PROTOCOL_HANDLERS ) {
if ( Handler.factory != null ) {
throw new IllegalStateException("URLStreamHandlerFactory already set.");
}
PROTOCOL_HANDLERS.clear();
Handler.factory = factory;
}
}
示例22
/**
* Sets the URL stream handler factory for the environment. This
* allows specification of the factory used in creating underlying
* stream handlers. This can be called once per JVM instance.
*
* @param factory The URL stream handler factory.
*/
public static void setURLStreamHandlerFactory(
URLStreamHandlerFactory factory) {
synchronized (PROTOCOL_HANDLERS) {
if (Handler.factory != null) {
throw new IllegalStateException(
"URLStreamHandlerFactory already set.");
}
PROTOCOL_HANDLERS.clear();
Handler.factory = factory;
}
}
示例23
/**
* Sets the URL stream handler factory for the environment. This
* allows specification of the factory used in creating underlying
* stream handlers. This can be called once per JVM instance.
*
* @param factory
* The URL stream handler factory.
*/
public static void setURLStreamHandlerFactory ( URLStreamHandlerFactory factory ) {
synchronized ( PROTOCOL_HANDLERS ) {
if ( Handler.factory != null ) {
throw new IllegalStateException("URLStreamHandlerFactory already set.");
}
PROTOCOL_HANDLERS.clear();
Handler.factory = factory;
}
}
示例24
/**
*
*/
@SuppressWarnings("deprecation")
protected void ensureJasperReportsContext()
{
if (
parameters.containsKey(JRExporterParameter.CLASS_LOADER)
|| parameters.containsKey(JRExporterParameter.URL_HANDLER_FACTORY)
)
{
net.sf.jasperreports.engine.util.LocalJasperReportsContext localJasperReportsContext =
new net.sf.jasperreports.engine.util.LocalJasperReportsContext(jasperReportsContext);
if (parameters.containsKey(JRExporterParameter.CLASS_LOADER))
{
localJasperReportsContext.setClassLoader((ClassLoader)parameters.get(JRExporterParameter.CLASS_LOADER));
}
if (parameters.containsKey(JRExporterParameter.URL_HANDLER_FACTORY))
{
localJasperReportsContext.setURLStreamHandlerFactory((URLStreamHandlerFactory)parameters.get(JRExporterParameter.URL_HANDLER_FACTORY));
}
setJasperReportsContext(localJasperReportsContext);
}
FontUtil.getInstance(jasperReportsContext).resetThreadMissingFontsCache();
}
示例25
public void addFactory(final URLStreamHandlerFactory factory) {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkSetFactory();
}
final Collection<URLStreamHandlerFactory> factories = this.factories;
synchronized (factories) {
factories.add(factory);
}
}
示例26
public void testNonAdvisoryNetworkRequestReplyXmlConfig() throws Exception {
final String xmlConfigString = new String("<beans" +
" xmlns=\"http://www.springframework.org/schema/beans\"" +
" xmlns:amq=\"http://activemq.apache.org/schema/core\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xsi:schemaLocation=\"http://www.springframework.org/schema/beans" +
" http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" +
" http://activemq.apache.org/schema/core" +
" http://activemq.apache.org/schema/core/activemq-core.xsd\">" +
" <broker xmlns=\"http://activemq.apache.org/schema/core\" id=\"broker\"" +
" allowTempAutoCreationOnSend=\"true\" schedulePeriodForDestinationPurge=\"1000\"" +
" brokerName=\"%HOST%\" persistent=\"false\" advisorySupport=\"false\" useJmx=\"false\" >" +
" <destinationPolicy>" +
" <policyMap>" +
" <policyEntries>" +
" <policyEntry optimizedDispatch=\"true\" gcInactiveDestinations=\"true\" gcWithNetworkConsumers=\"true\" inactiveTimoutBeforeGC=\"1000\">" +
" <destination>" +
" <tempQueue physicalName=\"" + replyQWildcard.getPhysicalName() + "\"/>" +
" </destination>" +
" </policyEntry>" +
" </policyEntries>" +
" </policyMap>" +
" </destinationPolicy>" +
" <networkConnectors>" +
" <networkConnector uri=\"multicast://default\">" +
" <staticallyIncludedDestinations>" +
" <queue physicalName=\"" + sendQ.getPhysicalName() + "\"/>" +
" <tempQueue physicalName=\"" + replyQWildcard.getPhysicalName() + "\"/>" +
" </staticallyIncludedDestinations>" +
" </networkConnector>" +
" </networkConnectors>" +
" <transportConnectors>" +
" <transportConnector uri=\"tcp://0.0.0.0:0\" discoveryUri=\"multicast://default\" />" +
" </transportConnectors>" +
" </broker>" +
"</beans>");
final String localProtocolScheme = "inline";
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if (localProtocolScheme.equalsIgnoreCase(protocol)) {
return new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) throws IOException {
return new URLConnection(u) {
@Override
public void connect() throws IOException {
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(xmlConfigString.replace("%HOST%", url.getFile()).getBytes(StandardCharsets.UTF_8));
}
};
}
};
}
return null;
}
});
a = new XBeanBrokerFactory().createBroker(new URI("xbean:" + localProtocolScheme + ":A"));
b = new XBeanBrokerFactory().createBroker(new URI("xbean:" + localProtocolScheme + ":B"));
brokers.add(a);
brokers.add(b);
doTestNonAdvisoryNetworkRequestReply();
}
示例27
public ChildFirstURLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
super(urls, parent, factory);
}
示例28
/**
* Creates a URL by parsing the given spec within a specified context. The
* new URL is created from the given context URL and the spec argument as
* described in RFC2396 "Uniform Resource Identifiers : Generic Syntax" :
* <p/>
* {@code
* <scheme>://<authority><path>?<query>#<fragment>
* }
* <p/>
* The reference is parsed into the scheme, authority, path, query and
* fragment parts. If the path component is empty and the scheme, authority,
* and query components are undefined, then the new URL is a reference to
* the current document. Otherwise, the fragment and query parts present in
* the spec are used in the new URL.
* <p/>
* If the scheme component is defined in the given spec and does not match
* the scheme of the context, then the new URL is created as an absolute URL
* based on the spec alone. Otherwise the scheme component is inherited from
* the context URL.
* <p/>
* If the authority component is present in the spec then the spec is
* treated as absolute and the spec authority and path will replace the
* context authority and path. If the authority component is absent in the
* spec then the authority of the new URL will be inherited from the context.
* <p/>
* If the spec's path component begins with a slash character "/" then the
* path is treated as absolute and the spec path replaces the context path.
* <p/>
* Otherwise, the path is treated as a relative path and is appended to the
* context path, as described in RFC2396. Also, in this case, the path is
* canonicalized through the removal of directory changes made by
* occurrences of ".." and ".".
* <p/>
* For a more detailed description of URL parsing, refer to RFC2396.
*
* @param context the context in which to parse the specification
* @param spec the String to parse as a URL
* @return URL created using the spec within the specified context
* @throws MalformedURLException if no protocol is specified, or an unknown
* protocol is found, or spec is null.
*/
public static URL createURL(URL context, String spec)
throws MalformedURLException {
if ((spec == null) || (spec.trim().length() == 0)) {
return new URL(context, spec);
}
String protocol = URI.create(spec).getScheme();
URLStreamHandlerFactory factory = _factories.get(protocol);
// If there is no URLStreamHandlerFactory registered for the
// scheme/protocol, then we just use the regular URL constructor.
if (factory == null) {
return new URL(context, spec);
}
// If there is a URLStreamHandlerFactory associated for the
// scheme/protocol, then we create a URLStreamHandler. And, then use
// then use the URLStreamHandler to create a URL.
URLStreamHandler handler = factory.createURLStreamHandler(protocol);
return new URL(context, spec, handler);
}
示例29
@Override
public URLStreamHandlerFactory streamHandler() {
return new KSStreamHandlerFactory();
}
示例30
public CubaSingleAppClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
super(urls, parent, factory);
}