/** * 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; } }
/** * 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); }
/** * Convert an array of String to an array of URL and return it. * * @param input The array of String to be converted * @param factory Handler factory to use to generate the URLs */ protected static URL[] convert(String input[], URLStreamHandlerFactory factory) { URLStreamHandler streamHandler = null; URL url[] = new URL[input.length]; for (int i = 0; i < url.length; i++) { try { String protocol = parseProtocol(input[i]); if (factory != null) streamHandler = factory.createURLStreamHandler(protocol); else streamHandler = null; url[i] = new URL(null, input[i], streamHandler); } catch (MalformedURLException e) { url[i] = null; } } return (url); }
/** * 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; }
/** * 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) { List<URL> path = new ArrayList<>(urls.length); for (URL url : urls) { path.add(url); } this.path = path; push(urls); if (factory != null) { jarHandler = factory.createURLStreamHandler("jar"); } else { jarHandler = null; } if (DISABLE_ACC_CHECKING) this.acc = null; else this.acc = acc; }
/** * Installs the GIT protocol that we use to identify certain file versions. */ protected void installGitProtocol() { // Install protocol. try { URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { @Override public URLStreamHandler createURLStreamHandler(String protocol) { if (protocol.equals(GitRevisionURLHandler.GIT_PROTOCOL)) { URLStreamHandler handler = new GitRevisionURLHandler(); return handler; } return null; } }); } catch (Throwable t) { if (!t.getMessage().contains("factory already defined")) { logger.info(t, t); } } }
/** * 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; } }
/** * Tries to parse a <code>String</code> as an URL. * * @param spec the <code>String</code> to parse * @param urlHandlerFactory an URL stream handler factory to use * @return an URL if the parsing is successful * @see #getURLHandler(String, URLStreamHandlerFactory) * @see #getURLHandlerFactory(URLStreamHandlerFactory) */ public static URL createURL(String spec, URLStreamHandlerFactory urlHandlerFactory) { URLStreamHandler handler = getURLHandler(spec, urlHandlerFactory); URL url; try { if (handler == null) { url = new URL(spec); } else { url = new URL(null, spec, handler); } } catch (MalformedURLException e) { url = null; } return url; }
/** * 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 * @see #getURLHandlerFactory(URLStreamHandlerFactory) */ 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; }
private JarURLLoader(URLClassLoader classloader, URLStreamHandlerCache cache, URLStreamHandlerFactory factory, URL baseURL, URL absoluteUrl, Set indexSet) { super(classloader, cache, factory, baseURL, absoluteUrl); URL newBaseURL = null; try { // Cache url prefix for all resources in this jar url. String base = baseURL.toExternalForm() + "!/"; newBaseURL = new URL("jar", "", -1, base, cache.get(factory, "jar")); } catch (MalformedURLException ignore) { // Ignore. } this.baseJarURL = newBaseURL; this.classPath = null; this.indexSet = indexSet; }
public synchronized URLStreamHandler get(URLStreamHandlerFactory factory, String protocol) { if (factory == null) return null; // Check if there're handler for the same protocol in cache. HashMap<String, URLStreamHandler> cache = factoryCache.get(factory); URLStreamHandler handler = cache.get(protocol); if (handler == null) { // Add it to cache. handler = factory.createURLStreamHandler(protocol); cache.put(protocol, handler); } return handler; }
public static void setupPizzaURLLoader() { if (!customUrlLoader) { URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { @Override public URLStreamHandler createURLStreamHandler(String protocol) { if (protocol.equals("pizza")) { return new PizzaUrlHandler(); } else { return null; } } }); customUrlLoader = true; } }
/** * Convenience method to set up the necessary HttpClient protocol and URL handler. * * Only works for HttpClient, because it's not possible (or at least very difficult) * to provide a different socket factory for the HttpURLConnection class. */ public static void setup(){ final String LOOPBACK = "loopback"; // $NON-NLS-1$ // This ensures tha HttpClient knows about the protocol Protocol.registerProtocol(LOOPBACK, new Protocol(LOOPBACK,new LoopbackHttpClientSocketFactory(),1)); // Now allow the URL handling to work. URLStreamHandlerFactory ushf = new URLStreamHandlerFactory(){ @Override public URLStreamHandler createURLStreamHandler(String protocol) { if (protocol.equalsIgnoreCase(LOOPBACK)){ return new URLStreamHandler(){ @Override protected URLConnection openConnection(URL u) throws IOException { return null;// not needed for HttpClient } }; } return null; } }; java.net.URL.setURLStreamHandlerFactory(ushf); }
static URLStreamHandlerFactory loadUrlStreamHandlerFactory(String protocol) { String artifact = System.getProperty(protocol+"MvnUrlHandler",String.format(HANDLER_FACTORT_ARTIFACT,protocol)); Collection<URLStreamHandlerFactory> urlStreamHandlerFactories = MavenServiceLoader.loadServices( artifact, URLStreamHandlerFactory.class); if(urlStreamHandlerFactories.isEmpty()){ return null; } else if(urlStreamHandlerFactories.size() == 1){ return urlStreamHandlerFactories.iterator().next(); } else { URLStreamHandlerFactory[] streamHandlerFactories = urlStreamHandlerFactories.toArray( new URLStreamHandlerFactory[urlStreamHandlerFactories.size()]); return new ChainURLStreamHandlerFactory(streamHandlerFactories); } }
public static void main(String args[]) { // Unlock Security Management System.setSecurityManager(new UnsafeSecurityManager()); // Set URL Stream Handler for jeditresource (hack/fix for jEdit Web // Start) try { URLStreamHandlerFactory factory; factory = new JEditURLStreamHandlerFactory(); if (factory != null) { URL.setURLStreamHandlerFactory(factory); } } catch (Exception ex) { ex.printStackTrace(); } // Loader Window LoaderJFrame loader = new LoaderJFrame(); loader.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //loader.autoload(); }
/** * Install the mvn protocol handler for URLs. */ public static void registerMavenUrlHandler() { 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); } // If the URL above failed, the mvn protocol needs to be installed. URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { @Override public URLStreamHandler createURLStreamHandler(String protocol) { if (ServiceConstants.PROTOCOL.equals(protocol)) { return new MavenUrlStreamHandler(); } else { return null; } } }); } }
@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; } } }); } }
/** * 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 { for (URLStreamHandlerFactory factory : userFactories) { URLStreamHandler handler = factory.createURLStreamHandler(protocol); if (handler != null) { return handler; } } return null; } }
/** * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[], java.lang.ClassLoader, * java.net.URLStreamHandlerFactory) */ public void test_Constructor$Ljava_net_URLLjava_lang_ClassLoaderLjava_net_URLStreamHandlerFactory() throws Exception { class TestFactory implements URLStreamHandlerFactory { public URLStreamHandler createURLStreamHandler(String protocol) { if ("jar".equals(protocol)) { return new Handler(); } fail("Should be jar Handler. But " + protocol); return null; } } final URL base = getClass().getResource("lf.jar"); final URL[] urls = { base }; final URLClassLoader ucl = new URLClassLoader(urls, null, new TestFactory()); final URL res = ucl.findResource("swt.dll"); assertNotNull(res); final URI e = new URI("jar:" + base.toExternalForm() + "!/swt.dll"); final URI a = res.toURI(); assertEquals(e, a); }
private static void hookURLStreamHandlerFactory(final Class<?> cls) { if (cls == null || !URLStreamHandlerFactory.class.isAssignableFrom(cls)) return; for (final Method method : cls.getMethods()) { // method.setAccessible(true); final Class<?> returnType = method.getReturnType(); if (HostnameVerifier.class.isAssignableFrom(returnType)) { hookMethod(method, XC_MethodReplacement.returnConstant(new AllowAllHostnameVerifierImpl())); } else if (javax.net.ssl.SSLSocketFactory.class.isAssignableFrom(returnType)) { hookMethod(method, XC_MethodReplacement.returnConstant(TrustAllSSLSocketFactory.getSocketFactory())); } else if (URLStreamHandler.class.isAssignableFrom(returnType)) { hookMethod(method, new CreateURLStreamHandlerHook()); } else if (HttpURLConnection.class.isAssignableFrom(returnType)) { hookMethod(method, new OkHttpClientModifyRequestCallback()); } } }
private JarURLLoader(URLClassLoader classloader, URLStreamHandlerCache cache, URLStreamHandlerFactory factory, URL baseURL, URL absoluteUrl, Set<String> indexSet) { super(classloader, cache, factory, baseURL, absoluteUrl); URL newBaseURL = null; try { // Cache url prefix for all resources in this jar url. String base = baseURL.toExternalForm() + "!/"; newBaseURL = new URL("jar", "", -1, base, cache.get(factory, "jar")); } catch (MalformedURLException ignore) { // Ignore. } this.baseJarURL = newBaseURL; this.classPath = null; this.indexSet = indexSet; }
@Override public void resultChanged(LookupEvent ev) { Collection<? extends URLStreamHandlerFactory> c = r.allInstances(); synchronized (this) { handlers = c.toArray(new URLStreamHandlerFactory[c.size()]); } }
@Override public URLStreamHandlerFactory createURLStreamHandlerFactory() { // Returning null causes this factory to pass though, which ends up using the platform's // implementation. return new URLStreamHandlerFactory() { @Override public URLStreamHandler createURLStreamHandler(String protocol) { return null; } }; }
/** * Construct a new ClassLoader with no defined repositories and the * specified parent ClassLoader. * * @param parent The parent ClassLoader * @param factory the URLStreamHandlerFactory to use when creating URLs */ public StandardClassLoader(ClassLoader parent, URLStreamHandlerFactory factory) { super((new URL[0]), parent, factory); this.factory = factory; }