1 /******************************************************************************* 2 * Copyright (c) 2006 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 *******************************************************************************/ 11 package org.eclipse.core.internal.runtime; 12 13 import java.io.IOException; 14 import java.net.URL; 15 import java.net.URLConnection; 16 import org.eclipse.core.internal.boot.PlatformURLConnection; 17 import org.eclipse.core.internal.boot.PlatformURLHandler; 18 import org.eclipse.core.runtime.FileLocator; 19 import org.eclipse.osgi.service.urlconversion.URLConverter; 20 21 /** 22 * Class which implements the URLConverter service. Manages conversion of a 23 * platform: URL to one with a more well known protocol. 24 * 25 * @since 3.2 26 */ 27 public class PlatformURLConverter implements URLConverter { 28 29 /* (non-Javadoc) 30 * @see org.eclipse.osgi.service.urlconversion.URLConverter#toFileURL(java.net.URL) 31 */ 32 public URL toFileURL(URL url) throws IOException { 33 URLConnection connection = url.openConnection(); 34 if (!(connection instanceof PlatformURLConnection)) 35 return url; 36 URL result = ((PlatformURLConnection) connection).getURLAsLocal(); 37 // if we have a bundle*: URL we should try to convert it 38 if (!result.getProtocol().startsWith(PlatformURLHandler.BUNDLE)) 39 return result; 40 return FileLocator.toFileURL(result); 41 } 42 43 /* (non-Javadoc) 44 * @see org.eclipse.osgi.service.urlconversion.URLConverter#resolve(java.net.URL) 45 */ 46 public URL resolve(URL url) throws IOException { 47 URLConnection connection = url.openConnection(); 48 if (!(connection instanceof PlatformURLConnection)) 49 return url; 50 URL result = ((PlatformURLConnection) connection).getResolvedURL(); 51 // if we have a bundle*: URL we should try to convert it 52 if (!result.getProtocol().startsWith(PlatformURLHandler.BUNDLE)) 53 return result; 54 return FileLocator.resolve(result); 55 } 56 } 57