Code example for URLConnection

Methods: getInputStream, setUseCaches

0
 
		isValid = true; 
 
		classPathURL = root.getURL();
		sourcePathURLs = new HashSet<URL>();
	} 
 
	private void populate() { 
		populatePropertiesURLFromClassURL(); 
		populateClassURLFromPropertiesURL(); 
		loadProperties(); 
	} 
 
	public void setClassURL(URL classUrl) {
		this.classURL = classUrl;
		populate(); 
	} 
 
	public void setPropertiesURL(URL propertiesUrl) {
		this.propertiesURL = propertiesUrl;
		populate(); 
	} 
 
	public void setClassPathURL(URL classPathUrl) {
		this.classPathURL = classPathUrl;
	} 
 
	public void addSourcePathURL(URL sourcePathUrl) {
		sourcePathURLs.add(sourcePathUrl);
	} 
 
	// ------------------------------------- 
	// URL initialization 
	// ------------------------------------- 
 
	private void populatePropertiesURLFromClassURL() { 
		if (propertiesURL == null && classURL != null) {
			final String path = classURL.toString().replaceFirst("\\" + CLASS_EXTENSION, PROPERTIES_EXTENSION);
 
			try { 
				propertiesURL = new URL(path);
			} catch (MalformedURLException e) {
				Logger.logError(e);
			} 
			if (isInvalidURL(propertiesURL)) {
				propertiesURL = null;
			} 
		} 
	} 
 
	private void populateClassURLFromPropertiesURL() { 
		if (classURL == null && propertiesURL != null) {
			final String path = propertiesURL.toString().replaceAll("\\" + PROPERTIES_EXTENSION, CLASS_EXTENSION);
 
			try { 
				classURL = new URL(path);
			} catch (MalformedURLException e) {
				Logger.logError(e);
			} 
			if (isInvalidURL(classURL)) {
				classURL = null;
			} 
		} 
	} 
 
	private void populateClassNameFromClassURL() { 
		populate(); 
 
		if (className == null && classURL != null) {
			final String path = classURL.toString();
 
			// .dll file? 
			int index = (path.toLowerCase().indexOf(".dll!/"));
 
			if (index > 0) {
				className = path.substring(index + 6);
				return; 
			} 
 
			// Class within .jar or regular file path? 
 
			index = path.lastIndexOf('!');
 
			if (index > 0) {
				// .jar file 
				className = path.substring(index + 2);
			} else { 
				// file path 
				className = path.substring(root.getURL().toString().length());
			} 
 
			// Remove the file extension from the class name 
			index = className.lastIndexOf('.');
 
			if (index > 0) {
				className = className.substring(0, index);
			} 
 
			// Replace all file separators with dots (from file path the package/namespace) 
			className = className.replaceAll("[\\\\\\/]", ".");
		} 
	} 
 
	private void populateHtmlURL() { 
		populate(); 
 
		if (htmlURL == null && classURL != null) {
			try { 
				htmlURL = new URL(classURL.toString().replaceFirst(CLASS_EXTENSION, HTML_EXTENSION));
			} catch (MalformedURLException ignore) {}
		} 
		if (htmlURL == null && propertiesURL != null) {
			try { 
				htmlURL = new URL(propertiesURL.toString().replaceFirst(PROPERTIES_EXTENSION, HTML_EXTENSION));
			} catch (MalformedURLException ignore) {}
		} 
		if (isInvalidURL(htmlURL)) { 
			htmlURL = null; 
		} 
	} 
 
	private boolean isInvalidURL(URL url) {
		if (url != null) {
			InputStream is = null;
 
			try { 
				URLConnection conn = URLJarCollector.openConnection(url);
 
				is = conn.getInputStream();
				return false; 
			} catch (IOException e) {
				return true; 
			} finally { 
				FileUtil.cleanupStream(is);
			} 
		} 
		return true; 
	} 
 
	// / ------------------------------------- 
	// / public 
	// / ------------------------------------- 
 
	public boolean isTeam() { 
		return false; 
	} 
 
	public URL getHtmlURL() {
		// Lazy 
		if (htmlURL == null) { 
			populateHtmlURL(); 
		} 
		return htmlURL; 
	} 
 
	public URL getPropertiesURL() {
		if (propertiesURL == null) {
			populatePropertiesURLFromClassURL(); 
		} 
		return propertiesURL;
	} 
 
	public Set<String> getFriendlyURLs() {
		populate(); 
 
		Set<String> urls = new HashSet<String>();
 
		URL url = null;
		if (classURL != null) {
			url = classURL;
		} else if (propertiesURL != null) {
			url = propertiesURL;
		} 
		if (url != null) {
			String sUrl = url.toString();
			String urlNoType = sUrl.substring(0, sUrl.lastIndexOf('.'));
 
			String path = url.getPath();
			String pathNoType = path.substring(0, path.lastIndexOf('.'));
			 
			urls.add(urlNoType);
			urls.add(pathNoType);
		} 
		if (getFullClassName() != null) { 
			if (System.getProperty("TESTING", "false").equals("true")) {
				urls.add(getFullClassName());
			} else { 
				urls.add(getUniqueFullClassName());
			} 
			urls.add(getUniqueFullClassNameWithVersion());
		} 
		if (root.isJAR()) { 
			urls.add(root.getURL().toString());
		} 
		if (root instanceof ClasspathRoot) { 
			String friendly = ((ClasspathRoot) root).getFriendlyProjectURL(itemUrl);
 
			if (friendly != null) {
				urls.add(friendly);
			} 
		} 
		return urls;
	} 
 
	/** 
	 * {@inheritDoc} 
	 */ 
	public void update(long lastModified, boolean force) {
		if (lastModified > this.lastModified || force) {
			if (force) {
				isValid = true; 
			} 
 
			// trying to guess all correct file locations 
			populate(); 
 
			this.lastModified = lastModified;
			if (classURL == null) {
				isValid = false; 
			} 
			loadProperties(); 
			if (root.isJAR() && !isPropertiesLoaded) {
				isValid = false; 
			} 
			if (isValid) { 
				validateType(false); 
			} 
			if (isValid) { 
				verifyName(); 
			} 
		} 
	} 
 
	protected void validateType(boolean resolve) {
		populate(); 
 
		final IHostManager hostManager = Container.getComponent(IHostManager.class);
 
		robotType = hostManager.getRobotType(this, resolve, classURL != null);
		if (!robotType.isValid()) {
			isValid = false; 
		} 
	} 
 
	// Stronger than update 
	public boolean validate() { 
		validateType(true); 
		return isValid; 
	} 
 
	// Note that ROBOCODE_CLASSNAME can be invalid, an hence can't be trusted when loaded! 
	// Hence, we read the fullClassName field instead and NOT the ROBOCODE_CLASSNAME property. 
	private boolean loadProperties() { 
		if (!isPropertiesLoaded && propertiesURL != null) {
			InputStream ios = null;
 
			try { 
				URLConnection con = URLJarCollector.openConnection(propertiesURL);
 
				ios = con.getInputStream();
				properties.load(ios);
				isPropertiesLoaded = true;
				return true; 
			} catch (IOException e) {
				return false;