List of usage examples for java.net URL toString
public String toString()
From source file:eu.stratosphere.nephele.io.compression.CompressionLoader.java
/** * Returns the path to the native libraries or <code>null</code> if an error occurred. * /*w w w.j av a2 s. c om*/ * @param libraryClass * the name of this compression library's wrapper class including full package name * @return the path to the native libraries or <code>null</code> if an error occurred */ private static String getNativeLibraryPath(final String libraryClass) { final ClassLoader cl = ClassLoader.getSystemClassLoader(); if (cl == null) { LOG.error("Cannot find system class loader"); return null; } final String classLocation = libraryClass.replace('.', '/') + ".class"; if (LOG.isDebugEnabled()) { LOG.debug("Class location is " + classLocation); } final URL location = cl.getResource(classLocation); if (location == null) { LOG.error("Cannot determine location of CompressionLoader class"); return null; } final String locationString = location.toString(); if (locationString.contains(".jar!")) { // Class if inside of a deployed jar file // Create and return path to native library cache final String pathName = GlobalConfiguration .getString(ConfigConstants.TASK_MANAGER_TMP_DIR_KEY, ConfigConstants.DEFAULT_TASK_MANAGER_TMP_PATH) .split(File.pathSeparator)[0] + File.separator + NATIVELIBRARYCACHENAME; final File path = new File(pathName); if (!path.exists()) { if (!path.mkdir()) { LOG.error("Cannot create directory for native library cache."); return null; } } return pathName; } else { String result = ""; int pos = locationString.indexOf(classLocation); if (pos < 0) { LOG.error("Cannot find extract native path from class location"); return null; } result = locationString.substring(0, pos) + "META-INF/lib"; // Strip the file:/ scheme, it confuses the class loader if (result.startsWith("file:")) { result = result.substring(5); } return result; } }
From source file:eu.mihosoft.vrl.fxscad.MainController.java
/** * Returns the location of the Jar archive or .class file the specified * class has been loaded from. <b>Note:</b> this only works if the class is * loaded from a jar archive or a .class file on the locale file system. * * @param cls class to locate//from www . j ava 2 s .co m * @return the location of the Jar archive the specified class comes from */ public static File getClassLocation(Class<?> cls) { // VParamUtil.throwIfNull(cls); String className = cls.getName(); ClassLoader cl = cls.getClassLoader(); URL url = cl.getResource(className.replace(".", "/") + ".class"); String urlString = url.toString().replace("jar:", ""); if (!urlString.startsWith("file:")) { throw new IllegalArgumentException("The specified class\"" + cls.getName() + "\" has not been loaded from a location" + "on the local filesystem."); } urlString = urlString.replace("file:", ""); urlString = urlString.replace("%20", " "); int location = urlString.indexOf(".jar!"); if (location > 0) { urlString = urlString.substring(0, location) + ".jar"; } else { //System.err.println("No Jar File found: " + cls.getName()); } return new File(urlString); }
From source file:io.cloudslang.content.services.WSManRemoteShellService.java
/** * Configures the HttpClientInputs object with the most common http parameters. * * @param httpClientInputs//ww w.j a va 2 s . c o m * @param url * @param wsManRequestInputs * @return the configured HttpClientInputs object. * @throws MalformedURLException */ private static HttpClientInputs setCommonHttpInputs(HttpClientInputs httpClientInputs, URL url, WSManRequestInputs wsManRequestInputs) throws MalformedURLException { httpClientInputs.setUrl(url.toString()); httpClientInputs.setUsername(wsManRequestInputs.getUsername()); httpClientInputs.setPassword(wsManRequestInputs.getPassword()); httpClientInputs.setAuthType(wsManRequestInputs.getAuthType()); httpClientInputs.setKerberosConfFile(wsManRequestInputs.getKerberosConfFile()); httpClientInputs.setKerberosLoginConfFile(wsManRequestInputs.getKerberosLoginConfFile()); httpClientInputs.setKerberosSkipPortCheck(wsManRequestInputs.getKerberosSkipPortForLookup()); httpClientInputs.setTrustAllRoots(wsManRequestInputs.getTrustAllRoots()); httpClientInputs.setX509HostnameVerifier(wsManRequestInputs.getX509HostnameVerifier()); httpClientInputs.setProxyHost(wsManRequestInputs.getProxyHost()); httpClientInputs.setProxyPort(wsManRequestInputs.getProxyPort()); httpClientInputs.setProxyUsername(wsManRequestInputs.getProxyUsername()); httpClientInputs.setProxyPassword(wsManRequestInputs.getProxyPassword()); httpClientInputs.setKeystore(wsManRequestInputs.getKeystore()); httpClientInputs.setKeystorePassword(wsManRequestInputs.getKeystorePassword()); httpClientInputs.setTrustKeystore(wsManRequestInputs.getTrustKeystore()); httpClientInputs.setTrustPassword(wsManRequestInputs.getTrustPassword()); String headers = httpClientInputs.getHeaders(); if (StringUtils.isEmpty(headers)) { httpClientInputs.setHeaders(CONTENT_TYPE_HEADER); } else { httpClientInputs.setHeaders(headers + NEW_LINE_SEPARATOR + CONTENT_TYPE_HEADER); } httpClientInputs.setMethod(HttpPost.METHOD_NAME); return httpClientInputs; }
From source file:azkaban.common.utils.Utils.java
public static List<String> getClassLoaderDescriptions(ClassLoader loader) { List<String> values = new ArrayList<String>(); while (loader != null) { if (loader instanceof URLClassLoader) { URLClassLoader urlLoader = (URLClassLoader) loader; for (URL url : urlLoader.getURLs()) values.add(url.toString()); } else {//w w w. jav a 2 s . c om values.add(loader.getClass().getName()); } loader = loader.getParent(); } return values; }
From source file:com.bynder.sdk.util.Utils.java
/** * Creates an implementation of the API endpoints defined by the service interface. * * @param <T> Class type of the API interface. * @param apiInterface API interface class. * @param baseUrl Domain URL where we want to point the API calls. * @param credentials Token credentials to call the API. * * @return Instance of the API interface class implementation. *//*w w w. j a v a 2 s . c om*/ public static <T> T createApiService(final Class<T> apiInterface, final URL baseUrl, final Credentials credentials) { OkHttpOAuthConsumer oauthConsumer = createHttpOAuthConsumer(credentials.getConsumerKey(), credentials.getConsumerSecret(), credentials.getToken(), credentials.getTokenSecret()); OkHttpClient httpClient = createHttpClient(oauthConsumer); Builder builder = new Builder(); builder.baseUrl(baseUrl.toString()); builder.addConverterFactory(new StringConverterFactory()); builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create()); builder.addConverterFactory(GsonConverterFactory .create(new GsonBuilder().registerTypeAdapter(Boolean.class, new BooleanTypeAdapter()).create())); builder.client(httpClient); Retrofit retrofit = builder.build(); return retrofit.create(apiInterface); }
From source file:org.nebulaframework.discovery.ws.WSDiscovery.java
/** * Internal method which registers the current ClusterManager in a given * Colombus Server.//from w ww . j av a 2 s . com * * @param url Server URL * @throws IllegalArgumentException if URL is invalid */ protected static void doRegisterCluster(URL url) throws IllegalArgumentException { Assert.notNull(url); try { log.debug("[WSDiscovery] Connecting Colombus on " + url); // Create CXF JAX-WS Proxy JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(ColombusManager.class); factory.setAddress(url.toString()); final ColombusManager mgr = (ColombusManager) factory.create(); // Get Broker Service Host IP String serviceUrl = ClusterManager.getInstance().getClusterInfo().getServiceUrl(); final String serviceIP = NetUtils.getHostAddress(serviceUrl); // Register in Colombus Service mgr.registerCluster(serviceIP); // Register for Cluster Shutdown Event to Unregister Cluster ServiceEventsSupport.addServiceHook(new ServiceHookCallback() { @Override public void onServiceEvent(ServiceMessage event) { mgr.unregisterCluster(serviceIP); } }, ClusterManager.getInstance().getClusterId().toString(), ServiceMessageType.CLUSTER_SHUTDOWN); log.info("[WSDiscovery] Registered on Colombus Server " + url.getHost()); } catch (UnknownHostException e) { log.warn("[WSDiscovery] Registration Failed : " + url.getHost()); } }
From source file:org.jodconverter.office.OnlineOfficeManagerPoolEntry.java
private static File getFile(final String resourceLocation) throws FileNotFoundException { Validate.notNull(resourceLocation, "Resource location must not be null"); if (resourceLocation.startsWith("classpath:")) { final String path = resourceLocation.substring("classpath:".length()); final String description = "class path resource [" + path + "]"; final ClassLoader cl = getDefaultClassLoader(); final URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path)); if (url == null) { throw new FileNotFoundException( description + " cannot be resolved to absolute file path because it does not exist"); }/*from w w w .j a v a 2 s. c o m*/ return getFile(url.toString()); } try { // try URL return getFile(new URL(resourceLocation)); } catch (MalformedURLException ex) { // no URL -> treat as file path return new File(resourceLocation); } }
From source file:org.agiso.core.lang.util.ClassUtils.java
public static String locate(String name, ClassLoader cl) /*throws ClassNotFoundException*/ { final URL location; final String classLocation = name.replace('.', '/') + ".class"; if (cl == null) { location = ClassLoader.getSystemResource(classLocation); } else {/*ww w . j av a 2s . c o m*/ location = cl.getResource(classLocation); } if (location != null) { Pattern p = Pattern.compile("^.*:(.*)!.*$"); Matcher m = p.matcher(location.toString()); if (m.find()) { return m.group(1); } else { if (logger.isLoggable(Level.WARNING)) { logger.warning( "Cannot parse location of '" + location + "'. " + "Probably not loaded from a Jar"); } // throw new ClassNotFoundException("Cannot parse location of '" // + location + "'. Probably not loaded from a Jar"); return null; } } else { if (logger.isLoggable(Level.WARNING)) { logger.warning("Cannot find class '" + name + " using the " + cl); } // throw new ClassNotFoundException("Cannot find class '" // + name + " using the " + cl); return null; } }
From source file:org.wso2.carbon.appmgt.gateway.utils.GatewayUtils.java
public static String getAppRootURL(MessageContext messageContext) { org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext) .getAxis2MessageContext(); try {//from ww w . j av a2s .com // SERVICE_PREFIX gives the URL of the root. e.g. https://192.168.0.1:8243 // WARNING : Service prefix always gives the IP address even if the request is made with a host name. // So we should only get the protocol from the service prefix. String servicePrefix = axis2MessageContext.getProperty("SERVICE_PREFIX").toString(); URL serverRootURL = new URL(servicePrefix); String protocol = serverRootURL.getProtocol(); // Get the published gateway URL for the protocol Environment defaultGatewayEnv = ServiceReferenceHolder.getInstance().getAPIManagerConfiguration() .getApiGatewayEnvironments().get(0); String commaSeparatedGatewayEndpoints = defaultGatewayEnv.getApiGatewayEndpoint(); String[] gatewayEndpoints = commaSeparatedGatewayEndpoints.split(","); URL gatewayEndpointURL = null; for (String gatewayEndpoint : gatewayEndpoints) { URL parsedEndpointURL = new URL(gatewayEndpoint); if (parsedEndpointURL.getProtocol().equals(protocol)) { gatewayEndpointURL = parsedEndpointURL; break; } } String webAppContext = (String) messageContext.getProperty(RESTConstants.REST_API_CONTEXT); String webAppVersion = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION); URL appRootURL = new URL(gatewayEndpointURL.getProtocol(), gatewayEndpointURL.getHost(), gatewayEndpointURL.getPort(), webAppContext + "/" + webAppVersion + "/"); return appRootURL.toString(); } catch (MalformedURLException e) { log.error("Error occurred while constructing the app root URL.", e); return null; } }
From source file:Main.java
public static byte[] downloadImage(final URL url) { InputStream in = null;/*from w w w. ja v a 2s . com*/ int responseCode = -1; try { final HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.connect(); responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { in = con.getInputStream(); return getByteFromInputStream(in); } } catch (final IOException e) { Log.e(LOG_TAG, "Failed to download image from: " + url.toString(), e); } return null; }