Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2008 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at Contributors: IBM Corporation - initial API and implementation /
 package org.eclipse.osgi.framework.internal.protocol;
The URLStreamHandlerProxy is a URLStreamHandler that acts as a proxy for registered URLStreamHandlerServices. When a URLStreamHandler is requested from the URLStreamHandlerFactory and it exists in the service registry, a URLStreamHandlerProxy is created which will pass all the requests from the requestor to the real URLStreamHandlerService. We can't return the real URLStreamHandlerService from the URLStreamHandlerFactory because the JVM caches URLStreamHandlers and therefore would not support a dynamic environment of URLStreamHandlerServices being registered and unregistered.
 public class URLStreamHandlerProxy extends URLStreamHandler implements ServiceTrackerCustomizer {
 	// TODO lots of type-based names 
 	protected BundleContext context;
 	protected String protocol;
 	protected int ranking = .;
 	public URLStreamHandlerProxy(String protocolServiceReference referenceBundleContext context) {
 		this. = context;
 		this. = protocol;
 		//set the handler and ranking
 	private void setNewHandler(ServiceReference referenceint rank) {
 		if ( != null)
 		 = reference;
 		 = rank;
 		if (reference == null)

 	protected boolean equals(URL url1URL url2) {
 		return .equals(url1url2);

 	protected int getDefaultPort() {

 	protected InetAddress getHostAddress(URL url) {

 	protected int hashCode(URL url) {

	protected boolean hostsEqual(URL url1URL url2) {
		return .hostsEqual(url1url2);

	protected URLConnection openConnection(URL urlthrows IOException {

	protected void parseURL(URL urlString strint startint end) {

	protected boolean sameFile(URL url1URL url2) {
		return .sameFile(url1url2);

	protected String toExternalForm(URL url) {

	public void setURL(URL uString protocolString hostint portString authorityString userInfoString fileString queryString ref) {
	public void setURL(URL urlString protocolString hostint portString fileString ref) {
		//using non-deprecated URLStreamHandler.setURL method. 
		//setURL(URL u, String protocol, String host, int port, String authority, String userInfo, String file, String query, String ref) 

	public Object addingService(ServiceReference reference) {
		//check to see if our protocol is being registered by another service
		if (!(prop instanceof String[]))
			return null;
		String[] protocols = (String[]) prop;
		for (int i = 0; i < protocols.lengthi++) {
			if (protocols[i].equals()) {
				//If our protocol is registered by another service, check the service ranking and switch URLStreamHandlers if nessecary.
				int newServiceRanking = getRank(reference);
				if (newServiceRanking >  ||  == null)
				return (reference);
		//we don't want to continue hearing events about a URLStreamHandlerService not registered under our protocol
		return (null);

	// check to see if the ranking has changed.  If so, re-select a new URLHandler
	public void modifiedService(ServiceReference referenceObject service) {
		int newRank = getRank(reference);
		if (reference == ) {
			if (newRank < ) {
				// The URLHandler we are currently using has dropped it's ranking below a URLHandler registered 
				// for the same protocol. We need to swap out URLHandlers.
				// this should get us the highest ranked service, if available
				if (newReference !=  && newReference != null) {
					setNewHandler(newReference, ((IntegernewReference.getProperty(.)).intValue());
else if (newRank > ) {
			// the service changed is another URLHandler that we are not currently using
			// If it's ranking is higher, we must swap it in.

	public void removedService(ServiceReference referenceObject service) {
		// check to see if our URLStreamHandler was unregistered.
		if (reference != )
		// If so, look for a lower ranking URLHandler
		// this should get us the highest ranking service left, if available
		// if newReference == null then we will use the NullURLStreamHandlerService here
	private int getRank(ServiceReference reference) {
		if (reference == null)
		Object property = reference.getProperty(.);
		return (property instanceof Integer) ? ((Integerproperty).intValue() : 0;
New to GrepCode? Check out our FAQ X